1 19 20 package org.openide.text; 21 22 23 import java.beans.PropertyChangeEvent ; 24 import java.io.*; 25 import javax.swing.JEditorPane ; 26 import javax.swing.event.ChangeListener ; 27 import javax.swing.text.*; 28 import org.openide.awt.UndoRedo; 29 import org.openide.cookies.*; 30 import org.openide.filesystems.*; 31 import org.openide.loaders.*; 32 import org.openide.nodes.*; 33 import org.openide.text.CloneableEditorSupport.Pane; 34 import org.openide.util.Exceptions; 35 import org.openide.util.Task; 36 import org.openide.util.actions.SystemAction; 37 import org.openide.windows.*; 38 39 46 @Deprecated 47 public class EditorSupport extends OpenSupport 48 implements EditorCookie.Observable, OpenCookie, CloseCookie, PrintCookie { 49 52 @Deprecated 53 public static final String EDITOR_MODE = CloneableEditorSupport.EDITOR_MODE; 54 55 56 @Deprecated 57 protected String modifiedAppendix = " *"; 59 60 private boolean listenToModifs = true; 61 62 63 private Del del; 64 65 69 public EditorSupport(MultiDataObject.Entry entry) { 70 super(entry, new DelEnv(entry.getDataObject())); 71 72 del = new Del ( 73 entry.getDataObject (), 74 (DelEnv)env, 75 allEditors 76 ); 77 } 78 79 82 protected String messageOpening () { 83 return del.superMessageOpening (); 84 } 85 86 89 protected String messageOpened () { 90 return del.superMessageOpened (); 91 } 92 93 98 protected String messageSave () { 99 return del.superMessageSave (); 100 } 101 102 106 protected String messageName () { 107 return del.superMessageName (); 108 } 109 110 114 protected String messageToolTip () { 115 return del.superMessageToolTip (); 116 } 117 118 119 121 protected void updateTitles () { 122 del.superUpdateTitles (); 123 } 124 125 126 129 protected CloneableTopComponent createCloneableTopComponent () { 130 prepareDocument (); 132 133 DataObject obj = findDataObject (); 134 Editor editor = new Editor (obj); 135 return editor; 136 } 137 138 146 protected UndoRedo.Manager createUndoRedoManager () { 147 return del.superUndoRedoManager (); 148 } 149 150 152 public void open() { 153 del.open(); 154 } 155 156 158 163 public boolean close () { 164 return del.close (); 165 } 166 167 171 protected boolean close (boolean ask) { 172 return del.superClose (ask); 173 } 174 175 182 public synchronized Task prepareDocument () { 183 return del.prepareDocument (); 184 } 185 186 198 public StyledDocument openDocument () throws IOException { 199 return del.openDocument (); 200 } 201 202 207 public StyledDocument getDocument () { 208 return del.getDocument (); 209 } 210 211 214 public boolean isDocumentLoaded() { 215 return del.isDocumentLoaded (); 216 } 217 218 222 public void saveDocument () throws IOException { 223 del.superSaveDocument (); 224 } 225 226 238 protected void saveFromKitToStream (StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException { 239 del.superSaveFromKitToStream (doc, kit, stream); 240 } 241 242 246 public boolean isModified () { 247 return del.isModified (); 248 } 249 250 253 protected MultiDataObject findDataObject () { 254 return entry.getDataObject (); 255 } 256 257 265 public final PositionRef createPositionRef (int offset, Position.Bias bias) { 266 return del.createPositionRef (offset, bias); 267 } 268 269 272 public Line.Set getLineSet () { 273 return del.getLineSet (); 274 } 275 276 278 282 public void setMIMEType (String s) { 283 del.setMIMEType (s); 284 } 285 286 288 @Deprecated 289 public void setActions (SystemAction[] actions) { 290 } 291 292 295 protected EditorKit createEditorKit () { 296 return del.superCreateEditorKit (); 297 } 298 299 307 public void setModificationListening (final boolean listenToModifs) { 308 this.listenToModifs = listenToModifs; 309 } 310 311 315 public void addChangeListener (ChangeListener l) { 316 del.addChangeListener (l); 317 } 318 319 322 public void removeChangeListener (ChangeListener l) { 323 del.removeChangeListener (l); 324 } 325 326 public final void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 327 del.addPropertyChangeListener (l); 328 } 329 330 public final void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 331 del.removePropertyChangeListener (l); 332 } 333 334 335 public void print() { 336 del.print (); 337 } 338 339 351 protected void loadFromStreamToKit (StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException { 352 del.superLoadFromStreamToKit (doc, stream, kit); 353 } 354 355 358 protected void reloadDocument() { 359 reloadDocumentTask ().waitFinished (); 360 } 361 362 367 protected Task reloadDocumentTask () { 368 return del.superReloadDocument (); 369 } 370 371 376 protected Editor openAt(PositionRef pos) { 377 CloneableEditorSupport.Pane p = del.openAt (pos, -1); 378 if (p instanceof Editor) { 379 return (Editor)p; 380 } 381 java.awt.Component c = p.getEditorPane(); 382 for (;;) { 383 if (c instanceof Editor) { 384 return (Editor)c; 385 } 386 c = c.getParent(); 387 } 388 } 389 390 395 protected boolean canClose () { 396 return del.superCanClose (); 397 } 398 399 406 public JEditorPane [] getOpenedPanes () { 407 return del.getOpenedPanes (); 408 } 409 410 413 protected void notifyUnmodified () { 414 EditorSupport.this.modifySaveCookie (false); 415 del.superNotifyUnmodified (); 416 } 417 418 424 protected boolean notifyModified () { 425 if (del.superNotifyModified ()) { 426 EditorSupport.this.modifySaveCookie (true); 427 return true; 428 } else { 429 return false; 430 } 431 } 432 433 434 436 protected void notifyClosed() { 437 del.superNotifyClosed (); 438 } 439 440 441 446 static EditorSupport extract (CloneableEditorSupport ces) { 447 EditorSupport.Del del = (Del)ces; 448 return del.es (); 449 } 450 451 452 455 final void modifySaveCookie (boolean add) { 456 if (listenToModifs) { 457 if (add) { 458 ((EntryEnv)env).addSaveCookie (); 459 } else { 460 ((EntryEnv)env).removeSaveCookie (); 461 } 462 } 463 } 464 465 466 468 public static class Editor extends CloneableEditor { 469 470 protected DataObject obj; 471 472 static final long serialVersionUID =-185739563792410059L; 473 474 475 public Editor () { 476 super(); 477 } 478 479 483 public Editor (DataObject obj) { 484 this(obj, (EditorSupport)obj.getCookie(EditorSupport.class)); 485 } 486 487 491 public Editor (DataObject obj, EditorSupport support) { 492 super (support.del); 493 this.obj = obj; 494 } 495 496 499 public void readExternal (ObjectInput in) 500 throws IOException, ClassNotFoundException { 501 super.readExternal(in); 502 503 Object ces = cloneableEditorSupport (); 504 if (ces instanceof Del) { 505 obj = ((Del)ces).getDataObjectHack2 (); 506 } 507 508 } 509 510 } 512 515 private final class Del extends DataEditorSupport implements EditorCookie.Observable { 516 517 518 private NodeListener nodeL; 519 520 521 524 public Del ( 525 DataObject obj, 526 CloneableEditorSupport.Env env, 527 CloneableTopComponent.Ref ref 528 ) { 529 super (obj, env); 530 this.allEditors = ref; 531 } 532 533 534 public final EditorSupport es () { 535 return EditorSupport.this; 536 } 537 538 protected void notifyUnmodified () { 539 EditorSupport.this.notifyUnmodified (); 540 } 541 542 protected boolean notifyModified () { 543 return EditorSupport.this.notifyModified (); 544 } 545 546 protected void notifyClosed() { 547 EditorSupport.this.notifyClosed (); 548 } 549 550 final void superNotifyUnmodified () { 551 super.notifyUnmodified (); 552 } 553 554 final boolean superNotifyModified () { 555 return super.notifyModified (); 556 } 557 final void superNotifyClosed() { 558 nodeL = null; 561 562 super.notifyClosed (); 563 } 564 565 protected CloneableEditor createCloneableEditor() { 566 if (true) throw new IllegalStateException ("Do not call!"); 567 CloneableTopComponent ctc = createCloneableTopComponent(); 568 if(ctc instanceof Editor) { 569 return (CloneableEditor)ctc; 570 } else { 571 return new Editor(getDataObject()); 572 } 573 } 574 575 576 protected Pane createPane () { 577 CloneableTopComponent ctc = createCloneableTopComponent(); 578 if(ctc instanceof Editor) { 579 return (CloneableEditor)ctc; 580 } else { 581 Pane pan = (Pane )ctc.getClientProperty("CloneableEditorSupport.Pane"); 582 if (pan != null) { 583 return pan; 584 } 585 if (ctc instanceof Pane ) { 586 return (Pane )ctc; 587 } 588 return new Editor(getDataObject()); 589 } 590 591 } 592 593 protected String messageToolTip() { 597 return EditorSupport.this.messageToolTip (); 598 } 599 600 protected String messageName() { 601 return EditorSupport.this.messageName (); 602 } 603 604 protected String messageOpening() { 605 return EditorSupport.this.messageOpening (); 606 } 607 608 protected String messageOpened() { 609 return EditorSupport.this.messageOpened (); 610 } 611 612 protected String messageSave() { 613 return EditorSupport.this.messageSave (); 614 } 615 616 protected void updateTitles () { 617 EditorSupport.this.updateTitles (); 618 } 619 620 621 final String superMessageToolTip() { 622 return super.messageToolTip (); 623 } 624 625 final String superMessageName() { 626 return super.messageName (); 627 } 628 629 final String superMessageOpening() { 630 return super.messageOpening (); 631 } 632 633 final String superMessageOpened() { 634 return super.messageOpened (); 635 } 636 637 final String superMessageSave() { 638 return super.messageSave (); 639 } 640 641 final void superUpdateTitles () { 642 super.updateTitles (); 643 } 644 645 646 650 protected boolean close(boolean ask) { 651 return EditorSupport.this.close (ask); 652 } 653 654 protected boolean superClose(boolean ask) { 655 return super.close (ask); 656 } 657 658 661 protected CloneableTopComponent createCloneableTopComponent() { 662 CloneableTopComponent ctc = EditorSupport.this.createCloneableTopComponent (); 663 if(ctc instanceof CloneableEditor) { 664 initializeCloneableEditor((CloneableEditor)ctc); 665 } 666 return ctc; 667 } 668 669 670 protected void initializeCloneableEditor (CloneableEditor editor) { 671 DataObject obj = getDataObject(); 672 if(obj.isValid()) { 673 org.openide.nodes.Node ourNode = obj.getNodeDelegate(); 674 editor.setActivatedNodes(new org.openide.nodes.Node[] {ourNode}); 675 editor.setIcon(ourNode.getIcon (java.beans.BeanInfo.ICON_COLOR_16x16)); 676 NodeListener nl = new DataNodeListener(editor); 677 ourNode.addNodeListener(org.openide.nodes.NodeOp.weakNodeListener (nl, ourNode)); 678 nodeL = nl; 679 } 680 } 681 682 683 final void superLoadFromStreamToKit( 687 StyledDocument doc,InputStream stream,EditorKit kit 688 ) throws IOException, BadLocationException { 689 super.loadFromStreamToKit (doc, stream, kit); 690 } 691 692 protected void loadFromStreamToKit( 693 StyledDocument doc,InputStream stream,EditorKit kit 694 ) throws IOException, BadLocationException { 695 EditorSupport.this.loadFromStreamToKit (doc, stream, kit); 696 } 697 698 protected void superSaveFromKitToStream( 699 StyledDocument doc,EditorKit kit,OutputStream stream 700 ) throws IOException, BadLocationException { 701 super.saveFromKitToStream (doc, kit, stream); 702 } 703 protected void saveFromKitToStream( 704 StyledDocument doc,EditorKit kit,OutputStream stream 705 ) throws IOException, BadLocationException { 706 EditorSupport.this.saveFromKitToStream (doc, kit, stream); 707 } 708 709 protected Task reloadDocument () { 710 return EditorSupport.this.reloadDocumentTask (); 711 } 712 713 final Task superReloadDocument () { 714 return super.reloadDocument (); 715 } 716 717 public void saveDocument() throws IOException { 718 EditorSupport.this.saveDocument(); 719 } 720 721 final void superSaveDocument() throws IOException { 722 super.saveDocument(); 723 } 724 final UndoRedo.Manager superUndoRedoManager() { 725 return super.createUndoRedoManager (); 726 } 727 728 protected UndoRedo.Manager createUndoRedoManager() { 732 return EditorSupport.this.createUndoRedoManager (); 733 } 734 735 EditorKit superCreateEditorKit() { 739 return super.createEditorKit (); 740 } 741 protected EditorKit createEditorKit() { 742 return EditorSupport.this.createEditorKit (); 743 } 744 745 750 final boolean superCanClose() { 754 return super.canClose (); 755 } 756 protected boolean canClose() { 757 return EditorSupport.this.canClose (); 758 } 759 760 761 762 private final class DataNodeListener extends NodeAdapter { 763 764 private final CloneableEditor editor; 765 766 DataNodeListener (CloneableEditor editor) { 767 this.editor = editor; 768 } 769 770 public void propertyChange (java.beans.PropertyChangeEvent ev) { 771 if (Node.PROP_DISPLAY_NAME.equals(ev.getPropertyName())) { 772 updateTitles(); 773 } 774 if (Node.PROP_ICON.equals(ev.getPropertyName())) { 775 final DataObject obj = getDataObject(); 776 if (obj.isValid()) { 777 org.openide.util.Mutex.EVENT.writeAccess(new Runnable () { 778 public void run() { 779 editor.setIcon(obj.getNodeDelegate().getIcon ( 780 java.beans.BeanInfo.ICON_COLOR_16x16)); 781 } 782 }); 783 } 784 } 785 } 786 } 788 } 789 790 791 793 private static class EntryEnv extends DataEditorSupport.Env 794 implements SaveCookie { 795 796 static final long serialVersionUID = 354528097109874355L; 797 798 801 public EntryEnv (MultiDataObject obj) { 802 super (obj); 803 } 804 805 808 protected FileObject getFile () { 809 return getDataObject ().getPrimaryFile (); 810 } 811 812 816 protected FileLock takeLock () throws IOException { 817 return ((MultiDataObject)getDataObject ()).getPrimaryEntry ().takeLock (); 818 } 819 820 823 public void propertyChange(PropertyChangeEvent ev) { 824 if (DataObject.PROP_PRIMARY_FILE.equals(ev.getPropertyName())) { 825 changeFile (); 826 } 827 if (DataObject.PROP_NAME.equals(ev.getPropertyName())) { 828 EditorSupport es = (EditorSupport)getDataObject ().getCookie ( 829 EditorSupport.class 830 ); 831 if (es != null) { 832 es.updateTitles (); 833 } 834 } 835 836 super.propertyChange (ev); 837 } 838 839 840 843 public void save() throws java.io.IOException { 844 EditorSupport es = (EditorSupport)getDataObject ().getCookie (EditorSupport.class); 848 if (es == null) 849 throw new IOException ("no EditorSupport found on this data object"); else 851 es.saveDocument (); 852 } 853 861 863 final void addSaveCookie() { 864 DataObject dataObj = getDataObject (); 865 if (dataObj instanceof MultiDataObject) { 867 if (dataObj.getCookie(SaveCookie.class) == null) { 868 getCookieSet((MultiDataObject)dataObj).add(this); 869 } 870 } 871 } 872 873 875 final void removeSaveCookie() { 876 DataObject dataObj = getDataObject (); 877 if (dataObj instanceof MultiDataObject) { 879 if (dataObj.getCookie(SaveCookie.class) == this) { 880 getCookieSet((MultiDataObject)dataObj).remove(this); 881 } 882 } 883 } 884 885 private static java.lang.reflect.Method getCookieSetMethod = null; 887 private static final org.openide.nodes.CookieSet getCookieSet (MultiDataObject obj) { 888 try { 889 if (getCookieSetMethod == null) { 890 getCookieSetMethod = MultiDataObject.class.getDeclaredMethod ("getCookieSet", new Class [] { }); getCookieSetMethod.setAccessible (true); 892 } 893 return (org.openide.nodes.CookieSet) getCookieSetMethod.invoke (obj, new Object [] { }); 894 } catch (Exception e) { 895 Exceptions.printStackTrace(e); 896 return new org.openide.nodes.CookieSet (); 897 } 898 } 899 900 906 public CloneableOpenSupport findCloneableOpenSupport() { 907 CloneableOpenSupport s = super.findCloneableOpenSupport (); 908 if (s != null) { 909 return s; 910 } 911 912 EditorSupport es = (EditorSupport)getDataObject ().getCookie (EditorSupport.class); 913 if (es != null) { 914 return es.del; 915 } else { 916 return null; 917 } 918 } 919 920 } 922 924 private static final class DelEnv extends EntryEnv { 925 926 static final long serialVersionUID = 174320972368471234L; 927 928 public DelEnv (MultiDataObject obj) { 929 super (obj); 930 } 931 932 934 public CloneableOpenSupport findCloneableOpenSupport() { 935 CloneableOpenSupport o = super.findCloneableOpenSupport (); 937 if (o instanceof EditorSupport) { 938 EditorSupport es = (EditorSupport)o; 939 return es.del; 940 } 941 return o; 942 } 943 } 944 } 945 | Popular Tags |