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.awt.* ;
import java.awt.event.* ;
import java.io.* ;
import java.util.* ;
import javax.swing.* ;
import javax.swing.event.* ;
import javax.swing.filechooser.* ;
import org.thdl.util.* ;
/ * * A GUI widget used to convert Tibetan documents from one encoding
to another .
@author Nathaniel Garson , Tibetan and Himalayan Digital Library * /
class ConvertDialog extends JDialog
implements FontConverterConstants
{
private static final boolean debug = false ;
// Attributes
2003-08-30 05:01:15 +00:00
private FontConversion controller ;
2003-06-24 03:02:29 +00:00
2003-08-30 05:01:15 +00:00
private Box fileBox , buttonBox ;
2003-06-24 03:02:29 +00:00
2003-08-30 05:01:15 +00:00
private JPanel content ;
2003-06-24 03:02:29 +00:00
2003-08-30 05:01:15 +00:00
JPanel getContentPanel ( ) { return content ; }
2003-06-24 03:02:29 +00:00
2003-08-30 05:01:15 +00:00
private JComboBox choices ;
2003-06-24 03:02:29 +00:00
2003-08-30 05:01:15 +00:00
private JComboBox warningLevels ;
2003-06-24 03:02:29 +00:00
2003-08-30 05:01:15 +00:00
private JTextField oldTextField , newTextField ;
2003-06-24 03:02:29 +00:00
2003-08-30 05:01:15 +00:00
private JButton browseOld , browseNew , convert , cancel , openDocOld , openDocNew , about ;
2003-06-24 03:02:29 +00:00
2003-08-30 05:01:15 +00:00
private JFileChooser jfc ;
2003-06-24 03:02:29 +00:00
2003-06-29 20:49:30 +00:00
private static final String BROWSENEW = " Browse... " ;
private static final String BROWSEOLD = BROWSENEW ;
private static final String CONVERT = " Convert " ;
private static final String CANCEL = " Close " ;
private static final String ABOUT = " About " ;
private static final String OPEN_WITH = " Open With... " ;
private static final String LOCATE_FILE = " Locate File " ;
2003-06-24 03:02:29 +00:00
private final ThdlActionListener tal = new ThdlActionListener ( ) {
public void theRealActionPerformed ( ActionEvent e ) {
ConvertDialog . this . theRealActionPerformed ( e ) ;
} } ;
2003-08-24 06:40:53 +00:00
private void updateWarningLevels ( ) {
2003-08-31 16:06:35 +00:00
if ( choices . getSelectedItem ( ) = = ACIP_TO_UNI
| | choices . getSelectedItem ( ) = = ACIP_TO_TMW )
2003-08-24 06:40:53 +00:00
this . warningLevels . enable ( ) ;
else
this . warningLevels . disable ( ) ;
}
2003-06-29 20:49:30 +00:00
private void init ( )
2003-06-24 03:02:29 +00:00
{
2003-06-29 16:45:15 +00:00
jfc = new JFileChooser ( controller . getDefaultDirectory ( ) ) ;
2003-06-29 20:49:30 +00:00
jfc . setDialogTitle ( LOCATE_FILE ) ;
2003-06-24 03:02:29 +00:00
jfc . setFileFilter ( new RTFFileFilter ( ) ) ;
content = new JPanel ( new GridLayout ( 0 , 1 ) ) ;
JPanel temp = new JPanel ( new FlowLayout ( FlowLayout . CENTER , 5 , 5 ) ) ;
2003-08-24 06:40:53 +00:00
temp . add ( new JLabel ( " Type of Conversion: " ) ) ;
2003-06-24 03:02:29 +00:00
temp . add ( choices ) ;
2003-08-24 06:40:53 +00:00
temp . add ( Box . createHorizontalStrut ( 20 ) ) ;
temp . add ( new JLabel ( " Warning Level: " ) ) ;
this . warningLevels
= new JComboBox ( new String [ ] { " None " , " Some " , " Most " , " All " } ) ;
this . warningLevels . setSelectedItem ( " Most " ) ;
this . warningLevels . addActionListener ( tal ) ;
updateWarningLevels ( ) ;
temp . add ( warningLevels ) ;
2003-06-24 03:02:29 +00:00
content . add ( temp ) ;
temp = new JPanel ( new FlowLayout ( FlowLayout . CENTER , 5 , 5 ) ) ;
2003-08-30 05:01:15 +00:00
temp . add ( new JLabel ( " Original File: " ) ) ;
2003-06-24 03:02:29 +00:00
oldTextField = new JTextField ( 25 ) ;
JPanel tfTemp = new JPanel ( ) ;
tfTemp . add ( oldTextField ) ;
temp . add ( tfTemp ) ;
browseOld = new JButton ( BROWSEOLD ) ;
browseOld . addActionListener ( tal ) ;
temp . add ( browseOld ) ;
2003-06-29 16:45:15 +00:00
openDocOld = new JButton ( OPEN_WITH ) ;
openDocOld . addActionListener ( tal ) ;
temp . add ( openDocOld ) ;
2003-06-24 03:02:29 +00:00
content . add ( temp ) ;
temp = new JPanel ( new FlowLayout ( FlowLayout . CENTER , 5 , 5 ) ) ;
2003-08-30 05:01:15 +00:00
temp . add ( new JLabel ( " Converted File: " ) ) ;
2003-06-24 03:02:29 +00:00
newTextField = new JTextField ( 25 ) ;
tfTemp = new JPanel ( ) ;
tfTemp . add ( newTextField ) ;
temp . add ( tfTemp ) ;
2003-06-25 00:49:11 +00:00
if ( true ) {
2003-06-24 03:02:29 +00:00
browseNew = new JButton ( BROWSENEW ) ;
browseNew . addActionListener ( tal ) ;
}
temp . add ( browseNew ) ;
2003-06-29 16:45:15 +00:00
openDocNew = new JButton ( OPEN_WITH ) ;
openDocNew . addActionListener ( tal ) ;
temp . add ( openDocNew ) ;
2003-06-24 03:02:29 +00:00
content . add ( temp ) ;
buttonBox = Box . createHorizontalBox ( ) ;
buttonBox . add ( Box . createHorizontalGlue ( ) ) ;
convert = new JButton ( CONVERT ) ;
convert . addActionListener ( tal ) ;
buttonBox . add ( convert ) ;
buttonBox . add ( Box . createHorizontalGlue ( ) ) ;
cancel = new JButton ( CANCEL ) ;
cancel . addActionListener ( tal ) ;
buttonBox . add ( cancel ) ;
buttonBox . add ( Box . createHorizontalGlue ( ) ) ;
2003-06-25 00:49:11 +00:00
about = new JButton ( ABOUT ) ;
about . addActionListener ( tal ) ;
buttonBox . add ( about ) ;
buttonBox . add ( Box . createHorizontalGlue ( ) ) ;
2003-06-24 03:02:29 +00:00
content . add ( buttonBox ) ;
setContentPane ( content ) ;
pack ( ) ;
2003-06-29 04:18:36 +00:00
setSize ( new Dimension ( 620 , 200 ) ) ;
2003-06-24 03:02:29 +00:00
}
2003-06-29 20:49:30 +00:00
private void setChoices ( String [ ] choices )
2003-06-24 03:02:29 +00:00
{
2003-08-30 05:01:15 +00:00
this . choices = new JComboBox ( choices ) ;
2003-06-24 03:02:29 +00:00
this . choices . addActionListener ( tal ) ;
}
// Accessors
2003-06-29 20:49:30 +00:00
private void setController ( FontConversion fc )
2003-06-24 03:02:29 +00:00
{
controller = fc ;
}
2003-06-29 20:49:30 +00:00
/** This constructor takes an owner; the other doesn't. */
2003-06-29 04:18:36 +00:00
public ConvertDialog ( Frame owner ,
FontConversion controller ,
2003-06-25 00:49:11 +00:00
String [ ] choices ,
boolean modal )
2003-06-24 03:02:29 +00:00
{
2003-06-29 04:18:36 +00:00
super ( owner , PROGRAM_TITLE , modal ) ;
initConvertDialog ( controller , choices , modal ) ;
}
2003-06-29 20:49:30 +00:00
/** This constructor does not take an owner; the other does. */
public ConvertDialog ( FontConversion controller ,
String [ ] choices ,
boolean modal )
{
super ( new JDialog ( ) , PROGRAM_TITLE , modal ) ;
initConvertDialog ( controller , choices , modal ) ;
}
2003-06-29 04:18:36 +00:00
private void initConvertDialog ( FontConversion controller ,
String [ ] choices ,
boolean modal ) {
2003-06-24 03:02:29 +00:00
setController ( controller ) ;
setChoices ( choices ) ;
init ( ) ;
if ( debug )
System . out . println ( " Default close operation: "
+ getDefaultCloseOperation ( ) ) ;
}
void theRealActionPerformed ( ActionEvent ae )
{
String cmd = ae . getActionCommand ( ) ;
if ( cmd . equals ( BROWSEOLD )
| | cmd . equals ( BROWSENEW ) )
{
JButton src = ( JButton ) ae . getSource ( ) ;
2003-06-29 16:45:15 +00:00
if ( jfc . showOpenDialog ( this ) ! = jfc . APPROVE_OPTION )
return ;
2003-06-24 03:02:29 +00:00
File chosenFile = jfc . getSelectedFile ( ) ;
if ( chosenFile = = null ) { return ; }
2003-06-29 16:45:15 +00:00
if ( src = = browseOld ) {
2003-06-24 03:02:29 +00:00
String fileName = chosenFile . getPath ( ) ;
oldTextField . setText ( fileName ) ;
updateNewFileGuess ( ) ;
2003-06-25 00:49:11 +00:00
ThdlOptions . setUserPreference ( " thdl.Jskad.working.directory " ,
chosenFile . getParentFile ( ) . getAbsolutePath ( ) ) ;
2003-06-29 16:45:15 +00:00
} else if ( src = = browseNew ) {
2003-06-24 03:02:29 +00:00
newTextField . setText ( chosenFile . getPath ( ) ) ;
2003-06-29 16:45:15 +00:00
} else
throw new Error ( " New button? " ) ;
2003-06-24 03:02:29 +00:00
} else if ( cmd . equals ( CONVERT ) ) {
2003-06-29 16:45:15 +00:00
File origFile = new File ( oldTextField . getText ( ) ) ;
if ( ! origFile . exists ( ) ) {
2003-06-24 03:02:29 +00:00
JOptionPane . showMessageDialog ( this ,
" The original file does not exist. Choose again. " ,
" No such file " ,
JOptionPane . ERROR_MESSAGE ) ;
return ;
}
2003-06-29 16:45:15 +00:00
File convertedFile = new File ( newTextField . getText ( ) ) ;
if ( null = = convertedFile ) {
2003-06-24 03:02:29 +00:00
JOptionPane . showMessageDialog ( this ,
" Please name the new file before proceeding. " ,
" No output file named " ,
JOptionPane . ERROR_MESSAGE ) ;
return ;
}
try {
2003-06-29 16:45:15 +00:00
if ( convertedFile . getCanonicalPath ( ) . equals ( origFile . getCanonicalPath ( ) ) ) {
2003-06-24 03:02:29 +00:00
JOptionPane . showMessageDialog ( this ,
" Please name the new file something different from the old file. " ,
" Input and output are the same " ,
JOptionPane . ERROR_MESSAGE ) ;
return ;
}
} catch ( IOException e ) {
// allow it.
}
2003-06-29 16:45:15 +00:00
if ( convertedFile . isDirectory ( ) ) {
JOptionPane . showMessageDialog ( this ,
" The target file you've chosen is a directory. Choose a file. " ,
" Cannot write to directory " ,
JOptionPane . ERROR_MESSAGE ) ;
return ;
} else if ( convertedFile . exists ( ) ) {
2003-06-29 04:18:36 +00:00
int overwriteExisingFile
= JOptionPane . showConfirmDialog ( this ,
" Do you want to overwrite "
2003-06-29 16:45:15 +00:00
+ convertedFile . getName ( )
+ " ? " ,
2003-06-29 04:18:36 +00:00
" Please select " ,
JOptionPane . YES_NO_OPTION ) ;
switch ( overwriteExisingFile ) {
case JOptionPane . YES_OPTION : // continue.
break ;
default :
return ;
}
}
2003-06-29 20:49:30 +00:00
try {
controller . doConversion ( this ,
origFile ,
convertedFile ,
2003-08-24 06:40:53 +00:00
( String ) choices . getSelectedItem ( ) ,
( String ) warningLevels . getSelectedItem ( ) ) ;
2003-06-29 20:49:30 +00:00
} 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'. " ,
" Out of Memory " ,
JOptionPane . ERROR_MESSAGE ) ;
}
2003-06-29 04:18:36 +00:00
} else if ( cmd . equals ( OPEN_WITH ) ) {
2003-06-24 03:02:29 +00:00
try {
2003-06-29 16:45:15 +00:00
JButton src = ( JButton ) ae . getSource ( ) ;
String fileToOpen ;
if ( src = = openDocNew ) {
fileToOpen = newTextField . getText ( ) ;
} else {
ThdlDebug . verify ( src = = openDocOld ) ;
fileToOpen = oldTextField . getText ( ) ;
}
if ( " " . equals ( fileToOpen ) ) {
JOptionPane . showMessageDialog ( this ,
" Please choose a file to open with the external viewer. " ,
" No file named " ,
JOptionPane . ERROR_MESSAGE ) ;
return ;
}
File namedFile = new File ( fileToOpen ) ;
if ( ! namedFile . exists ( ) ) {
JOptionPane . showMessageDialog ( this ,
" No such file exists, so it cannot be opened with the external viewer. " ,
" No such file " ,
JOptionPane . ERROR_MESSAGE ) ;
return ;
}
if ( ! namedFile . isFile ( ) ) {
JOptionPane . showMessageDialog ( this ,
" You've chosen a directory, not a file. Only files \ ncan be opened with the external viewer. " ,
" Not a regular file " ,
JOptionPane . ERROR_MESSAGE ) ;
return ;
}
2003-06-29 20:49:30 +00:00
openWithExternalViewer ( this , fileToOpen ) ;
2003-06-24 03:02:29 +00:00
} catch ( SecurityException se ) {
JOptionPane . showMessageDialog ( this ,
" Cannot proceed because your security policy interfered. " ,
" Access denied " ,
JOptionPane . ERROR_MESSAGE ) ;
}
} else if ( cmd . equals ( CANCEL ) ) {
this . dispose ( ) ;
2003-06-25 00:49:11 +00:00
} else if ( cmd . equals ( ABOUT ) ) {
JOptionPane . showMessageDialog ( this ,
2003-06-29 16:45:15 +00:00
" 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 ( ) ,
2003-06-25 00:49:11 +00:00
" About " ,
JOptionPane . PLAIN_MESSAGE ) ;
2003-06-24 03:02:29 +00:00
} else if ( cmd . equals ( " comboBoxChanged " ) ) {
2003-08-24 06:40:53 +00:00
JComboBox src = ( JComboBox ) ae . getSource ( ) ;
if ( src = = choices ) {
updateNewFileGuess ( ) ;
updateWarningLevels ( ) ;
}
2003-06-24 03:02:29 +00:00
}
}
2003-06-29 20:49:30 +00:00
/ * * Invokes a user - specified external viewer ( one that takes a
single command - line argument , the path to view ) on the file
with path fileToOpen .
@param parent the owner of any dialogs that come up
@param fileToOpen the path to open in the external viewer * /
static void openWithExternalViewer ( Component parent , String fileToOpen ) {
boolean done = false ;
File prog
= new File ( ThdlOptions . getStringOption ( " thdl.external.rtf.reader " ,
" C: \\ Program Files \\ Microsoft Office \\ Office \\ WINWORD.EXE " ) ) ;
while ( ! done ) {
String [ ] cmdArray = { prog . getPath ( ) , fileToOpen } ;
Runtime rtime = Runtime . getRuntime ( ) ;
try {
Process proc = rtime . exec ( cmdArray ) ;
proc = null ;
done = true ;
} catch ( IOException ioe ) {
JFileChooser jfc = new JFileChooser ( " C: \\ Program Files \\ " ) ;
jfc . setDialogTitle ( " Locate Program to Read RTF " ) ;
if ( jfc . showOpenDialog ( parent ) = = jfc . APPROVE_OPTION ) {
prog = jfc . getSelectedFile ( ) ;
ThdlOptions . setUserPreference ( " thdl.external.rtf.reader " ,
prog . getAbsolutePath ( ) ) ;
} else {
done = true ;
}
jfc . setDialogTitle ( LOCATE_FILE ) ;
}
}
}
/ * * Looks at the name of the original file and creates a name for
the converted file based on that . * /
2003-06-24 03:02:29 +00:00
private void updateNewFileGuess ( ) {
String oldFileName = oldTextField . getText ( ) ;
if ( oldFileName = = null | | oldFileName . equals ( " " ) )
return ;
String newFileNamePrefix ;
File of = new File ( oldFileName ) ;
String oldFileDirName = of . getParent ( ) ;
if ( oldFileDirName = = null )
oldFileDirName = " " ;
else
oldFileDirName = oldFileDirName + File . separator ;
String oldFileNameSansThingy = of . getName ( ) ;
if ( oldFileNameSansThingy . startsWith ( " TMW_ " ) ) {
oldFileNameSansThingy
= oldFileNameSansThingy . substring ( " TMW_ " . length ( ) ,
oldFileNameSansThingy . length ( ) ) ;
} else if ( oldFileNameSansThingy . startsWith ( " TM_ " ) ) {
oldFileNameSansThingy
= oldFileNameSansThingy . substring ( " TM_ " . length ( ) ,
oldFileNameSansThingy . length ( ) ) ;
} else if ( oldFileNameSansThingy . startsWith ( " TMW " ) ) {
oldFileNameSansThingy
= oldFileNameSansThingy . substring ( " TMW " . length ( ) ,
oldFileNameSansThingy . length ( ) ) ;
} else if ( oldFileNameSansThingy . startsWith ( " TM " ) ) {
oldFileNameSansThingy
= oldFileNameSansThingy . substring ( " TM " . length ( ) ,
oldFileNameSansThingy . length ( ) ) ;
}
String ct = ( String ) choices . getSelectedItem ( ) ;
if ( " Find all non-TMW " = = ct ) {
newFileNamePrefix = " FindAllNonTMW__ " ;
2003-06-25 00:49:11 +00:00
} else if ( FIND_SOME_NON_TMW = = ct ) {
2003-06-24 03:02:29 +00:00
newFileNamePrefix = " FindSomeNonTMW__ " ;
2003-06-25 00:49:11 +00:00
} else if ( FIND_SOME_NON_TM = = ct ) {
2003-06-24 03:02:29 +00:00
newFileNamePrefix = " FindSomeNonTM__ " ;
2003-06-25 00:49:11 +00:00
} else if ( FIND_ALL_NON_TM = = ct ) {
2003-06-24 03:02:29 +00:00
newFileNamePrefix = " FindAllNonTM__ " ;
} else { // conversion {to Wylie or TM} mode
2003-06-25 00:49:11 +00:00
if ( TMW_TO_WYLIE = = ct ) {
newFileNamePrefix = suggested_WYLIE_prefix ;
2003-09-02 06:39:33 +00:00
} else if ( TMW_TO_ACIP = = ct ) {
newFileNamePrefix = suggested_ACIP_prefix ;
2003-08-24 06:40:53 +00:00
} else if ( TMW_TO_UNI = = ct | | ACIP_TO_UNI = = ct ) {
2003-06-25 00:49:11 +00:00
newFileNamePrefix = suggested_TO_UNI_prefix ;
2003-08-31 16:06:35 +00:00
} else if ( TM_TO_TMW = = ct | | ACIP_TO_TMW = = ct ) {
2003-06-25 00:49:11 +00:00
newFileNamePrefix = suggested_TO_TMW_prefix ;
2003-06-24 03:02:29 +00:00
} else {
2003-06-25 00:49:11 +00:00
ThdlDebug . verify ( TMW_TO_TM = = ct ) ;
newFileNamePrefix = suggested_TO_TM_prefix ;
2003-06-24 03:02:29 +00:00
}
}
newTextField . setText ( oldFileDirName
+ newFileNamePrefix
+ oldFileNameSansThingy ) ;
}
public class RTFFileFilter extends javax . swing . filechooser . FileFilter
{
public boolean accept ( File f )
{
if ( f . isDirectory ( ) | | f . getName ( ) . indexOf ( " .rtf " ) > - 1 ) { return true ; }
return false ;
}
public String getDescription ( )
{
return " RTF files only " ;
}
}
}