1 19 20 package org.openide.text; 21 22 import java.io.IOException ; 23 import org.openide.cookies.CloseCookie; 24 import org.openide.cookies.EditCookie; 25 import org.openide.cookies.EditorCookie; 26 import org.openide.cookies.OpenCookie; 27 import org.openide.cookies.PrintCookie; 28 import org.openide.cookies.SaveCookie; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileLock; 31 import org.openide.loaders.DataObject; 32 import org.openide.loaders.MultiDataObject; 33 import org.openide.nodes.CookieSet; 34 import org.openide.nodes.Node.Cookie; 35 import org.openide.windows.CloneableOpenSupport; 36 37 42 final class SimpleES extends DataEditorSupport 43 implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie { 44 46 private final SaveCookie saveCookie = new SaveCookie() { 47 48 public void save() throws IOException { 49 SimpleES.this.saveDocument(); 50 } 51 }; 52 53 private CookieSet set; 54 55 59 SimpleES (DataObject obj, MultiDataObject.Entry entry, CookieSet set) { 60 super(obj, new Environment(obj, entry)); 61 this.set = set; 62 } 63 64 69 protected boolean notifyModified () { 70 if (!super.notifyModified()) 71 return false; 72 73 addSaveCookie(); 74 75 return true; 76 } 77 78 79 protected void notifyUnmodified () { 80 super.notifyUnmodified(); 81 82 removeSaveCookie(); 83 } 84 85 86 private void addSaveCookie() { 87 DataObject obj = getDataObject(); 88 89 if(obj.getCookie(SaveCookie.class) == null) { 91 set.add(saveCookie); 92 obj.setModified(true); 93 } 94 } 95 96 97 private void removeSaveCookie() { 98 DataObject obj = getDataObject(); 99 100 Cookie cookie = obj.getCookie(SaveCookie.class); 102 103 if(cookie != null && cookie.equals(saveCookie)) { 104 set.remove(saveCookie); 105 obj.setModified(false); 106 } 107 } 108 109 110 113 114 private static class Environment extends DataEditorSupport.Env { 115 private static final long serialVersionUID = 5451434321155443431L; 116 117 private MultiDataObject.Entry entry; 118 119 120 public Environment(DataObject obj, MultiDataObject.Entry entry) { 121 super(obj); 122 this.entry = entry; 123 } 124 125 126 127 protected FileObject getFile() { 128 return entry.getFile(); 129 } 130 131 132 protected FileLock takeLock() throws IOException { 133 return entry.takeLock(); 134 } 135 136 140 public CloneableOpenSupport findCloneableOpenSupport() { 141 return (SimpleES)getDataObject().getCookie(SimpleES.class); 142 } 143 } 145 } 146 | Popular Tags |