From b841a7f14b875537fef736106902fb4aa3c4314c Mon Sep 17 00:00:00 2001 From: dchandler Date: Sun, 29 Jun 2003 04:18:36 +0000 Subject: [PATCH] The converter GUI can now be run standalone or from Jskad's Tools menu. The converter GUI gives nicer error messages in at least one case. --- source/org/thdl/tib/input/ConvertDialog.java | 68 ++++++++++++++------ source/org/thdl/tib/input/ConverterGUI.java | 62 +++++++++++------- source/org/thdl/tib/input/Jskad.java | 22 +++++++ 3 files changed, 108 insertions(+), 44 deletions(-) diff --git a/source/org/thdl/tib/input/ConvertDialog.java b/source/org/thdl/tib/input/ConvertDialog.java index 3aefe33..7718a32 100644 --- a/source/org/thdl/tib/input/ConvertDialog.java +++ b/source/org/thdl/tib/input/ConvertDialog.java @@ -60,11 +60,12 @@ class ConvertDialog extends JDialog File oldFile, newFile; String default_directory; - final String BROWSENEW = "Browse"; + final String BROWSENEW = "Browse..."; final String BROWSEOLD = BROWSENEW; final String CONVERT = "Convert"; final String CANCEL = "Close"; final String ABOUT = "About"; + final String OPEN_WITH = "Open With..."; private final ThdlActionListener tal = new ThdlActionListener() { public void theRealActionPerformed(ActionEvent e) { @@ -123,6 +124,10 @@ class ConvertDialog extends JDialog browseNew.addActionListener(tal); } temp.add(browseNew); + openDoc = new JButton(OPEN_WITH); + openDoc.addActionListener(tal); + openDoc.setEnabled(false); + temp.add(openDoc); content.add(temp); buttonBox = Box.createHorizontalBox(); @@ -137,12 +142,6 @@ class ConvertDialog extends JDialog buttonBox.add(cancel); buttonBox.add(Box.createHorizontalGlue()); - openDoc = new JButton("Open Document"); - openDoc.addActionListener(tal); - buttonBox.add(openDoc); - buttonBox.add(Box.createHorizontalGlue()); - openDoc.setVisible(false); - about = new JButton(ABOUT); about.addActionListener(tal); buttonBox.add(about); @@ -151,7 +150,7 @@ class ConvertDialog extends JDialog content.add(buttonBox); setContentPane(content); pack(); - setSize(new Dimension(500,200)); + setSize(new Dimension(620,200)); } public void setChoices(String[] choices) @@ -204,11 +203,18 @@ class ConvertDialog extends JDialog return newFile; } - public ConvertDialog(FontConversion controller, + public ConvertDialog(Frame owner, + FontConversion controller, String[] choices, boolean modal) { - super(new JDialog(),PROGRAM_TITLE,modal); + super(owner,PROGRAM_TITLE,modal); + initConvertDialog(controller, choices, modal); + } + + private void initConvertDialog(FontConversion controller, + String[] choices, + boolean modal) { setController(controller); setChoices(choices); init(); @@ -217,6 +223,14 @@ class ConvertDialog extends JDialog + getDefaultCloseOperation()); } + public ConvertDialog(FontConversion controller, + String[] choices, + boolean modal) + { + super(new JDialog(),PROGRAM_TITLE,modal); + initConvertDialog(controller, choices, modal); + } + void theRealActionPerformed(ActionEvent ae) { String cmd = ae.getActionCommand(); @@ -239,12 +253,9 @@ class ConvertDialog extends JDialog newTextField.setText(chosenFile.getPath()); newFieldChanged = false; newFile = chosenFile; - openDoc.setVisible(false); + openDoc.setEnabled(true); } } else if(cmd.equals(CONVERT)) { - if (debug) - System.out.println("Need to write checks for complete info..."); - if(oldFieldChanged || getOldFile() == null) { if (debug) System.out.println("old field changed"); @@ -282,16 +293,33 @@ class ConvertDialog extends JDialog // allow it. } - // Success or failure is immaterial; we still want to bust - // out the "Open Document" button. + if (newFile.exists()) { + int overwriteExisingFile + = JOptionPane.showConfirmDialog(this, + "Do you want to overwrite " + + newFile.getName() + "?", + "Please select", + JOptionPane.YES_NO_OPTION); + + switch (overwriteExisingFile) { + case JOptionPane.YES_OPTION: // continue. + break; + default: + return; + } + } + controller.doConversion(this, getOldFile(), getNewFile(), (String)choices.getSelectedItem()); oldFieldChanged = false; newFieldChanged = false; - openDoc.setVisible(true); - } else if(cmd.equals("Open Document")) { + // Success or failure is immaterial; we still want to + // enable the OPEN_WITH button. If the conversion failed, + // the document contains the weird glyphs. + openDoc.setEnabled(true); + } else if(cmd.equals(OPEN_WITH)) { try { if(newFile == null) {return;} boolean done = false; @@ -323,9 +351,7 @@ class ConvertDialog extends JDialog JOptionPane.ERROR_MESSAGE); } } else if(cmd.equals(CANCEL)) { - System.runFinalization(); this.dispose(); - System.exit(0); } else if(cmd.equals(ABOUT)) { JOptionPane.showMessageDialog(this, "This Tibetan Converter is Copyright 2003\nTibetan and Himalayan Digital Library and\nis protected by the THDL Open Community\nLicense Version 1.0.\n\nCompiled " + ThdlVersion.getTimeOfCompilation(), @@ -396,7 +422,7 @@ class ConvertDialog extends JDialog public File updateFile(File setFile, JTextField textField) { - if(textField.equals(newTextField)) {openDoc.setVisible(false);} + if(textField.equals(newTextField)) {openDoc.setEnabled(false);} String txt = textField.getText(); if (txt.equals("")) return null; diff --git a/source/org/thdl/tib/input/ConverterGUI.java b/source/org/thdl/tib/input/ConverterGUI.java index 05e7e7f..7ccf5b3 100644 --- a/source/org/thdl/tib/input/ConverterGUI.java +++ b/source/org/thdl/tib/input/ConverterGUI.java @@ -23,6 +23,8 @@ import java.io.*; import org.thdl.util.*; import org.thdl.tib.text.*; import javax.swing.JOptionPane; +import java.awt.Frame; +import java.awt.Dialog; /** DLC FIXMEDOC * @author David Chandler */ @@ -30,16 +32,14 @@ public class ConverterGUI implements FontConversion, FontConverterConstants { /** Default constructor; does nothing */ ConverterGUI() { } - static { - // No need for the TM or TMW fonts. - System.setProperty("thdl.rely.on.system.tmw.fonts", "true"); - System.setProperty("thdl.do.not.rely.on.system.tmw.fonts", "false"); - } - /** * Runs the converter. */ public static void main(String[] args) { - System.exit(realMain(args, System.out)); + // No need for the TM or TMW fonts. + System.setProperty("thdl.rely.on.system.tmw.fonts", "true"); + System.setProperty("thdl.do.not.rely.on.system.tmw.fonts", "false"); + + System.exit(realMain(args, System.out, null)); } // DLC FIXMEDOC returns true on success @@ -61,7 +61,13 @@ public class ConverterGUI implements FontConversion, FontConverterConstants { JOptionPane.ERROR_MESSAGE); return false; } - if (0 != returnCode) { + if (3 == returnCode) { + JOptionPane.showMessageDialog(cd, + TibetanConverter.rtfErrorMessage, + "Conversion failed", + JOptionPane.ERROR_MESSAGE); + return false; + } else if (0 != returnCode) { JOptionPane.showMessageDialog(cd, "The conversion failed with code " + returnCode + "; please e-mail\ndchandler@users.sourceforge.net to learn what that means if\nyou can't find out from the output.", "Conversion failed", @@ -87,22 +93,32 @@ public class ConverterGUI implements FontConversion, FontConverterConstants { /** Runs the converter without exiting the program. * @return the exit code. */ - public static int realMain(String[] args, PrintStream out) { + public static int realMain(String[] args, PrintStream out, Frame owner) { + returnCode = 0; try { - - final ConvertDialog convDialog - = new ConvertDialog(new ConverterGUI(), - new String[]{ - TM_TO_TMW, - TMW_TO_UNI, - TMW_TO_WYLIE, - TMW_TO_TM, - FIND_SOME_NON_TMW, - FIND_SOME_NON_TM, - FIND_ALL_NON_TMW, - FIND_ALL_NON_TM - }, - true); + final ConvertDialog convDialog; + String[] choices = new String[]{ + TM_TO_TMW, + TMW_TO_UNI, + TMW_TO_WYLIE, + TMW_TO_TM, + FIND_SOME_NON_TMW, + FIND_SOME_NON_TM, + FIND_ALL_NON_TMW, + FIND_ALL_NON_TM + }; + if (null == owner) { + convDialog + = new ConvertDialog(new ConverterGUI(), + choices, + true); + } else { + convDialog + = new ConvertDialog(owner, + new ConverterGUI(), + choices, + true); + } /* Make it so that any time the user exits this program by * (almost) any means, the user's preferences are saved if diff --git a/source/org/thdl/tib/input/Jskad.java b/source/org/thdl/tib/input/Jskad.java index 6f90fcb..773c666 100644 --- a/source/org/thdl/tib/input/Jskad.java +++ b/source/org/thdl/tib/input/Jskad.java @@ -418,10 +418,32 @@ public class Jskad extends JPanel implements DocumentListener { } } }); + + JMenuItem converterItem = new JMenuItem("Launch Converter..."); + converterItem.addActionListener(new ThdlActionListener() { + public void theRealActionPerformed(ActionEvent e) { + int rv = ConverterGUI.realMain(new String[] { }, + System.out, + ((parentObject instanceof Frame) + ? (Frame)parentObject + : null)); + if (rv == 0) { + JOptionPane.showMessageDialog(Jskad.this, + "Converter closed normally.", + "Converter Done", JOptionPane.PLAIN_MESSAGE); + } else { + JOptionPane.showMessageDialog(Jskad.this, + "Converter closed abnormally.", + "Converter Error", JOptionPane.ERROR_MESSAGE); + } + } + }); toolsMenu.addSeparator(); toolsMenu.add(toTMItem); toolsMenu.add(toTMWItem); toolsMenu.add(toUnicodeItem); + toolsMenu.addSeparator(); + toolsMenu.add(converterItem); }