1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.IOException ; 27 import java.lang.reflect.InvocationTargetException ; 28 import javax.swing.Action ; 29 30 import org.openide.loaders.DataObject; 31 import org.openide.nodes.*; 32 import org.openide.util.actions.SystemAction; 33 import org.openide.util.NbBundle; 34 import org.openide.util.WeakListeners; 35 36 37 43 public class FileEntryNode extends AbstractNode { 44 45 46 static final long serialVersionUID = -7882925922830244768L; 47 48 49 private PresentableFileEntry entry; 50 51 52 59 public FileEntryNode (PresentableFileEntry entry, Children ch) { 60 super (ch); 61 this.entry = entry; 62 63 PropL propListener = new PropL (); 64 entry.addPropertyChangeListener( 65 WeakListeners.propertyChange(propListener, entry)); 66 entry.getDataObject().addPropertyChangeListener (propListener); 67 68 super.setName (entry.getName ()); 69 } 70 71 private String getBundleString(String s){ 72 return NbBundle.getMessage(FileEntryNode.class, s); 73 } 74 75 76 79 public PresentableFileEntry getFileEntry() { 80 return entry; 81 } 82 83 86 public boolean canDestroy () { 87 return entry.isDeleteAllowed (); 88 } 89 90 91 public void destroy () throws IOException { 92 entry.delete (); 93 super.destroy (); 94 } 95 96 99 public final boolean canCopy () { 100 return entry.isCopyAllowed (); 101 } 102 103 106 public final boolean canCut () { 107 return entry.isMoveAllowed (); 108 } 109 110 114 public void setName (String name) { 115 try { 116 entry.renameEntry (name); 117 super.setName (name); 118 } catch (IOException ex) { 119 throw new IllegalArgumentException (ex.getMessage ()); 120 } 121 } 122 123 127 public SystemAction getDefaultAction () { 128 if (entry.isTemplate ()) { 129 return null; 130 } else { 131 Action a = getPreferredAction(); 132 if(a instanceof SystemAction){ 133 return (SystemAction) a; 134 } else { 135 return null; 136 } 137 } 138 } 139 140 143 public Action getPreferredAction() { 144 if (entry.isTemplate ()) { 145 return null; 146 } else { 147 return super.getPreferredAction(); 148 } 149 } 150 151 157 public <T extends Node.Cookie> T getCookie(Class <T> cl) { 158 T c = entry.getCookie(cl); 159 if (c != null) { 160 return c; 161 } else { 162 return super.getCookie (cl); 163 } 164 } 165 166 170 protected Sheet createSheet () { 171 Sheet s = Sheet.createDefault (); 172 Sheet.Set ss = s.get (Sheet.PROPERTIES); 173 174 Node.Property p; 175 176 p = new PropertySupport.ReadWrite<String >( 177 PROP_NAME, 178 String .class, 179 getBundleString("PROP_name"), 180 getBundleString("HINT_name") 181 ) { 182 public String getValue() { 183 return entry.getName(); 184 } 185 186 public void setValue(String val) throws IllegalAccessException , 187 IllegalArgumentException , InvocationTargetException { 188 if (!canWrite()) { 189 throw new IllegalAccessException (); 190 } 191 FileEntryNode.this.setName(val); 192 } 193 194 public boolean canWrite () { 195 return entry.isRenameAllowed(); 196 } 197 }; 198 p.setName (DataObject.PROP_NAME); 199 ss.put (p); 200 201 try { 202 p = new PropertySupport.Reflection<Boolean >( 203 entry, Boolean.TYPE, "isTemplate", "setTemplate" ); 205 p.setName (DataObject.PROP_TEMPLATE); 206 p.setDisplayName (getBundleString("PROP_template")); 207 p.setShortDescription (getBundleString("HINT_template")); 208 ss.put (p); 209 } catch(Exception ex) { 210 throw new IllegalStateException (); 211 } 212 return s; 213 } 214 215 216 221 void fireChange (PropertyChangeEvent ev) { 222 String propertyName = ev.getPropertyName(); 223 if (propertyName.equals(Node.PROP_COOKIE)) { 224 fireCookieChange(); 225 return; 226 } 227 firePropertyChange(propertyName, ev.getOldValue(), ev.getNewValue()); 228 if (propertyName.equals(DataObject.PROP_NAME)) { 229 super.setName (entry.getName ()); 230 } 231 } 232 233 236 private class PropL extends Object implements PropertyChangeListener { 237 public void propertyChange (PropertyChangeEvent ev) { 238 fireChange (ev); 239 } 240 } 241 242 } 243 | Popular Tags |