From 04da61688dbe67ed963a9a221fe3b99707c7c3ea Mon Sep 17 00:00:00 2001 From: dchandler Date: Fri, 8 Nov 2002 04:05:06 +0000 Subject: [PATCH] A DuffPane is now a TibetanPane. A TibetanPane is much more lightweight but does line breaks correctly. I.e., I refactored DuffPane into two classes. I did this trying to track down a subtle bug in line breaking: 'gye ' breaks after 'gy' sometimes, with the dreng bo on the next line, but only when you resize the window certain ways, and only in Savant (and maybe QD and the translation tool, I don't know) but not in Jskad. I was not successful in finding the bug, but it still exists when I use TibetanPanes instead of DuffPanes in org.thdl.savant.tib.*. --- source/org/thdl/tib/input/DuffPane.java | 122 ++++++++++----------- source/org/thdl/tib/input/TibetanPane.java | 84 ++++++++++++++ 2 files changed, 141 insertions(+), 65 deletions(-) create mode 100644 source/org/thdl/tib/input/TibetanPane.java diff --git a/source/org/thdl/tib/input/DuffPane.java b/source/org/thdl/tib/input/DuffPane.java index 8fd9de3..c80901a 100644 --- a/source/org/thdl/tib/input/DuffPane.java +++ b/source/org/thdl/tib/input/DuffPane.java @@ -43,7 +43,7 @@ import org.thdl.util.StatusBar; * @author Edward Garrett, Tibetan and Himalayan Digital Library * @version 1.0 */ -public class DuffPane extends JTextPane implements KeyListener, FocusListener { +public class DuffPane extends TibetanPane implements KeyListener, FocusListener { /** * The status bar to update with messages about the current input mode. * Are we expecting a vowel? a subscript? et cetera. @@ -179,16 +179,10 @@ public class DuffPane extends JTextPane implements KeyListener, FocusListener { */ private boolean isRomanEnabled = true; /** -* The document displayed by this object. -*/ - private TibetanDocument doc; -/** * The caret of {@link #doc}, used to keep track of the * current entry/edit/deletion position. */ private Caret caret; -// private StyledEditorKit editorKit; - private StyleContext styleContext; private Style rootStyle; private boolean skipUpdate = false; private boolean isCutAndPasteEnabled = true; @@ -197,9 +191,7 @@ public class DuffPane extends JTextPane implements KeyListener, FocusListener { private int romanFontSize; private MutableAttributeSet romanAttributeSet; -public Clipboard rtfBoard; -public DataFlavor rtfFlavor; -public RTFEditorKit rtfEd = null; + private Clipboard rtfBoard; /** Initializes this object. All constructors should call this. */ @@ -289,25 +281,14 @@ public RTFEditorKit rtfEd = null; */ private void setupEditor() { rtfBoard = getToolkit().getSystemClipboard(); - rtfFlavor = new DataFlavor("text/rtf", "Rich Text Format"); - rtfEd = new TibetanRTFEditorKit(); - setEditorKit(rtfEd); - styleContext = new StyleContext(); - doc = new TibetanDocument(styleContext); - doc.setTibetanFontSize(defaultTibFontSize()); - setDocument(doc); - - Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE); - StyleConstants.setFontFamily(defaultStyle, "TibetanMachineWeb"); - StyleConstants.setFontSize(defaultStyle, defaultTibFontSize()); // FIXME make pref + newDocument(); romanFontFamily = ThdlOptions.getStringOption("thdl.default.roman.font.face", "Serif"); // FIXME write out this preference. - romanFontSize = defaultRomanFontSize(); // FIXME make pref + romanFontSize = defaultRomanFontSize(); setRomanAttributeSet(romanFontFamily, romanFontSize); -// newDocument(); caret = getCaret(); addKeyListener(this); @@ -397,10 +378,15 @@ public RTFEditorKit rtfEd = null; * Clears the current document. */ public void newDocument() { - styleContext = new StyleContext(); + StyleContext styleContext = new StyleContext(); doc = new TibetanDocument(styleContext); doc.setTibetanFontSize(defaultTibFontSize()); + + // 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. setDocument(doc); Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE); @@ -493,7 +479,7 @@ public RTFEditorKit rtfEd = null; */ public void append(int offset, String s, MutableAttributeSet attr) { try { - doc.insertString(offset, s, attr); + getTibDoc().insertString(offset, s, attr); } catch (BadLocationException ble) { } @@ -506,7 +492,7 @@ public RTFEditorKit rtfEd = null; */ public void setTibetanFontSize(int size) { if (size > 0) - doc.setTibetanFontSize(size); + getTibDoc().setTibetanFontSize(size); } /** @@ -515,7 +501,7 @@ public RTFEditorKit rtfEd = null; * text entry mode */ public int getTibetanFontSize() { - return doc.getTibetanFontSize(); + return getTibDoc().getTibetanFontSize(); } /** @@ -556,7 +542,7 @@ public RTFEditorKit rtfEd = null; */ private void backSpace(int k) { try { - doc.remove(caret.getDot()-k, k); + getTibDoc().remove(caret.getDot()-k, k); } catch (BadLocationException ble) { } @@ -628,7 +614,7 @@ public RTFEditorKit rtfEd = null; java.util.List sublist = newGlyphList.subList(k, newGlyphCount); DuffData[] dd = TibTextUtils.convertGlyphs(sublist); - doc.insertDuff(caret.getDot(), dd); + getTibDoc().insertDuff(caret.getDot(), dd); return newGlyphList; } @@ -652,13 +638,13 @@ public RTFEditorKit rtfEd = null; return; } - AttributeSet attr = doc.getCharacterElement(caret.getDot()-1).getAttributes(); + AttributeSet attr = getTibDoc().getCharacterElement(caret.getDot()-1).getAttributes(); String fontName = StyleConstants.getFontFamily(attr); int fontNum; if (0 != (fontNum = TibetanMachineWeb.getTMWFontNumber(fontName))) { try { - char c2 = doc.getText(caret.getDot()-1, 1).charAt(0); + 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()) @@ -679,10 +665,10 @@ public RTFEditorKit rtfEd = null; DuffCode dc_2 = new DuffCode(fontNum, c2); if (caret.getDot() > 2) { - attr = doc.getCharacterElement(caret.getDot()-2).getAttributes(); + attr = getTibDoc().getCharacterElement(caret.getDot()-2).getAttributes(); fontName = StyleConstants.getFontFamily(attr); if (0 != (fontNum = TibetanMachineWeb.getTMWFontNumber(fontName))) { - c2 = doc.getText(caret.getDot()-2, 1).charAt(0); + c2 = getTibDoc().getText(caret.getDot()-2, 1).charAt(0); dc_1 = new DuffCode(fontNum, c2); } } @@ -739,7 +725,7 @@ public RTFEditorKit rtfEd = null; DuffCode dc = dc_array[TibetanMachineWeb.TMW]; java.util.List achenlist = TibTextUtils.getVowel(dc,v); DuffData[] dd = TibTextUtils.convertGlyphs(achenlist); - doc.insertDuff(caret.getDot(), dd); + getTibDoc().insertDuff(caret.getDot(), dd); } /** @@ -754,7 +740,7 @@ public RTFEditorKit rtfEd = null; if (caret.getDot()==0) break special_bindu_block; - AttributeSet attr = doc.getCharacterElement(caret.getDot()-1).getAttributes(); + AttributeSet attr = getTibDoc().getCharacterElement(caret.getDot()-1).getAttributes(); String fontName = StyleConstants.getFontFamily(attr); int fontNum; @@ -762,7 +748,7 @@ public RTFEditorKit rtfEd = null; break special_bindu_block; try { - char c2 = doc.getText(caret.getDot()-1, 1).charAt(0); + 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; @@ -786,7 +772,7 @@ public RTFEditorKit rtfEd = null; } DuffData[] dd = TibTextUtils.convertGlyphs(TibTextUtils.getBindu(null)); - doc.insertDuff(caret.getDot(), dd); + getTibDoc().insertDuff(caret.getDot(), dd); initKeyboard(); } @@ -814,12 +800,14 @@ public RTFEditorKit rtfEd = null; appendStatus(" (because the window focus was lost)"); } + // FIXMEDOC class RTFSelection implements ClipboardOwner, Transferable { - DataFlavor[] supportedFlavor; - ByteArrayOutputStream rtfOut; - String plainText; + private DataFlavor[] supportedFlavor; + private ByteArrayOutputStream rtfOut; + private String plainText; - RTFSelection(StyledDocument doc, int offset, int length) { + // FIXMEDOC + RTFSelection(StyledDocument sdoc, int offset, int length) { supportedFlavor = new DataFlavor[2]; supportedFlavor[0] = rtfFlavor; supportedFlavor[1] = DataFlavor.stringFlavor; @@ -829,8 +817,8 @@ class RTFSelection implements ClipboardOwner, Transferable { StyledDocument newDoc = new DefaultStyledDocument(); for (int i=offset; i Conceptually, we'd like to override getDocument(), +* getStyledDocument(), setStyledDocument(..), and setDocument(..). +* However, JTextPane is not written flexibly enough to allow that. +* You'll get exceptions during construction of a DuffPane if you try. +*

+* +* @author Edward Garrett, Tibetan and Himalayan Digital Library */ +public class TibetanPane extends JTextPane { +/** +* The document displayed by this object. +*/ + protected TibetanDocument doc; + + protected DataFlavor rtfFlavor; + protected RTFEditorKit rtfEd = null; + + /** Creates a new TibetanPane with a null document model. */ + TibetanPane() { + super(); + setupJustTheEditor(); + doc = null; + } + + /** Creates a new TibetanPane with a document model d. */ + public TibetanPane(TibetanDocument d) { + super(); + setupJustTheEditor(); + doc = d; + setDocument(d); + } + + /** Returns the model of a Tibetan document associated with this + * JTextPane. */ + public TibetanDocument getTibDoc() { + return (TibetanDocument)doc; + } + +/** +* This method sets up the editor. +*/ + private void setupJustTheEditor() { + rtfFlavor = new DataFlavor("text/rtf", "Rich Text Format"); + rtfEd = new TibetanRTFEditorKit(); + setEditorKit(rtfEd); + } + +} // class TibetanPane