2002-09-30 03:10:00 +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 2001 THDL .
All Rights Reserved .
Contributor ( s ) : ______________________________________ .
* /
package org.thdl.tib.input ;
2005-07-11 03:10:32 +00:00
import java.awt.Font ;
import java.awt.datatransfer.Clipboard ;
import java.awt.datatransfer.ClipboardOwner ;
import java.awt.datatransfer.DataFlavor ;
import java.awt.datatransfer.StringSelection ;
import java.awt.datatransfer.Transferable ;
import java.awt.datatransfer.UnsupportedFlavorException ;
import java.awt.event.ActionEvent ;
import java.awt.event.FocusEvent ;
import java.awt.event.FocusListener ;
2006-08-12 22:23:01 +00:00
import java.awt.event.MouseAdapter ;
import java.awt.event.MouseEvent ;
2005-07-11 03:10:32 +00:00
import java.awt.event.KeyAdapter ;
import java.awt.event.KeyEvent ;
import java.io.BufferedWriter ;
import java.io.ByteArrayInputStream ;
import java.io.ByteArrayOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStream ;
import java.io.OutputStreamWriter ;
import java.util.ArrayList ;
import java.util.Hashtable ;
import java.util.Iterator ;
import java.util.LinkedList ;
import javax.swing.AbstractAction ;
import javax.swing.Action ;
import javax.swing.JOptionPane ;
import javax.swing.text.AttributeSet ;
import javax.swing.text.BadLocationException ;
import javax.swing.text.Caret ;
import javax.swing.text.DefaultStyledDocument ;
import javax.swing.text.JTextComponent ;
import javax.swing.text.Keymap ;
import javax.swing.text.MutableAttributeSet ;
import javax.swing.text.Position ;
import javax.swing.text.Style ;
import javax.swing.text.StyleConstants ;
import javax.swing.text.StyleContext ;
import javax.swing.text.StyledDocument ;
2006-08-12 22:23:01 +00:00
import javax.swing.JButton ;
import javax.swing.JToolBar ;
import javax.swing.JFrame ;
import javax.swing.JComponent ;
2005-07-11 03:10:32 +00:00
import org.thdl.tib.text.DuffCode ;
import org.thdl.tib.text.DuffData ;
import org.thdl.tib.text.InvalidTransliterationException ;
import org.thdl.tib.text.THDLWylieConstants ;
import org.thdl.tib.text.TibTextUtils ;
import org.thdl.tib.text.TibetanDocument ;
import org.thdl.tib.text.TibetanKeyboard ;
import org.thdl.tib.text.TibetanMachineWeb ;
import org.thdl.util.RTFFixerInputStream ;
import org.thdl.util.StatusBar ;
2002-10-06 18:23:27 +00:00
import org.thdl.util.ThdlDebug ;
Added a flexible mechanism for persistent boolean-, integer-, and
string-valued preferences built atop java.util.Properties.
How it works: the jvm is asked first, and then the user's prefs file, if it exists,
then the system-wide prefs file, and then the built-in preferences. Finally, for
robustness, a default may be optionally hard-coded in the source.
I made several things configurable, too:
the default Tibetan keyboard
the default font sizes and faces
whether you want developer-only features enabled
Savant's file extension (.savant)
etc.
The only known problems are the following:
The default location for the user's preferences file is windows-specific,
arbitrary, and not in the user documentation. Likewise for the location of the
system-wide preferences file. You can change them using 'java -D', though.
There is no "Save preferences" option yet, and closing the program does
not save preferences either.
2002-10-14 04:06:05 +00:00
import org.thdl.util.ThdlOptions ;
2002-10-06 18:23:27 +00:00
2006-08-12 22:23:01 +00:00
import javax.swing.text.Element ;
import org.thdl.tib.scanner.TibetanScanner ;
import org.thdl.tib.scanner.Word ;
import org.thdl.tib.text.Pronounciation ;
import org.thdl.tib.input.SettingsServiceProvider ;
import org.thdl.tib.input.DictionaryLoadState ;
import java.util.Observable ;
import org.thdl.tib.input.GlobalResourceHolder ;
2002-09-30 03:10:00 +00:00
/ * *
* Enables input of Tibetan text
* using Tibetan Computer Company ' s free cross - platform TibetanMachineWeb fonts .
* Two modes of text entry are allowed . In Tibetan mode , keystrokes are intercepted
* and reinterpreted according to the Tibetan keyboard installed . The result , of
* course , is Tibetan text , in the TibetanMachineWeb encoding . In Roman mode ,
* keystrokes are not intercepted , and the font defaults to a Roman or user - defined font .
* @author Edward Garrett , Tibetan and Himalayan Digital Library
* @version 1 . 0
* /
2006-08-12 22:23:01 +00:00
public class DuffPane extends TibetanPane
implements FocusListener , SettingsServiceProvider
{
2002-10-13 19:22:56 +00:00
/ * *
* The status bar to update with messages about the current input mode .
* Are we expecting a vowel ? a subscript ? et cetera .
* /
private StatusBar statBar = null ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* A central part of the Tibetan keyboard . As keys are typed , they are
* added to charList if they constitute a valid Wylie character . charList
* is added to in this manner until the user types punctuation , a vowel ,
* or some action or function key . Later , when glyphs are printed to the
2002-10-28 04:49:24 +00:00
* screen , the { @link # newGlyphList } is computed on the basis of charList .
2002-09-30 03:10:00 +00:00
* /
2003-10-12 16:44:28 +00:00
private java . util . ArrayList charList ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* This field holds a copy of the last { @link # newGlyphList } .
* Then , when a key is pressed , { @link # charList } is updated , a new
* newGlyphList is computed , and the newGlyphList is compared against
* this field . The text on the screen is then modified to reflect
* the new newGlyphList .
* /
2003-10-12 16:44:28 +00:00
private java . util . List oldGlyphList ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* A central component of the Tibetan input method . While { @link # charList charList }
* keeps track of the characters that have been entered , it does not organize them
* correctly into the proper glyphs . For example , charList might have four characters
* in it , 'b' , 's' , 'g' , and 'r' , but it does not know that they should be drawn as
* two glyphs , 'b' and ' s - g - r ' . newGlyphList is a list of glyphs
2002-10-14 00:19:30 +00:00
* ( { @link org . thdl . tib . text . DuffCode DuffCodes } ) which is formed by
2002-09-30 03:10:00 +00:00
* @link # recomputeGlyphs ( boolean areStacksOnRight , boolean definitelyTibetan , boolean definitelySanskrit ) recomputeGlyphs } ,
* which constructs an optimal arrangement of glyphs from the charList .
* /
2003-10-12 16:44:28 +00:00
private java . util . List newGlyphList ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* This field keeps track of what is currently being typed , to account
* for characters ( such as Wylie ' tsh ' ) which correspond to more than one
* keystroke in the keyboard . It is cleared or readjusted when it is clear
* that the user has moved on to a different character . For example , after the
* user types ' khr ' , holdCurrent will contain only 'r' , since ' khr ' is not
* a valid character .
* /
2003-10-12 16:44:28 +00:00
private StringBuffer holdCurrent ;
2002-10-13 18:40:07 +00:00
/ * *
* This field says whether or not the character atop { @link # charList }
* has been finalized , and therefore whether subsequent keystrokes are
* allowed to displace this character . For example , if 'k' is at the
* top of charList , and isTopHypothesis is true , then typing 'h' would
* replace 'k' with ' kh ' . On the other hand , were isTopHypothesis
* false , then typing 'h' would add 'h' to the top of charList instead .
* In short , is the top character on { @link # charList } a fact or just a
* hypothesis ?
2002-09-30 03:10:00 +00:00
* /
2003-10-12 16:44:28 +00:00
private boolean isTopHypothesis ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* Is the user in the process of typing a vowel ?
* /
2003-10-12 16:44:28 +00:00
private boolean isTypingVowel ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* Is it definitely the case that the user is typing Tibetan , rather than
* Sanskrit ?
* /
2003-10-12 16:44:28 +00:00
private boolean isDefinitelyTibetan ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* According to the active keyboard , what value
* { @link # isDefinitelyTibetan } should be assigned by default when the
* keyboard is initialized by { @link # initKeyboard ( ) initKeyboard }
2002-09-30 03:10:00 +00:00
* /
2003-10-12 16:44:28 +00:00
private boolean isDefinitelyTibetan_default ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* According to the active keyboard , what value { @link
* # isDefinitelyTibetan } should be assigned if the user has initiated a
* stack by typing a stack key . For example , in the Wylie keyboard ,
* there is a Sanskrit stacking key ( '+' ) , but no Tibetan stacking key .
* Therefore , if the user is stacking with '+' , this field should be
* false , since what the user is typing must be Sanskrit , not Tibetan . * /
2003-10-12 16:44:28 +00:00
private boolean isDefinitelyTibetan_withStackKey ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* True iff it is definitely the case that the user is typing Sanskrit
* ( for example a Sanskrit stack ) , rather than Tibetan
2002-09-30 03:10:00 +00:00
* /
2003-10-12 16:44:28 +00:00
private boolean isDefinitelySanskrit ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* According to the active keyboard , the value { @link
* # isDefinitelySanskrit } should be assigned by default when the
* keyboard is initialized by { @link # initKeyboard ( ) initKeyboard }
2002-09-30 03:10:00 +00:00
* /
2003-10-12 16:44:28 +00:00
private boolean isDefinitelySanskrit_default ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* According to the active keyboard , the value that should
2002-09-30 03:10:00 +00:00
* be assigned to { @link # isDefinitelySanskrit } if the
2002-10-14 00:19:30 +00:00
* user has initiated a stack by typing a stack key .
2002-09-30 03:10:00 +00:00
* For example , in the Wylie keyboard , there is a Sanskrit
* stacking key ( '+' ) , but no Tibetan stacking key .
* Therefore , if the user is stacking with '+' , this field
* should be true , since what the user is typing must
* be Sanskrit , not Tibetan .
* /
2003-10-12 16:44:28 +00:00
private boolean isDefinitelySanskrit_withStackKey ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* True iff consonant stacking is allowed at the moment . In the Wylie
2002-09-30 03:10:00 +00:00
* keyboard , consonant stacking is usually on , since stacking
* is automatic . However , in the TCC and Sambhota keyboards ,
* stacking is off by default , since you can only stack when
* you ' ve pressed a stacking key .
* /
2003-10-12 16:44:28 +00:00
private boolean isStackingOn ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* True iff , according to the active keyboard , stacking is on by
* default assuming no stack key has been pressed .
2002-09-30 03:10:00 +00:00
* /
2003-10-12 16:44:28 +00:00
private boolean isStackingOn_default ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* Automatic stacking in Wylie is from right to left . For
* example , if the user types ' brg ' , the resulting glyph
* sequence is 'b' plus ' rg ' , not ' br ' plus 'g' . If
* stacking results from the use of a stack key ,
* it is from left to right .
* /
2003-10-12 16:44:28 +00:00
private boolean isStackingRightToLeft ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* used for tracking changes in Wylie to TMW conversion
* /
2003-10-12 16:44:28 +00:00
private int lastStart ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* true iff the user is in Tibetan typing mode . This is true
* by default .
2002-09-30 03:10:00 +00:00
* /
2003-10-12 16:44:28 +00:00
private boolean isTibetan = true ;
2002-10-13 18:40:07 +00:00
/ * *
2002-10-14 00:19:30 +00:00
* true iff the user is allowed to type non - Tibetan . This is true
2002-09-30 03:10:00 +00:00
* by default
* /
2003-10-12 16:44:28 +00:00
private boolean isRomanEnabled = true ;
2002-10-13 18:40:07 +00:00
/ * *
2002-09-30 03:10:00 +00:00
* The caret of { @link # doc } , used to keep track of the
* current entry / edit / deletion position .
* /
2003-10-12 16:44:28 +00:00
private Caret caret ;
private Style rootStyle ;
private boolean skipUpdate = false ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
private String romanFontFamily ;
private int romanFontSize ;
2006-08-12 22:23:01 +00:00
/ * *
* The current dictionary information
* @TODO these three fields should be one class
* /
private boolean dictionaryEnabled = false ;
private boolean dictionaryLocal = false ;
private String dictionaryPath ;
2003-04-02 20:37:14 +00:00
private Hashtable actions ;
2006-08-12 22:23:01 +00:00
private Observable observableObj ;
protected Clipboard rtfBoard ;
/ * *
* Range
*
* /
class Range
{
int startPos ;
int endPos ;
public Range ( )
{
startPos = 0 ;
endPos = 0 ;
}
public Range ( int start , int end )
{
setRange ( start , end ) ;
}
public void setRange ( int start , int end )
{
startPos = start ;
endPos = end ;
}
int getStartPos ( )
{
return startPos ;
}
int getEndPos ( )
{
return endPos ;
}
boolean isValid ( )
{
return ( startPos > = 0 & & endPos > = 0 & & startPos < = endPos ) ;
}
}
/ * *
* the parent frame
* /
protected JFrame mainFrame = null ;
protected DictionaryFrame dictFrame = null ;
protected Pronounciation pronounciation ;
protected JButton buttonToggleDict = null ;
protected JButton buttonLookupDict = null ;
2002-09-30 03:10:00 +00:00
2002-10-22 03:53:33 +00:00
/ * * Initializes this object . All constructors should call
this . * /
2006-08-12 22:23:01 +00:00
private void initialize ( Object frame ,
StatusBar sb ,
2002-10-22 03:53:33 +00:00
TibetanKeyboard keyboard ,
java . net . URL keyboardURL )
{
2006-08-12 22:23:01 +00:00
observableObj = new Observable ( ) ;
GlobalResourceHolder . setSettingsServiceProvider ( this ) ;
if ( null ! = frame )
{
initDictionarySupport ( frame ) ;
}
2003-10-12 16:44:28 +00:00
if ( null ! = keyboard )
2002-10-22 03:53:33 +00:00
TibetanMachineWeb . setKeyboard ( keyboard ) ;
2003-10-12 16:44:28 +00:00
if ( null ! = keyboardURL )
2002-10-22 03:53:33 +00:00
TibetanMachineWeb . setKeyboard ( keyboardURL ) ;
2003-10-12 16:44:28 +00:00
setupKeyboard ( ) ;
setupEditor ( ) ;
2002-10-22 03:53:33 +00:00
if ( null ! = sb )
setStatusBar ( sb ) ;
2003-10-12 16:44:28 +00:00
// this(new StyledEditorKit(), keyboardURL);
2002-10-22 03:53:33 +00:00
}
2002-10-13 19:22:56 +00:00
/ * * Creates a new DuffPane that updates sb , if sb is not null ,
with messages about how the users ' keypresses are being
interpreted . * /
2006-08-12 22:23:01 +00:00
public DuffPane ( Object frame , StatusBar sb ) {
super ( ) ;
initialize ( frame , sb , null , null ) ;
}
public DuffPane ( Object frame ) {
super ( ) ;
initialize ( frame , null , null , null ) ;
}
public DuffPane ( Object frame , TibetanKeyboard keyboard ) {
super ( ) ;
initialize ( frame , null , keyboard , null ) ;
}
public DuffPane ( Object frame , java . net . URL keyboardURL ) {
super ( ) ;
initialize ( frame , null , null , keyboardURL ) ;
}
2003-10-12 16:44:28 +00:00
public DuffPane ( StatusBar sb ) {
super ( ) ;
2006-08-12 22:23:01 +00:00
initialize ( null , sb , null , null ) ;
2003-10-12 16:44:28 +00:00
}
public DuffPane ( ) {
super ( ) ;
2006-08-12 22:23:01 +00:00
initialize ( null , null , null , null ) ;
2003-10-12 16:44:28 +00:00
}
public DuffPane ( TibetanKeyboard keyboard ) {
super ( ) ;
2006-08-12 22:23:01 +00:00
initialize ( null , null , keyboard , null ) ;
2003-10-12 16:44:28 +00:00
}
public DuffPane ( java . net . URL keyboardURL ) {
super ( ) ;
2006-08-12 22:23:01 +00:00
initialize ( null , null , null , keyboardURL ) ;
2003-10-12 16:44:28 +00:00
}
2002-09-30 03:10:00 +00:00
2006-08-12 22:23:01 +00:00
2003-04-14 05:22:27 +00:00
/ * * For testing purposes , it ' s useful to create a DuffPane and not
* hook it up to the UI . If this is true , this DuffPane will
* manage its document ' s caret rather than having the UI do
* it . * /
private boolean manageCaret = false ;
/ * * For testing purposes , it ' s useful to create a DuffPane and not
* hook it up to the UI . If you call htis , then this DuffPane
* will manage its document ' s caret rather than having the UI do
* it . * /
void enableCaretManaging ( ) {
manageCaret = true ;
}
2002-10-13 19:22:56 +00:00
/ * * Sets the status bar to update with mode information . If sb is
null , no status bar will be updated . * /
public void setStatusBar ( StatusBar sb ) {
statBar = sb ;
}
/** If we have a status bar, update it. */
2002-10-22 03:53:33 +00:00
protected void updateStatus ( String newStatus ) {
2002-10-13 19:22:56 +00:00
if ( statBar ! = null ) {
/ * If we ' ve seen this message just before , append " x2 " to
the end of it . The third message will cause a toggle ,
which you can tell is different , so that ' s fine . * /
if ( newStatus . equals ( statBar . currentStatus ( ) ) ) {
newStatus = newStatus + " x2 " ;
}
2002-11-03 17:05:05 +00:00
statBar . replaceStatus ( ( isTopHypothesis ? " Guess: " : " Fact: " )
+ " [holding \" " + holdCurrent . toString ( ) + " \" ] "
+ newStatus ) ;
2002-10-13 19:22:56 +00:00
}
}
/** If we have a status bar, append msg to its current status. */
2002-10-22 03:53:33 +00:00
protected void appendStatus ( String msg ) {
2002-10-13 19:22:56 +00:00
if ( statBar ! = null ) {
statBar . replaceStatus ( statBar . currentStatus ( ) + msg ) ;
}
}
Added a flexible mechanism for persistent boolean-, integer-, and
string-valued preferences built atop java.util.Properties.
How it works: the jvm is asked first, and then the user's prefs file, if it exists,
then the system-wide prefs file, and then the built-in preferences. Finally, for
robustness, a default may be optionally hard-coded in the source.
I made several things configurable, too:
the default Tibetan keyboard
the default font sizes and faces
whether you want developer-only features enabled
Savant's file extension (.savant)
etc.
The only known problems are the following:
The default location for the user's preferences file is windows-specific,
arbitrary, and not in the user documentation. Likewise for the location of the
system-wide preferences file. You can change them using 'java -D', though.
There is no "Save preferences" option yet, and closing the program does
not save preferences either.
2002-10-14 04:06:05 +00:00
private static int defaultTibFontSize ( ) {
return ThdlOptions . getIntegerOption ( " thdl.default.tibetan.font.size " ,
36 ) ;
}
private static int defaultRomanFontSize ( ) {
return ThdlOptions . getIntegerOption ( " thdl.default.roman.font.size " ,
14 ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* This method sets up the editor , assigns fonts and point sizes ,
* sets the document , the caret , and adds key and focus listeners .
* /
2003-10-12 16:44:28 +00:00
private void setupEditor ( ) {
rtfBoard = getToolkit ( ) . getSystemClipboard ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
romanFontFamily = ThdlOptions . getStringOption ( " thdl.default.roman.font.face " ,
2003-04-14 05:22:27 +00:00
" Serif " ) ;
2003-10-12 16:44:28 +00:00
romanFontSize = defaultRomanFontSize ( ) ;
2003-07-01 03:43:33 +00:00
2006-08-12 22:23:01 +00:00
dictionaryEnabled = defaultDictionarySettingsEnabled ( ) ;
dictionaryLocal = defaultDictionarySettingsLocal ( ) ;
dictionaryPath = defaultDictionarySettingsPath ( ) ;
onDictionarySettingsChanged ( ) ;
2003-10-12 16:44:28 +00:00
newDocument ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
caret = getCaret ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
addFocusListener ( this ) ;
addKeyListener ( new KeyAdapter ( ) {
public void keyPressed ( KeyEvent kev ) {
if ( kev . isActionKey ( ) | | kev . getKeyCode ( ) = = KeyEvent . VK_BACK_SPACE )
initKeyboard ( ) ;
}
} ) ;
2003-04-07 16:41:49 +00:00
2003-10-12 16:44:28 +00:00
setupKeymap ( ) ;
}
2003-04-02 20:37:14 +00:00
2003-04-14 05:22:27 +00:00
/ * * Performs the keystroke key with selected ActionEvent - style
* modifiers modifiers . * /
void performKeyStroke ( int modifiers , String key ) {
// FIXME: do this assertion: assert(key.length() == 1);
if ( ! isEditable ( ) ) return ;
if ( ( ( modifiers & ActionEvent . CTRL_MASK ) = = ActionEvent . CTRL_MASK ) | |
( ( modifiers & ActionEvent . ALT_MASK ) = = ActionEvent . ALT_MASK ) | |
( ( modifiers & ActionEvent . META_MASK ) = = ActionEvent . META_MASK ) ) {
initKeyboard ( ) ;
return ;
}
if ( key ! = null ) {
if ( getSelectionStart ( ) < getSelectionEnd ( ) )
replaceSelection ( " " ) ;
if ( isTibetan ) {
processTibetanChar ( key . charAt ( 0 ) ) ;
} else {
2003-07-01 01:21:57 +00:00
processRomanChar ( key , getTibDoc ( ) . getRomanAttributeSet ( ) ) ;
2003-04-14 05:22:27 +00:00
}
if ( manageCaret ) {
caret . setDot ( getTibDoc ( ) . getLength ( ) ) ;
}
}
}
2003-04-02 20:37:14 +00:00
/ * *
* This method sets up the keymap used by DuffPane editors .
* The keymap defines a default behavior for key presses
* in both Tibetan and Roman mode .
* /
2003-09-12 05:06:37 +00:00
private void setupKeymap ( ) {
Action defaultAction = new AbstractAction ( ) {
public void actionPerformed ( ActionEvent e ) {
try {
DuffPane . this . performKeyStroke ( e . getModifiers ( ) ,
e . getActionCommand ( ) ) ;
} catch ( Throwable t ) {
System . err . println ( " JSKAD ERROR: " + t ) ;
t . printStackTrace ( System . err ) ;
System . exit ( 1 ) ;
}
}
} ;
createActionTable ( this ) ;
Keymap keymap = addKeymap ( " DuffBindings " , getKeymap ( ) ) ;
keymap . setDefaultAction ( defaultAction ) ;
setKeymap ( keymap ) ;
}
2003-10-12 16:44:28 +00:00
private void createActionTable ( JTextComponent textComponent ) {
actions = new Hashtable ( ) ;
Action [ ] actionsArray = textComponent . getActions ( ) ;
for ( int i = 0 ; i < actionsArray . length ; i + + ) {
Action a = actionsArray [ i ] ;
actions . put ( a . getValue ( Action . NAME ) , a ) ;
}
}
private Action getActionByName ( String name ) {
return ( Action ) ( actions . get ( name ) ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* This method sets up the Tibetan keyboard . Initially it is called
* by the constructor , but it is also called internally whenever
* the active keyboard is changed . It first sets various values with
* respect to stacking , and concerning the differences between
* Tibetan and Sanskrit ; and then it initializes the input method .
* /
2003-10-12 16:44:28 +00:00
private void setupKeyboard ( ) {
if ( TibetanMachineWeb . hasTibetanStackingKey ( ) ) {
if ( TibetanMachineWeb . hasSanskritStackingKey ( ) ) {
isDefinitelyTibetan_default = false ;
isDefinitelySanskrit_default = false ;
isStackingOn_default = false ;
isDefinitelyTibetan_withStackKey = false ;
isDefinitelySanskrit_withStackKey = false ;
} else {
isDefinitelyTibetan_default = false ;
isDefinitelySanskrit_default = false ; // DLC FIXME: trying to make ACIP keyboard happy.
isStackingOn_default = true ;
isDefinitelyTibetan_withStackKey = false ; // DLC FIXME: trying to make ACIP keyboard happy.
isDefinitelySanskrit_withStackKey = false ;
}
} else {
if ( TibetanMachineWeb . hasSanskritStackingKey ( ) ) {
isDefinitelyTibetan_default = true ;
isDefinitelySanskrit_default = false ;
isStackingOn_default = true ;
isDefinitelyTibetan_withStackKey = false ;
isDefinitelySanskrit_withStackKey = true ;
} else { //no stacking key at all
isDefinitelyTibetan_default = false ;
isDefinitelySanskrit_default = false ;
isStackingOn_default = true ;
}
}
charList = new ArrayList ( ) ;
oldGlyphList = new ArrayList ( ) ;
newGlyphList = new ArrayList ( ) ;
initKeyboard ( ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Registers the Extended Wylie keyboard , and sets it as
* the active keyboard .
* Unpredictable behavior will result
* if you set the keyboard in { @link org . thdl . tib . text . TibetanMachineWeb TibetanMachineWeb }
* but don ' t register it here .
* /
2003-10-12 16:44:28 +00:00
public void registerKeyboard ( ) {
registerKeyboard ( ( TibetanKeyboard ) null ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Registers a keyboard , and sets it as the active keyboard .
* Unpredictable behavior will result if you set the keyboard in { @link
* org . thdl . tib . text . TibetanMachineWeb TibetanMachineWeb } but don ' t
* register it in here .
* @param keyboardURL the URL of the keyboard you want to install * /
2003-10-12 16:44:28 +00:00
public void registerKeyboard ( java . net . URL keyboardURL ) {
TibetanMachineWeb . setKeyboard ( keyboardURL ) ;
setupKeyboard ( ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Registers a keyboard , and sets it as
* the active keyboard .
* Unpredictable behavior will result
* if you set the keyboard in { @link org . thdl . tib . text . TibetanMachineWeb TibetanMachineWeb }
* but don ' t register it in here .
* @param keyboard the keyboard you want to install
* /
2003-10-12 16:44:28 +00:00
public void registerKeyboard ( TibetanKeyboard keyboard ) {
TibetanMachineWeb . setKeyboard ( keyboard ) ;
setupKeyboard ( ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Clears the current document .
* /
2003-10-12 16:44:28 +00:00
public void newDocument ( ) {
StyleContext styleContext = new StyleContext ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
doc = new TibetanDocument ( styleContext ) ;
doc . setTibetanFontSize ( defaultTibFontSize ( ) ) ;
2002-11-08 04:05:06 +00:00
// Something about the order in which you do this step
// relative to the others in this method matters. You'll get
// a small cursor, though most everything else will be normal,
// if you call setDocument(doc) at the end of this method.
2003-10-12 16:44:28 +00:00
setDocument ( doc ) ;
ThdlDebug . verify ( getTibDoc ( ) = = doc ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
Style defaultStyle = styleContext . getStyle ( StyleContext . DEFAULT_STYLE ) ;
StyleConstants . setFontFamily ( defaultStyle , " TibetanMachineWeb " ) ;
StyleConstants . setFontSize ( defaultStyle , defaultTibFontSize ( ) ) ;
2003-04-14 05:22:27 +00:00
2003-10-12 16:44:28 +00:00
setRomanAttributeSet ( romanFontFamily , romanFontSize ) ;
2003-07-01 03:43:33 +00:00
2003-10-12 16:44:28 +00:00
newGlyphList . clear ( ) ;
2003-04-14 05:22:27 +00:00
initKeyboard ( ) ;
2003-10-12 16:44:28 +00:00
}
2002-09-30 03:10:00 +00:00
/ * *
* Initializes the keyboard input interpreter , setting all properties
* back to their default states . This method is called whenever an action
* or function key is pressed , and also whenever the user types punctuation
* or a vowel . It is not called when the user is typing characters that
* could be part of a stack , or require manipulation of glyphs - e . g .
* backspacing , redrawing , etc .
* /
2003-10-12 16:44:28 +00:00
private void initKeyboard ( ) {
charList . clear ( ) ;
oldGlyphList . clear ( ) ;
holdCurrent = new StringBuffer ( ) ;
updateStatus ( " Jskad is in its basic input mode " ) ;
isTopHypothesis = false ;
isTypingVowel = false ;
//for keyboard
isStackingOn = isStackingOn_default ;
isStackingRightToLeft = true ;
isDefinitelyTibetan = isDefinitelyTibetan_default ;
isDefinitelySanskrit = isDefinitelySanskrit_default ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Enables typing of Roman ( non - Tibetan ) text along
* with Tibetan .
* /
2003-10-12 16:44:28 +00:00
public void enableRoman ( ) {
isRomanEnabled = true ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Disables typing of Roman ( non - Tibetan ) text .
* /
2003-10-12 16:44:28 +00:00
public void disableRoman ( ) {
isRomanEnabled = false ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Checks to see if Roman input is enabled .
* @return true if so , false if not
* /
2003-10-12 16:44:28 +00:00
public boolean isRomanEnabled ( ) {
return isRomanEnabled ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Checks to see if currently in Roman input mode .
* @return true if so , false if not
* /
2003-10-12 16:44:28 +00:00
public boolean isRomanMode ( ) {
return ! isTibetan ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Toggles between Tibetan and Roman input modes .
* Does nothing if Roman input is disabled .
* @see # enableRoman ( )
* @see # disableRoman ( )
* /
2003-10-12 16:44:28 +00:00
public void toggleLanguage ( ) {
if ( isTibetan & & isRomanEnabled )
isTibetan = false ;
else
isTibetan = true ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Inserts Roman text into this object ' s document ,
* at the position of the caret .
* @param attr the attributes for the text to insert
* @param s the string of text to insert
* /
2003-10-12 16:44:28 +00:00
public void append ( String s , AttributeSet attr ) {
append ( caret . getDot ( ) , s , attr ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Inserts Roman text into this object ' s document .
* @param offset the position at which to insert text
* @param attr the attributes for the text to insert
* @param s the string of text to insert
* /
2003-10-12 16:44:28 +00:00
public void append ( int offset , String s , AttributeSet attr ) {
try {
getTibDoc ( ) . insertString ( offset , s , attr ) ;
}
catch ( BadLocationException ble ) {
2005-02-27 10:27:37 +00:00
ThdlDebug . noteIffyCode ( ) ;
2003-10-12 16:44:28 +00:00
}
}
2002-09-30 03:10:00 +00:00
/ * *
* Changes the default font size for Tibetan text entry mode .
*
* @param size a point size
* /
2003-10-12 16:44:28 +00:00
public void setTibetanFontSize ( int size ) {
if ( size > 0 )
getTibDoc ( ) . setTibetanFontSize ( size ) ;
}
2002-09-30 03:10:00 +00:00
2002-11-18 16:12:25 +00:00
/ * *
* Like { @link # setTibetanFontSize ( int ) } , but should be called only
* when the user has somewhat explicitly chosen the font size .
* This will set the font size but also record this as a user
* preference . Then you can choose to save the user preferences
2002-11-18 21:33:44 +00:00
* via { @link org . thdl . util . ThdlOptions # saveUserPreferences ( ) } .
2002-11-18 16:12:25 +00:00
*
* @param size a point size
* /
2003-10-12 16:44:28 +00:00
public void setByUserTibetanFontSize ( int size ) {
ThdlOptions . setUserPreference ( " thdl.default.tibetan.font.size " , size ) ;
setTibetanFontSize ( size ) ;
}
2002-11-18 16:12:25 +00:00
2002-09-30 03:10:00 +00:00
/ * *
* Gets the current point size for Tibetan text .
* @return the current default font size for Tibetan
* text entry mode
* /
2003-10-12 16:44:28 +00:00
public int getTibetanFontSize ( ) {
return getTibDoc ( ) . getTibetanFontSize ( ) ;
}
2002-09-30 03:10:00 +00:00
2003-06-20 15:27:20 +00:00
/ * *
* Overrides JTextComponent . setFont ( ) . This method
* calls setRomanAttributeSet ( ) , otherwise DuffPane
* would ignore calls to setFont ( ) .
* /
2003-10-12 16:44:28 +00:00
public void setFont ( Font font ) {
setRomanAttributeSet ( font . getName ( ) , font . getSize ( ) ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Changes the default font and font size for
* non - Tibetan ( Roman ) text entry mode .
*
* @param font a font name
* @param size a point size
* /
2003-10-12 16:44:28 +00:00
public void setRomanAttributeSet ( String font , int size ) {
2003-07-01 01:21:57 +00:00
if ( getTibDoc ( ) ! = null ) {
2003-08-31 16:06:35 +00:00
getTibDoc ( ) . setRomanAttributeSet ( romanFontFamily = font ,
romanFontSize = size ) ;
2003-07-01 01:21:57 +00:00
}
2003-10-12 16:44:28 +00:00
}
2002-09-30 03:10:00 +00:00
2002-11-18 16:12:25 +00:00
/ * * Like { @link # setRomanAttributeSet } , but allows for noting the
* ( explicit or implicit ) choice in the user ' s preferences
* file . * /
public void setByUserRomanAttributeSet ( String font , int size ) {
ThdlOptions . setUserPreference ( " thdl.default.roman.font.face " , font ) ;
ThdlOptions . setUserPreference ( " thdl.default.roman.font.size " , size ) ;
setRomanAttributeSet ( font , size ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Gets the current point size for non - Tibetan text .
* @return the current default font size for non - Tibetan
* ( Roman ) text entry mode
* /
2003-10-12 16:44:28 +00:00
public int getRomanFontSize ( ) {
return romanFontSize ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Gets the current font used for non - Tibetan text .
* @return the current default font for non - Tibetan
* ( Roman ) text entry mode
* /
2003-10-12 16:44:28 +00:00
public String getRomanFontFamily ( ) {
return romanFontFamily ;
}
2002-09-30 03:10:00 +00:00
2006-08-12 22:23:01 +00:00
/ * *
* //MP add comment
* /
private static boolean defaultDictionarySettingsEnabled ( ) {
return ThdlOptions . getBooleanOption ( " thdl.default.jskad.dictionary.enabled "
) ;
}
/ * *
* //MP add comment
* /
private static boolean defaultDictionarySettingsLocal ( ) {
return ThdlOptions . getBooleanOption ( " thdl.default.jskad.dictionary.local "
) ;
}
/ * *
* //MP add comment
* /
private static String defaultDictionarySettingsPath ( ) {
return ThdlOptions . getStringOption ( " thdl.default.jskad.dictionary.path " ,
" /home/mamrotek/thdl/dict/free " ) ;
}
/ * *
* //MP add comment
* /
public boolean getDictionarySettingsEnabled ( )
{
return dictionaryEnabled ;
}
public boolean getDictionarySettingsLocal ( )
{
return dictionaryLocal ;
}
public String getDictionarySettingsPath ( )
{
return dictionaryPath ;
}
/ * *
*
* /
public void setByUserDictionarySettings ( boolean enabled , boolean local , String path )
{
ThdlOptions . setUserPreference ( " thdl.default.jskad.dictionary.enabled " , enabled ) ;
ThdlOptions . setUserPreference ( " thdl.default.jskad.dictionary.local " , local ) ;
ThdlOptions . setUserPreference ( " thdl.default.jskad.dictionary.path " , path ) ;
setDictionarySettings ( enabled , local , path ) ;
}
/ * *
*
* /
public void setDictionarySettings ( boolean enabled , boolean local , String path )
{
dictionaryEnabled = enabled ;
dictionaryLocal = local ;
dictionaryPath = path ;
onDictionarySettingsChanged ( ) ;
}
public int getDictionaryLoadState ( )
{
return GlobalResourceHolder . getDictionaryLoadState ( ) ;
}
public void waitForDictionary ( )
{
GlobalResourceHolder . waitForDictionary ( ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Backspace and remove k elements from the current caret position .
*
* @param k the number of glyphs to remove by backspace
* /
2003-10-12 16:44:28 +00:00
private void backSpace ( int k ) {
try {
2003-04-14 05:22:27 +00:00
int newEnd = caret . getDot ( ) - k ;
if ( newEnd > = 0 ) {
getTibDoc ( ) . remove ( newEnd , k ) ;
if ( manageCaret ) caret . setDot ( newEnd ) ;
}
2003-10-12 16:44:28 +00:00
} catch ( BadLocationException ble ) {
2003-04-14 05:22:27 +00:00
ThdlDebug . noteIffyCode ( ) ;
2003-10-12 16:44:28 +00:00
}
}
2002-09-30 03:10:00 +00:00
/ * *
2002-11-02 05:21:12 +00:00
* Takes an old glyph list , which should be the currently visible set
* of glyphs preceding the cursor , and then tries to redraw the glyphs
* in light of the newly acquired keyboard input ( which led to a
* revised ' new ' glyph list ) . For example , the old glyph list might
* contain 'l' and 'n' , because the user had typed ' ln ' in Extended
* Wylie mode . This is what you ' d see on the screen . But assume that
* the new glyph list contains the stacked glyph ' l - ng ' , because the
* user has just finished typing ' lng ' . This method compares the
* glyphs , then figures out whether or not backspacing is necessary ,
* and draws whatever characters need to be drawn .
*
* < p > For example , suppose that oldGlyphList contains the two glyphs
* 'l' and 'n' , and newGlyphList contains a single glyph , ' lng ' . In
* this case , redrawGlyphs will be instructed to backspace over both
* 'l' and 'n' , and then insert ' lng ' . * /
2003-10-12 16:44:28 +00:00
private java . util . List redrawGlyphs ( java . util . List oldGlyphList , java . util . List newGlyphList ) {
if ( newGlyphList . isEmpty ( ) )
return newGlyphList ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
Iterator newIter = newGlyphList . iterator ( ) ;
DuffCode newDc = ( DuffCode ) newIter . next ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
int oldGlyphCount = oldGlyphList . size ( ) ;
int newGlyphCount = newGlyphList . size ( ) ;
int beginDifference = - 1 ; //at what point does the new glyph list begin to differ from the old one?
int k = 0 ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( oldGlyphCount ! = 0 ) {
int smallerGlyphCount ;
DuffCode oldDc ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( oldGlyphCount < newGlyphCount )
smallerGlyphCount = oldGlyphCount ;
else
smallerGlyphCount = newGlyphCount ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
Iterator oldIter = oldGlyphList . iterator ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
for ( ; k < smallerGlyphCount ; k + + ) {
oldDc = ( DuffCode ) oldIter . next ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( ! oldDc . equals ( newDc ) ) {
beginDifference = k ;
break ;
}
if ( newIter . hasNext ( ) )
newDc = ( DuffCode ) newIter . next ( ) ;
}
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( beginDifference = = - 1 )
beginDifference = smallerGlyphCount ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( beginDifference = = newGlyphCount ) {
if ( oldGlyphCount > newGlyphCount )
backSpace ( oldGlyphCount - newGlyphCount ) ; //deals with 'pd+m' problem
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
return newGlyphList ; //there is no difference between new and old glyph lists
}
}
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( beginDifference ! = - 1 )
backSpace ( oldGlyphCount - beginDifference ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
java . util . List sublist = newGlyphList . subList ( k , newGlyphCount ) ;
DuffData [ ] dd = TibTextUtils . convertGlyphs ( sublist ) ;
getTibDoc ( ) . insertDuff ( caret . getDot ( ) , dd ) ;
return newGlyphList ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Tries to insert the vowel v at the position of the caret .
* This method must deal with various issues , such as : can the preceding
* glyph take a vowel ? If not , then what ? If the preceding glyph can be
* followed by a vowel , then the method has to figure out what vowel
* glyph to affix , which may depend on the immediately preceding glyph ,
* but may depend on other factors as well . For example , when affixing
* gigu to the consonantal stack " k' " ( ie k plus achung ) , the value of
* the gigu will depend on " k " , not " ' " .
*
* @param v the vowel ( in Wylie ) you want to insert
2003-10-12 18:16:17 +00:00
* @return true if the status bar was updated
2002-09-30 03:10:00 +00:00
* /
2003-10-12 18:16:17 +00:00
private boolean putVowel ( String v ) {
boolean rv = false ;
2003-09-12 05:06:37 +00:00
if ( caret . getDot ( ) = = 0 ) {
if ( ! TibetanMachineWeb . isAChenRequiredBeforeVowel ( ) )
2003-10-12 18:16:17 +00:00
return printAChenWithVowel ( v ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 18:16:17 +00:00
return false ;
2003-09-12 05:06:37 +00:00
}
2002-09-30 03:10:00 +00:00
2003-09-12 05:06:37 +00:00
AttributeSet attr = getTibDoc ( ) . getCharacterElement ( caret . getDot ( ) - 1 ) . getAttributes ( ) ;
String fontName = StyleConstants . getFontFamily ( attr ) ;
int fontNum ;
2002-09-30 03:10:00 +00:00
2003-09-12 05:06:37 +00:00
if ( 0 ! = ( fontNum = TibetanMachineWeb . getTMWFontNumber ( fontName ) ) ) {
try {
char c2 = getTibDoc ( ) . getText ( caret . getDot ( ) - 1 , 1 ) . charAt ( 0 ) ;
int k = ( int ) c2 ;
if ( k < 32 | | k > 126 ) { //if previous character is formatting or some other non-character
if ( ! TibetanMachineWeb . isAChenRequiredBeforeVowel ( ) )
2003-10-12 18:16:17 +00:00
return printAChenWithVowel ( v ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 18:16:17 +00:00
return false ;
2003-09-12 05:06:37 +00:00
}
2002-09-30 03:10:00 +00:00
2003-09-12 05:06:37 +00:00
String wylie
2003-07-01 03:43:33 +00:00
= TibetanMachineWeb . getWylieForGlyph ( fontNum ,
k ,
TibTextUtils . weDoNotCareIfThereIsCorrespondingWylieOrNot ) ;
2003-09-12 05:06:37 +00:00
if ( TibetanMachineWeb . isWyliePunc ( wylie ) ) {
if ( charList . isEmpty ( ) & & ! TibetanMachineWeb . isAChenRequiredBeforeVowel ( ) ) {
2003-10-12 18:16:17 +00:00
return printAChenWithVowel ( v ) ;
2003-09-12 05:06:37 +00:00
}
}
2002-09-30 03:10:00 +00:00
2003-09-12 05:06:37 +00:00
DuffCode dc_1 = null ;
DuffCode dc_2 = new DuffCode ( fontNum , c2 ) ;
2002-09-30 03:10:00 +00:00
2003-09-12 05:06:37 +00:00
if ( caret . getDot ( ) > = 2 ) {
attr = getTibDoc ( ) . getCharacterElement ( caret . getDot ( ) - 2 ) . getAttributes ( ) ;
fontName = StyleConstants . getFontFamily ( attr ) ;
if ( 0 ! = ( fontNum = TibetanMachineWeb . getTMWFontNumber ( fontName ) ) ) {
c2 = getTibDoc ( ) . getText ( caret . getDot ( ) - 2 , 1 ) . charAt ( 0 ) ;
dc_1 = new DuffCode ( fontNum , c2 ) ;
}
}
2002-09-30 03:10:00 +00:00
2003-09-12 05:06:37 +00:00
java . util . List before_vowel = new ArrayList ( ) ;
if ( null ! = dc_1 )
before_vowel . add ( dc_1 ) ;
2002-09-30 03:10:00 +00:00
2003-09-12 05:06:37 +00:00
before_vowel . add ( dc_2 ) ;
java . util . List after_vowel = new ArrayList ( ) ;
try {
TibTextUtils . getVowel ( after_vowel , dc_1 , dc_2 , v ) ;
} catch ( IllegalArgumentException e ) {
// drop this vowel silently.
2003-10-12 18:16:17 +00:00
updateStatus ( " Dropping vowel as it would displace an existing vowel. [b] " ) ;
rv = true ;
2003-09-12 05:06:37 +00:00
}
2002-11-02 05:21:12 +00:00
if ( after_vowel . size ( ) > = before_vowel . size ( ) ) {
setNumberOfGlyphsForLastVowel ( after_vowel . size ( )
- before_vowel . size ( ) ) ;
} else {
setNumberOfGlyphsForLastVowel ( 0 ) ;
2003-09-12 05:06:37 +00:00
// can happen for pou (as opposed to puo) (FIXME)
2002-11-02 05:21:12 +00:00
}
2003-09-12 05:06:37 +00:00
redrawGlyphs ( before_vowel , after_vowel ) ;
}
catch ( BadLocationException ble ) {
System . out . println ( " no--can't insert here " ) ;
2002-11-02 05:21:12 +00:00
ThdlDebug . noteIffyCode ( ) ;
2003-09-12 05:06:37 +00:00
}
}
else { //0 font means not Tibetan font, so begin new Tibetan font section
if ( ! TibetanMachineWeb . isAChenRequiredBeforeVowel ( ) )
2003-10-12 18:16:17 +00:00
return printAChenWithVowel ( v ) ;
2003-09-12 05:06:37 +00:00
}
2003-10-12 18:16:17 +00:00
return rv ;
2003-09-12 05:06:37 +00:00
}
2002-09-30 03:10:00 +00:00
2002-11-02 05:21:12 +00:00
/ * *
* If the character last displayed was a vowel , this is how many glyphs
* the vowel was composed of . ( Some vowels , such as Wylie 'I' , consist
* of two glyphs . ) We use the getter and setter for this variable and
* never the variable itself . * /
2003-10-12 16:44:28 +00:00
private int numberOfGlyphsForLastVowel ;
2002-11-02 05:21:12 +00:00
private int getNumberOfGlyphsForLastVowel ( ) {
return numberOfGlyphsForLastVowel ;
}
private void setNumberOfGlyphsForLastVowel ( int x ) {
numberOfGlyphsForLastVowel = x ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Prints ACHEN together with the vowel v . When using the Wylie
* keyboard , or any other keyboard in which { @link org . thdl . tib . text . TibetanMachineWeb # isAChenRequiredBeforeVowel ( ) isAChenRequiredBeforeVowel ( ) }
* is false , this method is called frequently .
*
* @param v the vowel ( in Wylie ) which you want to print with ACHEN
2003-10-12 18:16:17 +00:00
* @return true if and only if the status bar was updated
2002-09-30 03:10:00 +00:00
* /
2003-10-12 18:16:17 +00:00
private boolean printAChenWithVowel ( String v ) {
boolean rv = false ;
2003-09-12 05:06:37 +00:00
DuffCode [ ] dc_array = ( DuffCode [ ] ) TibetanMachineWeb . getTibHash ( ) . get ( TibetanMachineWeb . ACHEN ) ;
DuffCode dc = dc_array [ TibetanMachineWeb . TMW ] ;
java . util . List achenlist = new ArrayList ( ) ;
try {
TibTextUtils . getVowel ( achenlist , dc , v ) ;
} catch ( IllegalArgumentException e ) {
// drop this vowel silently.
2003-10-12 18:16:17 +00:00
updateStatus ( " Dropping vowel as it would displace an existing vowel. [a] " ) ;
rv = true ;
2003-09-12 05:06:37 +00:00
}
DuffData [ ] dd = TibTextUtils . convertGlyphs ( achenlist ) ;
2003-10-12 16:44:28 +00:00
getTibDoc ( ) . insertDuff ( caret . getDot ( ) , dd ) ;
2003-10-12 18:16:17 +00:00
return rv ;
2003-09-12 05:06:37 +00:00
}
2002-09-30 03:10:00 +00:00
/ * *
* Puts a bindu / anusvara at the current caret position .
* In the TibetanMachineWeb font , top vowels ( like gigu ,
* drengbu , etc . ) are merged together with bindu in a
* single glyph . This method deals with the problem of
* correctly displaying a bindu given this complication .
* /
2003-10-12 16:44:28 +00:00
private void putBindu ( ) {
special_bindu_block : {
if ( caret . getDot ( ) = = 0 )
break special_bindu_block ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
AttributeSet attr = getTibDoc ( ) . getCharacterElement ( caret . getDot ( ) - 1 ) . getAttributes ( ) ;
String fontName = StyleConstants . getFontFamily ( attr ) ;
int fontNum ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( 0 = = ( fontNum = TibetanMachineWeb . getTMWFontNumber ( fontName ) ) )
break special_bindu_block ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
try {
char c2 = getTibDoc ( ) . getText ( caret . getDot ( ) - 1 , 1 ) . charAt ( 0 ) ;
int k = ( int ) c2 ;
if ( k < 32 | | k > 126 ) //if previous character is formatting or some other non-character
break special_bindu_block ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
String wylie
2003-07-01 03:43:33 +00:00
= TibetanMachineWeb . getWylieForGlyph ( fontNum ,
k ,
TibTextUtils . weDoNotCareIfThereIsCorrespondingWylieOrNot ) ;
2003-10-12 16:44:28 +00:00
if ( ! TibetanMachineWeb . isWylieVowel ( wylie ) )
break special_bindu_block ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
DuffCode dc = new DuffCode ( fontNum , c2 ) ;
java . util . List beforecaret = new ArrayList ( ) ;
beforecaret . add ( dc ) ;
java . util . List bindulist = new LinkedList ( ) ;
2003-09-12 05:06:37 +00:00
TibTextUtils . getBindu ( bindulist , dc ) ;
2003-10-12 16:44:28 +00:00
redrawGlyphs ( beforecaret , bindulist ) ;
initKeyboard ( ) ;
return ;
}
catch ( BadLocationException ble ) {
System . out . println ( " no--can't do this bindu maneuver " ) ;
2002-11-02 05:21:12 +00:00
ThdlDebug . noteIffyCode ( ) ;
2003-10-12 16:44:28 +00:00
}
}
2002-09-30 03:10:00 +00:00
2003-09-12 05:06:37 +00:00
java . util . List binduList = new LinkedList ( ) ;
TibTextUtils . getBindu ( binduList , null ) ;
2003-10-12 16:44:28 +00:00
DuffData [ ] dd = TibTextUtils . convertGlyphs ( binduList ) ;
getTibDoc ( ) . insertDuff ( caret . getDot ( ) , dd ) ;
initKeyboard ( ) ;
}
2002-09-30 03:10:00 +00:00
/ * *
* Required by implementations of the
* { @link java . awt . event . FocusListener FocusListener } interface ,
* this method simply initializes the keyboard whenever this
* object gains focus .
*
* @param e a FocusEvent
* /
2003-10-12 16:44:28 +00:00
public void focusGained ( FocusEvent e ) {
}
2002-09-30 03:10:00 +00:00
/ * *
* Required by implementations of the
* { @link java . awt . event . FocusListener FocusListener } interface ,
2002-10-22 03:53:33 +00:00
* this method resets the keyboard .
2002-09-30 03:10:00 +00:00
*
* @param e a FocusEvent
* /
2003-10-12 16:44:28 +00:00
public void focusLost ( FocusEvent e ) {
initKeyboard ( ) ;
setNumberOfGlyphsForLastVowel ( 0 ) ;
2002-10-22 03:53:33 +00:00
appendStatus ( " (because the window focus was lost) " ) ;
2003-10-12 16:44:28 +00:00
}
2002-09-30 03:10:00 +00:00
2005-02-26 22:57:38 +00:00
/ * * Converts the current selection to Unicode and then copies that
Unicode to the system clipboard . * /
public void copyAsUnicode ( ) {
copyOrCutToUnicode ( getSelectionStart ( ) , getSelectionEnd ( ) , false ) ;
}
/** Copies the current selection to the system clipboard. */
2003-10-12 16:44:28 +00:00
public void copy ( ) {
2002-10-22 03:53:33 +00:00
copy ( getSelectionStart ( ) , getSelectionEnd ( ) , false ) ;
}
/ * * If this . isEditable ( ) , then this copies the current selection
to the system clipboard and then deletes it . * /
2003-10-12 16:44:28 +00:00
public void cut ( ) {
2002-10-22 03:53:33 +00:00
copy ( getSelectionStart ( ) , getSelectionEnd ( ) , true ) ;
}
2002-11-27 23:29:31 +00:00
/** If this.isEditable(), then this deletes the current selection. */
2003-10-12 16:44:28 +00:00
public void deleteCurrentSelection ( )
{
2002-11-27 23:29:31 +00:00
int start = getSelectionStart ( ) , end = getSelectionEnd ( ) ;
if ( ! this . isEditable ( ) )
return ;
2003-04-14 05:22:27 +00:00
try {
ThdlDebug . verify ( getDocument ( ) = = getTibDoc ( ) ) ;
2002-11-27 23:29:31 +00:00
getDocument ( ) . remove ( start , end - start ) ;
} catch ( BadLocationException ble ) {
ble . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
}
2002-09-30 03:10:00 +00:00
/ * *
* Cuts or copies the specified portion of this object ' s document
* to the system clipboard . What is cut / copied is Wylie text -
* so , if you cut / copy a region of TibetanMachineWeb , it first converts
* it to Wylie before putting it on the clipboard . If the user
* tries to cut / copy non - TibetanMachineWeb , only the text preceding the
* first non - TibetanMachineWeb character is cut / copied .
* @param start the begin offset of the copy
* @param end the end offset of the copy
* @param remove this should be true if the operation is ' cut ' ,
* false if it is ' copy '
* /
2003-10-12 16:44:28 +00:00
private void copy ( int start , int end , boolean remove ) {
2005-02-26 22:57:38 +00:00
if ( start ! = end ) {
2003-04-14 05:22:27 +00:00
ThdlDebug . verify ( getDocument ( ) = = getTibDoc ( ) ) ;
2005-02-26 22:57:38 +00:00
RTFSelection rtfSelection
= new RTFSelection ( ( StyledDocument ) getDocument ( ) ,
start , end - start ) ;
2003-10-12 16:44:28 +00:00
try {
rtfBoard . setContents ( rtfSelection , rtfSelection ) ;
2005-02-26 20:46:40 +00:00
updateStatus ( " Copied to clipboard " ) ;
2003-10-12 16:44:28 +00:00
} catch ( IllegalStateException ise ) {
2005-02-26 22:57:38 +00:00
// TODO(dchandler): let's show a dialog box. The user
// should know that another app is monopolizing the
// clipboard.
2003-10-12 16:44:28 +00:00
ise . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
2005-02-26 20:46:40 +00:00
} else
updateStatus ( " Nothing to copy/cut " ) ;
2005-02-26 22:57:38 +00:00
if ( remove & & this . isEditable ( ) ) {
try {
ThdlDebug . verify ( getDocument ( ) = = getTibDoc ( ) ) ;
getDocument ( ) . remove ( start , end - start ) ;
updateStatus ( " Cut to clipboard " ) ;
} catch ( BadLocationException ble ) {
ble . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
}
}
2005-02-27 10:27:37 +00:00
2005-02-27 22:08:47 +00:00
/ * * Saves the TMW document underlying this DuffPane as Unicode .
2005-02-27 10:27:37 +00:00
@param utf8_text true if you want to save as UTF - 8 - encoded
Unicode text , false if you want to save as Unicode in RTF
@param out an OutputStream that will be closed when we ' re done
in every case
2005-02-27 22:08:47 +00:00
@returns true on perfect success , false otherwise - - for finer
grain , use the standalone TMW - > Unicode converter . * /
2005-02-27 10:27:37 +00:00
public boolean saveAsUnicode ( boolean utf8_text , OutputStream out )
throws IOException
{
boolean retval = true ;
ThdlDebug . verify ( getDocument ( ) = = getTibDoc ( ) ) ;
// construct new document so that we can use
// TibetanDocument.convertToUnicode(..).
TibetanDocument newDoc = new TibetanDocument ( ) ;
try {
for ( int i = 0 ; i < getTibDoc ( ) . getLength ( ) ; i + + ) {
String s = getTibDoc ( ) . getText ( i , 1 ) ;
AttributeSet as
= getTibDoc ( ) . getCharacterElement ( i ) . getAttributes ( ) ;
String fontName = StyleConstants . getFontFamily ( as ) ;
if ( 0 ! = TibetanMachineWeb . getTMFontNumber ( fontName ) )
retval = false ;
newDoc . insertString ( i , s , as ) ;
}
} catch ( BadLocationException ble ) {
throw new Error ( " this cannot happen " ) ;
}
if ( newDoc . convertToUnicode ( 0 , newDoc . getLength ( ) , null ,
ThdlOptions . getStringOption ( " thdl.tmw.to.unicode.font " ) . intern ( ) ,
new long [ ] { 0 } ) ) {
retval = false ;
}
if ( utf8_text ) {
BufferedWriter bw
= new BufferedWriter ( new OutputStreamWriter ( out , " UTF-8 " ) ) ;
newDoc . writeTextOutput ( bw ) ;
bw . close ( ) ;
} else {
try {
rtfEd . write ( out , newDoc , 0 , newDoc . getLength ( ) ) ;
} catch ( BadLocationException ble ) {
throw new Error ( " this cannot happen either " ) ;
}
out . close ( ) ;
}
return retval ;
}
2005-02-26 22:57:38 +00:00
/ * * Adds to the clipboard the Unicode you ' d get if you used a
TMW - > Unicode conversion on the specified portion of the
document . This is plain - text Unicode , not RTF . We lose info
about font size , formatting like centering etc . * /
private void copyOrCutToUnicode ( int start , int end , boolean remove ) {
if ( start ! = end ) {
ThdlDebug . verify ( getDocument ( ) = = getTibDoc ( ) ) ;
// construct new document that contains only portion of
// text you want to copy/cut so that we can use
// TibetanDocument.convertToUnicode(..).
TibetanDocument newDoc = new TibetanDocument ( ) ;
boolean warn_about_tm = false ;
2005-02-27 10:27:37 +00:00
try {
for ( int i = start ; i < end ; i + + ) {
2005-02-26 22:57:38 +00:00
String s = getTibDoc ( ) . getText ( i , 1 ) ;
AttributeSet as
= getTibDoc ( ) . getCharacterElement ( i ) . getAttributes ( ) ;
String fontName = StyleConstants . getFontFamily ( as ) ;
if ( 0 ! = TibetanMachineWeb . getTMFontNumber ( fontName ) )
warn_about_tm = true ;
newDoc . insertString ( i - start , s , as ) ;
}
2005-02-27 10:27:37 +00:00
} catch ( BadLocationException ble ) {
ble . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
2005-02-26 22:57:38 +00:00
}
String unicode = " [Jskad: Converting to Unicode failed.] " ;
if ( newDoc . convertToUnicode ( 0 , newDoc . getLength ( ) , null , null ,
new long [ ] { 0 } ) ) {
// TODO(dchandler): better error handling!
} else {
try {
unicode = newDoc . getText ( 0 , newDoc . getLength ( ) ) ;
if ( warn_about_tm )
unicode
= ( " [Jskad: Warning while copying as Unicode: We convert TibetanMachineWeb to Unicode but not TibetanMachine during this operation, but there was some TibetanMachine text present. This will appear as garbage! Convert your TibetanMachine to TibetanMachineWeb and try again if you want perfect results.] "
+ unicode ) ;
if ( start ! = end & & unicode . length ( ) = = 0 )
unicode = " [Jskad: Converting to Unicode shouldn't have produced the empty string! This is a bug in Jskad.] " ;
} catch ( BadLocationException ble ) {
// This should never happen.
unicode = " [Jskad: Oops! bigtime bug 13412412kjlwe32] " ;
}
}
StringSelection uSelection = new StringSelection ( unicode ) ;
try {
rtfBoard . setContents ( uSelection , uSelection ) ;
updateStatus ( " Copied to clipboard " ) ;
} catch ( IllegalStateException ise ) {
// TODO(dchandler): let's show a dialog box. The user
// should know that another app is monopolizing the
// clipboard.
ise . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
} else
updateStatus ( " Nothing to copy/cut " ) ;
Fixed bug 617156, "DuffPane ignores setEditable(false)".
I fixed this the easy way, by checking the value of isEditable() before
cutting, pasting, or adding typed text. I may have missed a spot, but
checking at a lower level is a bit less efficient.
Fixing this the hard way, the keymaps-and-overridden-default-action way,
seems like it will make the code uglier, not cleaner. And it won't get us
closer to fixing the killer bug, 614475, "Improper Line Wrapping".
2002-10-20 05:54:29 +00:00
2005-02-26 22:57:38 +00:00
if ( remove & & this . isEditable ( ) ) {
2003-10-12 16:44:28 +00:00
try {
2003-04-14 05:22:27 +00:00
ThdlDebug . verify ( getDocument ( ) = = getTibDoc ( ) ) ;
2005-02-26 22:57:38 +00:00
getDocument ( ) . remove ( start , end - start ) ;
2005-02-26 20:46:40 +00:00
updateStatus ( " Cut to clipboard " ) ;
2003-10-12 16:44:28 +00:00
} catch ( BadLocationException ble ) {
ble . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
Fixed bug 617156, "DuffPane ignores setEditable(false)".
I fixed this the easy way, by checking the value of isEditable() before
cutting, pasting, or adding typed text. I may have missed a spot, but
checking at a lower level is a bit less efficient.
Fixing this the hard way, the keymaps-and-overridden-default-action way,
seems like it will make the code uglier, not cleaner. And it won't get us
closer to fixing the killer bug, 614475, "Improper Line Wrapping".
2002-10-20 05:54:29 +00:00
}
2003-10-12 16:44:28 +00:00
}
2002-09-30 03:10:00 +00:00
2003-04-02 20:37:14 +00:00
public void paste ( ) {
2003-04-03 05:17:40 +00:00
/ * added by AM . If we are pasting over selected text , such text
such be substituted by the text to be pasted . * /
2004-08-19 19:51:21 +00:00
// Respect setEditable(boolean):
if ( this . isEditable ( ) ) {
deleteCurrentSelection ( ) ;
paste ( getSelectionStart ( ) ) ;
}
2003-04-02 20:37:14 +00:00
}
2002-09-30 03:10:00 +00:00
/ * *
* Pastes the contents of the system clipboard into this object ' s
2004-07-05 03:43:42 +00:00
* document , at the specified position . If the text was copied from an
* RTF compliant application , it will be pasted using with the original
* fonts and font sizes . Else it will be assumed that it is Wylie text .
2002-09-30 03:10:00 +00:00
* This Wylie is converted and pasted into the document as
* TibetanMachineWeb . If the text to paste is invalid Wylie ,
* then it will not be pasted , and instead an error message will
* appear .
* @param offset the position in the document you want to paste to
* /
2004-07-05 03:43:42 +00:00
public void paste ( int offset )
{
Fixed bug 617156, "DuffPane ignores setEditable(false)".
I fixed this the easy way, by checking the value of isEditable() before
cutting, pasting, or adding typed text. I may have missed a spot, but
checking at a lower level is a bit less efficient.
Fixing this the hard way, the keymaps-and-overridden-default-action way,
seems like it will make the code uglier, not cleaner. And it won't get us
closer to fixing the killer bug, 614475, "Improper Line Wrapping".
2002-10-20 05:54:29 +00:00
// Respect setEditable(boolean):
if ( ! this . isEditable ( ) )
return ;
2002-11-27 23:29:31 +00:00
2004-07-05 03:43:42 +00:00
try
{
2003-10-12 16:44:28 +00:00
Transferable contents = rtfBoard . getContents ( this ) ;
2004-08-19 19:51:21 +00:00
2004-07-05 03:43:42 +00:00
if ( contents . isDataFlavorSupported ( rtfFlavor ) )
{
2003-10-12 16:44:28 +00:00
InputStream in = ( InputStream ) contents . getTransferData ( rtfFlavor ) ;
int p1 = offset ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
//construct new document that contains only portion of text you want to paste
StyledDocument sd = new DefaultStyledDocument ( ) ;
2002-10-22 03:53:33 +00:00
// I swear this happened once when I pasted in some
// random junk just after Jskad started up.
ThdlDebug . verify ( null ! = in ) ;
2003-05-15 01:41:56 +00:00
boolean errorReading = false ;
2004-07-05 03:43:42 +00:00
try
{
2003-06-29 03:03:07 +00:00
if ( ! ThdlOptions . getBooleanOption ( " thdl.do.not.fix.rtf.hex.escapes " ) )
in = new RTFFixerInputStream ( in ) ;
2003-05-15 01:41:56 +00:00
rtfEd . read ( in , sd , 0 ) ;
2004-07-05 03:43:42 +00:00
}
catch ( Exception e )
{
2003-05-15 01:41:56 +00:00
errorReading = true ;
JOptionPane . showMessageDialog ( this ,
" You cannot paste from the application from which you copied. \ nIt uses an RTF format that is too advanced for the version \ nof Java Jskad is running atop. " ) ;
}
2004-07-05 03:43:42 +00:00
if ( ! errorReading )
{
for ( int i = 0 ; i < sd . getLength ( ) - 1 ; i + + ) { //getLength()-1 so that final newline is not included in paste
try {
String s = sd . getText ( i , 1 ) ;
AttributeSet as
= sd . getCharacterElement ( i ) . getAttributes ( ) ;
getTibDoc ( ) . insertString ( p1 + i , s , as ) ;
} catch ( BadLocationException ble ) {
ble . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
2003-05-15 01:41:56 +00:00
}
2004-07-05 03:43:42 +00:00
}
2003-05-15 01:41:56 +00:00
}
2003-10-12 16:44:28 +00:00
} else if ( contents . isDataFlavorSupported ( DataFlavor . stringFlavor ) )
{
2004-07-05 03:43:42 +00:00
// if (!isRomanEnabled) {
2003-10-12 16:44:28 +00:00
String data = ( String ) contents . getTransferData ( DataFlavor . stringFlavor ) ;
toTibetanMachineWeb ( data , offset ) ;
2004-07-05 03:43:42 +00:00
/ * }
2002-10-25 17:59:27 +00:00
else
{
2003-10-12 16:44:28 +00:00
String s = ( String ) contents . getTransferData ( DataFlavor . stringFlavor ) ;
replaceSelection ( s ) ;
2004-07-05 03:43:42 +00:00
} * /
2003-10-12 16:44:28 +00:00
}
} catch ( UnsupportedFlavorException ufe ) {
ufe . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
} catch ( IOException ioe ) {
ioe . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
} catch ( IllegalStateException ise ) {
ise . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
2002-09-30 03:10:00 +00:00
}
2003-10-12 16:44:28 +00:00
private void processRomanChar ( String key , AttributeSet attSet ) {
switch ( key . charAt ( 0 ) ) {
case KeyEvent . VK_TAB :
case KeyEvent . VK_ENTER :
case KeyEvent . VK_ESCAPE :
case KeyEvent . VK_DELETE :
case KeyEvent . VK_BACK_SPACE :
break ;
default :
append ( key , attSet ) ;
}
}
2002-09-30 03:10:00 +00:00
2003-04-02 20:37:14 +00:00
/ * * Utility method called by DuffPane ' s Keymap as the
2003-10-12 16:44:28 +00:00
default behavior when the user is in Tibetan typing mode .
2002-11-03 17:05:05 +00:00
@param c the character the user entered in whatever keyboard
is in use
2003-04-12 16:28:56 +00:00
* /
2003-10-12 16:44:28 +00:00
private void processTibetanChar ( char c ) {
2002-09-30 03:10:00 +00:00
2002-10-22 03:53:33 +00:00
// Have we modified the status bar?
2003-10-12 16:44:28 +00:00
boolean changedStatus = false ;
2002-10-22 03:53:33 +00:00
2003-10-12 16:44:28 +00:00
key_block :
{
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( TibetanMachineWeb . hasDisambiguatingKey ( ) )
if ( c = = TibetanMachineWeb . getDisambiguatingKey ( ) ) {
initKeyboard ( ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because you pressed the disambiguating key) " ) ;
2003-10-12 16:44:28 +00:00
break key_block ;
} ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( TibetanMachineWeb . hasSanskritStackingKey ( ) | | TibetanMachineWeb . hasTibetanStackingKey ( ) ) {
if ( c = = TibetanMachineWeb . getStackingKey ( ) ) {
if ( TibetanMachineWeb . isStackingMedial ( ) ) {
int size = charList . size ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( size = = 0 ) {
initKeyboard ( ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because you pressed the stacking key with nothing to stack on) " ) ;
2003-10-12 16:44:28 +00:00
} else if ( size > 1 & & isStackingRightToLeft ) {
String s = ( String ) charList . remove ( charList . size ( ) - 1 ) ;
newGlyphList = TibTextUtils . getGlyphs ( charList , isStackingRightToLeft , isDefinitelyTibetan , isDefinitelySanskrit ) ;
oldGlyphList = redrawGlyphs ( oldGlyphList , newGlyphList ) ;
initKeyboard ( ) ;
charList . add ( s ) ;
newGlyphList = TibTextUtils . getGlyphs ( charList , isStackingRightToLeft , isDefinitelyTibetan , isDefinitelySanskrit ) ;
oldGlyphList = redrawGlyphs ( oldGlyphList , newGlyphList ) ;
holdCurrent = new StringBuffer ( ) ;
isTopHypothesis = false ;
isStackingOn = true ;
isStackingRightToLeft = false ;
isDefinitelyTibetan = isDefinitelyTibetan_withStackKey ;
isDefinitelySanskrit = isDefinitelySanskrit_withStackKey ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
updateStatus ( " You have stacked a letter atop another. " ) ;
2003-10-12 16:44:28 +00:00
} else {
holdCurrent = new StringBuffer ( ) ;
isTopHypothesis = false ;
isStackingOn = true ;
isStackingRightToLeft = false ;
isDefinitelyTibetan = isDefinitelyTibetan_withStackKey ;
isDefinitelySanskrit = isDefinitelySanskrit_withStackKey ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
updateStatus ( " Some sort of stack-fu is happening/has happened. " ) ;
2003-10-12 16:44:28 +00:00
}
break key_block ;
}
else { //stacking must be pre/post
if ( ! isStackingOn | | ( isStackingOn & & isDefinitelyTibetan = = isDefinitelyTibetan_default ) ) {
initKeyboard ( ) ;
isStackingOn = true ;
isStackingRightToLeft = false ;
isDefinitelyTibetan = isDefinitelyTibetan_withStackKey ;
isDefinitelySanskrit = isDefinitelySanskrit_withStackKey ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
updateStatus ( " !isStackingOn || (isStackingOn && isDefinitelyTibetan==isDefinitelyTibetan_default) " ) ;
2003-10-12 16:44:28 +00:00
}
else { try {
char ch = getTibDoc ( ) . getText ( caret . getDot ( ) - 1 , 1 ) . charAt ( 0 ) ;
AttributeSet attr = getTibDoc ( ) . getCharacterElement ( caret . getDot ( ) - 1 ) . getAttributes ( ) ;
String fontName = StyleConstants . getFontFamily ( attr ) ;
int fontNum = TibetanMachineWeb . getTMWFontNumber ( fontName ) ;
if ( 0 = = fontNum ) {
initKeyboard ( ) ;
isStackingOn = true ;
isStackingRightToLeft = false ;
isDefinitelyTibetan = isDefinitelyTibetan_withStackKey ;
isDefinitelySanskrit = isDefinitelySanskrit_withStackKey ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because 0 == fontNum) " ) ;
2003-10-12 16:44:28 +00:00
} else {
initKeyboard ( ) ;
DuffCode dc = new DuffCode ( fontNum , ch ) ;
if ( ! TibetanMachineWeb . isStack ( dc ) & & ! TibetanMachineWeb . isSanskritStack ( dc ) ) {
isStackingOn = true ;
isStackingRightToLeft = false ;
isDefinitelyTibetan = isDefinitelyTibetan_withStackKey ;
isDefinitelySanskrit = isDefinitelySanskrit_withStackKey ;
}
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because 0 != fontNum) " ) ;
2003-10-12 16:44:28 +00:00
}
}
catch ( BadLocationException ble ) {
initKeyboard ( ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because a BadLocationException was thrown) " ) ;
2003-10-12 16:44:28 +00:00
} }
break key_block ;
}
}
}
switch ( c ) {
case KeyEvent . VK_TAB :
case KeyEvent . VK_ENTER :
case KeyEvent . VK_ESCAPE :
initKeyboard ( ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because you typed enter, tab, or escape) " ) ;
2003-10-12 16:44:28 +00:00
break ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
default :
String val = String . valueOf ( c ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( TibetanMachineWeb . isPunc ( val ) ) { //punctuation
val = TibetanMachineWeb . getWylieForPunc ( val ) ;
2002-09-30 03:10:00 +00:00
Numerous EWTS->Unicode and especially EWTS->TMW improvements.
Fixed ordering of Unicode wowels. [ku+A] gives the correct Unicode
now, e.g.
EWTS->TMW looks better for some wacky wowels like, I'm guessing here, [ku+A].
EWTS->TMW should now give errors any time the full input isn't used.
Previously, wacky wowels like [kai+-i] would lead to some droppage.
EWTS->TMW->Unicode testing is now in effect. This found a ton of
EWTS->TMW bugs, most or all of which are fixed now.
TMW->Unicode is improved/fixed for {
\u5350,\u534D,\u0F88+k,\u0F88+kh,U }. (Why U? "\u0f75" is
discouraged in favor of "\u0f71\u0f74".)
NOTE: TMW_RTF_TO_THDL_WYLIETest is still disabled for the nightly
builds' sake, but I ran it in my sandbox and it passed.
2005-07-11 02:51:06 +00:00
if ( val . startsWith ( THDLWylieConstants . BINDU ) )
2003-10-12 16:44:28 +00:00
putBindu ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
else {
DuffCode puncDc = TibetanMachineWeb . getGlyph ( val ) ;
MutableAttributeSet mas = TibetanMachineWeb . getAttributeSet ( puncDc . getFontNum ( ) ) ;
getTibDoc ( ) . appendDuff ( caret . getDot ( ) , String . valueOf ( puncDc . getCharacter ( ) ) , mas ) ;
}
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
initKeyboard ( ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because you typed punctuation) " ) ;
2003-10-12 16:44:28 +00:00
break key_block ;
}
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( charList . size ( ) = = 0 ) { //add current character to charList if possible
holdCurrent . append ( c ) ;
String s = holdCurrent . toString ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( TibetanMachineWeb . isVowel ( s ) ) {
s = TibetanMachineWeb . getWylieForVowel ( s ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 18:16:17 +00:00
int nglv = 0 ;
2003-10-12 16:44:28 +00:00
if ( isTypingVowel ) {
2002-11-02 05:21:12 +00:00
//note: this takes care of multiple keystroke vowels like 'ai'
2003-10-12 18:16:17 +00:00
backSpace ( nglv = getNumberOfGlyphsForLastVowel ( ) ) ;
2002-11-02 05:21:12 +00:00
}
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
isTypingVowel = true ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
2003-10-12 18:16:17 +00:00
if ( ! putVowel ( s ) ) {
updateStatus ( " You typed a vowel " + s + " (the simple way). We backspaced " + nglv + " spaces before adding it. " ) ;
}
2003-10-12 16:44:28 +00:00
} else {
if ( isTypingVowel ) {
isTypingVowel = false ;
s = String . valueOf ( c ) ;
holdCurrent = new StringBuffer ( s ) ;
}
if ( TibetanMachineWeb . isVowel ( s ) ) {
s = TibetanMachineWeb . getWylieForVowel ( s ) ;
isTypingVowel = true ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
2003-10-12 18:16:17 +00:00
if ( ! putVowel ( s ) ) {
updateStatus ( " You typed a vowel (the other way). " ) ;
}
2003-10-12 16:44:28 +00:00
} else if ( TibetanMachineWeb . isChar ( s ) ) {
s = TibetanMachineWeb . getWylieForChar ( s ) ;
charList . add ( s ) ;
isTopHypothesis = true ;
newGlyphList = TibTextUtils . getGlyphs ( charList , isStackingRightToLeft , isDefinitelyTibetan , isDefinitelySanskrit ) ;
oldGlyphList = redrawGlyphs ( oldGlyphList , newGlyphList ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
updateStatus ( " You typed a non-vowel, Tibetan character. " ) ;
2003-10-12 16:44:28 +00:00
} else {
Fixes bug 624133, "Input freezes after impossible character". Try 'shsM' in
ACIP or 'ShSm' in Extended Wylie to see the new behavior.
We use a trie to store valid input sequences. In the future, we could use
the same trie as a replacement for the more inefficient HashSets we use to
store characters, vowels, and punctuation. For example, we'd use
'validInputSequences.put("K", new Pair("consonant", "k"))' when reading
in the ACIP keyboard's description of the first consonant of the Tibetan
alphabet in 'TibetanKeyboard.java'.
Note that the current trie implementation is only useful for 7- or 8-bit
transcription systems, and works best for tries with low average depth, which
describes a transcription system's trie very well. If you used arbitrary
Unicode in your keyboard, you'd need a different trie implementation.
Improved the optional keyboard input mode status messages.
2002-11-02 18:44:24 +00:00
if ( TibetanMachineWeb . hasInputPrefix ( s ) ) {
isTopHypothesis = false ;
changedStatus = true ;
updateStatus ( " incomplete input (like the \" S \" in Extended Wylie's \" Sh \" ) " ) ;
} else {
2002-11-03 17:05:05 +00:00
// before we call initKeyboard, get info to prevent infinite loops:
StringBuffer prevHoldCurrent = new StringBuffer ( holdCurrent . toString ( ) ) ;
Fixes bug 624133, "Input freezes after impossible character". Try 'shsM' in
ACIP or 'ShSm' in Extended Wylie to see the new behavior.
We use a trie to store valid input sequences. In the future, we could use
the same trie as a replacement for the more inefficient HashSets we use to
store characters, vowels, and punctuation. For example, we'd use
'validInputSequences.put("K", new Pair("consonant", "k"))' when reading
in the ACIP keyboard's description of the first consonant of the Tibetan
alphabet in 'TibetanKeyboard.java'.
Note that the current trie implementation is only useful for 7- or 8-bit
transcription systems, and works best for tries with low average depth, which
describes a transcription system's trie very well. If you used arbitrary
Unicode in your keyboard, you'd need a different trie implementation.
Improved the optional keyboard input mode status messages.
2002-11-02 18:44:24 +00:00
// FIXME: ring a bell here so the user knows what's up.
initKeyboard ( ) ;
// The recursive call to
// processTibetanChar will update the
// status bar, so set this to true:
changedStatus = true ;
// This status message is bound to get
// overridden, but this is what we
// would say if we had a queue of
// messages or something like Emacs's
// M-x view-lossage to see a history
// of status messages:
2002-11-03 17:05:05 +00:00
appendStatus ( " (because you typed something invalid [1st way]) " ) ;
Fixes bug 624133, "Input freezes after impossible character". Try 'shsM' in
ACIP or 'ShSm' in Extended Wylie to see the new behavior.
We use a trie to store valid input sequences. In the future, we could use
the same trie as a replacement for the more inefficient HashSets we use to
store characters, vowels, and punctuation. For example, we'd use
'validInputSequences.put("K", new Pair("consonant", "k"))' when reading
in the ACIP keyboard's description of the first consonant of the Tibetan
alphabet in 'TibetanKeyboard.java'.
Note that the current trie implementation is only useful for 7- or 8-bit
transcription systems, and works best for tries with low average depth, which
describes a transcription system's trie very well. If you used arbitrary
Unicode in your keyboard, you'd need a different trie implementation.
Improved the optional keyboard input mode status messages.
2002-11-02 18:44:24 +00:00
2002-11-03 17:05:05 +00:00
if ( prevHoldCurrent . length ( ) ! = 0
& & ! prevHoldCurrent . toString ( ) . equals ( String . valueOf ( c ) ) ) {
2003-04-12 16:28:56 +00:00
processTibetanChar ( c ) ;
2002-11-03 17:05:05 +00:00
}
// else we'd go into an infinite loop
Fixes bug 624133, "Input freezes after impossible character". Try 'shsM' in
ACIP or 'ShSm' in Extended Wylie to see the new behavior.
We use a trie to store valid input sequences. In the future, we could use
the same trie as a replacement for the more inefficient HashSets we use to
store characters, vowels, and punctuation. For example, we'd use
'validInputSequences.put("K", new Pair("consonant", "k"))' when reading
in the ACIP keyboard's description of the first consonant of the Tibetan
alphabet in 'TibetanKeyboard.java'.
Note that the current trie implementation is only useful for 7- or 8-bit
transcription systems, and works best for tries with low average depth, which
describes a transcription system's trie very well. If you used arbitrary
Unicode in your keyboard, you'd need a different trie implementation.
Improved the optional keyboard input mode status messages.
2002-11-02 18:44:24 +00:00
}
2002-11-02 05:21:12 +00:00
}
2003-10-12 16:44:28 +00:00
}
} else { //there is already a character in charList
holdCurrent . append ( c ) ;
String s = holdCurrent . toString ( ) ;
if ( TibetanMachineWeb . isVowel ( s ) ) { //the holding string is a vowel
s = TibetanMachineWeb . getWylieForVowel ( s ) ;
2003-10-12 18:16:17 +00:00
if ( isTypingVowel ) {
//note: this takes care of multiple keystroke vowels like 'h>cj'
backSpace ( getNumberOfGlyphsForLastVowel ( ) ) ;
}
2003-10-12 16:44:28 +00:00
initKeyboard ( ) ;
isTypingVowel = true ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
2003-10-12 18:16:17 +00:00
if ( ! putVowel ( s ) ) {
updateStatus ( " You typed another vowel, so the first vowel was discarded. " ) ;
}
2003-10-12 16:44:28 +00:00
} else if ( TibetanMachineWeb . isChar ( s ) ) { //the holding string is a character
String s2 = TibetanMachineWeb . getWylieForChar ( s ) ;
if ( isTopHypothesis ) {
if ( TibetanMachineWeb . isAChungConsonant ( ) & & isStackingOn & & charList . size ( ) > 1 & & s2 . equals ( TibetanMachineWeb . ACHUNG ) ) {
charList . remove ( charList . size ( ) - 1 ) ;
newGlyphList = TibTextUtils . getGlyphs ( charList , isStackingRightToLeft , isDefinitelyTibetan , isDefinitelySanskrit ) ;
oldGlyphList = redrawGlyphs ( oldGlyphList , newGlyphList ) ;
putVowel ( TibetanMachineWeb . A_VOWEL ) ;
initKeyboard ( ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because we put a vowel and there's some achung stuff happening) " ) ;
2003-10-12 16:44:28 +00:00
break key_block ;
}
charList . set ( charList . size ( ) - 1 , s2 ) ;
newGlyphList = TibTextUtils . getGlyphs ( charList , isStackingRightToLeft , isDefinitelyTibetan , isDefinitelySanskrit ) ;
oldGlyphList = redrawGlyphs ( oldGlyphList , newGlyphList ) ;
2002-11-02 05:21:12 +00:00
changedStatus = true ;
updateStatus ( " we were holding a hypothesis, and we've updated it " ) ;
2003-10-12 16:44:28 +00:00
} else {
if ( ! isStackingOn ) {
initKeyboard ( ) ;
holdCurrent = new StringBuffer ( s ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because you weren't stacking, and there was a character already) " ) ;
2003-10-12 16:44:28 +00:00
} else if ( TibetanMachineWeb . isAChungConsonant ( ) & & s2 . equals ( TibetanMachineWeb . ACHUNG ) ) {
2002-10-13 19:22:56 +00:00
changedStatus = true ;
2003-10-12 18:16:17 +00:00
if ( ! putVowel ( TibetanMachineWeb . A_VOWEL ) ) {
appendStatus ( " (because you were stacking, and we put a vowel, and there was some achung business) " ) ;
}
initKeyboard ( ) ;
2003-10-12 16:44:28 +00:00
break key_block ;
}
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
charList . add ( s2 ) ;
isTopHypothesis = true ;
newGlyphList = TibTextUtils . getGlyphs ( charList , isStackingRightToLeft , isDefinitelyTibetan , isDefinitelySanskrit ) ;
oldGlyphList = redrawGlyphs ( oldGlyphList , newGlyphList ) ;
2002-11-02 05:21:12 +00:00
changedStatus = true ;
updateStatus ( " we weren't holding a hypothesis, but we are now " ) ;
2003-10-12 16:44:28 +00:00
}
} else { //the holding string is not a character
if ( isTopHypothesis ) { //finalize top character and add new hypothesis to top
holdCurrent = new StringBuffer ( ) ;
holdCurrent . append ( c ) ;
s = holdCurrent . toString ( ) ;
if ( TibetanMachineWeb . isVowel ( s ) ) {
String s2 = TibetanMachineWeb . getWylieForVowel ( s ) ;
putVowel ( s2 ) ;
initKeyboard ( ) ;
isTypingVowel = true ;
holdCurrent = new StringBuffer ( s ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
appendStatus ( " (because we put a vowel and the previous...) " ) ;
2003-10-12 16:44:28 +00:00
} else {
if ( TibetanMachineWeb . isStackingMedial ( ) & & ! isStackingRightToLeft ) {
initKeyboard ( ) ;
2002-11-03 17:05:05 +00:00
changedStatus = true ;
appendStatus ( " (because TibetanMachineWeb.isStackingMedial() && !isStackingRightToLeft) " ) ;
}
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( TibetanMachineWeb . isChar ( s ) ) {
String s2 = TibetanMachineWeb . getWylieForChar ( s ) ;
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
if ( ! isStackingOn ) {
initKeyboard ( ) ;
} else if ( TibetanMachineWeb . isAChungConsonant ( ) & & s2 . equals ( TibetanMachineWeb . ACHUNG ) ) {
2002-10-13 19:22:56 +00:00
changedStatus = true ;
2003-10-12 18:16:17 +00:00
if ( ! putVowel ( TibetanMachineWeb . A_VOWEL ) ) {
updateStatus ( " You typed an a-chung vowel. " ) ;
}
setNumberOfGlyphsForLastVowel ( 1 ) ;
holdCurrent = new StringBuffer ( s ) ;
isTypingVowel = true ;
2003-10-12 16:44:28 +00:00
break key_block ;
}
2002-09-30 03:10:00 +00:00
2003-10-12 16:44:28 +00:00
charList . add ( s2 ) ;
newGlyphList = TibTextUtils . getGlyphs ( charList , isStackingRightToLeft , isDefinitelyTibetan , isDefinitelySanskrit ) ;
oldGlyphList = redrawGlyphs ( oldGlyphList , newGlyphList ) ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
updateStatus ( " added character to charList " ) ;
2003-10-12 16:44:28 +00:00
} else {
holdCurrent = new StringBuffer ( s ) ;
isTopHypothesis = false ;
2002-10-13 19:22:56 +00:00
changedStatus = true ;
2002-11-03 17:05:05 +00:00
// FIXME: ring a bell here so the user knows what's up.
updateStatus ( " semireset (holdCurrent was reset) because you typed something invalid " ) ;
2003-10-12 16:44:28 +00:00
}
}
} else {
Fixes bug 624133, "Input freezes after impossible character". Try 'shsM' in
ACIP or 'ShSm' in Extended Wylie to see the new behavior.
We use a trie to store valid input sequences. In the future, we could use
the same trie as a replacement for the more inefficient HashSets we use to
store characters, vowels, and punctuation. For example, we'd use
'validInputSequences.put("K", new Pair("consonant", "k"))' when reading
in the ACIP keyboard's description of the first consonant of the Tibetan
alphabet in 'TibetanKeyboard.java'.
Note that the current trie implementation is only useful for 7- or 8-bit
transcription systems, and works best for tries with low average depth, which
describes a transcription system's trie very well. If you used arbitrary
Unicode in your keyboard, you'd need a different trie implementation.
Improved the optional keyboard input mode status messages.
2002-11-02 18:44:24 +00:00
// top char is just a guess! Just keep c
// in holdCurrent if it may become valid
// input, or reset if we've no hope.
if ( TibetanMachineWeb . hasInputPrefix ( s ) ) {
isTopHypothesis = false ;
changedStatus = true ;
updateStatus ( " incomplete input (like the \" S \" in Extended Wylie's \" Sh \" ) " ) ;
} else {
2002-11-03 17:05:05 +00:00
// before we call initKeyboard, get info to prevent infinite loops:
StringBuffer prevHoldCurrent = new StringBuffer ( holdCurrent . toString ( ) ) ;
Fixes bug 624133, "Input freezes after impossible character". Try 'shsM' in
ACIP or 'ShSm' in Extended Wylie to see the new behavior.
We use a trie to store valid input sequences. In the future, we could use
the same trie as a replacement for the more inefficient HashSets we use to
store characters, vowels, and punctuation. For example, we'd use
'validInputSequences.put("K", new Pair("consonant", "k"))' when reading
in the ACIP keyboard's description of the first consonant of the Tibetan
alphabet in 'TibetanKeyboard.java'.
Note that the current trie implementation is only useful for 7- or 8-bit
transcription systems, and works best for tries with low average depth, which
describes a transcription system's trie very well. If you used arbitrary
Unicode in your keyboard, you'd need a different trie implementation.
Improved the optional keyboard input mode status messages.
2002-11-02 18:44:24 +00:00
// FIXME: ring a bell here so the user knows what's up.
initKeyboard ( ) ;
// The recursive call to
// processTibetanChar will update the
// status bar, so set this to true:
changedStatus = true ;
// This status message is bound to get
// overridden, but this is what we
// would say if we had a queue of
// messages or something like Emacs's
// M-x view-lossage to see a history
// of status messages:
appendStatus ( " (because you typed something invalid [2nd way]) " ) ;
2002-11-03 17:05:05 +00:00
if ( prevHoldCurrent . length ( ) ! = 0
& & ! prevHoldCurrent . toString ( ) . equals ( String . valueOf ( c ) ) ) {
2003-04-12 16:28:56 +00:00
processTibetanChar ( c ) ;
2002-11-03 17:05:05 +00:00
}
// else we'd go into an infinite loop
Fixes bug 624133, "Input freezes after impossible character". Try 'shsM' in
ACIP or 'ShSm' in Extended Wylie to see the new behavior.
We use a trie to store valid input sequences. In the future, we could use
the same trie as a replacement for the more inefficient HashSets we use to
store characters, vowels, and punctuation. For example, we'd use
'validInputSequences.put("K", new Pair("consonant", "k"))' when reading
in the ACIP keyboard's description of the first consonant of the Tibetan
alphabet in 'TibetanKeyboard.java'.
Note that the current trie implementation is only useful for 7- or 8-bit
transcription systems, and works best for tries with low average depth, which
describes a transcription system's trie very well. If you used arbitrary
Unicode in your keyboard, you'd need a different trie implementation.
Improved the optional keyboard input mode status messages.
2002-11-02 18:44:24 +00:00
}
2003-10-12 16:44:28 +00:00
}
}
}
} //end switch
} //end key_block
2002-10-13 19:22:56 +00:00
if ( changedStatus = = false ) {
updateStatus ( " THAT KEY DID NOTHING BECAUSE OF THE CURRENT INPUT MODE. " ) ;
}
2003-10-12 16:44:28 +00:00
}
2002-09-30 03:10:00 +00:00
/ * *
* Converts a string of Extended Wylie to TibetanMachineWeb , and
* inserts it at the specified position .
*
* @param wylie the string of Wylie to convert
* @param offset the position at which to insert the conversion
* /
2003-10-12 16:44:28 +00:00
public void toTibetanMachineWeb ( String wylie , int offset ) {
try {
2005-07-07 01:30:03 +00:00
TibTextUtils . insertTibetanMachineWebForTranslit (
true , wylie , getTibDoc ( ) , offset ,
false // warnings?
) ;
} catch ( InvalidTransliterationException ite ) {
JOptionPane . showMessageDialog (
this ,
" The transliteration you are trying to convert is invalid: \ n "
+ ite . getMessage ( ) ) ;
return ;
2003-10-12 16:44:28 +00:00
}
}
2002-09-30 03:10:00 +00:00
/ * *
2003-10-19 20:16:06 +00:00
* Converts the currently selected text from Roman transliteration to
* TibetanMachineWeb .
* @param fromACIP true if the selection is ACIP , false if it is EWTS
2003-10-26 00:21:54 +00:00
* @param withWarnings true if and only if you want warnings to appear
* in the output , such as " this could be a mistranscription of blah... "
2003-10-19 20:16:06 +00:00
* * /
2003-10-26 00:21:54 +00:00
public void toTibetanMachineWeb ( boolean fromACIP , boolean withWarnings ) {
2003-10-12 16:44:28 +00:00
int start = getSelectionStart ( ) ;
int end = getSelectionEnd ( ) ;
2002-09-30 03:10:00 +00:00
2003-10-26 00:21:54 +00:00
toTibetanMachineWeb ( fromACIP , withWarnings , start , end ) ;
2003-10-12 16:44:28 +00:00
}
2002-09-30 03:10:00 +00:00
/ * *
* Converts a stretch of text from Extended Wylie to TibetanMachineWeb .
2003-10-19 20:16:06 +00:00
* @param fromACIP true if the selection is ACIP , false if it is EWTS
2003-10-26 00:21:54 +00:00
* @param withWarnings true if and only if you want warnings to appear
* in the output , such as " this could be a mistranscription of blah... "
2002-09-30 03:10:00 +00:00
* @param start the begin point for the conversion
2003-10-26 00:21:54 +00:00
* @param end the end point for the conversion * /
public void toTibetanMachineWeb ( boolean fromACIP , boolean withWarnings ,
int start , int end ) {
2003-10-12 16:44:28 +00:00
if ( start = = end )
return ;
StringBuffer sb ;
AttributeSet attr ;
String fontName ;
Position endPos ;
int i ;
try {
sb = new StringBuffer ( ) ;
endPos = getTibDoc ( ) . createPosition ( end ) ;
i = start ;
while ( i < endPos . getOffset ( ) + 1 ) {
attr = getTibDoc ( ) . getCharacterElement ( i ) . getAttributes ( ) ;
fontName = StyleConstants . getFontFamily ( attr ) ;
if ( ( 0 ! = TibetanMachineWeb . getTMWFontNumber ( fontName ) ) | | i = = endPos . getOffset ( ) ) {
if ( i ! = start ) {
try {
2005-07-07 01:30:03 +00:00
getTibDoc ( ) . remove ( start , i - start ) ;
i + = - 1 /* because i++ will occur */
+ TibTextUtils . insertTibetanMachineWebForTranslit (
! fromACIP , sb . toString ( ) , getTibDoc ( ) ,
start , withWarnings ) ;
} catch ( InvalidTransliterationException ite ) {
JOptionPane . showMessageDialog (
this ,
" The transliteration you are trying to convert is invalid: \ n "
+ ite . getMessage ( ) ) ;
2003-10-19 20:16:06 +00:00
return ;
2003-10-12 16:44:28 +00:00
}
}
start = i + 1 ;
}
else
sb . append ( getTibDoc ( ) . getText ( i , 1 ) ) ;
i + + ;
}
}
catch ( BadLocationException ble ) {
ble . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
}
2002-09-30 03:10:00 +00:00
2002-11-02 03:38:59 +00:00
/ * *
* Converts the entire associated document into Extended Wylie . If the
* document consists of both Tibetan and non - Tibetan fonts , however ,
* the conversion stops at the first non - Tibetan font .
2003-07-01 03:43:33 +00:00
* @param noSuchWylie an array which will not be touched if this is
* successful ; however , if there is no THDL Extended Wylie
* corresponding to one of these glyphs , then noSuchWylie [ 0 ] will be
* set to true
2002-11-02 03:38:59 +00:00
* @return the string of Wylie corresponding to the associated document
2003-07-04 00:12:59 +00:00
* @see org . thdl . tib . text . TibetanDocument # getWylie ( boolean [ ] ) * /
2003-07-01 03:43:33 +00:00
public String getWylie ( boolean noSuchWylie [ ] ) {
return getTibDoc ( ) . getWylie ( noSuchWylie ) ;
2002-11-02 03:38:59 +00:00
}
2003-04-14 05:22:27 +00:00
2006-08-12 22:23:01 +00:00
public void onDictionarySettingsChanged ( )
{
GlobalResourceHolder . getListener ( ) . update ( getObservable ( ) ,
2006-08-19 04:43:23 +00:00
( Object ) new Integer ( GlobalResourceHolder . Listener . DICTIONARY_SETTINGS ) ) ;
2006-08-12 22:23:01 +00:00
if ( null = = buttonToggleDict | | null = = buttonLookupDict )
return ;
2003-04-14 05:22:27 +00:00
2006-08-19 04:43:23 +00:00
boolean enableDictionaryButtons = GlobalResourceHolder . getDictionarySettings ( ) . isValid ( ) & &
GlobalResourceHolder . getDictionarySettings ( ) . isEnabled ( ) ;
buttonToggleDict . setEnabled ( enableDictionaryButtons ) ;
buttonLookupDict . setEnabled ( enableDictionaryButtons ) ;
2006-08-12 22:23:01 +00:00
}
protected void lookupDictionary ( )
{
Range selection = new Range ( getSelectionStart ( ) , getSelectionEnd ( ) ) ;
try
{
adjustSelection ( selection ) ;
}
catch ( Exception e )
{
System . err . println ( e . toString ( ) ) ;
return ;
}
showCurrentPhraseFromPosInfo ( selection ) ;
}
private void adjustSelection ( Range selection ) throws Exception
{
TibetanDocument doc = getTibDoc ( ) ;
int startPos = selection . getStartPos ( ) ;
int endPos = selection . getEndPos ( ) ;
while ( true )
{
Element el = doc . getCharacterElement ( endPos ) ;
char code = doc . getText ( endPos , 1 ) . charAt ( 0 ) ;
int fontNum = TibetanMachineWeb . getTMWFontNumber ( doc . getFont ( el . getAttributes ( ) ) . getName ( ) ) ; // getFamily
if ( 0 = = fontNum | | TibetanMachineWeb . isTMWFontCharBreakable ( code ) )
{
endPos - - ;
break ;
}
if ( endPos > = doc . getLength ( ) - 1 )
break ;
endPos + + ;
}
if ( endPos < startPos )
{
startPos = endPos ;
}
//
// span left
//
while ( true )
{
Element el = doc . getCharacterElement ( startPos ) ;
char code = doc . getText ( startPos , 1 ) . charAt ( 0 ) ;
int fontNum = TibetanMachineWeb . getTMWFontNumber ( doc . getFont ( el . getAttributes ( ) ) . getName ( ) ) ; // getFamily
if ( 0 = = fontNum | | TibetanMachineWeb . isTMWFontCharBreakable ( code ) )
{
startPos + + ;
break ;
}
if ( startPos < = 0 )
break ;
startPos - - ;
}
selection . setRange ( startPos , endPos ) ;
}
protected void showCurrentPhraseFromPosInfo ( Range range )
{
String word = " " ;
TibetanDocument doc = getTibDoc ( ) ;
int startPos = range . getStartPos ( ) ;
int endPos = range . getEndPos ( ) ;
//
// collect the text the good way
//
boolean [ ] noSuch = new boolean [ ] { false } ;
word = doc . getWylie ( startPos , endPos + 1 , noSuch ) ;
word = word . replaceAll ( " [ \\ / \\ _ \\ *] " , " " ) ;
//
// show dictionary data
//
if ( word . length ( ) > 0 )
{
try
{
dictFrame . reset ( ) ;
dictFrame . setOriginal ( word , doc , startPos , endPos ) ;
dictFrame . setPronounciation ( pronounciation . process ( word ) ) ;
}
catch ( Exception e )
{
// TODO
}
TibetanScanner scanner = GlobalResourceHolder . getTibetanScanner ( ) ;
if ( null ! = scanner )
{
scanner . scanBody ( word ) ;
scanner . finishUp ( ) ;
Word [ ] words = scanner . getWordArray ( ) ;
for ( int i = 0 ; i < words . length ; i + + )
{
dictFrame . addDescription ( words [ i ] . toString ( ) ) ;
}
scanner . clearTokens ( ) ;
dictFrame . setVisible ( true ) ;
dictFrame . requestFocusInWindow ( ) ;
dictFrame . gotoList ( ) ;
}
}
}
private void initDictionarySupport ( Object frame )
{
if ( frame instanceof JFrame )
mainFrame = ( JFrame ) frame ;
else
mainFrame = null ;
try
{
pronounciation = new Pronounciation ( ) ;
}
catch ( Exception e )
{
System . err . println ( e . toString ( ) ) ;
System . exit ( 1 ) ;
}
addKeyListener ( new KeyAdapter ( )
{
public void keyPressed ( KeyEvent ke )
{
if ( KeyEvent . VK_F12 = = ke . getKeyCode ( ) )
{
toggleDictionaryFrame ( ) ;
}
else if ( KeyEvent . VK_F11 = = ke . getKeyCode ( ) )
{
lookupDictionary ( ) ;
}
}
}
) ;
}
public void postInitialize ( JComponent parent )
{
//
// no main frame - do not init the dictionary frame
//
if ( null = = mainFrame )
return ;
dictFrame = new DictionaryFrame ( mainFrame ) ;
JToolBar toolbar = null ;
for ( int child = 0 ; child < parent . getComponentCount ( ) ; child + + )
{
if ( parent . getComponent ( child ) instanceof JToolBar )
{
toolbar = ( JToolBar ) parent . getComponent ( child ) ;
break ;
}
}
if ( toolbar ! = null )
{
buttonToggleDict = new JButton ( " Tog. Dictionary F12 " ) ;
buttonToggleDict . addMouseListener ( new MouseAdapter ( )
{
public void mouseClicked ( MouseEvent e )
{
toggleDictionaryFrame ( ) ;
}
} ) ;
toolbar . add ( buttonToggleDict ) ;
buttonLookupDict = new JButton ( " Lookup F11 " ) ;
buttonLookupDict . addMouseListener ( new MouseAdapter ( )
{
public void mouseClicked ( MouseEvent e )
{
lookupDictionary ( ) ;
}
} ) ;
toolbar . add ( buttonLookupDict ) ;
2006-08-19 04:43:23 +00:00
boolean enableDictionaryButtons = GlobalResourceHolder . getDictionarySettings ( ) . isValid ( ) & &
GlobalResourceHolder . getDictionarySettings ( ) . isEnabled ( ) ;
buttonToggleDict . setEnabled ( enableDictionaryButtons ) ;
buttonLookupDict . setEnabled ( enableDictionaryButtons ) ;
2006-08-12 22:23:01 +00:00
}
}
protected void toggleDictionaryFrame ( )
{
if ( dictFrame . isVisible ( ) )
dictFrame . setVisible ( false ) ;
else
{
//
// if text selected, feed the dictionary frame
//
dictFrame . setVisible ( true ) ;
}
}
public Observable getObservable ( )
{
return observableObj ;
}
protected void finalize ( )
{
GlobalResourceHolder . removeSettingsServiceProvider ( this ) ;
}
2003-04-14 05:22:27 +00:00
2005-02-26 20:46:40 +00:00
/ * * The JDK contains StringSelection , but we want to copy and paste
RTF sometimes . Enter RTFSelection . * /
2003-04-14 05:22:27 +00:00
class RTFSelection implements ClipboardOwner , Transferable {
2005-02-26 22:57:38 +00:00
private final DataFlavor [ ] supportedFlavors
= new DataFlavor [ ] { rtfFlavor , DataFlavor . stringFlavor } ;
2003-10-12 16:44:28 +00:00
private ByteArrayOutputStream rtfOut ;
private String plainText ;
RTFSelection ( StyledDocument sdoc , int offset , int length ) {
try {
2005-02-26 22:57:38 +00:00
// construct new document that contains only portion of
// text you want to copy this workaround is due to bug
// 4129911, which will not be fixed
//
2005-02-26 20:46:40 +00:00
// TODO(dchandler): Is this where we lose formatting like
// centering and indention?
2003-10-12 16:44:28 +00:00
StyledDocument newDoc = new DefaultStyledDocument ( ) ;
for ( int i = offset ; i < offset + length ; i + + ) {
try {
String s = sdoc . getText ( i , 1 ) ;
AttributeSet as = sdoc . getCharacterElement ( i ) . getAttributes ( ) ;
newDoc . insertString ( i - offset , s , as ) ;
} catch ( BadLocationException ble ) {
ble . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
}
2005-02-26 22:57:38 +00:00
plainText = getText ( offset , length ) ;
2003-10-12 16:44:28 +00:00
rtfOut = new ByteArrayOutputStream ( ) ;
2005-02-26 20:46:40 +00:00
//last two parameters ignored (bug 4129911?):
rtfEd . write ( rtfOut , newDoc , 0 , newDoc . getLength ( ) ) ;
2003-10-12 16:44:28 +00:00
} catch ( BadLocationException ble ) {
ble . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
} catch ( IOException ioe ) {
ioe . printStackTrace ( ) ;
ThdlDebug . noteIffyCode ( ) ;
}
}
public void lostOwnership ( Clipboard clipboard , Transferable contents ) {
}
public Object getTransferData ( DataFlavor flavor ) {
if ( flavor . equals ( rtfFlavor ) )
return new ByteArrayInputStream ( rtfOut . toByteArray ( ) ) ;
if ( flavor . equals ( DataFlavor . stringFlavor ) )
return plainText ;
return null ;
}
public DataFlavor [ ] getTransferDataFlavors ( ) {
2005-02-26 22:57:38 +00:00
return ( DataFlavor [ ] ) supportedFlavors . clone ( ) ;
2003-10-12 16:44:28 +00:00
}
public boolean isDataFlavorSupported ( DataFlavor flavor ) {
2005-02-26 20:46:40 +00:00
for ( int i = 0 ; i < supportedFlavors . length ; i + + )
if ( flavor . equals ( supportedFlavors [ i ] ) )
2003-10-12 16:44:28 +00:00
return true ;
return false ;
}
2005-02-26 22:57:38 +00:00
} // inner class DuffPane.RTFSelection
2003-04-14 05:22:27 +00:00
2006-08-12 22:23:01 +00:00
2005-02-26 22:57:38 +00:00
} // class DuffPane
2005-02-27 10:27:37 +00:00
// TODO(dchandler): search for 'catch (Throwable' and 'catch
// (Exception' and 'catch (Error'. These are rarely a good idea.