KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.netbeans.Module;
26 import org.openide.DialogDisplayer;
27 import org.openide.NotifyDescriptor;
28 import org.openide.nodes.Node;
29 import org.openide.util.NbBundle;
30 import org.openide.util.RequestProcessor;
31
32 /**
33  *
34  * @author Jiri Rechtacek
35  */

36 class ModuleNodeUtils {
37
38     private ModuleNodeUtils() {}
39
40     static private Set JavaDoc<ModuleNode.Item> getCategoryModules (Node category) {
41         assert ! category.isLeaf () : "Parent " + category + " cannot be leaf.";
42         Set JavaDoc<ModuleNode.Item> modules = new HashSet JavaDoc<ModuleNode.Item>();
43         for (Node child : category.getChildren().getNodes(true)) {
44             if (child instanceof ModuleNode.Item) {
45                 modules.add((ModuleNode.Item) child);
46             }
47         }
48         return modules;
49     }
50     
51     static Set JavaDoc<ModuleNode.Item> getAllSelectedModuleItems(Node[] nodes) {
52         Set JavaDoc<ModuleNode.Item> modules = new HashSet JavaDoc<ModuleNode.Item>();
53         
54         for (Node node : nodes) {
55             if (node instanceof ModuleNode.Item) {
56                 modules.add((ModuleNode.Item) node);
57             } else if (!node.isLeaf()) {
58                 modules.addAll(getCategoryModules(node));
59             }
60         }
61         
62         return modules;
63     }
64     
65     static Set JavaDoc<ModuleBean> getAllSelectedModuleBeans(Node[] nodes) {
66         Set JavaDoc<ModuleBean> beans = new HashSet JavaDoc<ModuleBean>();
67         
68         for (Node node : nodes) {
69             if (node instanceof ModuleNode.Item) {
70                 beans.add(((ModuleNode.Item) node).getItem());
71             } else if (!node.isLeaf()) {
72                 beans.addAll(getAllSelectedModuleBeans(node.getChildren().getNodes(true)));
73             }
74         }
75         
76         return beans;
77     }
78     
79     static Boolean JavaDoc isEnableCandidate (Node [] nodes) {
80         Boolean JavaDoc res = null;
81         for (ModuleBean b : getAllSelectedModuleBeans(nodes)) {
82             if (res == null) {
83                 res = !b.isEnabled();
84             } else {
85                 if (res == b.isEnabled()) {
86                     // mixed value
87
return null;
88                 }
89             }
90         }
91         return res;
92     }
93     
94     static boolean isEnableAllowed (Module m) {
95         if (! m.isValid ()) return false;
96         return ! ( ! (m.getProblems ().isEmpty ()) || m.isAutoload () || m.isEager () || m.getJarFile () == null );
97     }
98     
99     static String JavaDoc getUninstallActionName (Node [] activatedNodes, String JavaDoc oldName) {
100         if (activatedNodes == null || activatedNodes.length == 0) {
101             return oldName;
102         }
103         
104         String JavaDoc name;
105         // special handling of category
106
if (activatedNodes.length == 1 && ! (activatedNodes [0] instanceof ModuleNode.Item)) {
107             name = NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_UninstallAction", // NOI18N
108
activatedNodes [0].getDisplayName ()); // NOI18N
109
} else {
110             Set JavaDoc<ModuleNode.Item> items = ModuleNodeUtils.getAllSelectedModuleItems (activatedNodes);
111             name = NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_UninstallAction", // NOI18N
112
items.size () > 1 ?
113                             NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_UninstallAction_many") : // NOI18N
114
items.iterator().next().getDisplayName());
115         }
116         
117         return name;
118     }
119     
120     static boolean isUninstallAllowed (Module m) {
121         return ! (m.isAutoload () || m.isEager () || m.isFixed ());
122     }
123     
124     static boolean canUninstall (Node[] activatedNodes) {
125         if (activatedNodes == null || activatedNodes.length == 0) {
126             return false;
127         }
128
129         Set JavaDoc<ModuleNode.Item> items = ModuleNodeUtils.getAllSelectedModuleItems(activatedNodes);
130         if (items.isEmpty()) {
131             return false;
132         }
133
134         for (ModuleNode.Item item : items) {
135             if (!item.canDestroy()) {
136                 return false;
137             }
138         }
139
140         return true;
141     }
142
143     static void doUninstall (final Node[] activatedNodes) {
144         if (confirmUninstall (activatedNodes)) {
145             ModuleSelectionPanel.getGUI (false).setWaitingState (true, true);
146             RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
147                 public void run () {
148                     uninstallNodes (activatedNodes);
149                 }
150             });
151         }
152     }
153     
154     static boolean confirmUninstall (Node[] activatedNodes) {
155         assert activatedNodes != null : "Any ModuleNode must be selected";
156
157         Set JavaDoc<ModuleNode.Item> items = ModuleNodeUtils.getAllSelectedModuleItems(activatedNodes);
158
159         assert ! items.isEmpty () : "Any module must be selected";
160
161         String JavaDoc category = null;
162         if (activatedNodes.length == 1 && ! (activatedNodes [0] instanceof ModuleNode.Item)) {
163             category = activatedNodes [0].getDisplayName ();
164         }
165
166         Set JavaDoc<Module> modules = new HashSet JavaDoc<Module>(items.size());
167         for (ModuleNode.Item item : items) {
168             modules.add(item.getItem().getModule());
169         }
170
171         Component JavaDoc c = new ModuleUninstallPanel (modules, category);
172     c.getAccessibleContext ().setAccessibleDescription (
173             NbBundle.getMessage (ModuleSelectionPanel.class, "ACD_ModuleUninstallPanel_form")); // NOI18N
174

175         NotifyDescriptor nd = new NotifyDescriptor.Confirmation (c,
176                         NbBundle.getMessage (ModuleSelectionPanel.class, "CTL_ModuleUninstallPanel_UninstallConfirmation"),
177                         NotifyDescriptor.YES_NO_OPTION);
178
179         return NotifyDescriptor.YES_OPTION.equals (DialogDisplayer.getDefault ().notify (nd));
180     }
181
182     static private void uninstallNodes (Node[] nodes) {
183         assert nodes != null : "uninstallNodes cannot be called on null nodes.";
184         for (Node node : nodes) {
185             if (node.isLeaf()) {
186                 ((ModuleNode.Item) node).uninstall();
187             } else {
188                 uninstallNodes(node.getChildren().getNodes(true));
189             }
190         }
191     }
192     
193 }
194
Popular Tags