1 19 20 21 package org.openide.loaders; 22 23 24 import java.io.IOException ; 25 26 import org.openide.cookies.CloseCookie; 27 import org.openide.cookies.EditCookie; 28 import org.openide.cookies.EditorCookie; 29 import org.openide.cookies.OpenCookie; 30 import org.openide.cookies.PrintCookie; 31 import org.openide.cookies.SaveCookie; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileLock; 34 import org.openide.loaders.DataObject; 35 import org.openide.loaders.MultiDataObject; 36 import org.openide.nodes.CookieSet; 37 import org.openide.nodes.Node.Cookie; 38 import org.openide.text.DataEditorSupport; 39 import org.openide.windows.CloneableOpenSupport; 40 41 42 47 final class DefaultES extends DataEditorSupport 48 implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie { 49 51 private final SaveCookie saveCookie = new SaveCookie() { 52 53 public void save() throws IOException { 54 DefaultES.this.saveDocument(); 55 } 56 }; 57 58 private CookieSet set; 59 60 64 DefaultES (DataObject obj, MultiDataObject.Entry entry, CookieSet set) { 65 super(obj, new Environment(obj, entry)); 66 this.set = set; 67 setMIMEType("text/plain"); } 69 70 75 protected boolean notifyModified () { 76 if (!super.notifyModified()) 77 return false; 78 79 addSaveCookie(); 80 81 return true; 82 } 83 84 85 protected void notifyUnmodified () { 86 super.notifyUnmodified(); 87 88 removeSaveCookie(); 89 } 90 91 92 private void addSaveCookie() { 93 DataObject obj = getDataObject(); 94 95 if(obj.getCookie(SaveCookie.class) == null) { 97 set.add(saveCookie); 98 obj.setModified(true); 99 } 100 } 101 102 103 private void removeSaveCookie() { 104 DataObject obj = getDataObject(); 105 106 Cookie cookie = obj.getCookie(SaveCookie.class); 108 109 if(cookie != null && cookie.equals(saveCookie)) { 110 set.remove(saveCookie); 111 obj.setModified(false); 112 } 113 } 114 115 116 119 120 private static class Environment extends DataEditorSupport.Env { 121 private static final long serialVersionUID = 5451434321155443431L; 122 123 private MultiDataObject.Entry entry; 124 125 126 public Environment(DataObject obj, MultiDataObject.Entry entry) { 127 super(obj); 128 this.entry = entry; 129 } 130 131 132 133 protected FileObject getFile() { 134 return entry.getFile(); 135 } 136 137 138 protected FileLock takeLock() throws IOException { 139 return entry.takeLock(); 140 } 141 142 146 public CloneableOpenSupport findCloneableOpenSupport() { 147 DataObject obj = getDataObject (); 148 DefaultES ret; 149 if (obj instanceof DefaultDataObject) { 150 ret = (DefaultES)((DefaultDataObject)obj).getCookie (DefaultES.class, true); 151 } else { 152 ret = (DefaultES)getDataObject().getCookie(DefaultES.class); 153 } 154 155 super.findCloneableOpenSupport (); 158 159 return ret; 160 } 161 } 163 } 164 | Popular Tags |