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.
This commit is contained in:
dchandler 2003-06-29 04:18:36 +00:00
parent 7938648ca8
commit b841a7f14b
3 changed files with 108 additions and 44 deletions

View file

@ -60,11 +60,12 @@ class ConvertDialog extends JDialog
File oldFile, newFile; File oldFile, newFile;
String default_directory; String default_directory;
final String BROWSENEW = "Browse"; final String BROWSENEW = "Browse...";
final String BROWSEOLD = BROWSENEW; final String BROWSEOLD = BROWSENEW;
final String CONVERT = "Convert"; final String CONVERT = "Convert";
final String CANCEL = "Close"; final String CANCEL = "Close";
final String ABOUT = "About"; final String ABOUT = "About";
final String OPEN_WITH = "Open With...";
private final ThdlActionListener tal = new ThdlActionListener() { private final ThdlActionListener tal = new ThdlActionListener() {
public void theRealActionPerformed(ActionEvent e) { public void theRealActionPerformed(ActionEvent e) {
@ -123,6 +124,10 @@ class ConvertDialog extends JDialog
browseNew.addActionListener(tal); browseNew.addActionListener(tal);
} }
temp.add(browseNew); temp.add(browseNew);
openDoc = new JButton(OPEN_WITH);
openDoc.addActionListener(tal);
openDoc.setEnabled(false);
temp.add(openDoc);
content.add(temp); content.add(temp);
buttonBox = Box.createHorizontalBox(); buttonBox = Box.createHorizontalBox();
@ -137,12 +142,6 @@ class ConvertDialog extends JDialog
buttonBox.add(cancel); buttonBox.add(cancel);
buttonBox.add(Box.createHorizontalGlue()); 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 = new JButton(ABOUT);
about.addActionListener(tal); about.addActionListener(tal);
buttonBox.add(about); buttonBox.add(about);
@ -151,7 +150,7 @@ class ConvertDialog extends JDialog
content.add(buttonBox); content.add(buttonBox);
setContentPane(content); setContentPane(content);
pack(); pack();
setSize(new Dimension(500,200)); setSize(new Dimension(620,200));
} }
public void setChoices(String[] choices) public void setChoices(String[] choices)
@ -204,11 +203,18 @@ class ConvertDialog extends JDialog
return newFile; return newFile;
} }
public ConvertDialog(FontConversion controller, public ConvertDialog(Frame owner,
FontConversion controller,
String[] choices, String[] choices,
boolean modal) 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); setController(controller);
setChoices(choices); setChoices(choices);
init(); init();
@ -217,6 +223,14 @@ class ConvertDialog extends JDialog
+ getDefaultCloseOperation()); + getDefaultCloseOperation());
} }
public ConvertDialog(FontConversion controller,
String[] choices,
boolean modal)
{
super(new JDialog(),PROGRAM_TITLE,modal);
initConvertDialog(controller, choices, modal);
}
void theRealActionPerformed(ActionEvent ae) void theRealActionPerformed(ActionEvent ae)
{ {
String cmd = ae.getActionCommand(); String cmd = ae.getActionCommand();
@ -239,12 +253,9 @@ class ConvertDialog extends JDialog
newTextField.setText(chosenFile.getPath()); newTextField.setText(chosenFile.getPath());
newFieldChanged = false; newFieldChanged = false;
newFile = chosenFile; newFile = chosenFile;
openDoc.setVisible(false); openDoc.setEnabled(true);
} }
} else if(cmd.equals(CONVERT)) { } else if(cmd.equals(CONVERT)) {
if (debug)
System.out.println("Need to write checks for complete info...");
if(oldFieldChanged || getOldFile() == null) { if(oldFieldChanged || getOldFile() == null) {
if (debug) if (debug)
System.out.println("old field changed"); System.out.println("old field changed");
@ -282,16 +293,33 @@ class ConvertDialog extends JDialog
// allow it. // allow it.
} }
// Success or failure is immaterial; we still want to bust if (newFile.exists()) {
// out the "Open Document" button. 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, controller.doConversion(this,
getOldFile(), getOldFile(),
getNewFile(), getNewFile(),
(String)choices.getSelectedItem()); (String)choices.getSelectedItem());
oldFieldChanged = false; oldFieldChanged = false;
newFieldChanged = false; newFieldChanged = false;
openDoc.setVisible(true); // Success or failure is immaterial; we still want to
} else if(cmd.equals("Open Document")) { // 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 { try {
if(newFile == null) {return;} if(newFile == null) {return;}
boolean done = false; boolean done = false;
@ -323,9 +351,7 @@ class ConvertDialog extends JDialog
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
} }
} else if(cmd.equals(CANCEL)) { } else if(cmd.equals(CANCEL)) {
System.runFinalization();
this.dispose(); this.dispose();
System.exit(0);
} else if(cmd.equals(ABOUT)) { } else if(cmd.equals(ABOUT)) {
JOptionPane.showMessageDialog(this, 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(), "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) 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(); String txt = textField.getText();
if (txt.equals("")) if (txt.equals(""))
return null; return null;

View file

@ -23,6 +23,8 @@ import java.io.*;
import org.thdl.util.*; import org.thdl.util.*;
import org.thdl.tib.text.*; import org.thdl.tib.text.*;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import java.awt.Frame;
import java.awt.Dialog;
/** DLC FIXMEDOC /** DLC FIXMEDOC
* @author David Chandler */ * @author David Chandler */
@ -30,16 +32,14 @@ public class ConverterGUI implements FontConversion, FontConverterConstants {
/** Default constructor; does nothing */ /** Default constructor; does nothing */
ConverterGUI() { } 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. */ * Runs the converter. */
public static void main(String[] args) { 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 // DLC FIXMEDOC returns true on success
@ -61,7 +61,13 @@ public class ConverterGUI implements FontConversion, FontConverterConstants {
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
return false; 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, 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.", "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", "Conversion failed",
@ -87,12 +93,11 @@ public class ConverterGUI implements FontConversion, FontConverterConstants {
/** Runs the converter without exiting the program. /** Runs the converter without exiting the program.
* @return the exit code. */ * @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 { try {
final ConvertDialog convDialog;
final ConvertDialog convDialog String[] choices = new String[]{
= new ConvertDialog(new ConverterGUI(),
new String[]{
TM_TO_TMW, TM_TO_TMW,
TMW_TO_UNI, TMW_TO_UNI,
TMW_TO_WYLIE, TMW_TO_WYLIE,
@ -101,8 +106,19 @@ public class ConverterGUI implements FontConversion, FontConverterConstants {
FIND_SOME_NON_TM, FIND_SOME_NON_TM,
FIND_ALL_NON_TMW, FIND_ALL_NON_TMW,
FIND_ALL_NON_TM FIND_ALL_NON_TM
}, };
if (null == owner) {
convDialog
= new ConvertDialog(new ConverterGUI(),
choices,
true); true);
} else {
convDialog
= new ConvertDialog(owner,
new ConverterGUI(),
choices,
true);
}
/* Make it so that any time the user exits this program by /* Make it so that any time the user exits this program by
* (almost) any means, the user's preferences are saved if * (almost) any means, the user's preferences are saved if

View file

@ -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.addSeparator();
toolsMenu.add(toTMItem); toolsMenu.add(toTMItem);
toolsMenu.add(toTMWItem); toolsMenu.add(toTMWItem);
toolsMenu.add(toUnicodeItem); toolsMenu.add(toUnicodeItem);
toolsMenu.addSeparator();
toolsMenu.add(converterItem);
} }