From 2bb10f9842650e5c0b616bc653bd44ec9268619a Mon Sep 17 00:00:00 2001 From: Bruno Barbieri Date: Sun, 16 Jun 2013 03:14:33 -0300 Subject: [PATCH] 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. --- src/gui/GenericFileFilter.java | 37 +++++++++ src/gui/Main.java | 143 +++++++++++++++++++++------------ src/gui/MobiFileFilter.java | 32 -------- 3 files changed, 127 insertions(+), 85 deletions(-) create mode 100644 src/gui/GenericFileFilter.java delete mode 100644 src/gui/MobiFileFilter.java diff --git a/src/gui/GenericFileFilter.java b/src/gui/GenericFileFilter.java new file mode 100644 index 0000000..35d291c --- /dev/null +++ b/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)); + } + +} diff --git a/src/gui/Main.java b/src/gui/Main.java index 298ec71..6810422 100644 --- a/src/gui/Main.java +++ b/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. @@ -75,7 +78,7 @@ public class Main implements ListSelectionListener, ActionListener, System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Mobi Meta Editor"); - + HashSet optionsSet = new HashSet(); File inputFile = null; for (int i=0; i