1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.ActionEvent ; 11 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.IOException ; 15 16 import javax.swing.text.*; 17 import javax.swing.event.*; 18 import javax.swing.plaf.*; 19 20 59 public class JTextPane extends JEditorPane { 60 61 66 public JTextPane() { 67 super(); 68 setEditorKit(createDefaultEditorKit()); 69 } 70 71 78 public JTextPane(StyledDocument doc) { 79 this(); 80 setStyledDocument(doc); 81 } 82 83 91 public String getUIClassID() { 92 return uiClassID; 93 } 94 95 104 public void setDocument(Document doc) { 105 if (doc instanceof StyledDocument) { 106 super.setDocument(doc); 107 } else { 108 throw new IllegalArgumentException ("Model must be StyledDocument"); 109 } 110 } 111 112 119 public void setStyledDocument(StyledDocument doc) { 120 super.setDocument(doc); 121 } 122 123 128 public StyledDocument getStyledDocument() { 129 return (StyledDocument) getDocument(); 130 } 131 132 148 public void replaceSelection(String content) { 149 replaceSelection(content, true); 150 } 151 152 private void replaceSelection(String content, boolean checkEditable) { 153 if (checkEditable && !isEditable()) { 154 UIManager.getLookAndFeel().provideErrorFeedback(JTextPane.this); 155 return; 156 } 157 Document doc = getStyledDocument(); 158 if (doc != null) { 159 try { 160 Caret caret = getCaret(); 161 int p0 = Math.min(caret.getDot(), caret.getMark()); 162 int p1 = Math.max(caret.getDot(), caret.getMark()); 163 AttributeSet attr = getInputAttributes().copyAttributes(); 164 if (doc instanceof AbstractDocument) { 165 ((AbstractDocument)doc).replace(p0, p1 - p0, content,attr); 166 } 167 else { 168 if (p0 != p1) { 169 doc.remove(p0, p1 - p0); 170 } 171 if (content != null && content.length() > 0) { 172 doc.insertString(p0, content, attr); 173 } 174 } 175 } catch (BadLocationException e) { 176 UIManager.getLookAndFeel().provideErrorFeedback(JTextPane.this); 177 } 178 } 179 } 180 181 210 public void insertComponent(Component c) { 211 MutableAttributeSet inputAttributes = getInputAttributes(); 212 inputAttributes.removeAttributes(inputAttributes); 213 StyleConstants.setComponent(inputAttributes, c); 214 replaceSelection(" ", false); 215 inputAttributes.removeAttributes(inputAttributes); 216 } 217 218 234 public void insertIcon(Icon g) { 235 MutableAttributeSet inputAttributes = getInputAttributes(); 236 inputAttributes.removeAttributes(inputAttributes); 237 StyleConstants.setIcon(inputAttributes, g); 238 replaceSelection(" ", false); 239 inputAttributes.removeAttributes(inputAttributes); 240 } 241 242 259 public Style addStyle(String nm, Style parent) { 260 StyledDocument doc = getStyledDocument(); 261 return doc.addStyle(nm, parent); 262 } 263 264 270 public void removeStyle(String nm) { 271 StyledDocument doc = getStyledDocument(); 272 doc.removeStyle(nm); 273 } 274 275 281 public Style getStyle(String nm) { 282 StyledDocument doc = getStyledDocument(); 283 return doc.getStyle(nm); 284 } 285 286 302 public void setLogicalStyle(Style s) { 303 StyledDocument doc = getStyledDocument(); 304 doc.setLogicalStyle(getCaretPosition(), s); 305 } 306 307 313 public Style getLogicalStyle() { 314 StyledDocument doc = getStyledDocument(); 315 return doc.getLogicalStyle(getCaretPosition()); 316 } 317 318 324 public AttributeSet getCharacterAttributes() { 325 StyledDocument doc = getStyledDocument(); 326 Element run = doc.getCharacterElement(getCaretPosition()); 327 if (run != null) { 328 return run.getAttributes(); 329 } 330 return null; 331 } 332 333 349 public void setCharacterAttributes(AttributeSet attr, boolean replace) { 350 int p0 = getSelectionStart(); 351 int p1 = getSelectionEnd(); 352 if (p0 != p1) { 353 StyledDocument doc = getStyledDocument(); 354 doc.setCharacterAttributes(p0, p1 - p0, attr, replace); 355 } else { 356 MutableAttributeSet inputAttributes = getInputAttributes(); 357 if (replace) { 358 inputAttributes.removeAttributes(inputAttributes); 359 } 360 inputAttributes.addAttributes(attr); 361 } 362 } 363 364 370 public AttributeSet getParagraphAttributes() { 371 StyledDocument doc = getStyledDocument(); 372 Element paragraph = doc.getParagraphElement(getCaretPosition()); 373 if (paragraph != null) { 374 return paragraph.getAttributes(); 375 } 376 return null; 377 } 378 379 394 public void setParagraphAttributes(AttributeSet attr, boolean replace) { 395 int p0 = getSelectionStart(); 396 int p1 = getSelectionEnd(); 397 StyledDocument doc = getStyledDocument(); 398 doc.setParagraphAttributes(p0, p1 - p0, attr, replace); 399 } 400 401 406 public MutableAttributeSet getInputAttributes() { 407 return getStyledEditorKit().getInputAttributes(); 408 } 409 410 415 protected final StyledEditorKit getStyledEditorKit() { 416 return (StyledEditorKit) getEditorKit(); 417 } 418 419 423 private static final String uiClassID = "TextPaneUI"; 424 425 426 433 private void writeObject(ObjectOutputStream s) throws IOException { 434 s.defaultWriteObject(); 435 if (getUIClassID().equals(uiClassID)) { 436 byte count = JComponent.getWriteObjCounter(this); 437 JComponent.setWriteObjCounter(this, --count); 438 if (count == 0 && ui != null) { 439 ui.installUI(this); 440 } 441 } 442 } 443 444 445 447 453 protected EditorKit createDefaultEditorKit() { 454 return new StyledEditorKit(); 455 } 456 457 466 public final void setEditorKit(EditorKit kit) { 467 if (kit instanceof StyledEditorKit) { 468 super.setEditorKit(kit); 469 } else { 470 throw new IllegalArgumentException ("Must be StyledEditorKit"); 471 } 472 } 473 474 484 protected String paramString() { 485 return super.paramString(); 486 } 487 488 } 489 | Popular Tags |