mirror of https://github.com/dnomd343/mobi-meta
13 changed files with 0 additions and 1785 deletions
@ -1,177 +0,0 @@ |
|||
package cli; |
|||
|
|||
import java.util.Iterator; |
|||
import java.util.List; |
|||
import java.io.BufferedReader; |
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
import java.io.InputStreamReader; |
|||
import java.util.LinkedList; |
|||
|
|||
import mobimeta.*; |
|||
|
|||
public class WhisperPrep |
|||
{ |
|||
private File inDir; |
|||
private File outDir; |
|||
private BufferedReader in; |
|||
|
|||
public final static void main(String[] args) |
|||
{ |
|||
if (args.length != 2) printUsage(); |
|||
|
|||
File inDir = new File(args[0]); |
|||
File outDir = new File(args[1]); |
|||
|
|||
if (!inDir.exists() || !inDir.isDirectory()) |
|||
printUsage("Input directory " + args[0] + " does not exist."); |
|||
|
|||
if (!outDir.exists() || !outDir.isDirectory()) |
|||
printUsage("Output directory " + args[1] + " does not exist."); |
|||
|
|||
if (inDir.getAbsolutePath().equals(outDir.getAbsolutePath())) |
|||
printUsage("Input and output directories cannot be the same."); |
|||
|
|||
WhisperPrep wp = new WhisperPrep(inDir, outDir); |
|||
wp.start(); |
|||
} |
|||
|
|||
private static void printUsage() |
|||
{ |
|||
printUsage(null); |
|||
} |
|||
|
|||
private static void printUsage(String message) |
|||
{ |
|||
if (message != null) System.err.println(message); |
|||
System.err.println("Usage: WhisperPrep <input directory> <output directory>"); |
|||
System.exit(0); |
|||
} |
|||
|
|||
public WhisperPrep(File inDir, File outDir) |
|||
{ |
|||
this.inDir = inDir; |
|||
this.outDir = outDir; |
|||
} |
|||
|
|||
public void start() |
|||
{ |
|||
LinkedList<File> fileList = new LinkedList<File>(); |
|||
for (File f : inDir.listFiles()) |
|||
{ |
|||
if (f.isFile() && f.getName().toLowerCase().endsWith(".mobi")) |
|||
fileList.add(f); |
|||
} |
|||
|
|||
in = new BufferedReader(new InputStreamReader(System.in)); |
|||
|
|||
int fileCount = fileList.size(); |
|||
int current = 1; |
|||
for (File f : fileList) |
|||
{ |
|||
log("********************"); |
|||
log("File " + (current++) + "/" + fileCount); |
|||
log("Filename: " + f.getName()); |
|||
MobiMeta meta = null; |
|||
try |
|||
{ |
|||
meta = new MobiMeta(f); |
|||
} |
|||
catch (MobiMetaException e) |
|||
{ |
|||
log("Error: " + e.getMessage()); |
|||
continue; |
|||
} |
|||
|
|||
log("Fullname: " + meta.getFullName()); |
|||
List<EXTHRecord> exthList = meta.getEXTHRecords(); |
|||
String encoding = meta.getCharacterEncoding(); |
|||
String author = null; |
|||
String isbn = null; |
|||
String oldasin = null; |
|||
for (EXTHRecord rec : exthList) |
|||
{ |
|||
switch (rec.getRecordType()) |
|||
{ |
|||
case 100: |
|||
author = StreamUtils.byteArrayToString(rec.getData(), encoding); |
|||
break; |
|||
|
|||
case 104: |
|||
isbn = StreamUtils.byteArrayToString(rec.getData(), encoding); |
|||
break; |
|||
|
|||
case 113: |
|||
oldasin = StreamUtils.byteArrayToString(rec.getData(), encoding); |
|||
break; |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
if (author != null) log("Author: " + author); |
|||
if (isbn != null) log("ISBN: " + isbn); |
|||
String asin = null; |
|||
if (oldasin == null) |
|||
asin = getInput("ASIN: "); |
|||
else |
|||
asin = getInput("ASIN [" + oldasin + "]: "); |
|||
|
|||
if (asin.length() == 0) |
|||
{ |
|||
if (oldasin != null) |
|||
asin = oldasin; |
|||
else |
|||
asin = null; |
|||
} |
|||
|
|||
for (Iterator<EXTHRecord> it = exthList.iterator(); it.hasNext(); ) |
|||
{ |
|||
EXTHRecord rec = it.next(); |
|||
int recType = rec.getRecordType(); |
|||
if (recType == 113) |
|||
{ |
|||
if (asin != null) it.remove(); |
|||
} |
|||
else if (recType == 501) |
|||
it.remove(); |
|||
} |
|||
|
|||
if (asin != null) exthList.add(new EXTHRecord(113, asin, encoding)); |
|||
exthList.add(new EXTHRecord(501, "EBOK", encoding)); |
|||
meta.setEXTHRecords(); |
|||
|
|||
try |
|||
{ |
|||
meta.saveToNewFile(new File(outDir, f.getName())); |
|||
} |
|||
catch (MobiMetaException e) |
|||
{ |
|||
log("Error saving file: " + e.getMessage()); |
|||
} |
|||
|
|||
log(""); |
|||
} |
|||
} |
|||
|
|||
private void log(String message) |
|||
{ |
|||
System.out.println(message); |
|||
} |
|||
|
|||
private String getInput(String message) |
|||
{ |
|||
System.out.print(message); |
|||
String value = null; |
|||
try |
|||
{ |
|||
value = in.readLine(); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
} |
|||
|
|||
return (value == null)?"":value; |
|||
} |
|||
} |
@ -1,49 +0,0 @@ |
|||
package gui; |
|||
|
|||
import javax.swing.JTable; |
|||
import javax.swing.table.TableCellEditor; |
|||
import javax.swing.table.TableCellRenderer; |
|||
import javax.swing.table.TableColumn; |
|||
import javax.swing.table.TableModel; |
|||
|
|||
public class CustomJTable extends JTable |
|||
{ |
|||
/** |
|||
* This code was adapted from |
|||
* http://www.javalobby.org/java/forums/t84905.html
|
|||
* |
|||
* A traditional JTable lets you specify custom renderers and editors on a |
|||
* column by column basis. This class lets you have different renderers and |
|||
* editors in the same column (but different rows). |
|||
*/ |
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
public CustomJTable(TableModel model) |
|||
{ |
|||
super(model); |
|||
} |
|||
|
|||
public TableCellRenderer getCellRenderer(int row, int column) |
|||
{ |
|||
TableColumn tableColumn = getColumnModel().getColumn(column); |
|||
TableCellRenderer renderer = tableColumn.getCellRenderer(); |
|||
if (renderer != null) return renderer; |
|||
if (getValueAt(row, column) != null) |
|||
{ |
|||
return getDefaultRenderer(getValueAt(row, column).getClass()); |
|||
} |
|||
return getDefaultRenderer(getColumnClass(column)); |
|||
} |
|||
|
|||
public TableCellEditor getCellEditor(int row, int column) |
|||
{ |
|||
TableColumn tableColumn = getColumnModel().getColumn(column); |
|||
TableCellEditor editor = tableColumn.getCellEditor(); |
|||
if (editor != null) return editor; |
|||
if (getValueAt(row, column) != null) |
|||
{ |
|||
return getDefaultEditor(getValueAt(row, column).getClass()); |
|||
} |
|||
return getDefaultEditor(getColumnClass(column)); |
|||
} |
|||
} |
@ -1,8 +0,0 @@ |
|||
package gui; |
|||
|
|||
import mobimeta.EXTHRecord; |
|||
|
|||
public interface EXTHAddRecordListener |
|||
{ |
|||
public void addEXTHRecord(EXTHRecord rec); |
|||
} |
@ -1,37 +0,0 @@ |
|||
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)); |
|||
} |
|||
|
|||
} |
@ -1,14 +0,0 @@ |
|||
package gui; |
|||
|
|||
public class GuiException extends Exception |
|||
{ |
|||
/** |
|||
* |
|||
*/ |
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
public GuiException(String message) |
|||
{ |
|||
super(message); |
|||
} |
|||
} |
@ -1,201 +0,0 @@ |
|||
package gui; |
|||
|
|||
import mobimeta.*; |
|||
|
|||
import java.io.File; |
|||
import java.util.List; |
|||
|
|||
import javax.swing.table.AbstractTableModel; |
|||
|
|||
class GuiModel extends AbstractTableModel implements EXTHAddRecordListener, LanguageModel, MetaInfoProvider |
|||
{ |
|||
/** |
|||
* This is essentially a proxy to the MobiMeta object. In addition, it |
|||
* serves as a TableModel for the JTable |
|||
*/ |
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
private MobiMeta model = null; |
|||
|
|||
// static globals for convenience
|
|||
//
|
|||
private static String fullName = null; |
|||
private static List<EXTHRecord> exthRecords = null; |
|||
private static String characterEncoding = null; |
|||
|
|||
|
|||
public GuiModel() |
|||
{ |
|||
} |
|||
|
|||
public void setModel(File f) throws GuiException |
|||
{ |
|||
try |
|||
{ |
|||
model = new MobiMeta(f); |
|||
} |
|||
catch (MobiMetaException e) |
|||
{ |
|||
throw new GuiException(e.getMessage()); |
|||
} |
|||
|
|||
fullName = model.getFullName(); |
|||
exthRecords = model.getEXTHRecords(); |
|||
characterEncoding = model.getCharacterEncoding(); |
|||
} |
|||
|
|||
public String getColumnName(int col) |
|||
{ |
|||
if (col == 0) |
|||
return "Record Type"; |
|||
else |
|||
return "Value"; |
|||
} |
|||
|
|||
public int getColumnCount() |
|||
{ |
|||
return 2; |
|||
} |
|||
|
|||
public int getRowCount() |
|||
{ |
|||
return (exthRecords == null)?0:exthRecords.size(); |
|||
} |
|||
|
|||
public Object getValueAt(int row, int col) |
|||
{ |
|||
EXTHRecord rec = exthRecords.get(row); |
|||
int type = rec.getRecordType(); |
|||
String typeDesc = rec.getTypeDescription(); |
|||
|
|||
if (col == 0) |
|||
{ |
|||
if (typeDesc == null) |
|||
return Integer.toString(type); |
|||
else |
|||
return type + " (" + typeDesc + ")"; |
|||
} |
|||
else |
|||
{ |
|||
byte[] data = rec.getData(); |
|||
|
|||
if (typeDesc == null) |
|||
{ |
|||
StringBuffer sb = new StringBuffer(); |
|||
sb.append("{ "); |
|||
int len = data.length; |
|||
for (int i=0; i<len; i++) |
|||
{ |
|||
if (i > 0) sb.append(", "); |
|||
sb.append(data[i] & 0xff); |
|||
} |
|||
sb.append(" }"); |
|||
return sb.toString(); |
|||
} |
|||
else if (EXTHRecord.isBooleanType(type)) |
|||
{ |
|||
int value = StreamUtils.byteArrayToInt(data); |
|||
if (value == 0) |
|||
return Boolean.FALSE; |
|||
else |
|||
return Boolean.TRUE; |
|||
} |
|||
else |
|||
return StreamUtils.byteArrayToString(data, characterEncoding); |
|||
} |
|||
} |
|||
|
|||
public boolean isCellEditable(int row, int col) |
|||
{ |
|||
if ((col == 0) || MobiCommon.safeMode) return false; |
|||
|
|||
return exthRecords.get(row).isKnownType(); |
|||
} |
|||
|
|||
public void setValueAt(Object value, int row, int column) |
|||
{ |
|||
if (column != 1) return; |
|||
|
|||
if (value instanceof String) |
|||
exthRecords.get(row).setData((String)value, characterEncoding); |
|||
else if (value instanceof Boolean) |
|||
exthRecords.get(row).setData(((Boolean)value).booleanValue()); |
|||
else |
|||
return; |
|||
|
|||
fireTableCellUpdated(row, column); |
|||
} |
|||
|
|||
public String getFullName() |
|||
{ |
|||
return fullName; |
|||
} |
|||
|
|||
public void setFullName(String s) |
|||
{ |
|||
fullName = s; |
|||
} |
|||
|
|||
public void removeRecordAtRow(int row) |
|||
{ |
|||
exthRecords.remove(row); |
|||
fireTableRowsDeleted(row, row); |
|||
} |
|||
|
|||
|
|||
public void addEXTHRecord(EXTHRecord rec) |
|||
{ |
|||
exthRecords.add(rec); |
|||
int lastIndex = exthRecords.size() - 1; |
|||
fireTableRowsInserted(lastIndex, lastIndex); |
|||
} |
|||
|
|||
public static String getCharacterEncoding() |
|||
{ |
|||
return characterEncoding; |
|||
} |
|||
|
|||
public void save(File outputFile, boolean packHeader) throws GuiException |
|||
{ |
|||
if (packHeader) |
|||
{ |
|||
model.setFullName(fullName); |
|||
model.setEXTHRecords(); |
|||
} |
|||
|
|||
try |
|||
{ |
|||
model.saveToNewFile(outputFile, packHeader); |
|||
} |
|||
catch (MobiMetaException e) |
|||
{ |
|||
throw new GuiException("Problems encountered while saving file: " |
|||
+ e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
public int getLocale() |
|||
{ |
|||
return model.getLocale(); |
|||
} |
|||
|
|||
public int getDictInput() |
|||
{ |
|||
return model.getDictInput(); |
|||
} |
|||
|
|||
public int getDictOutput() |
|||
{ |
|||
return model.getDictOutput(); |
|||
} |
|||
|
|||
public void setLanguages(int locale, int dictInput, int dictOutput) |
|||
{ |
|||
model.setLanguages(locale, dictInput, dictOutput); |
|||
} |
|||
|
|||
public String getMetaInfo() |
|||
{ |
|||
return model.getMetaInfo(); |
|||
} |
|||
} |
@ -1,73 +0,0 @@ |
|||
package gui; |
|||
|
|||
import java.awt.BorderLayout; |
|||
import java.awt.FlowLayout; |
|||
|
|||
import javax.swing.JButton; |
|||
import javax.swing.JDialog; |
|||
import javax.swing.JFrame; |
|||
import javax.swing.JPanel; |
|||
import javax.swing.border.EmptyBorder; |
|||
import java.awt.event.ActionListener; |
|||
import java.awt.event.ActionEvent; |
|||
import javax.swing.JTextArea; |
|||
import javax.swing.JScrollPane; |
|||
|
|||
public class HeaderInfoDialog extends JDialog implements ActionListener |
|||
{ |
|||
|
|||
/** |
|||
* |
|||
*/ |
|||
private static final long serialVersionUID = 1L; |
|||
private final JPanel contentPanel = new JPanel(); |
|||
private JButton closeButton; |
|||
private JScrollPane scrollPane; |
|||
private JTextArea textArea; |
|||
|
|||
/** |
|||
* Create the dialog. |
|||
*/ |
|||
public HeaderInfoDialog(JFrame parent, MetaInfoProvider infoProvider) |
|||
{ |
|||
super(parent, "Header Info", true); |
|||
setBounds(100, 100, 450, 300); |
|||
getContentPane().setLayout(new BorderLayout()); |
|||
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); |
|||
getContentPane().add(contentPanel, BorderLayout.CENTER); |
|||
contentPanel.setLayout(new BorderLayout(0, 0)); |
|||
{ |
|||
scrollPane = new JScrollPane(); |
|||
contentPanel.add(scrollPane, BorderLayout.CENTER); |
|||
{ |
|||
textArea = new JTextArea(infoProvider.getMetaInfo()); |
|||
textArea.setEditable(false); |
|||
scrollPane.setViewportView(textArea); |
|||
} |
|||
} |
|||
{ |
|||
JPanel buttonPane = new JPanel(); |
|||
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); |
|||
getContentPane().add(buttonPane, BorderLayout.SOUTH); |
|||
{ |
|||
closeButton = new JButton("Close"); |
|||
closeButton.addActionListener(this); |
|||
closeButton.setActionCommand("Cancel"); |
|||
buttonPane.add(closeButton); |
|||
} |
|||
} |
|||
} |
|||
|
|||
protected JButton getCloseButton() { |
|||
return closeButton; |
|||
} |
|||
|
|||
public void actionPerformed(ActionEvent e) |
|||
{ |
|||
if (e.getSource() == closeButton) |
|||
{ |
|||
setVisible(false); |
|||
dispose(); |
|||
} |
|||
} |
|||
} |
@ -1,76 +0,0 @@ |
|||
package gui; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.Collections; |
|||
import java.util.Enumeration; |
|||
import java.util.HashMap; |
|||
import java.util.Properties; |
|||
import java.util.Vector; |
|||
|
|||
public class LanguageCodes |
|||
{ |
|||
public final static String PROP_FILENAME = "languagecodes.properties"; |
|||
|
|||
private HashMap<Integer,String> codeHash; |
|||
private HashMap<String,Integer> descriptionHash; |
|||
private String[] descriptions; |
|||
|
|||
public LanguageCodes() |
|||
{ |
|||
Properties props = new Properties(); |
|||
try |
|||
{ |
|||
props.load(ClassLoader.getSystemClassLoader().getResourceAsStream(PROP_FILENAME)); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
// we really have to find a better way to handle this
|
|||
} |
|||
|
|||
codeHash = new HashMap<Integer,String>(); |
|||
descriptionHash = new HashMap<String,Integer>(); |
|||
Vector<String> vec = new Vector<String>(); |
|||
Enumeration<Object> keys = props.keys(); |
|||
while (keys.hasMoreElements()) |
|||
{ |
|||
String key = (String)keys.nextElement(); |
|||
String description = props.getProperty(key) + " (" + key + ")"; |
|||
|
|||
try |
|||
{ |
|||
Integer code = Integer.valueOf(key); |
|||
codeHash.put(code, description); |
|||
descriptionHash.put(description, code); |
|||
vec.add(description); |
|||
} |
|||
catch (NumberFormatException e) |
|||
{ |
|||
} |
|||
Collections.sort(vec); |
|||
descriptions = vec.toArray(new String[vec.size()]); |
|||
} |
|||
} |
|||
|
|||
public String[] getDescriptions() |
|||
{ |
|||
return descriptions; |
|||
} |
|||
|
|||
public boolean codeExists(int code) |
|||
{ |
|||
return (codeToDescription(code) != null); |
|||
} |
|||
|
|||
public int descriptionToCode(String description) |
|||
{ |
|||
Integer code = descriptionHash.get(description); |
|||
if (code == null) return -1; |
|||
|
|||
return code.intValue(); |
|||
} |
|||
|
|||
public String codeToDescription(int code) |
|||
{ |
|||
return codeHash.get (Integer.valueOf(code)); |
|||
} |
|||
} |
@ -1,249 +0,0 @@ |
|||
package gui; |
|||
|
|||
import java.awt.BorderLayout; |
|||
import java.awt.FlowLayout; |
|||
|
|||
import javax.swing.JButton; |
|||
import javax.swing.JDialog; |
|||
import javax.swing.JFrame; |
|||
import javax.swing.JPanel; |
|||
import javax.swing.border.EmptyBorder; |
|||
import java.awt.GridBagLayout; |
|||
import java.awt.Component; |
|||
import javax.swing.Box; |
|||
import javax.swing.JLabel; |
|||
import java.awt.GridBagConstraints; |
|||
import java.awt.Font; |
|||
import javax.swing.SwingConstants; |
|||
import java.awt.Insets; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
|
|||
import javax.swing.JComboBox; |
|||
|
|||
public class LanguageDialog extends JDialog implements ActionListener |
|||
{ |
|||
/** |
|||
* |
|||
*/ |
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
private final JPanel contentPanel = new JPanel(); |
|||
|
|||
private JButton okButton; |
|||
private JButton cancelButton; |
|||
private JComboBox cbLanguage; |
|||
private JComboBox cbDictInput; |
|||
private JComboBox cbDictOutput; |
|||
|
|||
// store original values here
|
|||
private int locale; |
|||
private int dictInput; |
|||
private int dictOutput; |
|||
|
|||
// these values indicate if they are known values
|
|||
private boolean localeExists; |
|||
private boolean dictInputExists; |
|||
private boolean dictOutputExists; |
|||
|
|||
private LanguageModel model; |
|||
private LanguageCodes langCodes; |
|||
|
|||
/** |
|||
* Create the dialog. |
|||
*/ |
|||
public LanguageDialog(JFrame parent, LanguageModel model) |
|||
{ |
|||
super(parent, true); |
|||
this.model = model; |
|||
locale = model.getLocale(); |
|||
dictInput = model.getDictInput(); |
|||
dictOutput = model.getDictOutput(); |
|||
langCodes = new LanguageCodes(); |
|||
localeExists = langCodes.codeExists(locale); |
|||
dictInputExists = langCodes.codeExists(dictInput); |
|||
dictOutputExists = langCodes.codeExists(dictOutput); |
|||
|
|||
// assemble choices for combo boxes
|
|||
//
|
|||
String[] localeChoices = buildChoices(locale); |
|||
String[] dictInputChoices = buildChoices(dictInput); |
|||
String[] dictOutputChoices = buildChoices(dictOutput); |
|||
|
|||
|
|||
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); |
|||
setBounds(100, 100, 450, 300); |
|||
getContentPane().setLayout(new BorderLayout()); |
|||
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); |
|||
getContentPane().add(contentPanel, BorderLayout.CENTER); |
|||
contentPanel.setLayout(new BorderLayout(0, 0)); |
|||
{ |
|||
Component verticalStrut = Box.createVerticalStrut(20); |
|||
contentPanel.add(verticalStrut, BorderLayout.NORTH); |
|||
} |
|||
{ |
|||
Component verticalStrut = Box.createVerticalStrut(20); |
|||
contentPanel.add(verticalStrut, BorderLayout.SOUTH); |
|||
} |
|||
{ |
|||
Component horizontalStrut = Box.createHorizontalStrut(20); |
|||
contentPanel.add(horizontalStrut, BorderLayout.WEST); |
|||
} |
|||
{ |
|||
Component horizontalStrut = Box.createHorizontalStrut(20); |
|||
contentPanel.add(horizontalStrut, BorderLayout.EAST); |
|||
} |
|||
{ |
|||
JPanel panel = new JPanel(); |
|||
contentPanel.add(panel, BorderLayout.CENTER); |
|||
GridBagLayout gbl_panel = new GridBagLayout(); |
|||
gbl_panel.columnWidths = new int[] |
|||
{ 0, 0, 0 }; |
|||
gbl_panel.rowHeights = new int[] |
|||
{ 0, 0, 0, 0 }; |
|||
gbl_panel.columnWeights = new double[] |
|||
{ 0.0, 1.0, Double.MIN_VALUE }; |
|||
gbl_panel.rowWeights = new double[] |
|||
{ 0.0, 0.0, 0.0, Double.MIN_VALUE }; |
|||
panel.setLayout(gbl_panel); |
|||
{ |
|||
JLabel lblLanguage = new JLabel("Language"); |
|||
lblLanguage.setHorizontalAlignment(SwingConstants.RIGHT); |
|||
lblLanguage.setFont(new Font("Lucida Grande", Font.BOLD, 13)); |
|||
GridBagConstraints gbc_lblLanguage = new GridBagConstraints(); |
|||
gbc_lblLanguage.anchor = GridBagConstraints.EAST; |
|||
gbc_lblLanguage.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblLanguage.gridx = 0; |
|||
gbc_lblLanguage.gridy = 0; |
|||
panel.add(lblLanguage, gbc_lblLanguage); |
|||
} |
|||
{ |
|||
cbLanguage = new JComboBox(localeChoices); |
|||
if (localeExists) |
|||
cbLanguage.setSelectedItem(langCodes.codeToDescription(locale)); |
|||
else |
|||
cbLanguage.setSelectedIndex(localeChoices.length - 1); |
|||
|
|||
GridBagConstraints gbc_cbLanguage = new GridBagConstraints(); |
|||
gbc_cbLanguage.insets = new Insets(0, 0, 5, 0); |
|||
gbc_cbLanguage.fill = GridBagConstraints.HORIZONTAL; |
|||
gbc_cbLanguage.gridx = 1; |
|||
gbc_cbLanguage.gridy = 0; |
|||
panel.add(cbLanguage, gbc_cbLanguage); |
|||
} |
|||
{ |
|||
JLabel lblDictionaryInput = new JLabel("Dictionary Input"); |
|||
lblDictionaryInput.setHorizontalAlignment(SwingConstants.RIGHT); |
|||
lblDictionaryInput.setFont(new Font("Lucida Grande", Font.BOLD, |
|||
13)); |
|||
GridBagConstraints gbc_lblDictionaryInput = new GridBagConstraints(); |
|||
gbc_lblDictionaryInput.anchor = GridBagConstraints.EAST; |
|||
gbc_lblDictionaryInput.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblDictionaryInput.gridx = 0; |
|||
gbc_lblDictionaryInput.gridy = 1; |
|||
panel.add(lblDictionaryInput, gbc_lblDictionaryInput); |
|||
} |
|||
{ |
|||
cbDictInput = new JComboBox(dictInputChoices); |
|||
if (dictInputExists) |
|||
cbDictInput.setSelectedItem(langCodes.codeToDescription(dictInput)); |
|||
else |
|||
cbDictInput.setSelectedIndex(dictInputChoices.length - 1); |
|||
|
|||
GridBagConstraints gbc_cbDictInput = new GridBagConstraints(); |
|||
gbc_cbDictInput.insets = new Insets(0, 0, 5, 0); |
|||
gbc_cbDictInput.fill = GridBagConstraints.HORIZONTAL; |
|||
gbc_cbDictInput.gridx = 1; |
|||
gbc_cbDictInput.gridy = 1; |
|||
panel.add(cbDictInput, gbc_cbDictInput); |
|||
} |
|||
{ |
|||
JLabel lblDictionaryOutput = new JLabel("Dictionary Output"); |
|||
lblDictionaryOutput |
|||
.setHorizontalAlignment(SwingConstants.RIGHT); |
|||
lblDictionaryOutput.setFont(new Font("Lucida Grande", |
|||
Font.BOLD, 13)); |
|||
GridBagConstraints gbc_lblDictionaryOutput = new GridBagConstraints(); |
|||
gbc_lblDictionaryOutput.anchor = GridBagConstraints.EAST; |
|||
gbc_lblDictionaryOutput.insets = new Insets(0, 0, 0, 5); |
|||
gbc_lblDictionaryOutput.gridx = 0; |
|||
gbc_lblDictionaryOutput.gridy = 2; |
|||
panel.add(lblDictionaryOutput, gbc_lblDictionaryOutput); |
|||
} |
|||
{ |
|||
cbDictOutput = new JComboBox(dictOutputChoices); |
|||
if (dictOutputExists) |
|||
cbDictOutput.setSelectedItem(langCodes.codeToDescription(dictOutput)); |
|||
else |
|||
cbDictOutput.setSelectedIndex(dictOutputChoices.length - 1); |
|||
|
|||
GridBagConstraints gbc_cbDictOutput = new GridBagConstraints(); |
|||
gbc_cbDictOutput.fill = GridBagConstraints.HORIZONTAL; |
|||
gbc_cbDictOutput.gridx = 1; |
|||
gbc_cbDictOutput.gridy = 2; |
|||
panel.add(cbDictOutput, gbc_cbDictOutput); |
|||
} |
|||
} |
|||
{ |
|||
JPanel buttonPane = new JPanel(); |
|||
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); |
|||
getContentPane().add(buttonPane, BorderLayout.SOUTH); |
|||
{ |
|||
okButton = new JButton("OK"); |
|||
okButton.addActionListener(this); |
|||
buttonPane.add(okButton); |
|||
getRootPane().setDefaultButton(okButton); |
|||
} |
|||
{ |
|||
cancelButton = new JButton("Cancel"); |
|||
cancelButton.addActionListener(this); |
|||
buttonPane.add(cancelButton); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public void actionPerformed(ActionEvent event) |
|||
{ |
|||
Object source = event.getSource(); |
|||
|
|||
if (source == okButton) |
|||
{ |
|||
int mappedCode; |
|||
|
|||
mappedCode = langCodes.descriptionToCode((String)cbLanguage.getSelectedItem()); |
|||
if (mappedCode != -1) locale = mappedCode; |
|||
|
|||
mappedCode = langCodes.descriptionToCode((String)cbDictInput.getSelectedItem()); |
|||
if (mappedCode != -1) dictInput = mappedCode; |
|||
|
|||
mappedCode = langCodes.descriptionToCode((String)cbDictOutput.getSelectedItem()); |
|||
if (mappedCode != -1) dictOutput = mappedCode; |
|||
|
|||
model.setLanguages(locale, dictInput, dictOutput); |
|||
|
|||
setVisible(false); |
|||
dispose(); |
|||
} |
|||
else if (source == cancelButton) |
|||
{ |
|||
setVisible(false); |
|||
dispose(); |
|||
} |
|||
} |
|||
|
|||
private String[] buildChoices(int code) |
|||
{ |
|||
if (langCodes.codeExists(code)) |
|||
return langCodes.getDescriptions(); |
|||
else |
|||
{ |
|||
String[] descriptions = langCodes.getDescriptions(); |
|||
String[] choices = new String[descriptions.length + 1]; |
|||
System.arraycopy(descriptions, 0, choices, 0, |
|||
descriptions.length); |
|||
choices[choices.length - 1] = "Unknown (" + code |
|||
+ ")"; |
|||
return choices; |
|||
} |
|||
} |
|||
} |
@ -1,12 +0,0 @@ |
|||
package gui; |
|||
|
|||
public interface LanguageModel |
|||
{ |
|||
public int getLocale(); |
|||
|
|||
public int getDictInput(); |
|||
|
|||
public int getDictOutput(); |
|||
|
|||
public void setLanguages(int locale, int dictInput, int dictOutput); |
|||
} |
@ -1,708 +0,0 @@ |
|||
package gui; |
|||
|
|||
import java.io.*; |
|||
import java.util.HashSet; |
|||
import java.util.prefs.Preferences; |
|||
import java.awt.EventQueue; |
|||
|
|||
import javax.swing.JFrame; |
|||
import javax.swing.JOptionPane; |
|||
|
|||
import java.awt.GridBagLayout; |
|||
import javax.swing.JLabel; |
|||
|
|||
import java.awt.GridBagConstraints; |
|||
import java.awt.Insets; |
|||
import java.awt.Rectangle; |
|||
|
|||
import javax.swing.JSeparator; |
|||
import javax.swing.JPanel; |
|||
import java.awt.BorderLayout; |
|||
import java.awt.Component; |
|||
import javax.swing.Box; |
|||
import javax.swing.SwingConstants; |
|||
import java.awt.Font; |
|||
import javax.swing.JTextField; |
|||
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; |
|||
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; |
|||
import java.awt.event.MouseEvent; |
|||
|
|||
import javax.swing.JTextArea; |
|||
|
|||
import mobimeta.MobiCommon; |
|||
import javax.swing.JMenuBar; |
|||
import javax.swing.JMenu; |
|||
import javax.swing.JMenuItem; |
|||
|
|||
public class Main implements ListSelectionListener, ActionListener, |
|||
TableModelListener, LanguageModel |
|||
{ |
|||
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. |
|||
*/ |
|||
public static void main(String[] args) |
|||
{ |
|||
System.setProperty("apple.laf.useScreenMenuBar", "true"); |
|||
System.setProperty("com.apple.mrj.application.apple.menu.about.name", |
|||
"Mobi Meta Editor"); |
|||
|
|||
HashSet<String> optionsSet = new HashSet<String>(); |
|||
File inputFile = null; |
|||
for (int i=0; i<args.length; i++) |
|||
{ |
|||
String arg = args[i]; |
|||
if (arg.startsWith("-")) |
|||
optionsSet.add(arg); |
|||
else if (inputFile == null) |
|||
inputFile = new File(arg); |
|||
else |
|||
printUsage(); |
|||
} |
|||
|
|||
if ((inputFile != null) && (!inputFile.exists() || !inputFile.isFile())) |
|||
{ |
|||
System.err.println("Input file " + inputFile.getAbsolutePath() |
|||
+ " does not exist or is not a file."); |
|||
System.exit(1); |
|||
} |
|||
|
|||
if (optionsSet.contains("-h") || optionsSet.contains("--help")) |
|||
printUsage(); |
|||
|
|||
if (optionsSet.contains("-s")) |
|||
MobiCommon.safeMode = true; |
|||
|
|||
if (optionsSet.contains("-d")) |
|||
MobiCommon.debug = true; |
|||
|
|||
// get around inner class mumbo-jumbo
|
|||
//
|
|||
final File f = inputFile; |
|||
|
|||
EventQueue.invokeLater(new Runnable() |
|||
{ |
|||
public void run() |
|||
{ |
|||
try |
|||
{ |
|||
Main window = new Main(f); |
|||
window.frame.setVisible(true); |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
private static void printUsage() |
|||
{ |
|||
System.err.println("Usage: mobimeta [switches] [input file]"); |
|||
System.err.println(" -h\tthis message"); |
|||
System.err |
|||
.println(" -s\tsafe mode - does not change the size of the mobi header record"); |
|||
System.err.println(" -d\tdebug mode"); |
|||
System.exit(0); |
|||
} |
|||
|
|||
/** |
|||
* Create the application. |
|||
*/ |
|||
public Main(File inputFile) |
|||
{ |
|||
model = new GuiModel(); |
|||
model.addTableModelListener(this); |
|||
initialize(); |
|||
if (inputFile == null) |
|||
inputFile = getSourceFile(); |
|||
|
|||
if (inputFile == null) |
|||
{ |
|||
showAlert("No mobi file selected."); |
|||
System.exit(0); |
|||
} |
|||
} |
|||
|
|||
|
|||
public void valueChanged(ListSelectionEvent e) |
|||
{ |
|||
if (!MobiCommon.safeMode) |
|||
buttonRemove.setEnabled(table.getSelectedRow() != -1); |
|||
} |
|||
|
|||
public void pickSaveTarget() |
|||
{ |
|||
if (saveFileChooser == null) |
|||
{ |
|||
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.setCurrentDirectory(outputFile.getParentFile()); |
|||
saveFileChooser.setSelectedFile(outputFile); |
|||
} |
|||
|
|||
int ret = saveFileChooser.showSaveDialog(frame); |
|||
|
|||
if (saveFileChooser.getSelectedFile() != null && ret == JFileChooser.APPROVE_OPTION) |
|||
{ |
|||
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()); |
|||
} |
|||
} |
|||
|
|||
public void actionPerformed(ActionEvent event) |
|||
{ |
|||
Object source = event.getSource(); |
|||
|
|||
if (source == buttonAdd) |
|||
{ |
|||
NewRecordDialog dialog = new NewRecordDialog(frame, model); |
|||
dialog.setVisible(true); |
|||
} |
|||
else if (source == buttonRemove) |
|||
{ |
|||
int row = table.getSelectedRow(); |
|||
if (row == -1) return; |
|||
model.removeRecordAtRow(row); |
|||
} |
|||
else if ((source == buttonSave) || (source == mntmSave)) |
|||
{ |
|||
if (packHeader) model.setFullName(tfFullName.getText()); |
|||
try |
|||
{ |
|||
File tmpOutput = null; |
|||
if (lblInputFilename.getText().equals(lblOutputFilename.getText())) |
|||
tmpOutput = File.createTempFile("mobimeta", ".mobi"); |
|||
else |
|||
tmpOutput = outputFile; |
|||
|
|||
model.save(tmpOutput, packHeader); |
|||
if (!tmpOutput.equals(outputFile)) |
|||
{ |
|||
if (!tmpOutput.renameTo(outputFile)) |
|||
{ |
|||
showAlert("Error renaming temp file to " + outputFile.getAbsolutePath()); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
setWindowChangedStatus(false); |
|||
showAlert("File saved."); |
|||
} |
|||
catch (GuiException e) |
|||
{ |
|||
showAlert(e.getMessage()); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
showAlert("Could not create temp file for writing: " + e.getMessage()); |
|||
} |
|||
} |
|||
else if (source == btnLanguage) |
|||
{ |
|||
LanguageDialog dialog = new LanguageDialog(frame, this); |
|||
dialog.setVisible(true); |
|||
} |
|||
else if (source == btnHeaderInfo) |
|||
{ |
|||
HeaderInfoDialog dialog = new HeaderInfoDialog(frame, model); |
|||
dialog.setVisible(true); |
|||
} |
|||
else if (source == mntmOpen) |
|||
{ |
|||
getSourceFile(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Initialize the contents of the frame. |
|||
*/ |
|||
private void initialize() |
|||
{ |
|||
frame = new JFrame(); |
|||
frame.setTitle("Mobi Meta Editor"); |
|||
frame.setBounds(50, 50, 800, 700); |
|||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
frame.getContentPane().setLayout(new BorderLayout(0, 0)); |
|||
|
|||
Component horizontalStrut = Box.createHorizontalStrut(20); |
|||
frame.getContentPane().add(horizontalStrut, BorderLayout.WEST); |
|||
|
|||
Component horizontalStrut_1 = Box.createHorizontalStrut(20); |
|||
frame.getContentPane().add(horizontalStrut_1, BorderLayout.EAST); |
|||
|
|||
Component verticalStrut = Box.createVerticalStrut(20); |
|||
frame.getContentPane().add(verticalStrut, BorderLayout.NORTH); |
|||
|
|||
Component verticalStrut_1 = Box.createVerticalStrut(20); |
|||
frame.getContentPane().add(verticalStrut_1, BorderLayout.SOUTH); |
|||
|
|||
JPanel panel = new JPanel(); |
|||
frame.getContentPane().add(panel, BorderLayout.CENTER); |
|||
GridBagLayout gbl_panel = new GridBagLayout(); |
|||
gbl_panel.columnWidths = new int[] |
|||
{ 0, 0, 0, 0, 0 }; |
|||
gbl_panel.rowHeights = new int[] |
|||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
|||
gbl_panel.columnWeights = new double[] |
|||
{ 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE }; |
|||
gbl_panel.rowWeights = new double[] |
|||
{ 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE }; |
|||
panel.setLayout(gbl_panel); |
|||
|
|||
JLabel lblInput = new JLabel("Input Filename"); |
|||
lblInput.setFont(new Font("Lucida Grande", Font.BOLD, 13)); |
|||
lblInput.setVerticalAlignment(SwingConstants.TOP); |
|||
GridBagConstraints gbc_lblInput = new GridBagConstraints(); |
|||
gbc_lblInput.anchor = GridBagConstraints.NORTHEAST; |
|||
gbc_lblInput.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblInput.gridx = 0; |
|||
gbc_lblInput.gridy = 0; |
|||
panel.add(lblInput, gbc_lblInput); |
|||
|
|||
Component horizontalStrut_2 = Box.createHorizontalStrut(20); |
|||
GridBagConstraints gbc_horizontalStrut_2 = new GridBagConstraints(); |
|||
gbc_horizontalStrut_2.insets = new Insets(0, 0, 5, 5); |
|||
gbc_horizontalStrut_2.gridx = 1; |
|||
gbc_horizontalStrut_2.gridy = 0; |
|||
panel.add(horizontalStrut_2, gbc_horizontalStrut_2); |
|||
|
|||
lblInputFilename = new JTextArea(0, 40); |
|||
lblInputFilename.setEditable(false); |
|||
lblInputFilename.setLineWrap(true); |
|||
GridBagConstraints gbc_lblInputFilename = new GridBagConstraints(); |
|||
gbc_lblInputFilename.fill = GridBagConstraints.BOTH; |
|||
gbc_lblInputFilename.anchor = GridBagConstraints.NORTHWEST; |
|||
gbc_lblInputFilename.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblInputFilename.gridx = 2; |
|||
gbc_lblInputFilename.gridy = 0; |
|||
panel.add(lblInputFilename, gbc_lblInputFilename); |
|||
|
|||
JLabel lblOutput = new JLabel("Output Filename"); |
|||
lblOutput.setFont(new Font("Lucida Grande", Font.BOLD, 13)); |
|||
lblOutput.setVerticalAlignment(SwingConstants.TOP); |
|||
lblOutput.setHorizontalAlignment(SwingConstants.RIGHT); |
|||
GridBagConstraints gbc_lblOutput = new GridBagConstraints(); |
|||
gbc_lblOutput.anchor = GridBagConstraints.NORTHEAST; |
|||
gbc_lblOutput.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblOutput.gridx = 0; |
|||
gbc_lblOutput.gridy = 1; |
|||
panel.add(lblOutput, gbc_lblOutput); |
|||
|
|||
lblOutputFilename = new JTextArea(0, 40); |
|||
lblOutputFilename.setEditable(false); |
|||
lblOutputFilename.setLineWrap(true); |
|||
lblOutputFilename.addMouseListener(new MouseAdapter() |
|||
{ |
|||
public void mouseClicked(MouseEvent e) |
|||
{ |
|||
pickSaveTarget(); |
|||
} |
|||
}); |
|||
GridBagConstraints gbc_lblOutputFilename = new GridBagConstraints(); |
|||
gbc_lblOutputFilename.fill = GridBagConstraints.BOTH; |
|||
gbc_lblOutputFilename.anchor = GridBagConstraints.NORTHWEST; |
|||
gbc_lblOutputFilename.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblOutputFilename.gridx = 2; |
|||
gbc_lblOutputFilename.gridy = 1; |
|||
panel.add(lblOutputFilename, gbc_lblOutputFilename); |
|||
|
|||
JSeparator separator = new JSeparator(); |
|||
separator.setForeground(Color.DARK_GRAY); |
|||
GridBagConstraints gbc_separator = new GridBagConstraints(); |
|||
gbc_separator.fill = GridBagConstraints.BOTH; |
|||
gbc_separator.gridwidth = 3; |
|||
gbc_separator.insets = new Insets(0, 0, 5, 5); |
|||
gbc_separator.gridx = 0; |
|||
gbc_separator.gridy = 2; |
|||
panel.add(separator, gbc_separator); |
|||
|
|||
Component separatorStrut1 = Box.createVerticalStrut(10); |
|||
GridBagConstraints gbc_strut1 = new GridBagConstraints(); |
|||
gbc_strut1.fill = GridBagConstraints.VERTICAL; |
|||
gbc_strut1.gridwidth = 1; |
|||
gbc_strut1.insets = new Insets(0, 0, 5, 0); |
|||
gbc_strut1.gridx = 3; |
|||
gbc_strut1.gridy = 2; |
|||
panel.add(separatorStrut1, gbc_strut1); |
|||
|
|||
JLabel lblTitle = new JLabel("Full Name"); |
|||
lblTitle.setFont(new Font("Lucida Grande", Font.BOLD, 13)); |
|||
GridBagConstraints gbc_lblTitle = new GridBagConstraints(); |
|||
gbc_lblTitle.anchor = GridBagConstraints.NORTHEAST; |
|||
gbc_lblTitle.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblTitle.gridx = 0; |
|||
gbc_lblTitle.gridy = 3; |
|||
panel.add(lblTitle, gbc_lblTitle); |
|||
|
|||
tfFullName = new JTextField(15); |
|||
if (MobiCommon.safeMode) |
|||
tfFullName.setEditable(false); |
|||
else |
|||
{ |
|||
tfFullName.getDocument().addDocumentListener(new DocumentListener() |
|||
{ |
|||
public void changedUpdate(DocumentEvent arg0) |
|||
{ |
|||
packHeader = true; |
|||
setWindowChangedStatus(true); |
|||
} |
|||
|
|||
public void insertUpdate(DocumentEvent arg0) |
|||
{ |
|||
packHeader = true; |
|||
setWindowChangedStatus(true); |
|||
} |
|||
|
|||
public void removeUpdate(DocumentEvent arg0) |
|||
{ |
|||
packHeader = true; |
|||
setWindowChangedStatus(true); |
|||
} |
|||
}); |
|||
} |
|||
GridBagConstraints gbc_tfFullName = new GridBagConstraints(); |
|||
gbc_tfFullName.fill = GridBagConstraints.HORIZONTAL; |
|||
gbc_tfFullName.anchor = GridBagConstraints.NORTHWEST; |
|||
gbc_tfFullName.insets = new Insets(0, 0, 5, 5); |
|||
gbc_tfFullName.gridx = 2; |
|||
gbc_tfFullName.gridy = 3; |
|||
panel.add(tfFullName, gbc_tfFullName); |
|||
|
|||
JPanel panel_3 = new JPanel(); |
|||
GridBagConstraints gbc_panel_3 = new GridBagConstraints(); |
|||
gbc_panel_3.insets = new Insets(0, 0, 5, 5); |
|||
gbc_panel_3.fill = GridBagConstraints.BOTH; |
|||
gbc_panel_3.gridx = 2; |
|||
gbc_panel_3.gridy = 4; |
|||
panel.add(panel_3, gbc_panel_3); |
|||
|
|||
btnLanguage = new JButton("Language..."); |
|||
btnLanguage.addActionListener(this); |
|||
btnHeaderInfo = new JButton("Header Info..."); |
|||
btnHeaderInfo.addActionListener(this); |
|||
panel_3.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); |
|||
panel_3.add(btnLanguage); |
|||
panel_3.add(btnHeaderInfo); |
|||
|
|||
JLabel lblExthRecords = new JLabel("EXTH Records"); |
|||
lblExthRecords.setFont(new Font("Lucida Grande", Font.BOLD, 13)); |
|||
GridBagConstraints gbc_lblExthRecords = new GridBagConstraints(); |
|||
gbc_lblExthRecords.anchor = GridBagConstraints.NORTHEAST; |
|||
gbc_lblExthRecords.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblExthRecords.gridx = 0; |
|||
gbc_lblExthRecords.gridy = 5; |
|||
panel.add(lblExthRecords, gbc_lblExthRecords); |
|||
|
|||
JScrollPane scrollPane = new JScrollPane(); |
|||
GridBagConstraints gbc_scrollPane = new GridBagConstraints(); |
|||
gbc_scrollPane.fill = GridBagConstraints.BOTH; |
|||
gbc_scrollPane.insets = new Insets(0, 0, 5, 5); |
|||
gbc_scrollPane.gridx = 2; |
|||
gbc_scrollPane.gridy = 5; |
|||
panel.add(scrollPane, gbc_scrollPane); |
|||
|
|||
table = new CustomJTable(model); |
|||
table.getColumnModel().getColumn(0).setPreferredWidth(100); |
|||
table.getSelectionModel().addListSelectionListener(this); |
|||
scrollPane.setViewportView(table); |
|||
|
|||
JPanel panel_1 = new JPanel(); |
|||
GridBagConstraints gbc_panel_1 = new GridBagConstraints(); |
|||
gbc_panel_1.insets = new Insets(0, 0, 5, 5); |
|||
gbc_panel_1.anchor = GridBagConstraints.EAST; |
|||
gbc_panel_1.gridx = 2; |
|||
gbc_panel_1.gridy = 6; |
|||
panel.add(panel_1, gbc_panel_1); |
|||
panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); |
|||
|
|||
buttonAdd = new JButton("+"); |
|||
buttonAdd.addActionListener(this); |
|||
panel_1.add(buttonAdd); |
|||
|
|||
buttonRemove = new JButton("-"); |
|||
buttonRemove.addActionListener(this); |
|||
buttonRemove.setEnabled(false); |
|||
panel_1.add(buttonRemove); |
|||
|
|||
if (MobiCommon.safeMode) |
|||
{ |
|||
buttonAdd.setEnabled(false); |
|||
buttonRemove.setEnabled(false); |
|||
} |
|||
|
|||
JSeparator separator_1 = new JSeparator(); |
|||
separator_1.setForeground(Color.DARK_GRAY); |
|||
GridBagConstraints gbc_separator_1 = new GridBagConstraints(); |
|||
gbc_separator_1.insets = new Insets(0, 0, 5, 5); |
|||
gbc_separator_1.fill = GridBagConstraints.HORIZONTAL; |
|||
gbc_separator_1.gridwidth = 3; |
|||
gbc_separator_1.gridx = 0; |
|||
gbc_separator_1.gridy = 7; |
|||
panel.add(separator_1, gbc_separator_1); |
|||
|
|||
Component separatorStrut2 = Box.createVerticalStrut(10); |
|||
GridBagConstraints gbc_strut2 = new GridBagConstraints(); |
|||
gbc_strut2.fill = GridBagConstraints.VERTICAL; |
|||
gbc_strut2.gridwidth = 1; |
|||
gbc_strut2.insets = new Insets(0, 0, 5, 0); |
|||
gbc_strut2.gridx = 3; |
|||
gbc_strut2.gridy = 7; |
|||
panel.add(separatorStrut2, gbc_strut2); |
|||
|
|||
JPanel panel_2 = new JPanel(); |
|||
GridBagConstraints gbc_panel_2 = new GridBagConstraints(); |
|||
gbc_panel_2.insets = new Insets(0, 0, 0, 5); |
|||
gbc_panel_2.anchor = GridBagConstraints.WEST; |
|||
gbc_panel_2.gridwidth = 3; |
|||
gbc_panel_2.fill = GridBagConstraints.VERTICAL; |
|||
gbc_panel_2.gridx = 0; |
|||
gbc_panel_2.gridy = 8; |
|||
panel.add(panel_2, gbc_panel_2); |
|||
|
|||
buttonSave = new JButton("Save"); |
|||
buttonSave.addActionListener(this); |
|||
panel_2.add(buttonSave); |
|||
|
|||
JMenuBar menuBar = new JMenuBar(); |
|||
frame.setJMenuBar(menuBar); |
|||
|
|||
JMenu mnFile = new JMenu("File"); |
|||
menuBar.add(mnFile); |
|||
|
|||
mntmOpen = new JMenuItem("Open..."); |
|||
mntmOpen.addActionListener(this); |
|||
mnFile.add(mntmOpen); |
|||
|
|||
mntmSave = new JMenuItem("Save"); |
|||
mntmSave.addActionListener(this); |
|||
mnFile.add(mntmSave); |
|||
} |
|||
|
|||
private File getSourceFile() |
|||
{ |
|||
if (openFileChooser == null) |
|||
{ |
|||
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)); |
|||
} |
|||
|
|||
int ret = openFileChooser.showOpenDialog(frame); |
|||
|
|||
File source = openFileChooser.getSelectedFile(); |
|||
|
|||
if (source != null && ret == JFileChooser.APPROVE_OPTION) |
|||
{ |
|||
try |
|||
{ |
|||
prefs.put("lastDirectory", source.getParentFile().getAbsolutePath()); |
|||
model.setModel(source); |
|||
} |
|||
catch (GuiException e) |
|||
{ |
|||
showAlert("Could not parse mobi file: " + e.getMessage()); |
|||
System.exit(0); |
|||
} |
|||
|
|||
lblInputFilename.setText(source.getAbsolutePath()); |
|||
tfFullName.setText(model.getFullName()); |
|||
|
|||
outputFile = getOutputFile(source); |
|||
lblOutputFilename.setText(outputFile.getAbsolutePath()); |
|||
|
|||
// we trigger the window modified indicator when we set tfFullName
|
|||
//
|
|||
setWindowChangedStatus(false); |
|||
packHeader = false; |
|||
} |
|||
|
|||
return source; |
|||
} |
|||
|
|||
private File getOutputFile(File inputFile) |
|||
{ |
|||
File parent = inputFile.getParentFile(); |
|||
String inputName = inputFile.getName(); |
|||
int dot = inputName.lastIndexOf('.'); |
|||
String outputName; |
|||
|
|||
if (dot == -1) |
|||
outputName = inputName + "_new"; |
|||
else |
|||
outputName = inputName.substring(0, dot) + "_new" |
|||
+ inputName.substring(dot); |
|||
|
|||
return new File(parent, outputName); |
|||
} |
|||
|
|||
private void showAlert(String message) |
|||
{ |
|||
JOptionPane.showMessageDialog(frame, message); |
|||
} |
|||
|
|||
// scroll to the newly added/deleted row
|
|||
//
|
|||
public void tableChanged(TableModelEvent event) |
|||
{ |
|||
int eventType = event.getType(); |
|||
int row = event.getLastRow(); |
|||
if (eventType == TableModelEvent.INSERT) |
|||
{ |
|||
table.getSelectionModel().setSelectionInterval(row, row); |
|||
table.scrollRectToVisible(new Rectangle(table.getCellRect(row, 0, |
|||
true))); |
|||
packHeader = true; |
|||
setWindowChangedStatus(true); |
|||
} |
|||
else if (eventType == TableModelEvent.DELETE) |
|||
{ |
|||
boolean select = false; |
|||
int numRows = model.getRowCount(); |
|||
if (numRows > row) |
|||
{ |
|||
select = true; |
|||
} |
|||
else if (numRows > 0) |
|||
{ |
|||
select = true; |
|||
row = numRows - 1; |
|||
} |
|||
if (select) |
|||
{ |
|||
table.getSelectionModel().setSelectionInterval(row, row); |
|||
table.scrollRectToVisible(new Rectangle(table.getCellRect(row, |
|||
0, true))); |
|||
} |
|||
packHeader = true; |
|||
setWindowChangedStatus(true); |
|||
} |
|||
else if (eventType == TableModelEvent.UPDATE) |
|||
{ |
|||
packHeader = true; |
|||
setWindowChangedStatus(true); |
|||
} |
|||
} |
|||
|
|||
protected void setWindowChangedStatus(boolean status) |
|||
{ |
|||
frame.getRootPane().putClientProperty("Window.documentModified", |
|||
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
|
|||
// setLanguages() call so that we can set the window status changed flag
|
|||
//
|
|||
public int getLocale() |
|||
{ |
|||
return model.getLocale(); |
|||
} |
|||
|
|||
// we implement the LanguageModel interface because we want to intercept the
|
|||
// setLanguages() call so that we can set the window status changed flag
|
|||
//
|
|||
public int getDictInput() |
|||
{ |
|||
return model.getDictInput(); |
|||
} |
|||
|
|||
// we implement the LanguageModel interface because we want to intercept the
|
|||
// setLanguages() call so that we can set the window status changed flag
|
|||
//
|
|||
public int getDictOutput() |
|||
{ |
|||
return model.getDictOutput(); |
|||
} |
|||
|
|||
// we implement the LanguageModel interface because we want to intercept the
|
|||
// setLanguages() call so that we can set the window status changed flag
|
|||
//
|
|||
public void setLanguages(int locale, int dictInput, int dictOutput) |
|||
{ |
|||
model.setLanguages(locale, dictInput, dictOutput); |
|||
setWindowChangedStatus(true); |
|||
} |
|||
protected JMenuItem getMntmOpen() { |
|||
return mntmOpen; |
|||
} |
|||
protected JMenuItem getMntmSave() { |
|||
return mntmSave; |
|||
} |
|||
} |
@ -1,6 +0,0 @@ |
|||
package gui; |
|||
|
|||
public interface MetaInfoProvider |
|||
{ |
|||
public String getMetaInfo(); |
|||
} |
@ -1,175 +0,0 @@ |
|||
package gui; |
|||
|
|||
import java.awt.BorderLayout; |
|||
import java.awt.FlowLayout; |
|||
|
|||
import javax.swing.JButton; |
|||
import javax.swing.JDialog; |
|||
import javax.swing.JFrame; |
|||
import javax.swing.JPanel; |
|||
import javax.swing.border.EmptyBorder; |
|||
import javax.swing.JLabel; |
|||
import java.awt.GridBagLayout; |
|||
import java.awt.GridBagConstraints; |
|||
import javax.swing.JComboBox; |
|||
import java.awt.Insets; |
|||
import java.awt.Font; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
|
|||
import javax.swing.JTextField; |
|||
|
|||
import mobimeta.EXTHRecord; |
|||
|
|||
public class NewRecordDialog extends JDialog implements ActionListener |
|||
{ |
|||
|
|||
/** |
|||
* |
|||
*/ |
|||
private static final long serialVersionUID = 1L; |
|||
private final JPanel contentPanel = new JPanel(); |
|||
private JTextField tfValue; |
|||
private JComboBox typeCombo; |
|||
private EXTHAddRecordListener listener; |
|||
private JButton addButton; |
|||
private JButton cancelButton; |
|||
|
|||
|
|||
/** |
|||
* Create the dialog. |
|||
*/ |
|||
public NewRecordDialog(JFrame parent, EXTHAddRecordListener listener) |
|||
{ |
|||
super(parent, true); |
|||
|
|||
this.listener = listener; |
|||
String[] comboValues = new String[EXTHRecord.knownTypes.length]; |
|||
for (int i=0; i<comboValues.length; i++) |
|||
{ |
|||
comboValues[i] = EXTHRecord.knownTypes[i] + " (" + EXTHRecord.knownDesc[i] + ")"; |
|||
} |
|||
|
|||
setBounds(100, 100, 450, 300); |
|||
getContentPane().setLayout(new BorderLayout()); |
|||
contentPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); |
|||
getContentPane().add(contentPanel, BorderLayout.CENTER); |
|||
GridBagLayout gbl_contentPanel = new GridBagLayout(); |
|||
gbl_contentPanel.columnWidths = new int[]{0, 0}; |
|||
gbl_contentPanel.rowHeights = new int[]{0, 0, 0}; |
|||
gbl_contentPanel.columnWeights = new double[]{0.0, 1.0}; |
|||
gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE}; |
|||
contentPanel.setLayout(gbl_contentPanel); |
|||
{ |
|||
JLabel lblType = new JLabel("Type"); |
|||
lblType.setFont(new Font("Lucida Grande", Font.BOLD, 13)); |
|||
GridBagConstraints gbc_lblType = new GridBagConstraints(); |
|||
gbc_lblType.anchor = GridBagConstraints.EAST; |
|||
gbc_lblType.insets = new Insets(0, 0, 5, 5); |
|||
gbc_lblType.gridx = 0; |
|||
gbc_lblType.gridy = 0; |
|||
contentPanel.add(lblType, gbc_lblType); |
|||
} |
|||
{ |
|||
typeCombo = new JComboBox(comboValues); |
|||
GridBagConstraints gbc_typeCombo = new GridBagConstraints(); |
|||
gbc_typeCombo.insets = new Insets(0, 0, 5, 0); |
|||
gbc_typeCombo.fill = GridBagConstraints.HORIZONTAL; |
|||
gbc_typeCombo.gridx = 1; |
|||
gbc_typeCombo.gridy = 0; |
|||
contentPanel.add(typeCombo, gbc_typeCombo); |
|||
} |
|||
{ |
|||
JLabel lblValue = new JLabel("Value"); |
|||
lblValue.setFont(new Font("Lucida Grande", Font.BOLD, 13)); |
|||
GridBagConstraints gbc_lblValue = new GridBagConstraints(); |
|||
gbc_lblValue.anchor = GridBagConstraints.EAST; |
|||
gbc_lblValue.insets = new Insets(0, 0, 0, 5); |
|||
gbc_lblValue.gridx = 0; |
|||
gbc_lblValue.gridy = 1; |
|||
contentPanel.add(lblValue, gbc_lblValue); |
|||
} |
|||
{ |
|||
tfValue = new JTextField(); |
|||
GridBagConstraints gbc_tfValue = new GridBagConstraints(); |
|||
gbc_tfValue.fill = GridBagConstraints.HORIZONTAL; |
|||
gbc_tfValue.gridx = 1; |
|||
gbc_tfValue.gridy = 1; |
|||
contentPanel.add(tfValue, gbc_tfValue); |
|||
tfValue.setColumns(20); |
|||
} |
|||
{ |
|||
JPanel buttonPane = new JPanel(); |
|||
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); |
|||
getContentPane().add(buttonPane, BorderLayout.SOUTH); |
|||
{ |
|||
addButton = new JButton("Add"); |
|||
addButton.addActionListener(this); |
|||
buttonPane.add(addButton); |
|||
getRootPane().setDefaultButton(addButton); |
|||
} |
|||
{ |
|||
cancelButton = new JButton("Cancel"); |
|||
cancelButton.addActionListener(this); |
|||
buttonPane.add(cancelButton); |
|||
} |
|||
} |
|||
|
|||
setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
|||
} |
|||
|
|||
protected JComboBox getTypeCombo() { |
|||
return typeCombo; |
|||
} |
|||
|
|||
public void actionPerformed(ActionEvent event) |
|||
{ |
|||
Object source = event.getSource(); |
|||
|
|||
if (source == addButton) |
|||
{ |
|||
String value = tfValue.getText(); |
|||
if (value.length() == 0) return; |
|||
|
|||
int typeIndex = typeCombo.getSelectedIndex(); |
|||
if (typeIndex == -1) return; |
|||
|
|||
int type = EXTHRecord.knownTypes[typeIndex]; |
|||
EXTHRecord rec = null; |
|||
if (EXTHRecord.isBooleanType(type)) // this is an ugly hack - we really should present a checkbox to the user
|
|||
{ |
|||
boolean boolValue = false; |
|||
if (value.equals("1") |
|||
|| |
|||
value.toLowerCase().equals("true") |
|||
|| |
|||
value.toLowerCase().equals("on") |
|||
|| |
|||
value.toLowerCase().equals("yes")) |
|||
{ |
|||
boolValue = true; |
|||
} |
|||
rec = new EXTHRecord(type, boolValue); |
|||
} |
|||
else |
|||
rec = new EXTHRecord(type, value, GuiModel.getCharacterEncoding()); |
|||
listener.addEXTHRecord(rec); |
|||
setVisible(false); |
|||
dispose(); |
|||
} |
|||
else if (source == cancelButton) |
|||
{ |
|||
setVisible(false); |
|||
dispose(); |
|||
} |
|||
} |
|||
protected JButton getAddButton() { |
|||
return addButton; |
|||
} |
|||
protected JButton getCancelButton() { |
|||
return cancelButton; |
|||
} |
|||
protected JTextField getTfValue() { |
|||
return tfValue; |
|||
} |
|||
} |
Loading…
Reference in new issue