KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > favorites > templates > TemplatesPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.favorites.templates;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyVetoException JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.swing.Action JavaDoc;
32 import javax.swing.ActionMap JavaDoc;
33 import javax.swing.JFileChooser JavaDoc;
34 import javax.swing.JTree JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
36 import javax.swing.text.DefaultEditorKit JavaDoc;
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 /**
67  *
68  * @author Jiri Rechtacek
69  */

70 public class TemplatesPanel extends TopComponent implements ExplorerManager.Provider {
71     private ExplorerManager manager;
72     private TemplateTreeView view;
73     private JTree JavaDoc tree;
74     static private Set JavaDoc newlyCreatedFolders;
75     
76     static private FileObject templatesRoot;
77     
78     /** Creates new form TemplatesPanel */
79     public TemplatesPanel () {
80         
81         ActionMap JavaDoc 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)); // NOI18N
86

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 JavaDoc startEditing;
114         private void invokeInplaceEditing () {
115             if (startEditing == null) {
116                 Object JavaDoc o = tree.getActionMap ().get ("startEditing"); // NOI18N
117
if (o != null && o instanceof Action JavaDoc) {
118                     startEditing = (Action JavaDoc) o;
119                 }
120             }
121             assert startEditing != null : "startEditing is on tree ActionMap " + Arrays.asList (tree.getActionMap ().keys ());
122             startEditing.actionPerformed (new ActionEvent JavaDoc (tree, 0, "startEditing")); // NOI18N
123
}
124     }
125     
126     private class SelectionListener implements PropertyChangeListener JavaDoc {
127         public void propertyChange (java.beans.PropertyChangeEvent JavaDoc 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 JavaDoc () {
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 JavaDoc o = d.getPrimaryFile ().getAttribute ("simple"); // NOI18N
167
return o == null || Boolean.TRUE.equals (o);
168             } else {
169                 return false;
170             }
171         }
172     }
173     
174     /** This method is called from within the constructor to
175      * initialize the form.
176      * WARNING: Do NOT modify this code. The content of this method is
177      * always regenerated by the Form Editor.
178      */

179     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
180
private void initComponents() {
181         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
182
183         templatesLabel = new javax.swing.JLabel JavaDoc();
184         treePanel = new javax.swing.JPanel JavaDoc();
185         buttonsPanel = new javax.swing.JPanel JavaDoc();
186         addButton = new javax.swing.JButton JavaDoc();
187         newFolderButton = new javax.swing.JButton JavaDoc();
188         jSeparator1 = new javax.swing.JSeparator JavaDoc();
189         duplicateButton = new javax.swing.JButton JavaDoc();
190         renameButton = new javax.swing.JButton JavaDoc();
191         jSeparator2 = new javax.swing.JSeparator JavaDoc();
192         moveUpButton = new javax.swing.JButton JavaDoc();
193         moveDownButton = new javax.swing.JButton JavaDoc();
194         jSeparator3 = new javax.swing.JSeparator JavaDoc();
195         deleteButton = new javax.swing.JButton JavaDoc();
196
197         setPreferredSize(new java.awt.Dimension JavaDoc(500, 300));
198         setLayout(new java.awt.GridBagLayout JavaDoc());
199
200         org.openide.awt.Mnemonics.setLocalizedText(templatesLabel, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("LBL_TemplatesPanel_TemplatesLabel")); // NOI18N
201
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
202         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
203         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
204         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
205         add(templatesLabel, gridBagConstraints);
206
207         treePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
208         treePanel.setLayout(new java.awt.BorderLayout JavaDoc());
209         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(0, 12, 11, 0);
217         add(treePanel, gridBagConstraints);
218
219         buttonsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
220
221         org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_Add")); // NOI18N
222
addButton.setToolTipText(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "TT_TemplatesPanel_Add")); // NOI18N
223
addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
224             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
225                 addButtonActionPerformed(evt);
226             }
227         });
228         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
229         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
230         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
231         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 8, 3, 0);
232         buttonsPanel.add(addButton, gridBagConstraints);
233         addButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_Add")); // NOI18N
234

235         org.openide.awt.Mnemonics.setLocalizedText(newFolderButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_NewFolder")); // NOI18N
236
newFolderButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
237             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
238                 newFolderButtonActionPerformed(evt);
239             }
240         });
241         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
242         gridBagConstraints.gridx = 0;
243         gridBagConstraints.gridy = 1;
244         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
245         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 8, 3, 0);
246         buttonsPanel.add(newFolderButton, gridBagConstraints);
247         newFolderButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_New")); // NOI18N
248

249         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
250         gridBagConstraints.gridx = 0;
251         gridBagConstraints.gridy = 2;
252         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
256
duplicateButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
257             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
258                 duplicateButtonActionPerformed(evt);
259             }
260         });
261         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
262         gridBagConstraints.gridx = 0;
263         gridBagConstraints.gridy = 3;
264         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
265         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 8, 3, 0);
266         buttonsPanel.add(duplicateButton, gridBagConstraints);
267         duplicateButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_Duplicate")); // NOI18N
268

269         org.openide.awt.Mnemonics.setLocalizedText(renameButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_RenameButton")); // NOI18N
270
renameButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
271             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
272                 renameButtonActionPerformed(evt);
273             }
274         });
275         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
276         gridBagConstraints.gridx = 0;
277         gridBagConstraints.gridy = 4;
278         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
279         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 8, 3, 0);
280         buttonsPanel.add(renameButton, gridBagConstraints);
281         renameButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_Rename")); // NOI18N
282

283         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
284         gridBagConstraints.gridx = 0;
285         gridBagConstraints.gridy = 8;
286         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
290
moveUpButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
291             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
292                 moveUpButtonActionPerformed(evt);
293             }
294         });
295         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
296         gridBagConstraints.gridx = 0;
297         gridBagConstraints.gridy = 6;
298         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
299         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 8, 3, 0);
300         buttonsPanel.add(moveUpButton, gridBagConstraints);
301         moveUpButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_MoveUp")); // NOI18N
302

303         org.openide.awt.Mnemonics.setLocalizedText(moveDownButton, org.openide.util.NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_MoveDown")); // NOI18N
304
moveDownButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
305             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
306                 moveDownButtonActionPerformed(evt);
307             }
308         });
309         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
310         gridBagConstraints.gridx = 0;
311         gridBagConstraints.gridy = 7;
312         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
313         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 8, 3, 0);
314         buttonsPanel.add(moveDownButton, gridBagConstraints);
315         moveDownButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_MoveDown")); // NOI18N
316

317         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
318         gridBagConstraints.gridx = 0;
319         gridBagConstraints.gridy = 5;
320         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
324
deleteButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
325             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
326                 deleteButtonActionPerformed(evt);
327             }
328         });
329         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(0, 8, 3, 0);
335         buttonsPanel.add(deleteButton, gridBagConstraints);
336         deleteButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplatesPanel.class, "ACD_TemplatesPanel_Delete")); // NOI18N
337

338         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(0, 11, 11, 8);
345         add(buttonsPanel, gridBagConstraints);
346     }// </editor-fold>//GEN-END:initComponents
347

348     private void moveDownButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
349
moveDown (manager.getSelectedNodes ());//GEN-LAST:event_moveDownButtonActionPerformed
350
}
351
352     private void moveUpButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
353
moveUp (manager.getSelectedNodes ());//GEN-LAST:event_moveUpButtonActionPerformed
354
}
355     
356     private void newFolderButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_newFolderButtonActionPerformed
357

358         DataFolder df = doNewFolder (manager.getSelectedNodes ());
359         assert df != null : "New DataFolder has been created.";
360         
361         try {
362             // invoke inplace editing
363
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 JavaDoc pve) {
377             Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, pve);
378         }
379 //GEN-LAST:event_newFolderButtonActionPerformed
380
}
381     
382     private void deleteButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_deleteButtonActionPerformed
383
Node [] nodes = manager.getSelectedNodes ();
384         for (int i = 0; i < nodes.length; i++) {
385             try {
386                 nodes[i].destroy();
387             }
388             catch (IOException JavaDoc ioe) {
389                 Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, ioe);
390             }
391         }//GEN-LAST:event_deleteButtonActionPerformed
392
}
393
394     private void duplicateButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_duplicateButtonActionPerformed
395
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 //GEN-LAST:event_duplicateButtonActionPerformed
400
}
401     
402     private void renameButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_renameButtonActionPerformed
403
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 ();//GEN-LAST:event_renameButtonActionPerformed
407
}
408     
409     private void addButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
410
doAdd (manager.getSelectedNodes ());//GEN-LAST:event_addButtonActionPerformed
411
}
412     
413     
414     // Variables declaration - do not modify//GEN-BEGIN:variables
415
private javax.swing.JButton JavaDoc addButton;
416     private javax.swing.JPanel JavaDoc buttonsPanel;
417     private javax.swing.JButton JavaDoc deleteButton;
418     private javax.swing.JButton JavaDoc duplicateButton;
419     private javax.swing.JSeparator JavaDoc jSeparator1;
420     private javax.swing.JSeparator JavaDoc jSeparator2;
421     private javax.swing.JSeparator JavaDoc jSeparator3;
422     private javax.swing.JButton JavaDoc moveDownButton;
423     private javax.swing.JButton JavaDoc moveUpButton;
424     private javax.swing.JButton JavaDoc newFolderButton;
425     private javax.swing.JButton JavaDoc renameButton;
426     private javax.swing.JLabel JavaDoc templatesLabel;
427     private javax.swing.JPanel JavaDoc treePanel;
428     // End of variables declaration//GEN-END:variables
429

430     private static class TemplateNode extends FilterNode {
431
432         private static Action JavaDoc [] ACTIONS_ON_LEAF = new Action JavaDoc [] {
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 JavaDoc [] ACTIONS_ON_FOLDER = new Action JavaDoc [] {
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             // #69623: IllegalArgumentException when call getFolder() on an unvalid DataObject
469
if (dobj.isValid ()) {
470                 DataFolder folder = null;
471                 if (dobj instanceof DataFolder) {
472                     folder = (DataFolder) dobj;
473                 } else {
474                     // check parent
475
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 JavaDoc [] getActions (boolean context) {
487             return isLeaf () ? ACTIONS_ON_LEAF : ACTIONS_ON_FOLDER;
488         }
489         
490         public Action JavaDoc 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             // try if has a data folder (alert: leaf node can be a empty folder)
530
folder = (DataFolder) nodes [0].getLookup ().lookup (DataFolder.class);
531             
532             // if not this node then try its parent
533
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 JavaDoc file, DataFolder preferred) {
542         if (file == null) {
543             throw new IllegalArgumentException JavaDoc ("Argument file cannot be null!"); // NOI18N
544
}
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 JavaDoc 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 JavaDoc chooser = new JFileChooser JavaDoc ();
567         chooser.setDialogTitle (NbBundle.getBundle(TemplatesPanel.class).getString("LBL_TemplatesPanel_JFileChooser_Title")); // NOI18N
568
chooser.setApproveButtonText (NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_JFileChooser_AddButtonName")); // NOI18N
569
chooser.setFileHidingEnabled (false);
570         chooser.setMultiSelectionEnabled (false);
571         int result = chooser.showOpenDialog (null);
572         if (JFileChooser.APPROVE_OPTION == result) {
573             File JavaDoc 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         // new folder
583
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")); // NOI18N
591
assert df != null : "New subfolder found in folder " + pref;
592         } catch (IOException JavaDoc 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 JavaDoc 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"); // NOI18N
612
}
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         // workaround issue 62192, don't try to move on broken index
667
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         // workaround issue 62192, don't try to move on broken index
685
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     // action
694
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 JavaDoc getName () {
704             return NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_Add"); // NOI18N
705
}
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 JavaDoc getName () {
726             return NbBundle.getBundle(TemplatesPanel.class).getString("BTN_TemplatesPanel_NewFolder"); // NOI18N
727
}
728
729         public HelpCtx getHelpCtx () {
730             return null;
731         }
732         
733         protected boolean asynchronous () {
734             return true;
735         }
736     }
737     
738 }
739
Popular Tags