Added GUI support for color-coding. Added support for color-coding

and choosing the warning level to TibetanConverter.

Better error checking in the GUI converter.
This commit is contained in:
dchandler 2003-09-06 22:56:10 +00:00
parent 1308f14807
commit 0d6d6ed611
8 changed files with 141 additions and 74 deletions

View file

@ -37,6 +37,9 @@ class ConvertDialog extends JDialog
{
private static final boolean debug = false;
private JCheckBox colors;
private static final String colorDesc = "Color-coding (ACIP to RTF only)";
// Attributes
private FontConversion controller;
@ -95,6 +98,11 @@ class ConvertDialog extends JDialog
updateWarningLevels();
temp.add(warningLevels);
this.colors = new JCheckBox(colorDesc, false);
this.colors.addActionListener(tal);
updateWarningLevels();
temp.add(colors);
content.add(temp);
temp = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
@ -151,7 +159,7 @@ class ConvertDialog extends JDialog
content.add(buttonBox);
setContentPane(content);
pack();
setSize(new Dimension(620,200));
setSize(new Dimension(640,235));
}
private void setChoices(String[] choices)
@ -226,6 +234,13 @@ class ConvertDialog extends JDialog
JOptionPane.ERROR_MESSAGE);
return;
}
if ("".equals(newTextField.getText())) {
JOptionPane.showMessageDialog(this,
"Choose a target file.",
"No target chosen",
JOptionPane.ERROR_MESSAGE);
return;
}
File convertedFile = new File(newTextField.getText());
if(null == convertedFile) {
JOptionPane.showMessageDialog(this,
@ -274,7 +289,8 @@ class ConvertDialog extends JDialog
origFile,
convertedFile,
(String)choices.getSelectedItem(),
(String)warningLevels.getSelectedItem());
(String)warningLevels.getSelectedItem(),
colors.isSelected());
} catch (OutOfMemoryError e) {
JOptionPane.showMessageDialog(this,
"The converter ran out of memory. Please give the\nJVM more memory by using java -XmxYYYm where YYY\nis the amount of memory your system has, or\nsomething close to it. E.g., try\n'java -Xmx512m -jar Jskad.jar'.",

View file

@ -48,15 +48,23 @@ public class ConverterGUI implements FontConversion, FontConverterConstants {
}
public boolean doConversion(ConvertDialog cd, File oldFile, File newFile,
String whichConversion, String warningLevel) {
String whichConversion, String warningLevel,
boolean colors) {
PrintStream ps;
try {
if (whichConversion == ACIP_TO_UNI_TEXT) {
JOptionPane.showMessageDialog(cd,
"This conversion will lose information about relative font sizes.\n{KA (KHA) GA} will be treated like {KA KHA GA}, that is.",
"Loss of information may result",
JOptionPane.WARNING_MESSAGE);
}
returnCode
= TibetanConverter.reallyConvert(new FileInputStream(oldFile),
ps = new PrintStream(new FileOutputStream(newFile),
false),
whichConversion,
warningLevel);
warningLevel,
colors);
ps.close();
} catch (FileNotFoundException e) {
returnCode = 39;

View file

@ -35,8 +35,10 @@ interface FontConversion
displaying the results if you want happy users. The
conversion performed is specified by the interned String
whichConversion, which must be one of the known conversions.
If you want colors to be used in the output (which is only
supported by a few conversions), then colors must be true.
@return true on success, false otherwise */
boolean doConversion(ConvertDialog cd, File oldFile,
File newFile, String whichConversion,
String warningLevel);
String warningLevel, boolean colors);
}

View file

@ -99,6 +99,10 @@ public class TMW_RTF_TO_THDL_WYLIETest extends TestCase {
private void helper(String mode, String extension, int erc) {
String[] args = new String[] {
"--colors",
"no",
"--warning-level",
"All",
mode,
getTestFileName("Test1")
};

View file

@ -78,40 +78,51 @@ public class TibetanConverter implements FontConverterConstants {
boolean findAllNonTMWMode = false;
boolean findSomeNonTMMode = false;
boolean findAllNonTMMode = false;
boolean colors = false;
// Process arguments:
if ((args.length != 1 && args.length != 2)
final int numArgs = 6;
if ((args.length != 1 && args.length != numArgs)
|| (args.length == 1
&& !(args[0].equals("-v")
|| args[0].equals("--version")))
|| (args.length == 2
&& !((findAllNonTMWMode
= args[0].equals("--find-all-non-tmw"))
|| (convertToTMMode
= args[0].equals("--to-tibetan-machine"))
|| (convertToTMWMode
= args[0].equals("--to-tibetan-machine-web"))
|| (convertACIPToUniMode
= args[0].equals("--acip-to-unicode"))
|| (convertACIPToTMWMode
= args[0].equals("--acip-to-tmw"))
|| (convertToUnicodeMode
= args[0].equals("--to-unicode"))
|| (convertToWylieRTFMode
= args[0].equals("--to-wylie"))
|| (convertToWylieTextMode
= args[0].equals("--to-wylie-text"))
|| (convertToACIPRTFMode
= args[0].equals("--to-acip"))
|| (convertToACIPTextMode
= args[0].equals("--to-acip-text"))
|| (findSomeNonTMWMode
= args[0].equals("--find-some-non-tmw"))
|| (findSomeNonTMMode
= args[0].equals("--find-some-non-tm"))
|| (findAllNonTMMode
= args[0].equals("--find-all-non-tm"))
))) {
|| (args.length == numArgs
&& (!(args[numArgs - 6].equals("--colors"))
|| !((colors = args[numArgs - 5].equals("yes"))
|| args[numArgs - 5].equals("no"))
|| !(args[numArgs - 4].equals("--warning-level"))
|| !(args[numArgs - 3].equals("Most")
|| args[numArgs - 3].equals("Some")
|| args[numArgs - 3].equals("All")
|| args[numArgs - 3].equals("None"))
|| !((findAllNonTMWMode
= args[numArgs - 2].equals("--find-all-non-tmw"))
|| (convertToTMMode
= args[numArgs - 2].equals("--to-tibetan-machine"))
|| (convertToTMWMode
= args[numArgs - 2].equals("--to-tibetan-machine-web"))
|| (convertACIPToUniMode
= args[numArgs - 2].equals("--acip-to-unicode"))
|| (convertACIPToTMWMode
= args[numArgs - 2].equals("--acip-to-tmw"))
|| (convertToUnicodeMode
= args[numArgs - 2].equals("--to-unicode"))
|| (convertToWylieRTFMode
= args[numArgs - 2].equals("--to-wylie"))
|| (convertToWylieTextMode
= args[numArgs - 2].equals("--to-wylie-text"))
|| (convertToACIPRTFMode
= args[numArgs - 2].equals("--to-acip"))
|| (convertToACIPTextMode
= args[numArgs - 2].equals("--to-acip-text"))
|| (findSomeNonTMWMode
= args[numArgs - 2].equals("--find-some-non-tmw"))
|| (findSomeNonTMMode
= args[numArgs - 2].equals("--find-some-non-tm"))
|| (findAllNonTMMode
= args[numArgs - 2].equals("--find-all-non-tm"))
)))) {
out.println("TibetanConverter --find-all-non-tmw | --find-some-non-tmw");
out.println(" | --to-tibetan-machine | --to-tibetan-machine-web");
out.println(" | --to-unicode | --to-wylie | --to-acip");
@ -224,8 +235,7 @@ public class TibetanConverter implements FontConverterConstants {
}
}
return reallyConvert(in, out, conversionTag,
"Most" // DLC make me configurable
);
args[numArgs - 3].intern(), colors);
} catch (ThdlLazyException e) {
out.println("TibetanConverter has a BUG:");
e.getRealException().printStackTrace(out);
@ -243,11 +253,12 @@ public class TibetanConverter implements FontConverterConstants {
return code so that TibetanConverter's usage message is
honored. */
static int reallyConvert(InputStream in, PrintStream out, String ct,
String warningLevel) {
String warningLevel, boolean colors) {
if (ACIP_TO_UNI_TEXT == ct || ACIP_TO_TMW == ct) {
try {
ArrayList al = ACIPTshegBarScanner.scanStream(in, null,
250 - 1 // DLC FIXME: make me configurable
ThdlOptions.getIntegerOption("thdl.most.errors.a.tibetan.acip.document.can.have",
250 - 1)
);
if (null == al)
return 47;
@ -259,9 +270,10 @@ public class TibetanConverter implements FontConverterConstants {
warningLevel))
return 46;
} else {
if (ct != ACIP_TO_TMW) throw new Error("badness");
if (!ACIPConverter.convertToTMW(al, out, null, warnings,
embeddedWarnings,
warningLevel))
warningLevel, colors))
return 46;
}
if (embeddedWarnings && warnings.length() > 0)
@ -295,7 +307,6 @@ public class TibetanConverter implements FontConverterConstants {
+ rtfErrorMessage);
return 3;
}
try {
in.close();
} catch (IOException e) {

View file

@ -145,9 +145,14 @@ public class TibetanDocument extends DefaultStyledDocument {
}
}
private static boolean allowColors = true; // DLC FIXME: make me configurable
private static boolean allowColors = false;
/** Enables the use of colors. */
public static void enableColors() { allowColors = true; }
/** Disables the use of colors. */
public static void disableColors() { allowColors = false; }
/** Returns true if and only if the use of colors is currently
* enabled. */
public static boolean colorsEnabled() { return allowColors; }
/**
@ -167,6 +172,8 @@ public class TibetanDocument extends DefaultStyledDocument {
* automatically, according to the current Roman font size.
* @param offset the position at which you want to insert text
* @param s the string you want to insert
* @param color the color in which to insert, which is used if and only
* if {@link #colorsEnabled() colors are enabled}
* @see #setRomanAttributeSet(AttributeSet)
*/
public void appendRoman(int offset, String s, Color color) throws BadLocationException {
@ -180,6 +187,8 @@ public class TibetanDocument extends DefaultStyledDocument {
* Inserts Latin text at the end of the document. The font size is
* applied automatically, according to the current Roman font size.
* @param s the string you want to insert
* @param color the color in which to insert, which is used if and only
* if {@link #colorsEnabled() colors are enabled}
* @see #setRomanAttributeSet(AttributeSet)
*/
public void appendRoman(String s, Color color) {
@ -203,8 +212,10 @@ public class TibetanDocument extends DefaultStyledDocument {
/**
* Inserts a stretch of TibetanMachineWeb data into the document.
* @param glyphs the array of Tibetan data you want to insert
* @param pos the position at which you want to insert text
* @param glyphs the array of Tibetan data you want to insert
* @param color the color in which to insert, which is used if and only
* if {@link #colorsEnabled() colors are enabled}
*/
public int insertDuff(int pos, DuffData[] glyphs, Color color) {
return insertDuff(tibetanFontSize, pos, glyphs, true, color);
@ -216,6 +227,9 @@ public class TibetanDocument extends DefaultStyledDocument {
/**
* Appends all DuffCodes in glyphs to the end of this document.
* @param glyphs the array of Tibetan data you want to insert
* @param color the color in which to insert, which is used if and only
* if {@link #colorsEnabled() colors are enabled}
*/
public void appendDuffCodes(DuffCode[] glyphs, Color color) {
// PERFORMANCE FIXME: this isn't so speedy, but it reuses

View file

@ -38,6 +38,14 @@ import org.thdl.tib.text.DuffCode;
public class ACIPConverter {
// DLC NOW: (KA)'s info is lost when you convert to Unicode text instead of Unicode RTF. Give an ERROR.
// DLC NOW: IMPLEMENT (KA) font shrinking
// DLC NOW: BAo isn't converting.
// DLC NOW: tRAStA is not converter correctly to Unicode, and no
// warning is given when converting to TMW (Wait! isn't the "a
// stack occurs w/o a vowel" warning given?)
/** Command-line converter. Gives error messages on standard
* output about why we can't convert the document perfectly and
* exits with non-zero return code, or is silent otherwise and
@ -51,8 +59,6 @@ public class ACIPConverter {
ThdlOptions.setUserPreference("thdl.rely.on.system.tm.fonts", true);
ThdlOptions.setUserPreference("thdl.debug", true);
TibetanDocument.enableColors();
boolean verbose = true;
if (args.length != 1) {
System.out.println("Bad args! Need just the name of the ACIP text file.");
@ -74,7 +80,6 @@ public class ACIPConverter {
System.exit(1);
}
final boolean abortUponScanningError = false;
// DLC NOW: BAo isn't converting.
if (errors.length() > 0) {
System.err.println("Errors scanning ACIP input file: ");
System.err.println(errors);
@ -84,7 +89,8 @@ public class ACIPConverter {
}
}
String warningLevel = "Most"; // DLC make me configurable.
String warningLevel = "Most";
boolean colors = true;
StringBuffer warnings = null;
boolean putWarningsInOutput = false;
if ("None" != warningLevel) {
@ -92,7 +98,7 @@ public class ACIPConverter {
putWarningsInOutput = true;
}
convertToTMW(al, System.out, errors, warnings,
putWarningsInOutput, warningLevel);
putWarningsInOutput, warningLevel, colors);
int retCode = 0;
if (errors.length() > 0) {
System.err.println("Errors converting ACIP input file: ");
@ -112,8 +118,6 @@ public class ACIPConverter {
if (verbose) System.err.println("Converted " + args[0] + " perfectly.");
}
System.exit(retCode);
// DLC NOW: tRAStA is not converter correctly to Unicode, and
// no warning is given when converting to TMW.
}
/** Writes TMW/Latin to out. If errors occur in converting a
@ -124,6 +128,9 @@ public class ACIPConverter {
* warnings if warnings is non-null. Returns true upon perfect
* success or if there were merely warnings, false if errors
* occurred.
* @param colors true if and only if you want Sanskrit in one
* color, errors/warnings in another, and tsheg-bars affected by
* prefix rules in another
* @throws IOException if we cannot write to out
*/
public static boolean convertToTMW(ArrayList scan,
@ -131,7 +138,8 @@ public class ACIPConverter {
StringBuffer errors,
StringBuffer warnings,
boolean writeWarningsToResult,
String warningLevel)
String warningLevel,
boolean colors)
throws IOException
{
TibetanDocument tdoc = new TibetanDocument();
@ -141,7 +149,7 @@ public class ACIPConverter {
20));
boolean rv
= convertToTMW(scan, tdoc, errors, warnings,
writeWarningsToResult, warningLevel);
writeWarningsToResult, warningLevel, colors);
tdoc.writeRTFOutputStream(out);
return rv;
}
@ -151,17 +159,14 @@ public class ACIPConverter {
StringBuffer errors,
StringBuffer warnings,
boolean writeWarningsToResult,
String warningLevel)
String warningLevel,
boolean colors)
throws IOException
{
return convertTo(false, scan, null, tdoc, errors, warnings,
writeWarningsToResult, warningLevel);
writeWarningsToResult, warningLevel, colors);
}
// DLC FIXME: sometimes { } is \u0F0B, and sometimes it is a
// space. Treat it as a tsheg only when it appears after a
// syllable or another tsheg.
/** Returns UTF-8 encoded Unicode. A bit indirect, so use this
* for testing only if performance is a concern. If errors occur
* in scanning the ACIP or in converting a tsheg bar, then they
@ -194,16 +199,20 @@ public class ACIPConverter {
}
}
/** Writes Unicode to out. If errors occur in converting a tsheg
* bar, then they are appended to errors if errors is non-null.
* Furthermore, errors are written to out. If writeWarningsToOut
* is true, then warnings also will be written to out.
/** Writes Unicode text (not RTF) to out. <em>NOTE WELL: This
* inherently cannot describe the ACIP {(KA) KHA} properly, as
* that requires showing KA in a smaller font than KHA, which is
* not possible in plain text.</em> If errors occur in converting
* a tsheg bar, then they are appended to errors if errors is
* non-null. Furthermore, errors are written to out. If
* writeWarningsToOut is true, then warnings also will be written
* to out.
* @return true upon perfect success, false if errors occurred.
* @param scan result of ACIPTshegBarScanner.scan(..)
* @param out stream to which to write converted text
* @param errors if non-null, all error messages are appended
* @param warnings if non-null, all warning messages are appended
* to this
* @param warnings if non-null, all warning messages appropriate
* to warningLevel are appended
* @param writeWarningsToOut if true, then all warning messages
* are written to out in the appropriate places
* @throws IOException if we cannot write to out
@ -217,7 +226,7 @@ public class ACIPConverter {
throws IOException
{
return convertTo(true, scan, out, null, errors, warnings,
writeWarningsToOut, warningLevel);
writeWarningsToOut, warningLevel, false);
}
private static boolean peekaheadFindsSpacesAndComma(ArrayList /* of ACIPString */ scan,
@ -245,9 +254,14 @@ public class ACIPConverter {
StringBuffer errors,
StringBuffer warnings,
boolean writeWarningsToOut,
String warningLevel)
String warningLevel,
boolean colors)
throws IOException
{
if (colors)
tdoc.enableColors();
else
tdoc.disableColors();
int sz = scan.size();
boolean hasErrors = false;
BufferedWriter writer = null;
@ -276,7 +290,6 @@ public class ACIPConverter {
if (null != writer) writer.write(text);
if (null != tdoc) tdoc.appendRoman(text, Color.RED);
}
// DLC NOW: Warning: We're going with {'}{R}{DA}, but only because our knowledge of prefix rules says that {'}{R+DA} is not a legal Tibetan tsheg bar ("syllable")
if (null != warnings) {
warnings.append("Warning: Lexical warning: ");
@ -357,6 +370,8 @@ public class ACIPConverter {
color = Color.BLACK;
} else {
// Sanskrit
// DLC FIXME: a funny vowel, the presence of a sanskrit-only stack, and a funny mark like ACIP ':' should cause green too.
color = Color.GREEN;
}
@ -453,7 +468,6 @@ public class ACIPConverter {
if (null != tdoc) {
if (null != duff && 0 != duff.length) {
tdoc.appendDuffCodes(duff, color);
// DLC NOW FIXME: use TibTextUtils.getVowel logic to make the output beautiful.
} else {
// this happens when you have an
// [#ERROR]-producing tsheg bar.
@ -471,6 +485,3 @@ public class ACIPConverter {
return !hasErrors;
}
}
// DLC FIXME: putting Tibetan in black, Sanskrit in green, and Latin
// in yellow would help you quickly decide if ZHIGN maybe should've
// been ZHING.

View file

@ -276,6 +276,7 @@ class TParseTree {
TStackListList up = getUniqueParse(false);
if (null == up || up.size() != 1) {
// DLC FIXME: code duplication
boolean isLastStack[] = new boolean[1];
TStackListList nip = getNonIllegalParses();
if (nip.size() != 1) {
@ -285,9 +286,9 @@ class TParseTree {
if (getBestParse().hasStackWithoutVowel(pl, isLastStack)) {
if (isLastStack[0]) {
if (warningLevel == "All" || warningLevel == "Most")
return "Warning: The last stack does not have a vowel in the ACIP {" + ((null != originalACIP) ? originalACIP : recoverACIP()) + "}";
return "Warning: The last stack does not have a vowel in the ACIP {" + ((null != originalACIP) ? originalACIP : recoverACIP()) + "}; this may indicate a typo, because Sanskrit, which this is (because it's not legal Tibetan), should have a vowel after each stack.";
} else {
return "Warning: There is a stack, before the last stack, without a vowel in the ACIP {" + ((null != originalACIP) ? originalACIP : recoverACIP()) + "}";
return "Warning: There is a stack, before the last stack, without a vowel in the ACIP {" + ((null != originalACIP) ? originalACIP : recoverACIP()) + "}; this may indicate a typo, because Sanskrit, which this is (because it's not legal Tibetan), should have a vowel after each stack.";
}
}
if ("All" == warningLevel) {
@ -298,9 +299,9 @@ class TParseTree {
if (nip.get(0).hasStackWithoutVowel(pl, isLastStack)) {
if (isLastStack[0]) {
if (warningLevel == "All" || warningLevel == "Most")
return "Warning: The last stack does not have a vowel in the ACIP {" + ((null != originalACIP) ? originalACIP : recoverACIP()) + "}";
return "Warning: The last stack does not have a vowel in the ACIP {" + ((null != originalACIP) ? originalACIP : recoverACIP()) + "}; this may indicate a typo, because Sanskrit, which this is (because it's not legal Tibetan), should have a vowel after each stack.";
} else {
return "Warning: There is a stack, before the last stack, without a vowel in the ACIP {" + ((null != originalACIP) ? originalACIP : recoverACIP()) + "}";
return "Warning: There is a stack, before the last stack, without a vowel in the ACIP {" + ((null != originalACIP) ? originalACIP : recoverACIP()) + "}; this may indicate a typo, because Sanskrit, which this is (because it's not legal Tibetan), should have a vowel after each stack.";
}
}
}