1 16 17 package swingwtx.swing; 18 19 import swingwt.awt.Component; 20 import swingwtx.swing.text.AttributeSet; 21 import swingwtx.swing.text.Document; 22 import swingwtx.swing.text.EditorKit; 23 import swingwtx.swing.text.Element; 24 import swingwtx.swing.text.MutableAttributeSet; 25 import swingwtx.swing.text.Style; 26 import swingwtx.swing.text.StyledDocument; 27 import swingwtx.swing.text.StyledEditorKit; 28 29 public class JTextPane extends JEditorPane { 30 31 public JTextPane() { 32 super(); 33 setEditorKit(createDefaultEditorKit()); 34 } 35 public JTextPane(StyledDocument doc) { 36 this(); 37 setStyledDocument(doc); 38 } 39 public String getUIClassID() { 40 return getClass().getName(); 41 } 42 public void setDocument(Document doc) { 43 if (doc instanceof StyledDocument) { 44 super.setDocument(doc); 45 } else { 46 throw new IllegalArgumentException ("Model must be StyledDocument"); 47 } 48 } 49 public void setStyledDocument(StyledDocument doc) { 50 super.setDocument(doc); 51 } 52 public StyledDocument getStyledDocument() { 53 return (StyledDocument) getDocument(); 54 } 55 public void replaceSelection(String content) { 56 replaceSelection(content, null); 57 } 58 private void replaceSelection(String content, AttributeSet attr) { 59 } 60 public void insertComponent(Component c) { 61 } 62 public void insertIcon(Icon g) { 63 } 64 public Style addStyle(String nm, Style parent) { 65 StyledDocument doc = getStyledDocument(); 66 return doc.addStyle(nm, parent); 67 } 68 public void removeStyle(String nm) { 69 StyledDocument doc = getStyledDocument(); 70 doc.removeStyle(nm); 71 } 72 public Style getStyle(String nm) { 73 StyledDocument doc = getStyledDocument(); 74 return doc.getStyle(nm); 75 } 76 public void setLogicalStyle(Style s) { 77 StyledDocument doc = getStyledDocument(); 78 doc.setLogicalStyle(getCaretPosition(), s); 79 } 80 public Style getLogicalStyle() { 81 StyledDocument doc = getStyledDocument(); 82 return doc.getLogicalStyle(getCaretPosition()); 83 } 84 public AttributeSet getCharacterAttributes() { 85 StyledDocument doc = getStyledDocument(); 86 Element run = doc.getCharacterElement(getCaretPosition()); 87 if (run != null) { 88 return run.getAttributes(); 89 } 90 return null; 91 } 92 public void setCharacterAttributes(AttributeSet attr, boolean replace) { 93 int p0 = getSelectionStart(); 94 int p1 = getSelectionEnd(); 95 if (p0 != p1) { 96 StyledDocument doc = getStyledDocument(); 97 doc.setCharacterAttributes(p0, p1 - p0, attr, replace); 98 } else { 99 MutableAttributeSet inputAttributes = getInputAttributes(); 100 if (replace) { 101 inputAttributes.removeAttributes(inputAttributes); 102 } 103 inputAttributes.addAttributes(attr); 104 } 105 } 106 public AttributeSet getParagraphAttributes() { 107 StyledDocument doc = getStyledDocument(); 108 Element paragraph = doc.getParagraphElement(getCaretPosition()); 109 if (paragraph != null) { 110 return paragraph.getAttributes(); 111 } 112 return null; 113 } 114 public void setParagraphAttributes(AttributeSet attr, boolean replace) { 115 int p0 = getSelectionStart(); 116 int p1 = getSelectionEnd(); 117 StyledDocument doc = getStyledDocument(); 118 doc.setParagraphAttributes(p0, p1 - p0, attr, replace); 119 } 120 public MutableAttributeSet getInputAttributes() { 121 return null; 122 } 124 protected final StyledEditorKit getStyledEditorKit() { 125 return (StyledEditorKit) getEditorKit(); 126 } 127 protected EditorKit createDefaultEditorKit() { 128 return new StyledEditorKit(); 129 } 130 public final void setEditorKit(EditorKit kit) { 131 } 132 133 134 } 135 | Popular Tags |