2003-06-24 03:02:29 +00:00
/ *
The contents of this file are subject to the THDL Open Community License
Version 1 . 0 ( the " License " ) ; you may not use this file except in compliance
with the License . You may obtain a copy of the License on the THDL web site
( http : //www.thdl.org/).
Software distributed under the License is distributed on an " AS IS " basis ,
WITHOUT WARRANTY OF ANY KIND , either express or implied . See the
License for the specific terms governing rights and limitations under the
License .
The Initial Developer of this software is the Tibetan and Himalayan Digital
Library ( THDL ) . Portions created by the THDL are Copyright 2003 THDL .
All Rights Reserved .
Contributor ( s ) : ______________________________________ .
* /
package org.thdl.tib.input ;
import java.io.* ;
import org.thdl.util.* ;
import org.thdl.tib.text.* ;
import javax.swing.JOptionPane ;
2003-06-29 04:18:36 +00:00
import java.awt.Frame ;
import java.awt.Dialog ;
2003-06-24 03:02:29 +00:00
2003-07-05 19:26:58 +00:00
/ * * The ConverterGUI is a Swing GUI application . It is used for
* converting TibetanMachine - , TibetanMachineWeb - , and THDL Extended
* Wylie - encoded Rich Text Files to any of several formats , including
* Unicode . The TibetanMachine - to - TibetanMachineWeb conversion is
* flawless , and is used as an intermediate step in other
* conversions , for example the TibetanMachine - to - Unicode conversion .
2003-06-24 03:02:29 +00:00
* @author David Chandler * /
2003-06-25 00:49:11 +00:00
public class ConverterGUI implements FontConversion , FontConverterConstants {
2003-06-24 03:02:29 +00:00
/** Default constructor; does nothing */
ConverterGUI ( ) { }
2003-06-29 04:18:36 +00:00
/ * *
* Runs the converter . * /
public static void main ( String [ ] args ) {
2003-06-24 03:02:29 +00:00
// No need for the TM or TMW fonts.
System . setProperty ( " thdl.rely.on.system.tmw.fonts " , " true " ) ;
2003-07-05 18:00:29 +00:00
System . setProperty ( " thdl.rely.on.system.tm.fonts " , " true " ) ;
2003-06-24 03:02:29 +00:00
2003-06-29 04:18:36 +00:00
System . exit ( realMain ( args , System . out , null ) ) ;
2003-06-24 03:02:29 +00:00
}
public boolean doConversion ( ConvertDialog cd , File oldFile , File newFile ,
2003-08-24 06:40:53 +00:00
String whichConversion , String warningLevel ) {
2003-06-24 03:02:29 +00:00
PrintStream ps ;
try {
returnCode
= TibetanConverter . reallyConvert ( new FileInputStream ( oldFile ) ,
ps = new PrintStream ( new FileOutputStream ( newFile ) ,
false ) ,
2003-08-24 06:40:53 +00:00
whichConversion ,
warningLevel ) ;
2003-06-24 03:02:29 +00:00
ps . close ( ) ;
} catch ( FileNotFoundException e ) {
returnCode = 39 ;
2003-06-26 22:48:05 +00:00
JOptionPane . showMessageDialog ( cd ,
" The conversion failed because either the old \ nfile could not be found or the new file could \ nnot be written (because it was open \ nelsewhere or read-only or what have you). " ,
" Conversion failed " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
2003-06-24 03:02:29 +00:00
}
2003-06-29 04:18:36 +00:00
if ( 3 = = returnCode ) {
JOptionPane . showMessageDialog ( cd ,
TibetanConverter . rtfErrorMessage ,
" Conversion failed " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
2003-07-01 03:43:33 +00:00
} else if ( 44 = = returnCode ) {
JOptionPane . showMessageDialog ( cd ,
" Though an output file has been created, it contains ugly \ nerror messages like \ n \" <<[[JSKAD_TMW_TO_WYLIE_ERROR_NO_SUCH_WYLIE: \ n Cannot convert DuffCode... \" . \ nPlease edit the output by hand to replace all such \ ncreatures with the correct EWTS transliteration. " ,
" Attention required " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
2003-09-02 06:39:33 +00:00
} else if ( 49 = = returnCode ) {
JOptionPane . showMessageDialog ( cd ,
" Though an output file has been created, it contains ugly \ nerror messages like \ n \" <<[[JSKAD_TMW_TO_ACIP_ERROR_NO_SUCH_ACIP: \ n Cannot convert DuffCode... \" . \ nPlease edit the output by hand to replace all such \ ncreatures with the correct ACIP transliteration. " ,
" Attention required " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
2003-06-29 21:31:48 +00:00
} else if ( 43 = = returnCode ) {
JOptionPane . showMessageDialog ( cd ,
" Though an output file has been created, this conversion did nothing. \ nDid you choose the correct original file? \ nDid you choose the correct type of conversion? " ,
" Nothing to do " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
2003-06-30 02:20:52 +00:00
} else if ( 42 = = returnCode ) {
JOptionPane . showMessageDialog ( cd ,
" Some of the document cannot be converted. The output \ ncontains the problem glyphs. E-mail David Chandler \ nwith your suggestions about the proper way to handle \ nsuch a document. " ,
" Errors in Conversion " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
2003-08-24 06:40:53 +00:00
} else if ( 45 = = returnCode ) {
if ( warningLevel = = " None " ) throw new Error ( " FIXME: make this an assertion " ) ;
JOptionPane . showMessageDialog ( cd ,
" No errors occurred, but some warnings are embedded in \ nthe output as [#WARNING...]. " ,
" Warnings in Conversion " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
} else if ( 46 = = returnCode ) {
JOptionPane . showMessageDialog ( cd ,
" Errors occurred, and are embedded in the output \ nas [#ERROR...]. "
+ ( ( warningLevel = = " None " )
? " "
: " Warnings may have occurred; if so, \ nthey are embedded in the output as [#WARNING...]. " ) ,
" Errors in Conversion " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
} else if ( 47 = = returnCode ) {
JOptionPane . showMessageDialog ( cd ,
" So many errors occurred that the document is likely \ nEnglish, not Tibetan. No output was produced. " ,
" Many Errors in Conversion " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
2003-07-05 21:02:46 +00:00
} else if ( 1 = = returnCode ) {
if ( FIND_SOME_NON_TMW = = whichConversion
| | FIND_ALL_NON_TMW = = whichConversion ) {
JOptionPane . showMessageDialog ( cd ,
" Something besides TibetanMachineWeb was found; see output file. " ,
" Not entirely TMW " ,
JOptionPane . PLAIN_MESSAGE ) ;
} else if ( FIND_SOME_NON_TM = = whichConversion
| | FIND_ALL_NON_TM = = whichConversion ) {
JOptionPane . showMessageDialog ( cd ,
" Something besides TibetanMachine was found; see output file. " ,
" Not entirely TM " ,
JOptionPane . PLAIN_MESSAGE ) ;
2003-08-24 06:40:53 +00:00
} else {
throw new Error ( " Who returned this?? " ) ;
2003-07-05 21:02:46 +00:00
}
return false ;
2003-06-29 04:18:36 +00:00
} else if ( 0 ! = returnCode ) {
2003-06-24 03:02:29 +00:00
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 " ,
JOptionPane . ERROR_MESSAGE ) ;
return false ;
} else {
2003-07-05 21:02:46 +00:00
if ( ! ThdlOptions . getBooleanOption ( " thdl.skip.conversion.success.message " ) ) {
if ( FIND_SOME_NON_TMW = = whichConversion
| | FIND_ALL_NON_TMW = = whichConversion ) {
JOptionPane . showMessageDialog ( cd ,
" Nothing except TibetanMachineWeb was found. " ,
" All TMW " ,
JOptionPane . PLAIN_MESSAGE ) ;
} else if ( FIND_SOME_NON_TM = = whichConversion
| | FIND_ALL_NON_TM = = whichConversion ) {
JOptionPane . showMessageDialog ( cd ,
" Nothing except TibetanMachine was found. " ,
" All TM " ,
JOptionPane . PLAIN_MESSAGE ) ;
} else {
JOptionPane . showMessageDialog ( cd ,
" The conversion went perfectly. " ,
" Conversion succeeded " ,
JOptionPane . PLAIN_MESSAGE ) ;
}
}
2003-06-24 03:02:29 +00:00
return true ;
}
}
public String getDefaultDirectory ( ) {
return ThdlOptions . getStringOption ( " thdl.Jskad.working.directory " ,
null ) ;
}
2003-07-05 19:26:58 +00:00
/** the exit status of this application */
2003-06-24 03:02:29 +00:00
private static int returnCode = 0 ;
/ * * Runs the converter without exiting the program .
* @return the exit code . * /
2003-06-29 04:18:36 +00:00
public static int realMain ( String [ ] args , PrintStream out , Frame owner ) {
returnCode = 0 ;
2003-06-24 03:02:29 +00:00
try {
2003-06-29 04:18:36 +00:00
final ConvertDialog convDialog ;
if ( null = = owner ) {
convDialog
= new ConvertDialog ( new ConverterGUI ( ) ,
2003-08-30 05:01:15 +00:00
CHOICES ,
2003-06-29 04:18:36 +00:00
true ) ;
} else {
convDialog
= new ConvertDialog ( owner ,
new ConverterGUI ( ) ,
2003-08-30 05:01:15 +00:00
CHOICES ,
2003-06-29 04:18:36 +00:00
true ) ;
}
2003-06-24 03:02:29 +00:00
/ * Make it so that any time the user exits this program by
* ( almost ) any means , the user ' s preferences are saved if
* the SecurityManager allows it and the path is correct .
* This means that the program used to " Open Document "
* will be remembered . * /
Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( ) {
public void run ( ) {
try {
if ( ! ThdlOptions . saveUserPreferences ( ) ) {
JOptionPane . showMessageDialog ( convDialog ,
" You previously cleared preferences, \ nso you cannot now save them. " ,
" Cannot Save User Preferences " ,
JOptionPane . PLAIN_MESSAGE ) ;
}
} catch ( IOException ioe ) {
System . out . println ( " IO Exception saving user preferences to " + ThdlOptions . getUserPreferencesPath ( ) ) ;
ioe . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
}
} ) ;
convDialog . setVisible ( true ) ;
return returnCode ;
} catch ( ThdlLazyException e ) {
out . println ( " ConverterGUI has a BUG: " ) ;
e . getRealException ( ) . printStackTrace ( out ) ;
return 7 ;
}
}
}