1 19 20 package org.netbeans.core.ui; 21 22 import javax.swing.Action ; 23 import org.openide.actions.FileSystemAction; 24 import org.openide.actions.MoveDownAction; 25 import org.openide.actions.MoveUpAction; 26 import org.openide.actions.NewTemplateAction; 27 import org.openide.actions.PasteAction; 28 import org.openide.actions.PropertiesAction; 29 import org.openide.actions.ReorderAction; 30 import org.openide.actions.ToolsAction; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.Repository; 35 import org.openide.loaders.DataFolder; 36 import org.openide.loaders.DataObject; 37 import org.openide.loaders.DataShadow; 38 import org.openide.loaders.TemplateWizard; 39 import org.openide.loaders.XMLDataObject; 40 import org.openide.nodes.FilterNode; 41 import org.openide.nodes.Node; 42 import org.openide.nodes.Sheet; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.actions.SystemAction; 45 46 51 public class LookupNode extends DataFolder.FolderNode implements NewTemplateAction.Cookie { 52 53 private static final String EA_HIDDEN = "hidden"; private static final String EA_HELPCTX = "helpID"; 58 private static final HelpCtx INSTANCE_DEFAULT_HELP = new HelpCtx("org.openide.loaders.InstanceDataObject"); 62 private static final HelpCtx FOLDER_DEFAULT_HELP = new HelpCtx("org.openide.loaders.DataFolder"); private static final String PREFIX_SETTING_CATEGORIES = "UI"; 65 67 public LookupNode (DataFolder folder) { 68 folder.super(new Ch(folder)); 69 getCookieSet ().add (this); 72 } 73 74 75 private boolean isUISettingCategoryNode() { 76 DataFolder df = (DataFolder) super.getCookie (DataFolder.class); 77 if (df != null) { 78 String name = df.getPrimaryFile ().getPath(); 79 return name.startsWith(PREFIX_SETTING_CATEGORIES); 80 } else return false; 81 } 82 83 public HelpCtx getHelpCtx () { 84 Object o = getDataObject().getPrimaryFile().getAttribute(EA_HELPCTX); 85 if (o != null) { 86 return new HelpCtx(o.toString()); 87 } 88 HelpCtx ctx = getDataObject().getHelpCtx(); 90 if (ctx != null && 91 ctx != HelpCtx.DEFAULT_HELP && 92 !FOLDER_DEFAULT_HELP.equals(ctx)) { 93 return ctx; 94 } 95 Node n = getParentNode(); 97 if (n != null) 98 ctx = n.getHelpCtx(); 99 return ctx; 100 } 101 102 103 public final Action [] getActions(boolean context) { 104 if (isUISettingCategoryNode()) { 105 return new Action [0]; 106 } else { 107 return new Action [] { 108 SystemAction.get(FileSystemAction.class), 109 null, 110 SystemAction.get(PasteAction.class), 111 null, 112 SystemAction.get(MoveUpAction.class), 113 SystemAction.get(MoveDownAction.class), 114 SystemAction.get(ReorderAction.class), 115 null, 116 SystemAction.get(NewTemplateAction.class), 117 null, 118 SystemAction.get(ToolsAction.class), 119 SystemAction.get(PropertiesAction.class), 120 }; 121 } 122 } 123 124 129 130 public final Node.Cookie getCookie (Class type) { 131 if (isUISettingCategoryNode()) return null; 132 return super.getCookie (type); 133 } 134 135 138 public final TemplateWizard getTemplateWizard () { 139 TemplateWizard templateWizard = createWizard (); 140 141 templateWizard.setTemplatesFolder (findFolder (root (), findName (), true)); 142 templateWizard.setTargetFolder (findFolder (root (), findName (), false)); 143 return templateWizard; 144 } 145 146 148 protected TemplateWizard createWizard () { 149 return new TemplateWizard (); 150 } 151 152 155 protected LookupNode createChild (DataFolder folder) { 156 return new LookupNode (folder); 157 } 158 159 162 protected Node createChild (Node node) { 163 return node.cloneNode (); 164 } 165 166 169 protected String root () { 170 return "Services"; } 172 173 176 private static String prefTemplates (String root) { 177 return "Templates/" + root; } 179 180 182 private static String prefObjects (String root) { 183 return root; 184 } 185 186 189 private String findName () { 190 DataFolder df = (DataFolder)getCookie (DataFolder.class); 191 if (df == null) { 192 return ""; 193 } 194 String name = df.getPrimaryFile ().getPath(); 195 if (name.startsWith (prefObjects (root ()))) { 196 name = name.substring (prefObjects (root ()).length ()); 197 } 198 return name; 199 } 200 201 206 static DataFolder findFolder (String root, String name, boolean template) { 207 try { 208 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 209 if (template) { 210 name = '/' + prefTemplates (root) + name; 211 } else { 212 name = '/' + prefObjects (root) + name; 213 } 214 FileObject fo = fs.findResource (name); 215 216 if (fo == null && template) { 217 name = prefTemplates (root); 220 } 221 222 if (fo == null) { 223 fo = FileUtil.createFolder (fs.getRoot (), name); 225 } 226 227 return DataFolder.findFolder (fo); 228 } catch (java.io.IOException ex) { 229 throw (IllegalStateException ) new IllegalStateException (ex.toString()).initCause(ex); 230 } 231 } 232 233 236 public final void refreshKey (Node node) { 237 ((Ch)getChildren()).refreshKey0(node); 238 } 239 240 public boolean canDestroy() { 241 return false; 242 } 243 244 public boolean canCut() { 245 return false; 246 } 247 248 public boolean canCopy() { 249 return false; 250 } 251 252 protected Sheet createSheet() { 253 return new Sheet(); 254 } 255 256 public boolean canRename() { 257 return false; 258 } 259 260 public Node cloneNode () { 261 return new LookupNode((DataFolder)super.getCookie(DataFolder.class)); 262 } 263 264 265 266 private static final class Leaf extends FilterNode { 267 DataObject data; 268 Node parent; 269 270 Leaf (Node node, DataObject data, Node parent) { 271 super(node, ((data instanceof XMLDataObject) || node.isLeaf()) ? Children.LEAF : new FilterNode.Children(node)); 272 this.data = data; 273 this.parent = parent; 274 } 275 276 public boolean equals(Object o) { 278 return this == o || getOriginal().equals(o) || (o != null && o.equals(getOriginal())); 279 } 280 public int hashCode() { 281 return getOriginal().hashCode(); 282 } 283 284 public HelpCtx getHelpCtx() { 285 Object o = data.getPrimaryFile().getAttribute(EA_HELPCTX); 286 if (o != null) { 287 return new HelpCtx(o.toString()); 288 } 289 HelpCtx ctx = getOriginal().getHelpCtx(); 291 if (ctx != null && 292 ctx != HelpCtx.DEFAULT_HELP && 293 !INSTANCE_DEFAULT_HELP.equals(ctx)) { 294 return ctx; 295 } 296 Node n = getParentNode(); 298 if (n == null) 299 n = parent; 300 if (n != null) 301 ctx = n.getHelpCtx(); 302 return ctx; 303 } 304 305 public Action getPreferredAction() { 306 return null; 307 } 308 309 } 310 311 312 315 private static final class Ch extends FilterNode.Children { 316 317 public Ch (DataFolder folder) { 318 super(folder.getNodeDelegate ()); 319 } 320 321 322 void refreshKey0(Node node) { 323 refreshKey(node); 324 } 325 326 331 @Override 332 protected Node[] createNodes(Node node) { 333 DataObject obj = (DataObject)node.getCookie(DataObject.class); 334 336 if ( 337 obj != null && Boolean.TRUE.equals (obj.getPrimaryFile ().getAttribute (EA_HIDDEN)) 338 ) { 339 return new Node[0]; 340 } 341 342 LookupNode parent = (LookupNode)getNode (); 343 344 if (obj != null) { 345 if (obj instanceof DataFolder && node.equals (obj.getNodeDelegate ())) { 346 node = parent.createChild ((DataFolder)obj); 347 return new Node[] { node }; 348 } else if (obj instanceof DataShadow) { 349 DataObject orig = ((DataShadow) obj).getOriginal(); 350 FileObject fo = orig.getPrimaryFile(); 351 352 if (fo.isFolder() && !fo.getChildren(false).hasMoreElements()) return null; 354 355 if (orig instanceof DataFolder) { 356 return new Node[] { 357 parent.createChild ((DataFolder) orig) 358 }; 359 } else { 360 obj = orig; 361 node = orig.getNodeDelegate(); 362 } 363 } 364 node = new Leaf(node, obj, parent); 365 } 366 367 node = parent.createChild (node); 368 369 return new Node[] { node }; 370 } 371 372 } 373 374 } 375 | Popular Tags |