1 19 20 package org.netbeans.modules.favorites; 21 22 import java.awt.Image ; 23 import java.awt.event.ActionEvent ; 24 import java.beans.PropertyVetoException ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import javax.swing.AbstractAction ; 30 import javax.swing.Action ; 31 import javax.swing.ImageIcon ; 32 import javax.swing.JFileChooser ; 33 import javax.swing.JMenuItem ; 34 import javax.swing.SwingUtilities ; 35 import org.openide.DialogDisplayer; 36 import org.openide.NotifyDescriptor; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileStateInvalidException; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.loaders.DataFolder; 41 import org.openide.loaders.DataObject; 42 import org.openide.loaders.DataObjectNotFoundException; 43 import org.openide.loaders.DataShadow; 44 import org.openide.nodes.Node; 45 import org.openide.util.Exceptions; 46 import org.openide.util.HelpCtx; 47 import org.openide.util.NbBundle; 48 import org.openide.util.Utilities; 49 import org.openide.util.actions.NodeAction; 50 import org.openide.windows.TopComponent; 51 import org.openide.windows.WindowManager; 52 53 56 public final class Actions extends Object { 57 58 60 private static File currentDir = null; 61 62 private Actions () { 63 } 65 66 public static Action view () { return View.getDefault(); } 67 public static Action add () { return Add.getDefault(); } 68 public static Action addOnFavoritesNode () { return AddOnFavoritesNode.getDefault(); } 69 public static Action remove () { return Remove.getDefault(); } 70 public static Action select () { return Select.getDefault(); } 71 72 77 private static class View extends AbstractAction implements HelpCtx.Provider { 78 79 private static final View VIEW = new View (); 80 81 public View() { 82 putValue(NAME, NbBundle.getMessage(Actions.class, 83 "ACT_View")); 84 Image image = Utilities.loadImage("org/netbeans/modules/favorites/resources/actionView.gif"); putValue(SMALL_ICON, image != null ? new ImageIcon (image) : null); 86 } 87 88 public static Action getDefault () { 89 return VIEW; 90 } 91 92 public void actionPerformed(ActionEvent evt) { 93 final TopComponent projectsTab = Tab.findDefault(); 94 projectsTab.open(); 95 projectsTab.requestActive(); 96 } 97 98 public HelpCtx getHelpCtx() { 99 return new HelpCtx(View.class); 100 } 101 } 103 104 107 private static class Select extends NodeAction { 108 private static final Select SELECT = new Select (); 109 110 public static Action getDefault () { 111 return SELECT; 112 } 113 114 private Select () { 115 super(); 116 putValue("noIconInMenu", Boolean.TRUE); } 118 119 protected void performAction(Node[] activatedNodes) { 120 Tab proj = Tab.findDefault(); 121 proj.open(); 122 proj.requestActive(); 123 proj.doSelectNode((DataObject)activatedNodes[0].getCookie(DataObject.class)); 124 } 125 126 protected boolean enable(Node[] activatedNodes) { 127 if (activatedNodes.length != 1) { 128 return false; 129 } 130 return true; 131 138 } 139 140 public String getName() { 141 return NbBundle.getMessage(Select.class, "ACT_Select_Main_Menu"); } 143 144 145 public JMenuItem getPopupPresenter() { 146 JMenuItem mi = super.getPopupPresenter(); 147 mi.setText(NbBundle.getMessage(Select.class, "ACT_Select")); return mi; 149 } 150 151 protected String iconResource() { 152 return "org/netbeans/modules/favorites/resources/actionSelect.gif"; } 154 155 public HelpCtx getHelpCtx() { 156 return null; 157 } 158 159 protected boolean asynchronous() { 160 return false; 161 } 162 163 } 165 168 private static class Remove extends NodeAction { 169 static final long serialVersionUID =-6471281373153172312L; 170 171 173 private static final Remove REMOVE = new Remove (); 174 175 public static Action getDefault () { 176 return REMOVE; 177 } 178 179 181 public boolean enable (Node[] arr) { 182 if ((arr == null) || (arr.length == 0)) return false; 183 184 for (int i = 0; i < arr.length; i++) { 185 DataObject shad = (DataObject) arr[i].getCookie (DataObject.class); 186 if (shad == null || shad.getFolder() != Favorites.getFolder()) { 188 return false; 189 } 190 } 191 return true; 192 } 193 194 198 public String getName() { 199 return NbBundle.getMessage ( 200 Actions.class, "ACT_Remove"); } 202 203 206 public HelpCtx getHelpCtx() { 207 return new HelpCtx(Remove.class); 208 } 209 210 215 protected void performAction (Node[] arr) { 216 for (int i = 0; i < arr.length; i++) { 217 DataObject shad = (DataObject) arr[i].getCookie(DataObject.class); 218 219 if (shad != null && shad.getFolder() == Favorites.getFolder()) { 220 try { 221 shad.delete(); 222 } 223 catch (IOException ex) { 224 Exceptions.printStackTrace(ex); 225 } 226 } 227 } 228 } 229 230 protected boolean asynchronous() { 231 return false; 232 } 233 234 } 236 241 public static class Add extends NodeAction { 242 static final long serialVersionUID =-6471281373153172312L; 243 244 private static final Add ADD = new Add (); 246 247 public static Action getDefault () { 248 return ADD; 249 } 250 251 private Add () { 252 putValue("noIconInMenu", Boolean.TRUE); } 254 255 257 public boolean enable (Node[] arr) { 258 if ((arr == null) || (arr.length == 0)) return false; 259 if (arr.length == 1 && arr[0] instanceof Favorites) return true; 260 261 262 263 for (int i = 0; i < arr.length; i++) { 264 DataObject dataObject = (DataObject) arr[i].getCookie (DataObject.class); 265 if (dataObject == null) { 267 return false; 268 } 269 FileObject fo = dataObject.getPrimaryFile(); 270 if (fo != null) { 271 if (isInFavorites(fo)) { 273 return false; 274 } 275 File file = FileUtil.toFile(fo); 277 if (file != null) { 278 if (file.getParent() == null) { 279 return false; 281 } 282 } 283 } 284 285 try { 287 if(dataObject.getPrimaryFile().getFileSystem().isDefault()) { 288 return false; 289 } 290 } catch(FileStateInvalidException fsie) { 291 return false; 292 } 293 } 294 return true; 295 } 296 297 300 private boolean isInFavorites (FileObject fo) { 301 DataFolder f = Favorites.getFolder(); 302 303 DataObject [] arr = f.getChildren(); 304 for (int i = 0; i < arr.length; i++) { 305 if (arr[i] instanceof DataShadow) { 306 if (fo.equals(((DataShadow) arr[i]).getOriginal().getPrimaryFile())) { 307 return true; 308 } 309 } 310 } 311 return false; 312 } 313 314 318 public String getName() { 319 return NbBundle.getMessage ( 320 Actions.class, "ACT_Add"); } 322 323 326 public HelpCtx getHelpCtx() { 327 return new HelpCtx(Add.class); 328 } 329 330 335 protected void performAction (final Node[] activatedNodes) { 336 final DataFolder f = Favorites.getFolder(); 337 final DataObject [] arr = f.getChildren(); 338 final List <DataObject> listAdd = new ArrayList <DataObject>(); 339 340 DataObject createdDO = null; 341 Node[] toShadows = activatedNodes; 342 343 try { 344 if (activatedNodes.length == 1 && activatedNodes[0] instanceof Favorites) { 345 FileObject fo = chooseFileObject(); 347 if (fo == null) return; 348 toShadows = new Node[] {DataObject.find(fo).getNodeDelegate()}; 349 } 350 351 352 createdDO = createShadows(f, toShadows, listAdd); 353 354 reorderAfterAddition(f, arr, listAdd); 356 selectAfterAddition(createdDO); 357 } catch (DataObjectNotFoundException e) { 358 Exceptions.printStackTrace(e); 359 } 360 } 361 362 366 private static FileObject chooseFileObject() { 367 FileObject retVal = null; 368 File chooserSelection = null; 369 JFileChooser chooser = new JFileChooser (); 370 chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES ); 371 chooser.setDialogTitle(NbBundle.getBundle(Actions.class).getString ("CTL_DialogTitle")); 372 chooser.setApproveButtonText(NbBundle.getBundle(Actions.class).getString ("CTL_ApproveButtonText")); 373 if (currentDir != null) { 374 chooser.setCurrentDirectory(currentDir); 375 } 376 int option = chooser.showOpenDialog( WindowManager.getDefault().getMainWindow() ); if ( option == JFileChooser.APPROVE_OPTION ) { 378 chooserSelection = chooser.getSelectedFile(); 379 File selectedFile = FileUtil.normalizeFile(chooserSelection); 380 if (!selectedFile.exists()) { 382 if ((selectedFile.getParentFile() != null) && selectedFile.getParentFile().exists()) { 383 if (selectedFile.getName().equals(selectedFile.getParentFile().getName())) { 384 selectedFile = selectedFile.getParentFile(); 385 } 386 } 387 } 388 if (!selectedFile.exists()) { 390 String message = NbBundle.getMessage(Actions.class,"ERR_FileDoesNotExist",selectedFile.getPath()); 391 String title = NbBundle.getMessage(Actions.class,"ERR_FileDoesNotExistDlgTitle"); 392 DialogDisplayer.getDefault().notify 393 (new NotifyDescriptor(message,title,NotifyDescriptor.DEFAULT_OPTION, 394 NotifyDescriptor.INFORMATION_MESSAGE, new Object [] { NotifyDescriptor.CLOSED_OPTION }, 395 NotifyDescriptor.OK_OPTION)); 396 } else { 397 retVal = FileUtil.toFileObject(selectedFile); 398 assert retVal != null; 399 } 400 } 401 currentDir = chooser.getCurrentDirectory(); 402 return retVal; 403 } 404 405 private void selectAfterAddition(final DataObject createdDO) { 406 final Tab projectsTab = Tab.findDefault(); 407 projectsTab.open(); 408 projectsTab.requestActive(); 409 if (createdDO != null) { 411 Node n = Favorites.getNode(); 412 Node [] nodes = projectsTab.getExplorerManager().getRootContext().getChildren().getNodes(true); 413 final Node [] toSelect = new Node[1]; 414 boolean setSelected = false; 415 for (int i = 0; i < nodes.length; i++) { 416 if (createdDO.getName().equals(nodes[i].getName())) { 417 toSelect[0] = nodes[i]; 418 setSelected = true; 419 break; 420 } 421 } 422 if (setSelected) { 423 SwingUtilities.invokeLater(new Runnable () { 424 public void run() { 425 try { 426 projectsTab.getExplorerManager().setExploredContextAndSelection(toSelect[0],toSelect); 427 } catch (PropertyVetoException ex) { 428 } 430 } 431 }); 432 } 433 } 434 } 435 436 private static DataObject createShadows(final DataFolder favourities, final Node[] activatedNodes, final List <DataObject> listAdd) { 437 DataObject createdDO = null; 438 for (int i = 0; i < activatedNodes.length; i++) { 439 DataObject obj = (DataObject) activatedNodes[i].getCookie(DataObject.class); 440 441 if (obj != null) { 442 try { 443 if (createdDO == null) { 444 createdDO = obj.createShadow(favourities); 446 listAdd.add(createdDO); 447 } else { 448 listAdd.add(obj.createShadow(favourities)); 449 } 450 } 451 catch (IOException ex) { 452 Exceptions.printStackTrace(ex); 453 } 454 } 455 } 456 return createdDO; 457 } 458 459 private static void reorderAfterAddition(final DataFolder favourities, final DataObject[] children, final List <DataObject> listAdd) { 460 List <DataObject> listDest = new ArrayList <DataObject>(); 461 if (listAdd.size() > 0) { 462 DataObject root = null; 464 for (int i = 0; i < children.length; i++) { 466 FileObject fo = children[i].getPrimaryFile(); 467 if ("Favorites/Root.instance".equals(fo.getPath())) { root = children[i]; 469 } 470 } 471 if (root != null) { 472 for (int i = 0; i < children.length; i++) { 473 if (!root.equals(children[i])) { 474 listDest.add(children[i]); 475 } 476 } 477 listDest.addAll(listAdd); 478 listDest.add(root); 479 } else { 480 for (int i = 0; i < children.length; i++) { 482 listDest.add(children[i]); 483 } 484 listDest.addAll(listAdd); 485 } 486 DataObject [] newOrder = listDest.toArray(new DataObject[listDest.size()]); 488 try { 489 favourities.setOrder(newOrder); 490 } catch (IOException ex) { 491 Exceptions.printStackTrace(ex); 492 } 493 } 494 } 495 496 protected boolean asynchronous() { 497 return false; 498 } 499 500 } 502 506 public static class AddOnFavoritesNode extends Add { 507 static final long serialVersionUID =-6471284573153172312L; 508 509 private static final AddOnFavoritesNode ADD_ON_FAVORITES_NODE = new AddOnFavoritesNode (); 510 511 public static Action getDefault () { 512 return ADD_ON_FAVORITES_NODE; 513 } 514 515 519 public String getName() { 520 return NbBundle.getMessage ( 521 Actions.class, "ACT_AddOnFavoritesNode"); } 523 } 524 525 } 526 | Popular Tags |