1 19 package org.netbeans.modules.favorites.templates; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyVetoException ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.Arrays ; 28 import java.util.Set ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import javax.swing.Action ; 32 import javax.swing.ActionMap ; 33 import javax.swing.JFileChooser ; 34 import javax.swing.JTree ; 35 import javax.swing.SwingUtilities ; 36 import javax.swing.text.DefaultEditorKit ; 37 import org.openide.actions.CopyAction; 38 import org.openide.actions.CutAction; 39 import org.openide.actions.DeleteAction; 40 import org.openide.actions.PasteAction; 41 import org.openide.actions.RenameAction; 42 import org.openide.explorer.ExplorerManager; 43 import org.openide.explorer.ExplorerUtils; 44 import org.openide.explorer.view.BeanTreeView; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.filesystems.Repository; 48 import org.openide.loaders.DataFilter; 49 import org.openide.loaders.DataFolder; 50 import org.openide.loaders.DataObject; 51 import org.openide.loaders.DataObjectNotFoundException; 52 import org.openide.nodes.Children; 53 import org.openide.nodes.FilterNode; 54 import org.openide.nodes.Index; 55 import org.openide.nodes.Node; 56 import org.openide.util.HelpCtx; 57 import org.openide.util.Lookup; 58 import org.openide.util.NbBundle; 59 import org.openide.util.actions.NodeAction; 60 import org.openide.util.actions.SystemAction; 61 import org.openide.util.lookup.AbstractLookup; 62 import org.openide.util.lookup.InstanceContent; 63 import org.openide.util.lookup.ProxyLookup; 64 import org.openide.windows.TopComponent; 65 66 70 public class TemplatesPanel extends TopComponent implements ExplorerManager.Provider { 71 private ExplorerManager manager; 72 private TemplateTreeView view; 73 private JTree tree; 74 static private Set newlyCreatedFolders; 75 76 static private FileObject templatesRoot; 77 78 79 public TemplatesPanel () { 80 81 ActionMap map = getActionMap (); 82 map.put (DefaultEditorKit.copyAction, ExplorerUtils.actionCopy (getExplorerManager ())); 83 map.put (DefaultEditorKit.cutAction, ExplorerUtils.actionCut (getExplorerManager ())); 84 map.put (DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste (getExplorerManager ())); 85 map.put ("delete", ExplorerUtils.actionDelete (getExplorerManager (), true)); 87 initComponents (); 88 createTemplateView (); 89 treePanel.add (view, BorderLayout.CENTER); 90 91 associateLookup (ExplorerUtils.createLookup (getExplorerManager (), map)); 92 initialize (); 93 94 } 95 96 public ExplorerManager getExplorerManager () { 97 if (manager == null) { 98 manager = new ExplorerManager (); 99 } 100 return manager; 101 } 102 103 private void createTemplateView () { 104 if (view == null) { 105 view = new TemplateTreeView (); 106 } 107 view.setRootVisible (false); 108 view.setPopupAllowed (true); 109 view.setDefaultActionAllowed (false); 110 } 111 112 private static class TemplateTreeView extends BeanTreeView { 113 private Action startEditing; 114 private void invokeInplaceEditing () { 115 if (startEditing == null) { 116 Object o = tree.getActionMap ().get ("startEditing"); if (o != null && o instanceof Action ) { 118 startEditing = (Action ) o; 119 } 120 } 121 assert startEditing != null : "startEditing is on tree ActionMap " + Arrays.asList (tree.getActionMap ().keys ()); 122 startEditing.actionPerformed (new ActionEvent (tree, 0, "startEditing")); } 124 } 125 126 private class SelectionListener implements PropertyChangeListener { 127 public void propertyChange (java.beans.PropertyChangeEvent evt) { 128 if (ExplorerManager.PROP_SELECTED_NODES.equals (evt.getPropertyName ())) { 129 final Node [] nodes = (Node []) evt.getNewValue (); 130 deleteButton.setEnabled (nodes != null && nodes.length > 0); 131 renameButton.setEnabled (nodes != null && nodes.length == 1); 132 duplicateButton.setEnabled (nodes != null && nodes.length == 1 && nodes [0].isLeaf ()); 133 SwingUtilities.invokeLater (new Runnable () { 134 public void run () { 135 moveUpButton.setEnabled (isMoveUpEnabled (nodes)); 136 moveDownButton.setEnabled (isMoveDownEnabled (nodes)); 137 } 138 }); 139 } 140 } 141 } 142 143 private void initialize () { 144 getExplorerManager ().setRootContext (getTemplateRootNode ()); 145 getExplorerManager ().addPropertyChangeListener (new SelectionListener ()); 146 deleteButton.setEnabled (false); 147 renameButton.setEnabled (false); 148 duplicateButton.setEnabled (false); 149 moveUpButton.setEnabled (false); 150 moveDownButton.setEnabled (false); 151 addButton.setEnabled (true); 152 } 153 154 static Node getTemplateRootNode () { 155 DataFolder df = DataFolder.findFolder (getTemplatesRoot ()); 156 return new TemplateNode (new FilterNode (df.getNodeDelegate (), df.createNodeChildren (new TemplateFilter ()))); 157 } 158 159 static private final class TemplateFilter implements DataFilter { 160 public boolean acceptDataObject (DataObject obj) { 161 return acceptTemplate (obj); 162 } 163 164 private boolean acceptTemplate (DataObject d) { 165 if (d.isTemplate () || d instanceof DataFolder) { 166 Object o = d.getPrimaryFile ().getAttribute ("simple"); return o == null || Boolean.TRUE.equals (o); 168 } else { 169 return false; 170 } 171 } 172 } 173 174 179 private void initComponents() { 181 java.awt.GridBagConstraints gridBagConstraints; 182 183 templatesLabel = new javax.swing.JLabel (); 184 treePanel = new javax.swing.JPanel (); 185 buttonsPanel = new javax.swing.JPanel (); 186 addButton = new javax.swing.JButton (); 187 newFolderButton = new javax.swing.JButton (); 188 jSeparator1 = new javax.swing.JSeparator (); 189 duplicateButton = new javax.swing.JButton (); 190 renameButton = new javax.swing.JButton (); 191 jSeparator2 = new javax.swing.JSeparator (); 192 moveUpButton = new javax.swing.JButton (); 193 moveDownButton = new javax.swing.JButton (); 194 jSeparator3 = new javax.swing.JSeparator (); 195 deleteButton = new javax.swing.JButton (); 196 197 setPreferredSize(new java.awt.Dimension (500, 300)); 198 setLayout(new java.awt.GridBagLayout ()); 199 200 org.openide.awt.Mnemonics.setLocalizedText(templatesLabel, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("LBL_TemplatesPanel_TemplatesLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 202 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 203 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 204 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 205 add(templatesLabel, gridBagConstraints); 206 207 treePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 208 treePanel.setLayout(new java.awt.BorderLayout ()); 209 gridBagConstraints = new java.awt.GridBagConstraints (); 210 gridBagConstraints.gridx = 0; 211 gridBagConstraints.gridy = 1; 212 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 213 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 214 gridBagConstraints.weightx = 1.0; 215 gridBagConstraints.weighty = 1.0; 216 gridBagConstraints.insets = new java.awt.Insets (0, 12, 11, 0); 217 add(treePanel, gridBagConstraints); 218 219 buttonsPanel.setLayout(new java.awt.GridBagLayout ()); 220 221 org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_Add")); addButton.setToolTipText(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "TT_TemplatesPanel_Add")); addButton.addActionListener(new java.awt.event.ActionListener () { 224 public void actionPerformed(java.awt.event.ActionEvent evt) { 225 addButtonActionPerformed(evt); 226 } 227 }); 228 gridBagConstraints = new java.awt.GridBagConstraints (); 229 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 230 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 231 gridBagConstraints.insets = new java.awt.Insets (0, 8, 3, 0); 232 buttonsPanel.add(addButton, gridBagConstraints); 233 addButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_Add")); 235 org.openide.awt.Mnemonics.setLocalizedText(newFolderButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_NewFolder")); newFolderButton.addActionListener(new java.awt.event.ActionListener () { 237 public void actionPerformed(java.awt.event.ActionEvent evt) { 238 newFolderButtonActionPerformed(evt); 239 } 240 }); 241 gridBagConstraints = new java.awt.GridBagConstraints (); 242 gridBagConstraints.gridx = 0; 243 gridBagConstraints.gridy = 1; 244 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 245 gridBagConstraints.insets = new java.awt.Insets (0, 8, 3, 0); 246 buttonsPanel.add(newFolderButton, gridBagConstraints); 247 newFolderButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_New")); 249 gridBagConstraints = new java.awt.GridBagConstraints (); 250 gridBagConstraints.gridx = 0; 251 gridBagConstraints.gridy = 2; 252 gridBagConstraints.insets = new java.awt.Insets (4, 8, 4, 0); 253 buttonsPanel.add(jSeparator1, gridBagConstraints); 254 255 org.openide.awt.Mnemonics.setLocalizedText(duplicateButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_DuplicateButton")); duplicateButton.addActionListener(new java.awt.event.ActionListener () { 257 public void actionPerformed(java.awt.event.ActionEvent evt) { 258 duplicateButtonActionPerformed(evt); 259 } 260 }); 261 gridBagConstraints = new java.awt.GridBagConstraints (); 262 gridBagConstraints.gridx = 0; 263 gridBagConstraints.gridy = 3; 264 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 265 gridBagConstraints.insets = new java.awt.Insets (0, 8, 3, 0); 266 buttonsPanel.add(duplicateButton, gridBagConstraints); 267 duplicateButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_Duplicate")); 269 org.openide.awt.Mnemonics.setLocalizedText(renameButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_RenameButton")); renameButton.addActionListener(new java.awt.event.ActionListener () { 271 public void actionPerformed(java.awt.event.ActionEvent evt) { 272 renameButtonActionPerformed(evt); 273 } 274 }); 275 gridBagConstraints = new java.awt.GridBagConstraints (); 276 gridBagConstraints.gridx = 0; 277 gridBagConstraints.gridy = 4; 278 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 279 gridBagConstraints.insets = new java.awt.Insets (0, 8, 3, 0); 280 buttonsPanel.add(renameButton, gridBagConstraints); 281 renameButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_Rename")); 283 gridBagConstraints = new java.awt.GridBagConstraints (); 284 gridBagConstraints.gridx = 0; 285 gridBagConstraints.gridy = 8; 286 gridBagConstraints.insets = new java.awt.Insets (4, 8, 4, 0); 287 buttonsPanel.add(jSeparator2, gridBagConstraints); 288 289 org.openide.awt.Mnemonics.setLocalizedText(moveUpButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_MoveUp")); moveUpButton.addActionListener(new java.awt.event.ActionListener () { 291 public void actionPerformed(java.awt.event.ActionEvent evt) { 292 moveUpButtonActionPerformed(evt); 293 } 294 }); 295 gridBagConstraints = new java.awt.GridBagConstraints (); 296 gridBagConstraints.gridx = 0; 297 gridBagConstraints.gridy = 6; 298 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 299 gridBagConstraints.insets = new java.awt.Insets (0, 8, 3, 0); 300 buttonsPanel.add(moveUpButton, gridBagConstraints); 301 moveUpButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_MoveUp")); 303 org.openide.awt.Mnemonics.setLocalizedText(moveDownButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_MoveDown")); moveDownButton.addActionListener(new java.awt.event.ActionListener () { 305 public void actionPerformed(java.awt.event.ActionEvent evt) { 306 moveDownButtonActionPerformed(evt); 307 } 308 }); 309 gridBagConstraints = new java.awt.GridBagConstraints (); 310 gridBagConstraints.gridx = 0; 311 gridBagConstraints.gridy = 7; 312 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 313 gridBagConstraints.insets = new java.awt.Insets (0, 8, 3, 0); 314 buttonsPanel.add(moveDownButton, gridBagConstraints); 315 moveDownButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_MoveDown")); 317 gridBagConstraints = new java.awt.GridBagConstraints (); 318 gridBagConstraints.gridx = 0; 319 gridBagConstraints.gridy = 5; 320 gridBagConstraints.insets = new java.awt.Insets (4, 8, 4, 0); 321 buttonsPanel.add(jSeparator3, gridBagConstraints); 322 323 org.openide.awt.Mnemonics.setLocalizedText(deleteButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_Delete")); deleteButton.addActionListener(new java.awt.event.ActionListener () { 325 public void actionPerformed(java.awt.event.ActionEvent evt) { 326 deleteButtonActionPerformed(evt); 327 } 328 }); 329 gridBagConstraints = new java.awt.GridBagConstraints (); 330 gridBagConstraints.gridx = 0; 331 gridBagConstraints.gridy = 9; 332 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 333 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 334 gridBagConstraints.insets = new java.awt.Insets (0, 8, 3, 0); 335 buttonsPanel.add(deleteButton, gridBagConstraints); 336 deleteButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_Delete")); 338 gridBagConstraints = new java.awt.GridBagConstraints (); 339 gridBagConstraints.gridx = 1; 340 gridBagConstraints.gridy = 1; 341 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 342 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 343 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 344 gridBagConstraints.insets = new java.awt.Insets (0, 11, 11, 8); 345 add(buttonsPanel, gridBagConstraints); 346 } 348 private void moveDownButtonActionPerformed (java.awt.event.ActionEvent evt) { moveDown (manager.getSelectedNodes ()); } 351 352 private void moveUpButtonActionPerformed (java.awt.event.ActionEvent evt) { moveUp (manager.getSelectedNodes ()); } 355 356 private void newFolderButtonActionPerformed (java.awt.event.ActionEvent evt) { 358 DataFolder df = doNewFolder (manager.getSelectedNodes ()); 359 assert df != null : "New DataFolder has been created."; 360 361 try { 362 Node [] nodes = manager.getSelectedNodes (); 364 Node targerNode = null; 365 if (nodes == null || nodes.length == 0) { 366 targerNode = manager.getRootContext (); 367 } else { 368 targerNode = nodes [0].isLeaf () ? nodes [0].getParentNode () : nodes [0]; 369 } 370 371 targerNode.getChildren ().getNodes (true); 372 Node newSubfolder = targerNode.getChildren ().findChild (df.getName ()); 373 assert newSubfolder != null : "Node for subfolder found in nodes: " + Arrays.asList (targerNode.getChildren ().getNodes ()); 374 manager.setSelectedNodes (new Node [] { newSubfolder }); 375 view.invokeInplaceEditing (); 376 } catch (PropertyVetoException pve) { 377 Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, pve); 378 } 379 } 381 382 private void deleteButtonActionPerformed (java.awt.event.ActionEvent evt) { Node [] nodes = manager.getSelectedNodes (); 384 for (int i = 0; i < nodes.length; i++) { 385 try { 386 nodes[i].destroy(); 387 } 388 catch (IOException ioe) { 389 Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, ioe); 390 } 391 } } 393 394 private void duplicateButtonActionPerformed (java.awt.event.ActionEvent evt) { Node [] nodes = manager.getSelectedNodes (); 396 assert nodes != null : "Selected Nodes cannot be null."; 397 assert nodes.length == 1 : "One one node can be selected, but was " + Arrays.asList (nodes); 398 createDuplicateFromNode (nodes [0]); 399 } 401 402 private void renameButtonActionPerformed (java.awt.event.ActionEvent evt) { Node [] nodes = manager.getSelectedNodes (); 404 assert nodes != null : "Selected Nodes cannot be null."; 405 assert nodes.length == 1 : "One one node can be selected, but was " + Arrays.asList (nodes); 406 view.invokeInplaceEditing (); } 408 409 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { doAdd (manager.getSelectedNodes ()); } 412 413 414 private javax.swing.JButton addButton; 416 private javax.swing.JPanel buttonsPanel; 417 private javax.swing.JButton deleteButton; 418 private javax.swing.JButton duplicateButton; 419 private javax.swing.JSeparator jSeparator1; 420 private javax.swing.JSeparator jSeparator2; 421 private javax.swing.JSeparator jSeparator3; 422 private javax.swing.JButton moveDownButton; 423 private javax.swing.JButton moveUpButton; 424 private javax.swing.JButton newFolderButton; 425 private javax.swing.JButton renameButton; 426 private javax.swing.JLabel templatesLabel; 427 private javax.swing.JPanel treePanel; 428 430 private static class TemplateNode extends FilterNode { 431 432 private static Action [] ACTIONS_ON_LEAF = new Action [] { 433 SystemAction.get (CutAction.class), 434 SystemAction.get (CopyAction.class), 435 SystemAction.get (PasteAction.class), 436 null, 437 SystemAction.get (DeleteAction.class), 438 SystemAction.get (RenameAction.class), 439 }; 440 441 private static Action [] ACTIONS_ON_FOLDER = new Action [] { 442 SystemAction.get (AddAction.class), 443 SystemAction.get (NewFolderAction.class), 444 null, 445 SystemAction.get (CutAction.class), 446 SystemAction.get (CopyAction.class), 447 SystemAction.get (PasteAction.class), 448 null, 449 SystemAction.get (DeleteAction.class), 450 SystemAction.get (RenameAction.class), 451 }; 452 453 public TemplateNode (Node n) { 454 this (n, new DataFolderFilterChildren (n), new InstanceContent ()); 455 } 456 457 public TemplateNode (Node n, org.openide.nodes.Children ch) { 458 this (n, ch, new InstanceContent ()); 459 } 460 461 private TemplateNode (Node originalNode, org.openide.nodes.Children ch, InstanceContent content) { 462 super (originalNode, ch, 463 new ProxyLookup (new Lookup [] { new AbstractLookup (content), originalNode.getLookup () } ) 464 ); 465 466 DataObject dobj = getDOFromNode (originalNode); 467 468 if (dobj.isValid ()) { 470 DataFolder folder = null; 471 if (dobj instanceof DataFolder) { 472 folder = (DataFolder) dobj; 473 } else { 474 if (dobj.getPrimaryFile ().getParent () != null && dobj.getPrimaryFile ().getParent ().isValid ()) { 476 folder = dobj.getFolder (); 477 } 478 } 479 if (folder != null) { 480 content.add (new DataFolder.Index (folder, this)); 481 } 482 } 483 484 content.add (this); 485 } 486 public Action [] getActions (boolean context) { 487 return isLeaf () ? ACTIONS_ON_LEAF : ACTIONS_ON_FOLDER; 488 } 489 490 public Action getPreferredAction () { 491 return null; 492 } 493 494 } 495 496 private static class DataFolderFilterChildren extends FilterNode.Children { 497 public DataFolderFilterChildren (Node n) { 498 super (n); 499 } 500 501 protected Node[] createNodes(Node key) { 502 Node n = (Node) key; 503 Node [] orig = super.createNodes (key); 504 Node [] filtered = new Node [orig.length]; 505 for (int i = 0; i < orig.length; i++) { 506 DataObject dobj = getDOFromNode (orig [i]); 507 if (dobj.isTemplate ()) { 508 filtered [i] = new TemplateNode (orig [i], Children.LEAF); 509 } else { 510 filtered [i] = new TemplateNode (orig [i]); 511 } 512 } 513 return filtered; 514 } 515 516 } 517 518 static private DataObject getDOFromNode (Node n) { 519 DataObject dobj = (DataObject) n.getLookup ().lookup (DataObject.class); 520 assert dobj != null : "DataObject for node " + n; 521 return dobj; 522 } 523 524 static private DataFolder getTargetFolder (Node [] nodes) { 525 DataFolder folder = null; 526 if (nodes == null || nodes.length == 0) { 527 folder = DataFolder.findFolder (getTemplatesRoot ()); 528 } else { 529 folder = (DataFolder) nodes [0].getLookup ().lookup (DataFolder.class); 531 532 if (folder == null && nodes [0].isLeaf ()) { 534 Node parent = nodes [0].getParentNode (); 535 folder = (DataFolder) parent.getLookup ().lookup (DataFolder.class); 536 } 537 } 538 return folder; 539 } 540 541 static DataObject createTemplateFromFile (File file, DataFolder preferred) { 542 if (file == null) { 543 throw new IllegalArgumentException ("Argument file cannot be null!"); } 545 FileObject sourceFO = FileUtil.toFileObject (FileUtil.normalizeFile (file)); 546 assert sourceFO != null : "FileObject found for file " + file; 547 DataObject sourceDO = null; 548 try { 549 sourceDO = DataObject.find (sourceFO); 550 } catch (DataObjectNotFoundException donfe) { 551 Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, donfe); 552 } 553 assert sourceDO != null : "DataObject found for FileObject " + sourceFO; 554 DataFolder folder = preferred == null ? DataFolder.findFolder (getTemplatesRoot ()) : preferred; 555 DataObject template = null; 556 try { 557 template = sourceDO.copy (folder); 558 template.setTemplate (true); 559 } catch (IOException ioe) { 560 Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, ioe); 561 } 562 return template; 563 } 564 565 private static void doAdd (Node [] nodes) { 566 JFileChooser chooser = new JFileChooser (); 567 chooser.setDialogTitle (NbBundle.getBundle(TemplatesPanel.class).getString("LBL_TemplatesPanel_JFileChooser_Title")); chooser.setApproveButtonText (NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_JFileChooser_AddButtonName")); chooser.setFileHidingEnabled (false); 570 chooser.setMultiSelectionEnabled (false); 571 int result = chooser.showOpenDialog (null); 572 if (JFileChooser.APPROVE_OPTION == result) { 573 File f = chooser.getSelectedFile (); 574 assert f != null; 575 DataObject newDO = createTemplateFromFile (f, getTargetFolder (nodes)); 576 } 577 } 578 579 private static DataFolder doNewFolder (Node [] nodes) { 580 DataFolder df = null; 581 582 DataFolder pref = getTargetFolder (nodes); 584 if (pref == null) { 585 pref = DataFolder.findFolder (getTemplatesRoot ()); 586 assert pref != null : "DataFolder found for FO " + getTemplatesRoot (); 587 } 588 589 try { 590 df = DataFolder.create (pref, NbBundle.getBundle(TemplatesPanel.class).getString("TXT_TemplatesPanel_NewFolderName")); assert df != null : "New subfolder found in folder " + pref; 592 } catch (IOException ioe) { 593 Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, ioe); 594 } 595 596 return df; 597 } 598 599 static DataObject createDuplicateFromNode (Node n) { 600 DataObject source = getDOFromNode (n); 601 try { 602 return source.copy (source.getFolder ()); 603 } catch (IOException ioe) { 604 Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, ioe); 605 } 606 return null; 607 } 608 609 static FileObject getTemplatesRoot () { 610 if (templatesRoot == null) { 611 templatesRoot = Repository.getDefault ().getDefaultFileSystem ().findResource ("Templates"); } 613 return templatesRoot; 614 } 615 616 private boolean isMoveUpEnabled (Node [] nodes) { 617 if (nodes == null || nodes.length != 1 || ! nodes [0].isLeaf ()) { 618 return false; 619 } 620 621 int pos = getNodePosition (nodes [0]); 622 return pos != -1 && pos > 0; 623 } 624 625 private boolean isMoveDownEnabled (Node [] nodes) { 626 if (nodes == null || nodes.length != 1 || ! nodes [0].isLeaf ()) { 627 return false; 628 } 629 int count = nodes [0].getParentNode ().getChildren ().getNodesCount (); 630 int pos = getNodePosition (nodes [0]); 631 return pos != -1 && pos < (count - 1); 632 } 633 634 private int getNodePosition (Node n) { 635 Index supp = getIndexSupport (n); 636 637 DataFolder df = (DataFolder) n.getParentNode ().getLookup ().lookup (DataFolder.class); 638 df.getNodeDelegate ().getChildren ().getNodes (true); 639 640 int pos = supp.indexOf (n); 641 642 assert pos != -1 : "Node " + n + " has position " + pos + " in children " + Arrays.asList (n.getParentNode ().getChildren ().getNodes ()); 643 644 return pos; 645 } 646 647 private Index getIndexSupport (Node n) { 648 Node parent = n.getParentNode (); 649 assert parent != null : "Node " + n + " has a parent."; 650 651 Index index = (Index) parent.getLookup ().lookup (Index.class); 652 assert index != null : "Node " + parent + " has Index cookie."; 653 654 return index; 655 } 656 657 private void moveUp (Node[] nodes) { 658 assert nodes != null : "Nodes to moveUp cannot be null."; 659 assert nodes.length == 1 : "Only one node can be moveUp, not " + Arrays.asList (nodes); 660 assert nodes [0].isLeaf () : "Only leaf node can be moveUp, not " + nodes [0]; 661 Node n = nodes [0]; 662 663 Index supp = getIndexSupport (n); 664 int origPos = getNodePosition (n); 665 666 if (origPos == -1) { 668 return ; 669 } 670 671 supp.moveUp (origPos); 672 assert origPos - 1 == getNodePosition (n) : "Node " + n + " has been moved from " + origPos + " to pos " + getNodePosition (n); 673 } 674 675 private void moveDown (Node[] nodes) { 676 assert nodes != null : "Nodes to moveDown cannot be null."; 677 assert nodes.length == 1 : "Only one node can be moveDown, not " + Arrays.asList (nodes); 678 assert nodes [0].isLeaf () : "Only leaf node can be moveDown, not " + nodes [0]; 679 Node n = nodes [0]; 680 681 Index supp = getIndexSupport (n); 682 int origPos = getNodePosition (n); 683 684 if (origPos == -1) { 686 return ; 687 } 688 689 supp.moveDown (origPos); 690 assert origPos + 1 == getNodePosition (n) : "Node " + n + " has been moved from " + origPos + " to pos " + getNodePosition (n); 691 } 692 693 private static class AddAction extends NodeAction { 695 protected void performAction (Node[] activatedNodes) { 696 doAdd (activatedNodes); 697 } 698 699 protected boolean enable (Node[] activatedNodes) { 700 return activatedNodes != null && activatedNodes.length == 1; 701 } 702 703 public String getName () { 704 return NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_Add"); } 706 707 public HelpCtx getHelpCtx () { 708 return null; 709 } 710 711 protected boolean asynchronous () { 712 return true; 713 } 714 } 715 716 private static class NewFolderAction extends NodeAction { 717 protected void performAction (Node[] activatedNodes) { 718 doNewFolder (activatedNodes); 719 } 720 721 protected boolean enable (Node[] activatedNodes) { 722 return activatedNodes != null && activatedNodes.length == 1; 723 } 724 725 public String getName () { 726 return NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_NewFolder"); } 728 729 public HelpCtx getHelpCtx () { 730 return null; 731 } 732 733 protected boolean asynchronous () { 734 return true; 735 } 736 } 737 738 } 739 | Popular Tags |