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 11 years ago
parent
commit
2bb10f9842
  1. 37
      src/gui/GenericFileFilter.java
  2. 143
      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));
}
}

143
src/gui/Main.java

@ -2,6 +2,7 @@ package gui;
import java.io.*; import java.io.*;
import java.util.HashSet; import java.util.HashSet;
import java.util.prefs.Preferences;
import java.awt.EventQueue; import java.awt.EventQueue;
import javax.swing.JFrame; import javax.swing.JFrame;
@ -10,7 +11,6 @@ import javax.swing.JOptionPane;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import javax.swing.JLabel; import javax.swing.JLabel;
import java.awt.FileDialog;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Rectangle; import java.awt.Rectangle;
@ -27,6 +27,7 @@ import java.awt.Color;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import java.awt.FlowLayout; import java.awt.FlowLayout;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTable; import javax.swing.JTable;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
@ -34,6 +35,7 @@ import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener; import javax.swing.event.TableModelListener;
import javax.swing.filechooser.FileFilter;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
@ -49,23 +51,24 @@ import javax.swing.JMenuItem;
public class Main implements ListSelectionListener, ActionListener, public class Main implements ListSelectionListener, ActionListener,
TableModelListener, LanguageModel TableModelListener, LanguageModel
{ {
private FileDialog openFileChooser = null; private JFileChooser openFileChooser = null;
private FileDialog saveFileChooser = null; private JFileChooser saveFileChooser = null;
private JFrame frame; private Preferences prefs = null;
private JTextArea lblInputFilename; private JFrame frame;
private JTextArea lblOutputFilename; private JTextArea lblInputFilename;
private JTextField tfFullName; private JTextArea lblOutputFilename;
private JTable table; private JTextField tfFullName;
private JButton buttonRemove; private JTable table;
private JButton buttonAdd; private JButton buttonRemove;
private JButton buttonSave; private JButton buttonAdd;
private JButton btnLanguage; private JButton buttonSave;
private JButton btnHeaderInfo; private JButton btnLanguage;
private GuiModel model; private JButton btnHeaderInfo;
private File outputFile; private GuiModel model;
private boolean packHeader = false; private File outputFile;
private JMenuItem mntmOpen; private boolean packHeader = false;
private JMenuItem mntmSave; private JMenuItem mntmOpen;
private JMenuItem mntmSave;
/** /**
* Launch the application. * Launch the application.
@ -75,7 +78,7 @@ public class Main implements ListSelectionListener, ActionListener,
System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", System.setProperty("com.apple.mrj.application.apple.menu.about.name",
"Mobi Meta Editor"); "Mobi Meta Editor");
HashSet<String> optionsSet = new HashSet<String>(); HashSet<String> optionsSet = new HashSet<String>();
File inputFile = null; File inputFile = null;
for (int i=0; i<args.length; i++) for (int i=0; i<args.length; i++)
@ -95,7 +98,7 @@ public class Main implements ListSelectionListener, ActionListener,
+ " does not exist or is not a file."); + " does not exist or is not a file.");
System.exit(1); System.exit(1);
} }
if (optionsSet.contains("-h") || optionsSet.contains("--help")) if (optionsSet.contains("-h") || optionsSet.contains("--help"))
printUsage(); printUsage();
@ -160,26 +163,50 @@ public class Main implements ListSelectionListener, ActionListener,
if (!MobiCommon.safeMode) if (!MobiCommon.safeMode)
buttonRemove.setEnabled(table.getSelectedRow() != -1); buttonRemove.setEnabled(table.getSelectedRow() != -1);
} }
public void pickSaveTarget() public void pickSaveTarget()
{ {
if (saveFileChooser == null) if (saveFileChooser == null)
{ {
saveFileChooser = new FileDialog(frame, "Select mobi file", FileDialog.SAVE); saveFileChooser = new JFileChooser();
saveFileChooser.setFilenameFilter(new MobiFileFilter()); 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) if (outputFile != null)
{ {
saveFileChooser.setDirectory(outputFile.getParent()); saveFileChooser.setCurrentDirectory(outputFile.getParentFile());
saveFileChooser.setFile(outputFile.getName()); 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()); lblOutputFilename.setText(outputFile.getAbsolutePath());
} }
} }
@ -209,7 +236,7 @@ public class Main implements ListSelectionListener, ActionListener,
tmpOutput = File.createTempFile("mobimeta", ".mobi"); tmpOutput = File.createTempFile("mobimeta", ".mobi");
else else
tmpOutput = outputFile; tmpOutput = outputFile;
model.save(tmpOutput, packHeader); model.save(tmpOutput, packHeader);
if (!tmpOutput.equals(outputFile)) if (!tmpOutput.equals(outputFile))
{ {
@ -219,7 +246,7 @@ public class Main implements ListSelectionListener, ActionListener,
return; return;
} }
} }
setWindowChangedStatus(false); setWindowChangedStatus(false);
showAlert("File saved."); showAlert("File saved.");
} }
@ -402,7 +429,7 @@ public class Main implements ListSelectionListener, ActionListener,
gbc_tfFullName.gridx = 2; gbc_tfFullName.gridx = 2;
gbc_tfFullName.gridy = 3; gbc_tfFullName.gridy = 3;
panel.add(tfFullName, gbc_tfFullName); panel.add(tfFullName, gbc_tfFullName);
JPanel panel_3 = new JPanel(); JPanel panel_3 = new JPanel();
GridBagConstraints gbc_panel_3 = new GridBagConstraints(); GridBagConstraints gbc_panel_3 = new GridBagConstraints();
gbc_panel_3.insets = new Insets(0, 0, 5, 5); gbc_panel_3.insets = new Insets(0, 0, 5, 5);
@ -410,7 +437,7 @@ public class Main implements ListSelectionListener, ActionListener,
gbc_panel_3.gridx = 2; gbc_panel_3.gridx = 2;
gbc_panel_3.gridy = 4; gbc_panel_3.gridy = 4;
panel.add(panel_3, gbc_panel_3); panel.add(panel_3, gbc_panel_3);
btnLanguage = new JButton("Language..."); btnLanguage = new JButton("Language...");
btnLanguage.addActionListener(this); btnLanguage.addActionListener(this);
btnHeaderInfo = new JButton("Header Info..."); btnHeaderInfo = new JButton("Header Info...");
@ -440,7 +467,7 @@ public class Main implements ListSelectionListener, ActionListener,
table.getColumnModel().getColumn(0).setPreferredWidth(100); table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getSelectionModel().addListSelectionListener(this); table.getSelectionModel().addListSelectionListener(this);
scrollPane.setViewportView(table); scrollPane.setViewportView(table);
JPanel panel_1 = new JPanel(); JPanel panel_1 = new JPanel();
GridBagConstraints gbc_panel_1 = new GridBagConstraints(); GridBagConstraints gbc_panel_1 = new GridBagConstraints();
gbc_panel_1.insets = new Insets(0, 0, 5, 5); gbc_panel_1.insets = new Insets(0, 0, 5, 5);
@ -458,7 +485,7 @@ public class Main implements ListSelectionListener, ActionListener,
buttonRemove.addActionListener(this); buttonRemove.addActionListener(this);
buttonRemove.setEnabled(false); buttonRemove.setEnabled(false);
panel_1.add(buttonRemove); panel_1.add(buttonRemove);
if (MobiCommon.safeMode) if (MobiCommon.safeMode)
{ {
buttonAdd.setEnabled(false); buttonAdd.setEnabled(false);
@ -497,17 +524,17 @@ public class Main implements ListSelectionListener, ActionListener,
buttonSave = new JButton("Save"); buttonSave = new JButton("Save");
buttonSave.addActionListener(this); buttonSave.addActionListener(this);
panel_2.add(buttonSave); panel_2.add(buttonSave);
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar); frame.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File"); JMenu mnFile = new JMenu("File");
menuBar.add(mnFile); menuBar.add(mnFile);
mntmOpen = new JMenuItem("Open..."); mntmOpen = new JMenuItem("Open...");
mntmOpen.addActionListener(this); mntmOpen.addActionListener(this);
mnFile.add(mntmOpen); mnFile.add(mntmOpen);
mntmSave = new JMenuItem("Save"); mntmSave = new JMenuItem("Save");
mntmSave.addActionListener(this); mntmSave.addActionListener(this);
mnFile.add(mntmSave); mnFile.add(mntmSave);
@ -517,23 +544,33 @@ public class Main implements ListSelectionListener, ActionListener,
{ {
if (openFileChooser == null) if (openFileChooser == null)
{ {
openFileChooser = new FileDialog(frame, "Select mobi file", FileDialog.LOAD); openFileChooser = new JFileChooser();
openFileChooser.setFilenameFilter(new MobiFileFilter()); 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);
if (source != null) File source = openFileChooser.getSelectedFile();
if (source != null && ret == JFileChooser.APPROVE_OPTION)
{ {
try try
{ {
model.setModel(source); prefs.put("lastDirectory", source.getParentFile().getAbsolutePath());
model.setModel(source);
} }
catch (GuiException e) catch (GuiException e)
{ {
@ -552,7 +589,7 @@ public class Main implements ListSelectionListener, ActionListener,
setWindowChangedStatus(false); setWindowChangedStatus(false);
packHeader = false; packHeader = false;
} }
return source; return source;
} }
@ -619,7 +656,7 @@ public class Main implements ListSelectionListener, ActionListener,
setWindowChangedStatus(true); setWindowChangedStatus(true);
} }
} }
protected void setWindowChangedStatus(boolean status) protected void setWindowChangedStatus(boolean status)
{ {
frame.getRootPane().putClientProperty("Window.documentModified", frame.getRootPane().putClientProperty("Window.documentModified",

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