KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.favorites.templates;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.lang.ref.Reference JavaDoc;
27 import java.lang.ref.WeakReference JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import org.openide.awt.Mnemonics;
34 import org.openide.cookies.EditCookie;
35 import org.openide.cookies.OpenCookie;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.util.actions.CallableSystemAction;
39
40 import org.openide.DialogDescriptor;
41 import org.openide.DialogDisplayer;
42 import org.openide.explorer.ExplorerManager;
43 import org.openide.loaders.DataObject;
44 import org.openide.nodes.Node;
45
46 /**
47  *
48  * @author Jiri Rechtacek
49  */

50 public class TemplatesAction extends CallableSystemAction {
51
52     /** Weak reference to the dialog showing singleton Template Manager. */
53     private Reference JavaDoc<Dialog JavaDoc> dialogWRef = new WeakReference JavaDoc<Dialog JavaDoc> (null);
54     
55     public TemplatesAction() {
56         putValue("noIconInMenu", Boolean.TRUE); //NOI18N
57
}
58     
59     public void performAction () {
60         
61         Dialog JavaDoc dialog = dialogWRef.get ();
62
63         if (dialog == null || ! dialog.isShowing ()) {
64
65             final TemplatesPanel tp = new TemplatesPanel ();
66             JButton JavaDoc closeButton = new JButton JavaDoc ();
67             Mnemonics.setLocalizedText (closeButton, NbBundle.getMessage (TemplatesAction.class, "BTN_TemplatesPanel_CloseButton")); // NOI18N
68
JButton JavaDoc openInEditor = new JButton JavaDoc ();
69             openInEditor.setEnabled (false);
70             OpenInEditorListener l = new OpenInEditorListener (tp, openInEditor);
71             openInEditor.addActionListener (l);
72             tp.getExplorerManager ().addPropertyChangeListener (l);
73             Mnemonics.setLocalizedText (openInEditor, NbBundle.getMessage (TemplatesAction.class, "BTN_TemplatesPanel_OpenInEditorButton")); // NOI18N
74
DialogDescriptor dd = new DialogDescriptor (tp,
75                                     NbBundle.getMessage (TemplatesAction.class, "LBL_TemplatesPanel_Title"), // NOI18N
76
false, // modal
77
new Object JavaDoc [] { openInEditor, closeButton },
78                                     closeButton,
79                                     DialogDescriptor.DEFAULT_ALIGN,
80                                     null,
81                                     null);
82             dd.setClosingOptions (null);
83             // set helpctx to null again, DialogDescriptor replaces null with HelpCtx.DEFAULT_HELP
84
dd.setHelpCtx (null);
85             
86             dialog = DialogDisplayer.getDefault ().createDialog (dd);
87             dialog.setVisible (true);
88             dialogWRef = new WeakReference JavaDoc<Dialog JavaDoc> (dialog);
89             
90         } else {
91             dialog.toFront ();
92         }
93         
94     }
95     
96     protected boolean asynchronous() {
97         return true;
98     }
99
100     public String JavaDoc getName () {
101         return NbBundle.getMessage (TemplatesAction.class, "LBL_TemplatesAction_Name"); // NOI18N
102
}
103
104     protected String JavaDoc iconResource () {
105         return null;
106     }
107
108     public HelpCtx getHelpCtx () {
109         return HelpCtx.DEFAULT_HELP;
110     }
111
112     /**
113      * Adding hint.
114      */

115     protected void initialize () {
116     super.initialize ();
117         putProperty (TemplatesAction.SHORT_DESCRIPTION, NbBundle.getMessage (TemplatesAction.class, "HINT_TemplatesAction")); // NOI18N
118
}
119     
120     // helper classes
121
private static class OpenInEditorListener implements ActionListener JavaDoc, PropertyChangeListener JavaDoc {
122         TemplatesPanel tp;
123         JButton JavaDoc b;
124         public OpenInEditorListener (TemplatesPanel panel, JButton JavaDoc button) {
125             tp = panel;
126             b = button;
127         }
128         
129         // ActionListener
130
public void actionPerformed (ActionEvent JavaDoc ev) {
131             Node [] nodes = (Node []) tp.getExplorerManager ().getSelectedNodes ();
132             assert nodes != null && nodes.length > 0 : "Selected templates cannot be null or empty.";
133             Set JavaDoc nodes2open = getNodes2Open (nodes);
134             assert ! nodes2open.isEmpty () : "Selected templates to open cannot by empty for nodes " + Arrays.asList (nodes);
135             Iterator JavaDoc/*<Node>*/ it = nodes2open.iterator ();
136             while (it.hasNext ()) {
137                 Node n = (Node) it.next ();
138                 EditCookie ec = (EditCookie) n.getLookup ().lookup (EditCookie.class);
139                 if (ec != null) {
140                     ec.edit ();
141                 } else {
142                     OpenCookie oc = (OpenCookie) n.getLookup ().lookup (OpenCookie.class);
143                     if (oc != null) {
144                         oc.open ();
145                     } else {
146                         assert false : "Node " + n + " has to have a EditCookie or OpenCookie.";
147                     }
148                 }
149             }
150         }
151
152         // PropertyChangeListener
153
public void propertyChange (java.beans.PropertyChangeEvent JavaDoc evt) {
154             if (ExplorerManager.PROP_SELECTED_NODES.equals (evt.getPropertyName ())) {
155                 Node [] nodes = (Node []) evt.getNewValue ();
156                 boolean res = nodes != null;
157                 int i = 0;
158                 while (res && i < nodes.length) {
159                     Node n = nodes [i];
160                     EditCookie ec = (EditCookie) n.getLookup ().lookup (EditCookie.class);
161                     OpenCookie oc = (OpenCookie) n.getLookup ().lookup (OpenCookie.class);
162                     res = ec != null || oc != null;
163                     
164                     // 65037: Template Manager should not offer to Open in Editor an empty pseudotemplate
165
if (res) {
166                         DataObject dobj = (DataObject) n.getLookup ().lookup (DataObject.class);
167                         assert dobj != null : "DataObject for node " + n;
168                         res = dobj.getPrimaryFile ().getSize () > 0;
169                     }
170                     
171                     i++;
172                 }
173                 b.setEnabled (res);
174             }
175         }
176     }
177     
178     static private Set JavaDoc<Node> getNodes2Open (Node [] nodes) {
179         Set JavaDoc<Node> nodes2open = new HashSet JavaDoc<Node> (nodes.length);
180         for (int i = 0; i < nodes.length; i++) {
181             if (nodes [i].isLeaf ()) {
182                 nodes2open.add (nodes [i]);
183             } else {
184                 nodes2open.addAll (getNodes2Open (nodes [i].getChildren ().getNodes (true)));
185             }
186         }
187         return nodes2open;
188     }
189
190 }
191
Popular Tags