KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > catalog > ModuleNodeActions


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.autoupdate.catalog;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.JMenu JavaDoc;
27 import javax.swing.JMenuItem JavaDoc;
28 import javax.swing.JRadioButtonMenuItem JavaDoc;
29 import org.netbeans.Module;
30 import org.openide.awt.Mnemonics;
31 import org.openide.nodes.Node;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.NbBundle;
34 import org.openide.util.actions.NodeAction;
35 import org.openide.util.actions.Presenter;
36
37 /**
38  * @author Jiri Rechtacek, Jesse Glick
39  */

40 public class ModuleNodeActions {
41     
42     public static class EnableDisableAction extends NodeAction {
43         private String JavaDoc name;
44
45         protected void performAction (final Node[] activatedNodes) {
46             assert activatedNodes != null : "Cannot performAction when activatedNodes is null";
47             final Boolean JavaDoc b = ModuleNodeUtils.isEnableCandidate (activatedNodes);
48             assert b != null : "Cannot performAction if mixed statuses on activatedNodes " + Arrays.asList (activatedNodes);
49             
50             // fix of 62480: join all enable/disable to one group
51
ModuleBean.AllModulesBean.getDefault ().pause ();
52             Set JavaDoc<ModuleBean> beans = ModuleNodeUtils.getAllSelectedModuleBeans(activatedNodes);
53             for (ModuleBean bean : beans) {
54                 bean.setEnabled (b.booleanValue ());
55             }
56             ModuleBean.AllModulesBean.getDefault ().resume ();
57         }
58
59         protected boolean enable (Node[] activatedNodes) {
60             if (activatedNodes == null) {
61                 return false;
62             }
63             
64             Set JavaDoc<ModuleNode.Item> items = ModuleNodeUtils.getAllSelectedModuleItems(activatedNodes);
65
66             boolean allowed = ! items.isEmpty ();
67             String JavaDoc moduleName = ""; // NOI18N
68
for (ModuleNode.Item item : items) {
69                 Module m = item.getItem().getModule();
70                 moduleName = m.getDisplayName ();
71                 if (!ModuleNodeUtils.isEnableAllowed(m)) {
72                     allowed = false;
73                     break;
74                 }
75             }
76             
77             // set action's name
78
Boolean JavaDoc b = ModuleNodeUtils.isEnableCandidate (activatedNodes);
79
80             // special handling of category
81
if (activatedNodes.length == 1 && ! (activatedNodes [0] instanceof ModuleNode.Item)) {
82                 name = NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_format", // NOI18N
83
b == null || b.booleanValue () ? NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_enable") : // NOI18N
84
NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_disable"), // NOI18N
85
activatedNodes[0].getDisplayName ()); // NOI18N
86
} else {
87                 name = NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_format", // NOI18N
88
b == null || b.booleanValue () ? NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_enable") : // NOI18N
89
NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_disable"), // NOI18N
90
items.size () > 1 ? NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_many") : moduleName); // NOI18N
91
}
92             
93             return allowed && (ModuleNodeUtils.isEnableCandidate (activatedNodes) != null);
94         }
95
96         public String JavaDoc getName() {
97             return name;
98         }
99
100         public HelpCtx getHelpCtx() {
101             //return new HelpCtx (ModuleNodeActions.EnableAllAction.class);
102
return null;
103         }
104
105         protected boolean asynchronous() {
106             return true;
107         }
108     }
109
110     public static class EnableAllAction extends NodeAction {
111
112         protected void performAction (Node[] activatedNodes) {
113             assert activatedNodes != null : "Cannot performAction when activatedNodes is null";
114             
115             // fix of 62480: join all enable/disable to one group
116
ModuleBean.AllModulesBean.getDefault ().pause ();
117             for (ModuleBean bean : ModuleBean.AllModulesBean.getDefault().getModules()) {
118                 if (!bean.isEnabled() && ModuleNodeUtils.isEnableAllowed(bean.getModule())) {
119                     bean.setEnabled(true);
120                 }
121             }
122             ModuleBean.AllModulesBean.getDefault ().resume ();
123         }
124
125         protected boolean enable (Node[] activatedNodes) {
126             // XXX: don't enable if all modules are enabled
127
return activatedNodes != null && activatedNodes.length > 0;
128         }
129
130         public String JavaDoc getName() {
131             return NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableAllAction"); // NOI18N
132
}
133
134         public HelpCtx getHelpCtx() {
135             //return new HelpCtx (ModuleNodeActions.EnableDisableAction.class);
136
return null;
137         }
138
139         protected boolean asynchronous() {
140             return true;
141         }
142     }
143
144     public static class UninstallAction extends NodeAction {
145         private String JavaDoc name;
146         
147         protected void performAction (Node[] activatedNodes) {
148             ModuleNodeUtils.doUninstall (activatedNodes);
149         }
150
151         protected boolean enable (Node[] activatedNodes) {
152             name = ModuleNodeUtils.getUninstallActionName (activatedNodes, name);
153             return ModuleNodeUtils.canUninstall (activatedNodes);
154         }
155
156         public String JavaDoc getName() {
157             return name;
158         }
159
160         public HelpCtx getHelpCtx() {
161             //return new HelpCtx (ModuleNodeActions.UninstallAction.class);
162
return null;
163         }
164
165         protected boolean asynchronous() {
166             return true;
167         }
168     }
169
170     public static class SortAction extends NodeAction implements Presenter.Popup {
171         
172         // private fields
173
JMenu JavaDoc subMenu;
174         JRadioButtonMenuItem JavaDoc sortByCategory;
175         JRadioButtonMenuItem JavaDoc sortByName;
176         JRadioButtonMenuItem JavaDoc sortByCluster;
177                 
178         protected void performAction (Node[] activatedNodes) {
179         }
180
181         protected boolean enable (Node[] activatedNodes) {
182             return true;
183         }
184
185         public String JavaDoc getName () {
186             return NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_SortAction"); // NOI18N
187
}
188
189         public HelpCtx getHelpCtx () {
190             return null;
191         }
192         
193         public JMenuItem JavaDoc getPopupPresenter() {
194             return getSubmenuPopupPresenter();
195         }
196         
197         public JMenuItem JavaDoc getSubmenuPopupPresenter() {
198             if (subMenu == null) {
199                 
200                 subMenu = new JMenu JavaDoc ();
201                 Mnemonics.setLocalizedText (subMenu, NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_SortAction")); // NOI18N
202

203                 sortByCategory = new JRadioButtonMenuItem JavaDoc();
204                 Mnemonics.setLocalizedText (sortByCategory, NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_SortByCategory")); // NOI18N
205
sortByCategory.addActionListener (new MenuListener ());
206                 subMenu.add (sortByCategory);
207                 
208                 sortByName = new JRadioButtonMenuItem JavaDoc();
209                 Mnemonics.setLocalizedText (sortByName, NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_SortByName")); // NOI18N
210
sortByName.addActionListener (new MenuListener ());
211                 subMenu.add (sortByName);
212                 
213                 sortByCluster = new JRadioButtonMenuItem JavaDoc();
214                 Mnemonics.setLocalizedText(sortByCluster, NbBundle.getMessage(ModuleNodeActions.class, "CTL_ModuleNodeActions_SortByCluster"));
215                 sortByCluster.addActionListener(new MenuListener());
216                 subMenu.add(sortByCluster);
217                 
218             }
219             sortByCategory.setSelected(ModuleNode.getModuleSortMode() == ModuleNode.SortMode.BY_CATEGORY);
220             sortByName.setSelected(ModuleNode.getModuleSortMode() == ModuleNode.SortMode.BY_DISPLAY_NAME);
221             sortByCluster.setSelected(ModuleNode.getModuleSortMode() == ModuleNode.SortMode.BY_CLUSTER);
222             return subMenu;
223         }
224         
225         private class MenuListener implements ActionListener JavaDoc {
226
227             public void actionPerformed (ActionEvent JavaDoc e) {
228                 JMenuItem JavaDoc source = (JMenuItem JavaDoc)e.getSource ();
229                 
230                 if (sortByCategory.equals (source)) {
231                     ModuleNode.setModuleSortMode(ModuleNode.SortMode.BY_CATEGORY);
232                 } else if (sortByName.equals (source)) {
233                     ModuleNode.setModuleSortMode(ModuleNode.SortMode.BY_DISPLAY_NAME);
234                 } else if (sortByCluster.equals(source)) {
235                     ModuleNode.setModuleSortMode(ModuleNode.SortMode.BY_CLUSTER);
236                 } else {
237                     assert false : "Invalid source " + source + " in actionPerformed()";
238                 }
239
240             }
241
242         }
243
244
245     }
246 }
247
Popular Tags