Browse Source

Switch to javax.swing.JFileChooser from java.awt.FileDialog to make file filters work (a generic class for file filters was also implemented).

Store directory of the last opened file using the Java Preferences API to make JFileChooser act the same way as FileDialog in that regard, that is, the last directory is remembered between different executions.
master
Bruno Barbieri 12 years ago
parent
commit
2bb10f9842
  1. 37
      src/gui/GenericFileFilter.java
  2. 107
      src/gui/Main.java
  3. 32
      src/gui/MobiFileFilter.java

37
src/gui/GenericFileFilter.java

@ -0,0 +1,37 @@
package gui;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.filechooser.FileFilter;
class GenericFileFilter extends FileFilter implements FilenameFilter
{
public String extension;
public String getExtension() {
return extension;
}
public GenericFileFilter(String extension) {
this.extension = extension.toLowerCase();
}
public String getDescription()
{
return extension;
}
public boolean accept(File f, String name)
{
return (accept(f));
}
public boolean accept(File f)
{
if (f.isDirectory()) return true;
return (f.getName().toLowerCase().endsWith(extension));
}
}

107
src/gui/Main.java

@ -2,6 +2,7 @@ package gui;
import java.io.*;
import java.util.HashSet;
import java.util.prefs.Preferences;
import java.awt.EventQueue;
import javax.swing.JFrame;
@ -10,7 +11,6 @@ import javax.swing.JOptionPane;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.FileDialog;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Rectangle;
@ -27,6 +27,7 @@ import java.awt.Color;
import javax.swing.JScrollPane;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@ -34,6 +35,7 @@ import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.filechooser.FileFilter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
@ -49,23 +51,24 @@ import javax.swing.JMenuItem;
public class Main implements ListSelectionListener, ActionListener,
TableModelListener, LanguageModel
{
private FileDialog openFileChooser = null;
private FileDialog saveFileChooser = null;
private JFrame frame;
private JTextArea lblInputFilename;
private JTextArea lblOutputFilename;
private JTextField tfFullName;
private JTable table;
private JButton buttonRemove;
private JButton buttonAdd;
private JButton buttonSave;
private JButton btnLanguage;
private JButton btnHeaderInfo;
private GuiModel model;
private File outputFile;
private boolean packHeader = false;
private JMenuItem mntmOpen;
private JMenuItem mntmSave;
private JFileChooser openFileChooser = null;
private JFileChooser saveFileChooser = null;
private Preferences prefs = null;
private JFrame frame;
private JTextArea lblInputFilename;
private JTextArea lblOutputFilename;
private JTextField tfFullName;
private JTable table;
private JButton buttonRemove;
private JButton buttonAdd;
private JButton buttonSave;
private JButton btnLanguage;
private JButton btnHeaderInfo;
private GuiModel model;
private File outputFile;
private boolean packHeader = false;
private JMenuItem mntmOpen;
private JMenuItem mntmSave;
/**
* Launch the application.
@ -165,21 +168,45 @@ public class Main implements ListSelectionListener, ActionListener,
{
if (saveFileChooser == null)
{
saveFileChooser = new FileDialog(frame, "Select mobi file", FileDialog.SAVE);
saveFileChooser.setFilenameFilter(new MobiFileFilter());
saveFileChooser = new JFileChooser();
saveFileChooser.setDialogTitle("Select mobi file");
saveFileChooser.setAcceptAllFileFilterUsed(false);
saveFileChooser.addChoosableFileFilter(new GenericFileFilter(".azw"));
saveFileChooser.addChoosableFileFilter(new GenericFileFilter(".mobi"));
}
// Use the same file filter as the open dialog
GenericFileFilter tmpFilter = (GenericFileFilter) openFileChooser.getFileFilter();
for (FileFilter f : saveFileChooser.getChoosableFileFilters())
{
GenericFileFilter f2 = (GenericFileFilter) f;
if (f2.getExtension().equals(tmpFilter.getExtension()))
{
saveFileChooser.setFileFilter(f2);
break;
}
}
if (outputFile != null)
{
saveFileChooser.setDirectory(outputFile.getParent());
saveFileChooser.setFile(outputFile.getName());
saveFileChooser.setCurrentDirectory(outputFile.getParentFile());
saveFileChooser.setSelectedFile(outputFile);
}
saveFileChooser.setVisible(true);
int ret = saveFileChooser.showSaveDialog(frame);
if (saveFileChooser.getFile() != null)
if (saveFileChooser.getSelectedFile() != null && ret == JFileChooser.APPROVE_OPTION)
{
outputFile = new File(saveFileChooser.getDirectory(), saveFileChooser.getFile());
outputFile = saveFileChooser.getSelectedFile();
// Get selected file filter
GenericFileFilter filter = (GenericFileFilter) saveFileChooser.getFileFilter();
// Add extension to file name based on the selected filter
if (!filter.accept(outputFile))
outputFile = new File(outputFile.getAbsolutePath()+filter.getExtension());
lblOutputFilename.setText(outputFile.getAbsolutePath());
}
}
@ -517,23 +544,33 @@ public class Main implements ListSelectionListener, ActionListener,
{
if (openFileChooser == null)
{
openFileChooser = new FileDialog(frame, "Select mobi file", FileDialog.LOAD);
openFileChooser.setFilenameFilter(new MobiFileFilter());
openFileChooser = new JFileChooser();
openFileChooser.setDialogTitle("Select mobi file");
openFileChooser.setAcceptAllFileFilterUsed(false);
openFileChooser.addChoosableFileFilter(new GenericFileFilter(".azw"));
openFileChooser.addChoosableFileFilter(new GenericFileFilter(".mobi"));
openFileChooser.setFileFilter(openFileChooser.getChoosableFileFilters()[1]);
}
// Remember last opened directory between sessions
if (prefs == null)
{
String key = frame.getTitle()+"."+this.getClass().getName();
prefs = Preferences.userRoot().node(key);
String lastDir = prefs.get("lastDirectory", "");
openFileChooser.setCurrentDirectory(new File(lastDir));
}
openFileChooser.setVisible(true);
int ret = openFileChooser.showOpenDialog(frame);
File source = null;
String dir = openFileChooser.getDirectory();
String file = openFileChooser.getFile();
if ((dir != null) && (file != null))
source = new File(dir, file);
File source = openFileChooser.getSelectedFile();
if (source != null)
if (source != null && ret == JFileChooser.APPROVE_OPTION)
{
try
{
model.setModel(source);
prefs.put("lastDirectory", source.getParentFile().getAbsolutePath());
model.setModel(source);
}
catch (GuiException e)
{

32
src/gui/MobiFileFilter.java

@ -1,32 +0,0 @@
package gui;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.filechooser.FileFilter;
class MobiFileFilter extends FileFilter implements FilenameFilter
{
// to make it work with JFileChooser
//
public boolean accept(File f)
{
if (f.isDirectory()) return true;
return (f.getName().toLowerCase().endsWith(".azw") || f.getName().toLowerCase().endsWith(".mobi"));
}
public String getDescription()
{
return "*.azw,*.mobi";
}
// to make it work with java.awt.FileDialog
//
public boolean accept(File f, String name)
{
return (name.toLowerCase().endsWith(".azw") || name.toLowerCase().endsWith(".mobi"));
}
}
Loading…
Cancel
Save