Browse Source

Merge pull request #2 from BrunoReX/master

Make sure the table component is updated
master
Kin-Wai Koo 12 years ago
parent
commit
7270eb86a5
  1. 37
      src/gui/GenericFileFilter.java
  2. 111
      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));
}
}

111
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.
@ -165,21 +168,45 @@ public class Main implements ListSelectionListener, ActionListener,
{ {
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());
} }
} }
@ -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]);
} }
openFileChooser.setVisible(true); // 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));
}
File source = null; int ret = openFileChooser.showOpenDialog(frame);
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)
{ {
@ -624,6 +661,10 @@ public class Main implements ListSelectionListener, ActionListener,
{ {
frame.getRootPane().putClientProperty("Window.documentModified", frame.getRootPane().putClientProperty("Window.documentModified",
Boolean.valueOf(status)); Boolean.valueOf(status));
// Make sure the table component is updated
table.repaint();
table.revalidate();
} }
// we implement the LanguageModel interface because we want to intercept the // we implement the LanguageModel interface because we want to intercept the

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