1 19 20 package org.netbeans.modules.gsf; 21 22 import java.io.IOException ; 23 import java.io.ObjectInput ; 24 import java.util.List ; 25 import javax.swing.text.Document ; 26 import javax.swing.text.StyledDocument ; 27 import org.netbeans.api.java.classpath.ClassPath; 28 import org.netbeans.api.gsf.CancellableTask; 29 import org.netbeans.api.retouche.source.ClasspathInfo; 30 import org.netbeans.api.retouche.source.Source; 31 import org.netbeans.api.retouche.source.WorkingCopy; 32 import org.netbeans.modules.gsf.Language; 33 import org.netbeans.spi.palette.PaletteController; 35 import org.openide.cookies.EditCookie; 36 import org.openide.cookies.EditorCookie; 37 import org.openide.cookies.OpenCookie; 38 import org.openide.cookies.PrintCookie; 39 import org.openide.cookies.SaveCookie; 40 import org.openide.filesystems.FileLock; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.loaders.DataFolder; 44 import org.openide.loaders.DataObject; 45 import org.openide.loaders.DataObjectExistsException; 46 import org.openide.loaders.MultiDataObject; 47 import org.openide.loaders.MultiFileLoader; 48 import org.openide.nodes.Node; 49 import org.openide.nodes.Node.Cookie; 50 import org.openide.text.CloneableEditor; 51 import org.openide.text.CloneableEditorSupport; 52 import org.openide.text.DataEditorSupport; 53 import org.openide.util.Lookup; 54 import org.openide.util.NbBundle; 55 import org.openide.util.Utilities; 56 import org.openide.util.lookup.AbstractLookup; 57 import org.openide.util.lookup.InstanceContent; 58 import org.openide.util.lookup.ProxyLookup; 59 import org.openide.windows.CloneableOpenSupport; 60 61 public class GsfDataObject extends MultiDataObject { 62 63 private GenericEditorSupport jes; 64 private Language language; 65 66 public GsfDataObject(FileObject pf, MultiFileLoader loader, Language language) throws DataObjectExistsException { 67 super(pf, loader); 68 this.language = language; 69 } 70 71 public @Override Node createNodeDelegate() { 72 return new GsfDataNode(this, language); 73 } 74 75 public @Override Cookie getCookie(Class type) { 76 77 if (type.isAssignableFrom(GenericEditorSupport.class)) { 78 return createEditorSupport (); 79 } 80 return super.getCookie(type); 81 } 82 83 84 protected @Override DataObject handleCreateFromTemplate(DataFolder df, String name) throws IOException { 85 if (name == null) { 86 name = FileUtil.findFreeFileName(df.getPrimaryFile(), 88 getPrimaryFile().getName(), language.getExtensions()[0]); 89 } 90 else if (!Utilities.isJavaIdentifier(name)) { 91 throw new IOException (NbBundle.getMessage(GsfDataObject.class, "FMT_Not_Valid_FileName", name)); 92 } 93 DataObject retValue = super.handleCreateFromTemplate(df, name); 96 FileObject fo = retValue.getPrimaryFile (); 97 assert fo != null; 98 ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE); 99 String pkgName; 100 if (cp != null) { 101 pkgName = cp.getResourceName(fo.getParent(),'.',false); 102 } 103 else { 104 pkgName = ""; } 106 return retValue; 108 } 109 110 111 private synchronized GenericEditorSupport createEditorSupport () { 112 if (jes == null) { 113 jes = new GenericEditorSupport(this, language); 114 } 115 return jes; 116 } 117 118 public static class GenericEditorSupport extends DataEditorSupport implements OpenCookie, EditCookie, EditorCookie, PrintCookie, EditorCookie.Observable { 119 120 private static class Environment extends DataEditorSupport.Env { 121 122 private static final long serialVersionUID = -1; 123 124 private transient SaveSupport saveCookie = null; 125 126 private class SaveSupport implements SaveCookie { 127 public void save() throws java.io.IOException { 128 ((GenericEditorSupport)findCloneableOpenSupport()).saveDocument(); 129 getDataObject().setModified(false); 130 } 131 } 132 133 public Environment(GsfDataObject obj) { 134 super(obj); 135 } 136 137 protected FileObject getFile() { 138 return this.getDataObject().getPrimaryFile(); 139 } 140 141 protected FileLock takeLock() throws java.io.IOException { 142 return this.getFile().lock(); 143 } 144 145 public @Override CloneableOpenSupport findCloneableOpenSupport() { 146 return (CloneableEditorSupport) ((GsfDataObject)this.getDataObject()).getCookie(EditorCookie.class); 147 } 148 149 150 public void addSaveCookie() { 151 GsfDataObject javaData = (GsfDataObject) this.getDataObject(); 152 if (javaData.getCookie(SaveCookie.class) == null) { 153 if (this.saveCookie == null) 154 this.saveCookie = new SaveSupport(); 155 javaData.getCookieSet().add(this.saveCookie); 156 javaData.setModified(true); 157 } 158 } 159 160 public void removeSaveCookie() { 161 GsfDataObject javaData = (GsfDataObject) this.getDataObject(); 162 if (javaData.getCookie(SaveCookie.class) != null) { 163 javaData.getCookieSet().remove(this.saveCookie); 164 javaData.setModified(false); 165 } 166 } 167 } 168 169 170 public GenericEditorSupport(GsfDataObject dataObject, Language language) { 171 super(dataObject, new Environment(dataObject)); 172 setMIMEType(language.getMimeType()); 173 } 174 175 176 protected boolean notifyModified() { 177 if (!super.notifyModified()) 178 return false; 179 ((Environment)this.env).addSaveCookie(); 180 return true; 181 } 182 183 184 protected @Override void notifyUnmodified() { 185 super.notifyUnmodified(); 186 ((Environment)this.env).removeSaveCookie(); 187 } 188 189 protected @Override CloneableEditor createCloneableEditor() { 190 return new GsfEditor(this); 191 } 192 193 public @Override boolean close(boolean ask) { 194 return super.close(ask); 195 } 196 } 197 198 private static final class GsfEditor extends CloneableEditor { 199 200 private static final long serialVersionUID = -1; 201 202 public GsfEditor() { 203 } 204 205 public GsfEditor(GenericEditorSupport sup) { 206 super(sup); 207 initialize(); 208 } 209 210 void associatePalette(GenericEditorSupport s) { 211 DataObject dataObject = s.getDataObject(); 212 if (!(dataObject instanceof GsfDataObject)) { 213 return; 214 } 215 216 GsfDataObject gdo = (GsfDataObject)s.getDataObject(); 217 PaletteController pc = gdo.language.getPalette(); 218 if (pc == null) { 219 return; 220 } 221 222 Node nodes[] = { gdo.getNodeDelegate() }; 223 InstanceContent instanceContent = new InstanceContent(); 224 associateLookup(new ProxyLookup(new Lookup[] { new AbstractLookup(instanceContent), nodes[0].getLookup()})); 225 instanceContent.add(getActionMap()); 226 227 setActivatedNodes(nodes); 228 229 instanceContent.add(pc); 230 } 231 232 private void initialize() { 233 associatePalette((GenericEditorSupport)cloneableEditorSupport()); 234 } 235 236 @Override 237 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 238 super.readExternal(in); 239 initialize(); 240 } 241 242 243 244 245 } 246 247 } 252 | Popular Tags |