1 19 20 package org.netbeans.modules.autoupdate.catalog; 21 22 import java.awt.Component ; 23 import java.util.HashSet ; 24 import java.util.Set ; 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 36 class ModuleNodeUtils { 37 38 private ModuleNodeUtils() {} 39 40 static private Set <ModuleNode.Item> getCategoryModules (Node category) { 41 assert ! category.isLeaf () : "Parent " + category + " cannot be leaf."; 42 Set <ModuleNode.Item> modules = new HashSet <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 <ModuleNode.Item> getAllSelectedModuleItems(Node[] nodes) { 52 Set <ModuleNode.Item> modules = new HashSet <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 <ModuleBean> getAllSelectedModuleBeans(Node[] nodes) { 66 Set <ModuleBean> beans = new HashSet <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 isEnableCandidate (Node [] nodes) { 80 Boolean res = null; 81 for (ModuleBean b : getAllSelectedModuleBeans(nodes)) { 82 if (res == null) { 83 res = !b.isEnabled(); 84 } else { 85 if (res == b.isEnabled()) { 86 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 getUninstallActionName (Node [] activatedNodes, String oldName) { 100 if (activatedNodes == null || activatedNodes.length == 0) { 101 return oldName; 102 } 103 104 String name; 105 if (activatedNodes.length == 1 && ! (activatedNodes [0] instanceof ModuleNode.Item)) { 107 name = NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_UninstallAction", activatedNodes [0].getDisplayName ()); } else { 110 Set <ModuleNode.Item> items = ModuleNodeUtils.getAllSelectedModuleItems (activatedNodes); 111 name = NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_UninstallAction", items.size () > 1 ? 113 NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_UninstallAction_many") : 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 <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 () { 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 <ModuleNode.Item> items = ModuleNodeUtils.getAllSelectedModuleItems(activatedNodes); 158 159 assert ! items.isEmpty () : "Any module must be selected"; 160 161 String category = null; 162 if (activatedNodes.length == 1 && ! (activatedNodes [0] instanceof ModuleNode.Item)) { 163 category = activatedNodes [0].getDisplayName (); 164 } 165 166 Set <Module> modules = new HashSet <Module>(items.size()); 167 for (ModuleNode.Item item : items) { 168 modules.add(item.getItem().getModule()); 169 } 170 171 Component c = new ModuleUninstallPanel (modules, category); 172 c.getAccessibleContext ().setAccessibleDescription ( 173 NbBundle.getMessage (ModuleSelectionPanel.class, "ACD_ModuleUninstallPanel_form")); 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 |