1 19 20 package org.netbeans.core.projects; 21 22 import org.openide.NotifyDescriptor; 23 import org.openide.cookies.InstanceCookie; 24 import org.openide.loaders.DataObject; 25 import org.openide.loaders.DataFolder; 26 import org.openide.loaders.DataShadow; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileSystem; 29 import org.openide.filesystems.FileStateInvalidException; 30 import org.openide.filesystems.Repository; 31 import org.openide.util.NbBundle; 32 import org.openide.util.Utilities; 33 import org.netbeans.beaninfo.editors.ListImageEditor; 34 import java.awt.Image ; 35 import java.beans.PropertyEditor ; 36 import java.lang.reflect.InvocationTargetException ; 37 import java.lang.ref.WeakReference ; 38 import java.util.ArrayList ; 39 import java.util.Arrays ; 40 import java.util.List ; 41 import javax.swing.Action ; 42 import org.openide.actions.ToolsAction; 43 import org.openide.nodes.FilterNode; 44 import org.openide.nodes.Node; 45 import org.openide.nodes.PropertySupport; 46 import org.openide.util.Exceptions; 47 import org.openide.util.actions.SystemAction; 48 49 55 public final class SettingChildren extends FilterNode.Children { 56 57 58 public static final String PROP_LAYER_SESSION = "Session-Layer"; 60 public static final String PROP_LAYER_MODULES = "Modules-Layer"; 62 public SettingChildren (Node original) { 63 super (original); 64 } 65 66 protected Node copyNode (Node node) { 67 boolean filter = false; 68 try { 69 DataObject d = (DataObject) node.getCookie (DataObject.class); 70 if (d != null) { 71 InstanceCookie.Of inst = (InstanceCookie.Of)d.getCookie(InstanceCookie.Of.class); 72 if (inst != null && (inst.instanceOf(Node.class) || inst.instanceOf(Node.Handle.class))) { 73 d = null; 77 } 78 } 79 DataFolder folder = (DataFolder) node.getCookie (DataFolder.class); 80 FileSystem fs = d == null || folder != null ? null : d.getPrimaryFile ().getFileSystem (); 81 filter = fs == null ? false : fs.equals (Repository.getDefault ().getDefaultFileSystem ()); 82 } catch (FileStateInvalidException e) { 83 } 85 86 return filter ? new SettingFilterNode (node) : 87 node.isLeaf() ? node.cloneNode() : new TrivialFilterNode(node); 88 } 89 90 private static Action [] removeActions(Action [] allActions, Action [] toDeleteActions) { 91 Action [] retVal = allActions; 92 List <Action > actions = new ArrayList <Action >(Arrays.asList(allActions)); for (int i = 0; i < toDeleteActions.length; i++) { 94 Action a = toDeleteActions[i]; 95 if (actions.contains(a)) { 96 actions.remove(a); 97 retVal = actions.toArray(new Action [actions.size()]); 98 } 99 } 100 return retVal; 101 } 102 103 private static final class TrivialFilterNode extends FilterNode { 104 public TrivialFilterNode(Node n) { 105 super(n, new SettingChildren(n)); 106 } 107 public boolean equals(Object o) { 109 return this == o || getOriginal().equals(o) || (o != null && o.equals(getOriginal())); 110 } 111 public int hashCode() { 112 return getOriginal().hashCode(); 113 } 114 public Action [] getActions(boolean context) { 115 return removeActions(super.getActions(context), new Action [] {SystemAction.get(ToolsAction.class)}); 116 } 117 public String getHtmlDisplayName() { 118 return null; 119 } 120 } 121 122 123 public static class FileStateProperty extends PropertySupport<Integer > { 124 static final int ACTION_DEFINE = 1; 125 static final int ACTION_REVERT = 2; 126 static final int ACTION_DELETE = 3; 127 128 private FileObject primaryFile = null; 129 private int layer; 130 131 public FileStateProperty (String name) { 132 this (null, 0, name, true); 133 } 134 135 public FileStateProperty (FileObject primaryFile, int layer, String name, boolean readonly) { 136 super (name, Integer .class, 137 NbBundle.getMessage (FileStateProperty.class, "LBL_FSP_" + name), NbBundle.getMessage (FileStateProperty.class, "LBL_FSP_Desc_" + name), true, !readonly); 140 141 this.primaryFile = primaryFile; 142 this.layer = layer; 143 144 setValue (ListImageEditor.PROP_VALUES, new Integer [] { 145 FileStateManager.FSTATE_DEFINED, 146 FileStateManager.FSTATE_IGNORED, 147 FileStateManager.FSTATE_INHERITED, 148 FileStateManager.FSTATE_UNDEFINED, 149 }); 150 151 setValue (ListImageEditor.PROP_IMAGES, new Image [] { 152 Utilities.loadImage ("org/netbeans/core/resources/setting-defined.gif"), Utilities.loadImage ("org/netbeans/core/resources/setting-ignored.gif"), Utilities.loadImage ("org/netbeans/core/resources/setting-inherited.gif"), Utilities.loadImage ("org/openide/resources/actions/empty.gif") }); 157 } 158 159 public boolean canWrite () { 160 if (!super.canWrite ()) 161 return false; 162 163 Integer val = null; 164 try { 165 val = getValue(); 166 } catch (Exception e) { 167 } 169 170 return val != null && 171 val != FileStateManager.FSTATE_DEFINED && 172 (layer != FileStateManager.LAYER_MODULES || val != FileStateManager.FSTATE_UNDEFINED); 173 } 174 175 public Integer getValue() throws IllegalAccessException , InvocationTargetException { 176 return FileStateManager.getDefault().getFileState(primaryFile, layer); 177 } 178 179 public void setValue(Integer val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 180 FileStateManager fsm = FileStateManager.getDefault (); 181 int action = val; 182 183 try { 184 switch (action) { 185 case ACTION_DEFINE: 186 case ACTION_REVERT: 187 boolean go = true; 188 189 for (int i = 0; i < layer; i++) { 190 int state = fsm.getFileState (primaryFile, i); 191 if (state == FileStateManager.FSTATE_DEFINED) { 192 194 NotifyDescriptor nd = new NotifyDescriptor.Confirmation ( 195 NbBundle.getMessage (SettingChildren.class, "MSG_ask_remove_above_defined_files"), NotifyDescriptor.YES_NO_OPTION); 197 198 Object answer = org.openide.DialogDisplayer.getDefault ().notify (nd); 199 if (answer.equals (NotifyDescriptor.NO_OPTION)) 200 go = false; 201 202 break; 203 } 204 } 205 206 if (go) 207 fsm.define (primaryFile, layer, action == ACTION_REVERT); 208 209 break; 210 211 case ACTION_DELETE: 212 fsm.delete (primaryFile, layer); 213 break; 214 215 default: 216 throw new IllegalArgumentException ("Required file state change isn't allowed. Action=" + action); } 218 } catch (java.io.IOException e) { 219 Exceptions.printStackTrace(e); 220 } 221 } 222 223 public PropertyEditor getPropertyEditor () { 224 return new FileStateEditor (); 225 } 226 227 public String getShortDescription () { 228 Integer val = null; 229 String s = null; 230 231 if (primaryFile != null) { 232 try { 233 val = getValue(); 234 } catch (Exception e) { 235 } 237 238 switch (val == null ? -1 : val) { 239 case FileStateManager.FSTATE_DEFINED: 240 s = NbBundle.getMessage (SettingChildren.class, "LBL_fstate_defined"); 241 break; 242 case FileStateManager.FSTATE_IGNORED: 243 s = NbBundle.getMessage (SettingChildren.class, "LBL_fstate_ignored"); 244 break; 245 case FileStateManager.FSTATE_INHERITED: 246 s = NbBundle.getMessage (SettingChildren.class, "LBL_fstate_inherited"); 247 break; 248 case FileStateManager.FSTATE_UNDEFINED: 249 s = NbBundle.getMessage (SettingChildren.class, "LBL_fstate_undefined"); 250 break; 251 } 252 } 253 else { 254 s = super.getShortDescription (); 255 } 256 return s == null || s.length () == 0 ? null : s; 257 } 258 } 259 260 261 private static final class SettingFilterNode extends FilterNode { 262 private FSL weakL = null; 263 264 public SettingFilterNode (Node original) { 265 super (original); 266 disableDelegation(DELEGATE_SET_VALUE | DELEGATE_GET_VALUE); 268 269 FileObject pf = ((DataObject) getCookie (DataObject.class)).getPrimaryFile (); 270 weakL = new FSL (this); 271 FileStateManager.getDefault ().addFileStatusListener (weakL, pf); 272 273 specialProp (new FileStateProperty (pf, FileStateManager.LAYER_SESSION, PROP_LAYER_SESSION, false)); 274 specialProp (new FileStateProperty (pf, FileStateManager.LAYER_MODULES, PROP_LAYER_MODULES, false)); 275 } 276 277 279 public String getDisplayName() { 280 String retVal = null; 281 DataObject dobj= (DataObject) getCookie (DataObject.class); 282 if (dobj != null && dobj instanceof DataShadow) { 283 DataShadow dsh = (DataShadow)dobj; 284 Node origNode = dsh.getOriginal().getNodeDelegate(); 285 if (origNode != null) { 286 retVal = origNode.getDisplayName(); 287 } 288 } 289 return (retVal != null) ? retVal : super.getDisplayName(); 290 } 291 292 294 private void specialProp (Node.Property p) { 295 setValue (p.getName (), p); 296 } 297 298 public boolean equals(Object o) { 300 return this == o || getOriginal().equals(o) || (o != null && o.equals(getOriginal())); 301 } 302 public int hashCode() { 303 return getOriginal().hashCode(); 304 } 305 307 public Action [] getActions(boolean context) { 308 return removeActions(super.getActions(context), new Action [] {SystemAction.get(ToolsAction.class)}); 309 } 310 311 private static class FSL implements FileStateManager.FileStatusListener { 312 WeakReference <SettingFilterNode> node = null; 313 public FSL (SettingFilterNode sfn) { 314 node = new WeakReference <SettingFilterNode> (sfn); 315 } 316 public void fileStatusChanged (FileObject mfo) { 317 SettingFilterNode n = node.get (); 318 if (n == null) { 319 FileStateManager.getDefault ().removeFileStatusListener (this, null); 320 return; 321 } 322 323 n.firePropertyChange (PROP_LAYER_SESSION, null, null); 324 n.firePropertyChange (PROP_LAYER_MODULES, null, null); 325 } 326 } 327 } 328 } 329 | Popular Tags |