1 19 20 package org.netbeans.modules.properties; 21 22 import java.awt.datatransfer.*; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.io.*; 26 27 import javax.swing.*; 28 import javax.swing.event.ChangeListener ; 29 import javax.swing.event.ChangeEvent ; 30 31 import org.openide.loaders.*; 32 import org.openide.*; 33 import org.openide.util.datatransfer.*; 34 import org.openide.filesystems.FileObject; 35 import org.openide.util.*; 36 import org.openide.nodes.*; 37 38 45 public abstract class PresentableFileEntry extends FileEntry 46 implements Node.Cookie { 47 48 49 static final long serialVersionUID = 3328227388376142699L; 50 51 52 private transient Node nodeDelegate; 53 54 55 private boolean modif = false; 56 57 58 private transient PropertyChangeSupport changeSupport; 59 60 61 private ChangeListener cookieL = new ChangeListener () { 62 public void stateChanged (ChangeEvent ev) { 63 firePropertyChange (Node.PROP_COOKIE, null, null); 64 } 65 }; 66 67 68 private transient CookieSet cookieSet; 69 70 private transient Object nodeDelegateMutex = new Object (); 72 73 80 public PresentableFileEntry(MultiDataObject obj, FileObject fo) { 81 super (obj, fo); 82 } 83 84 85 86 protected abstract Node createNodeDelegate(); 87 88 95 public final Node getNodeDelegate () { 96 synchronized (nodeDelegateMutex) { 97 if (nodeDelegate == null) { 98 nodeDelegate = createNodeDelegate(); 99 } 100 return nodeDelegate; 101 } 102 } 103 104 115 private static boolean setTemplate(FileObject fo, 116 boolean newValue) throws IOException { 117 Object old = fo.getAttribute(DataObject.PROP_TEMPLATE); 118 boolean oldValue = Boolean.TRUE.equals(old); 119 if (newValue == oldValue) { 120 return false; 121 } 122 fo.setAttribute(DataObject.PROP_TEMPLATE, 123 newValue ? Boolean.TRUE : null); 124 return true; 125 } 126 127 136 public final void setTemplate(boolean newValue) throws IOException { 137 if (!setTemplate(getFile(), newValue)) { 138 return; 140 } 141 firePropertyChange(DataObject.PROP_TEMPLATE, 142 Boolean.valueOf(!newValue), 143 Boolean.valueOf(newValue)); 144 } 145 146 152 public boolean isTemplate() { 153 Object o = getFile().getAttribute(DataObject.PROP_TEMPLATE); 154 return Boolean.TRUE.equals(o); 155 } 156 157 165 public FileObject rename (String name) throws IOException { 166 String oldName = getName(); 167 FileObject fo = super.rename(name); 168 firePropertyChange(DataObject.PROP_NAME, oldName, name); 169 return fo; 170 } 171 172 179 public FileObject renameEntry (String name) throws IOException { 180 return rename(name); 181 } 182 183 184 public void delete () throws IOException { 185 super.delete(); 186 187 firePropertyChange(DataObject.PROP_VALID, Boolean.TRUE, Boolean.FALSE); 188 } 189 190 191 194 public abstract boolean isDeleteAllowed (); 195 196 199 public abstract boolean isCopyAllowed (); 200 201 204 public abstract boolean isMoveAllowed (); 205 206 210 public boolean isShadowAllowed () { 211 return true; 212 } 213 214 217 public abstract boolean isRenameAllowed (); 218 219 220 223 public boolean isModified() { 224 return modif; 225 } 226 227 234 public void setModified(boolean modif) { 235 if (this.modif != modif) { 236 this.modif = modif; 237 firePropertyChange(DataObject.PROP_MODIFIED, 238 Boolean.valueOf(!modif), 239 Boolean.valueOf(modif)); 240 } 241 } 242 243 248 public abstract HelpCtx getHelpCtx (); 249 250 257 public String getName () { 258 return getFile ().getName (); 259 } 260 261 268 public final DataFolder getFolder () { 269 FileObject fo = getFile ().getParent (); 270 return fo == null ? null : DataFolder.findFolder (fo); 273 } 274 275 276 280 282 public synchronized void addPropertyChangeListener (PropertyChangeListener l) { 283 getChangeSupport ().addPropertyChangeListener (l); 284 } 285 286 288 public void removePropertyChangeListener (PropertyChangeListener l) { 289 getChangeSupport ().removePropertyChangeListener (l); 290 } 291 292 299 protected final void firePropertyChange (String name, Object oldValue, Object newValue) { 300 getChangeSupport ().firePropertyChange (name, oldValue, newValue); 301 } 302 303 308 private synchronized final PropertyChangeSupport getChangeSupport () { 309 if (changeSupport == null) { 310 changeSupport = new PropertyChangeSupport (this); 311 } 312 return changeSupport; 313 } 314 315 323 protected final synchronized void setCookieSet (CookieSet s) { 324 if (cookieSet != null) { 325 cookieSet.removeChangeListener (cookieL); 326 } 327 328 s.addChangeListener (cookieL); 329 cookieSet = s; 330 331 firePropertyChange (Node.PROP_COOKIE, null, null); 332 } 333 334 342 protected final CookieSet getCookieSet () { 343 CookieSet s = cookieSet; 344 if (s != null) { 345 return s; 346 } 347 synchronized (this) { 348 if (cookieSet != null) { 349 return cookieSet; 350 } 351 setCookieSet (new CookieSet ()); 353 return cookieSet; 354 } 355 } 356 357 364 @SuppressWarnings ("unchecked") 365 public <T extends Node.Cookie> T getCookie(Class <T> type) { 366 CookieSet c = cookieSet; 367 if (c != null) { 368 T cookie = c.getCookie(type); 369 if (cookie != null) { 370 return cookie; 371 } 372 } 373 374 if (type.isInstance (this)) { 375 return (T) this; 376 } 377 return null; 378 } 379 } 380 | Popular Tags |