1 19 20 package org.netbeans.modules.favorites; 21 22 import java.awt.Image ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import javax.swing.Action ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 33 import org.netbeans.api.queries.VisibilityQuery; 34 import org.openide.actions.CopyAction; 35 import org.openide.actions.CutAction; 36 import org.openide.actions.DeleteAction; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileStateInvalidException; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.filesystems.Repository; 41 import org.openide.loaders.DataFolder; 42 import org.openide.loaders.DataObject; 43 import org.openide.loaders.DataShadow; 44 import org.openide.nodes.FilterNode; 45 import org.openide.nodes.Node; 46 import org.openide.util.Exceptions; 47 import org.openide.util.NbBundle; 48 import org.openide.util.Utilities; 49 50 54 final class Favorites extends FilterNode { 55 56 private static Node node; 57 58 private static Node root; 59 60 61 private Favorites(Node node) { 62 super(node, new Chldrn (node, false)); 63 } 64 65 public Image getIcon (int type) { 66 return Utilities.loadImage("org/netbeans/modules/favorites/resources/actionSelect.gif"); } 68 69 public Image getOpenedIcon (int type) { 70 return Utilities.loadImage("org/netbeans/modules/favorites/resources/actionSelect.gif"); } 72 73 public boolean canCopy () { 74 return false; 75 } 76 77 public boolean canCut () { 78 return false; 79 } 80 81 public boolean canRename () { 82 return false; 83 } 84 85 public static DataFolder getFolder () { 86 try { 87 FileObject fo = FileUtil.createFolder ( 88 Repository.getDefault().getDefaultFileSystem().getRoot(), 89 "Favorites" ); 91 DataFolder folder = DataFolder.findFolder(fo); 92 return folder; 93 } catch (IOException ex) { 94 Exceptions.printStackTrace(ex); 95 return DataFolder.findFolder ( 96 Repository.getDefault().getDefaultFileSystem().getRoot() 97 ); 98 } 99 100 } 101 102 104 public static synchronized Node getNode () { 105 if (node == null) { 106 node = new Favorites (getFolder().getNodeDelegate ()); 107 } 108 return node; 109 } 110 111 113 public static URL getHome () 114 throws FileStateInvalidException, MalformedURLException { 115 String s = System.getProperty("user.home"); 117 File home = new File (s); 118 home = FileUtil.normalizeFile (home); 119 120 return home.toURI ().toURL (); 121 } 122 123 125 static File fileForNode (Node n) { 126 DataObject obj = (DataObject)n.getCookie (DataObject.class); 127 if (obj == null) return null; 128 129 return FileUtil.toFile ( 130 obj.getPrimaryFile() 131 ); 132 } 133 134 public Handle getHandle () { 135 return new RootHandle (); 136 } 137 138 public Action [] getActions(boolean context) { 139 return new Action [] {Actions.addOnFavoritesNode()}; 140 } 141 142 private static class RootHandle implements Node.Handle { 143 static final long serialVersionUID = 1907300072945111595L; 144 145 147 public Node getNode () { 148 return Favorites.getNode (); 149 } 150 } 151 152 private static class Chldrn extends FilterNode.Children 153 implements ChangeListener , Runnable { 154 private ChangeListener weak; 155 private boolean hideHidden; 156 157 public Chldrn (Node node, boolean hideHidden) { 158 super (node); 159 this.hideHidden = hideHidden; 160 161 weak = org.openide.util.WeakListeners.change(this, VisibilityQuery.getDefault()); 162 VisibilityQuery.getDefault().addChangeListener(weak); 163 } 164 165 protected Node[] createNodes(Node node) { 166 if (hideHidden) { 167 DataObject obj = (DataObject)node.getCookie(DataObject.class); 168 if (obj != null && !VisibilityQuery.getDefault().isVisible(obj.getPrimaryFile())) { 169 return null; 170 } 171 } 172 173 return new Node[] { new ProjectFilterNode ( 174 node, 175 (node.isLeaf ()) ? org.openide.nodes.Children.LEAF : new Chldrn (node, true) 176 )}; 177 } 178 179 public void stateChanged(ChangeEvent e) { 180 MUTEX.postWriteRequest(this); 181 } 182 183 public void run() { 184 Node[] arr = original.getChildren().getNodes(); 185 for (int i = 0; i < arr.length; i++) { 186 refreshKey(arr[i]); 187 } 188 } 189 } 191 195 private static class ProjectFilterNode extends FilterNode { 196 197 198 public ProjectFilterNode (Node node, org.openide.nodes.Children children) { 199 super (node, children); 200 } 201 202 public String getDisplayName () { 203 if (Favorites.getNode().equals(this.getParentNode())) { 205 DataShadow ds = (DataShadow) getCookie(DataShadow.class); 206 if (ds != null) { 207 String name = ds.getOriginal().getName(); 208 String path = FileUtil.getFileDisplayName(ds.getOriginal().getPrimaryFile()); 209 return NbBundle.getMessage(Favorites.class, "CTL_DisplayNameTemplate", name, path); 210 } else { 211 return super.getDisplayName(); 212 } 213 } else { 214 return super.getDisplayName(); 215 } 216 223 } 224 225 public String getHtmlDisplayName() { 227 return getOriginal().getHtmlDisplayName(); 228 } 229 230 public boolean canDestroy () { 231 boolean canDestroy = super.canDestroy (); 232 DataShadow link = (DataShadow) getCookie (DataShadow.class); 233 234 if (canDestroy && isDeleteOriginal (link)) { 237 canDestroy = link.getOriginal ().isDeleteAllowed (); 238 } 239 240 return canDestroy; 241 } 242 243 public void destroy () throws IOException { 244 if (canDestroy ()) { 245 DataShadow link = (DataShadow) getCookie (DataShadow.class); 246 DataObject original = isDeleteOriginal (link) ? link.getOriginal () : null; 247 248 super.destroy (); 249 250 if (original != null) { 251 original.delete (); 252 } 253 } 254 } 255 256 private boolean isDeleteOriginal (DataShadow link) { 257 return false; 258 } 259 260 public Action [] getActions(boolean context) { 261 Action [] arr; 262 arr = super.getActions(context); 263 264 boolean isRoot = false; 266 DataObject dataObject = (DataObject) getCookie(DataObject.class); 267 if (dataObject != null) { 268 FileObject fo = dataObject.getPrimaryFile(); 269 if (fo != null) { 270 File file = FileUtil.toFile(fo); 272 if (file != null) { 273 if (file.getParent() == null) { 274 isRoot = true; 275 } 276 } 277 } 278 } 279 280 if (isRoot) { 281 return createActionsForRoot(arr); 282 } else { 283 if (Favorites.getNode().equals(this.getParentNode())) { 284 DataShadow ds = (DataShadow) getCookie(DataShadow.class); 285 if (ds != null) { 286 if (ds.getOriginal().getPrimaryFile().isFolder()) { 287 return createActionsForFavoriteFolder(arr); 288 } else { 289 return createActionsForFavoriteFile(arr); 290 } 291 } 292 } else { 293 DataObject dObj = (DataObject) getCookie(DataObject.class); 294 if (dObj != null) { 295 if (dObj.getPrimaryFile().isFolder()) { 296 return createActionsForFolder(arr); 297 } else { 298 return createActionsForFile(arr); 299 } 300 } 301 } 302 } 303 return arr; 305 } 306 307 308 private Action [] createActionsForRoot (Action [] arr) { 309 return arr; 311 } 312 313 314 private Action [] createActionsForFavoriteFolder (Action [] arr) { 315 boolean added = false; 316 List <Action > newArr = new ArrayList <Action >(); 317 for (int i = 0; i < arr.length; i++) { 318 if (!added && ((arr[i] instanceof CopyAction) || (arr[i] instanceof CutAction))) { 320 added = true; 321 newArr.add(Actions.remove()); 322 newArr.add(null); 323 } 324 if (!(arr[i] instanceof DeleteAction)) { 326 newArr.add(arr[i]); 327 } 328 } 329 if (!added) { 330 added = true; 331 newArr.add(null); 332 newArr.add(Actions.remove()); 333 } 334 335 return newArr.toArray (new Action [newArr.size()]); 336 } 337 338 339 private Action [] createActionsForFavoriteFile (Action [] arr) { 340 boolean added = false; 341 List <Action > newArr = new ArrayList <Action >(); 342 for (int i = 0; i < arr.length; i++) { 343 if (!added && ((arr[i] instanceof CopyAction) || (arr[i] instanceof CutAction))) { 345 added = true; 346 newArr.add(Actions.remove()); 347 newArr.add(null); 348 } 349 if (!(arr[i] instanceof DeleteAction)) { 351 newArr.add(arr[i]); 352 } 353 } 354 if (!added) { 355 added = true; 356 newArr.add(null); 357 newArr.add(Actions.remove()); 358 } 359 return newArr.toArray (new Action [newArr.size()]); 360 } 361 362 363 private Action [] createActionsForFolder (Action [] arr) { 364 boolean added = false; 365 List <Action > newArr = new ArrayList <Action >(); 366 for (int i = 0; i < arr.length; i++) { 367 if (!added && ((arr[i] instanceof CopyAction) || (arr[i] instanceof CutAction))) { 369 added = true; 370 newArr.add(Actions.add()); 371 newArr.add(null); 372 } 373 newArr.add(arr[i]); 374 } 375 if (!added) { 376 added = true; 377 newArr.add(null); 378 newArr.add(Actions.add()); 379 } 380 return newArr.toArray (new Action [newArr.size()]); 381 } 382 383 384 private Action [] createActionsForFile (Action [] arr) { 385 boolean added = false; 386 List <Action > newArr = new ArrayList <Action >(); 387 for (int i = 0; i < arr.length; i++) { 388 if (!added && ((arr[i] instanceof CopyAction) || (arr[i] instanceof CutAction))) { 390 added = true; 391 newArr.add(Actions.add()); 392 newArr.add(null); 393 } 394 newArr.add(arr[i]); 395 } 396 if (!added) { 397 added = true; 398 newArr.add(null); 399 newArr.add(Actions.add()); 400 } 401 return newArr.toArray (new Action [newArr.size()]); 402 } 403 404 } 405 } 406 | Popular Tags |