1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.ArrayList ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.logging.Level ; 28 import org.openide.DialogDisplayer; 29 import org.openide.NotifyDescriptor; 30 import org.openide.cookies.CloseCookie; 31 import org.openide.cookies.EditCookie; 32 import org.openide.cookies.EditorCookie; 33 import org.openide.cookies.OpenCookie; 34 import org.openide.cookies.PrintCookie; 35 import org.openide.filesystems.FileLock; 36 import org.openide.filesystems.FileObject; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.NbBundle; 39 import org.openide.nodes.Node; 40 41 43 final class DefaultDataObject extends MultiDataObject implements OpenCookie { 44 static final long serialVersionUID =-4936309935667095746L; 45 46 47 49 53 DefaultDataObject (FileObject fo, MultiFileLoader loader) throws DataObjectExistsException { 54 super (fo, loader); 55 } 56 57 59 protected Node createNodeDelegate () { 60 DataNode dn = new DataNode (this, org.openide.nodes.Children.LEAF); 61 62 dn.setShortDescription (NbBundle.getMessage (DefaultDataObject.class, 64 "HINT_DefaultDataObject")); return dn; 66 } 67 68 72 73 public String getName() { 74 return getPrimaryFile ().getNameExt (); 75 } 76 77 80 public HelpCtx getHelpCtx () { 81 return HelpCtx.DEFAULT_HELP; 82 } 83 84 85 92 protected FileObject handleRename (String name) throws IOException { 93 FileLock lock = getPrimaryFile ().lock (); 94 int pos = name.lastIndexOf('.'); 95 96 try { 97 if (pos < 0){ 98 getPrimaryFile ().rename (lock, name, null); 100 } else if (pos == 0){ 101 getPrimaryFile ().rename (lock, name, getPrimaryFile ().getExt ()); 102 } else { 103 if (!name.equals(getPrimaryFile ().getNameExt())){ 104 getPrimaryFile ().rename (lock, name.substring(0, pos), 105 name.substring(pos+1, name.length())); 106 DataObjectPool.getPOOL().revalidate( 107 new HashSet <FileObject> (java.util.Collections.singleton(getPrimaryFile ())) 108 ); 109 } 110 } 111 } finally { 112 lock.releaseLock (); 113 } 114 return getPrimaryFile (); 115 } 116 117 120 protected DataObject handleCreateFromTemplate ( 121 DataFolder df, String name 122 ) throws IOException { 123 if (name != null && name.endsWith("." + getPrimaryFile ().getExt ())) name = name.substring(0, name.lastIndexOf("." + getPrimaryFile ().getExt ())); 127 return super.handleCreateFromTemplate (df, name); 128 } 129 130 132 public void open() { 133 EditorCookie ic = getCookie(EditorCookie.class); 134 if (ic != null) { 135 ic.open(); 136 } else { 137 List <Object > options = new ArrayList <Object >(); 139 options.add (NotifyDescriptor.OK_OPTION); 140 options.add (NotifyDescriptor.CANCEL_OPTION); 141 NotifyDescriptor nd = new NotifyDescriptor ( 142 NbBundle.getMessage (DefaultDataObject.class, "MSG_BinaryFileQuestion"), 143 NbBundle.getMessage (DefaultDataObject.class, "MSG_BinaryFileWarning"), 144 NotifyDescriptor.DEFAULT_OPTION, 145 NotifyDescriptor.QUESTION_MESSAGE, 146 options.toArray(), null 147 ); 148 Object ret = DialogDisplayer.getDefault().notify (nd); 149 if (ret != NotifyDescriptor.OK_OPTION) { 150 return; 151 } 152 153 EditorCookie c = getCookie(EditorCookie.class, true); 154 c.open (); 155 } 156 } 157 158 160 public <T extends Node.Cookie> T getCookie(Class <T> c) { 161 return getCookie (c, false); 162 } 163 164 167 final <T extends Node.Cookie> T getCookie(Class <T> c, boolean force) { 168 if (c == OpenCookie.class) { 169 return c.cast(this); 170 } 171 172 T cook = super.getCookie (c); 173 if (cook != null) { 174 return cook; 175 } 176 177 if ( 178 c.isAssignableFrom(EditCookie.class) 179 || 180 c.isAssignableFrom(EditorCookie.Observable.class) 181 || 182 c.isAssignableFrom(PrintCookie.class) 183 || 184 c.isAssignableFrom(CloseCookie.class) 185 || 186 c == DefaultES.class 187 ) { 188 try { 189 if (!force) { 190 byte[] arr = new byte[2048]; 193 InputStream is = getPrimaryFile().getInputStream(); 194 try { 195 int len = is.read (arr); 196 for (int i = 0; i < len; i++) { 197 if (arr[i] >= 0 && arr[i] <= 31 && arr[i] != '\n' && arr[i] != '\r' && arr[i] != '\t') { 198 return null; 199 } 200 } 201 } finally { 202 is.close (); 203 } 204 } 205 DefaultES support = new DefaultES ( 206 this, getPrimaryEntry(), getCookieSet () 207 ); 208 getCookieSet().add(support); 209 return getCookieSet ().getCookie(c); 210 } catch (IOException ex) { 211 LOG.log(Level.INFO, "Cannot read " + getPrimaryEntry(), ex); } 213 } 214 return null; 215 } 216 } 217 | Popular Tags |