1 19 20 package org.netbeans.modules.autoupdate.catalog; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.Arrays ; 25 import java.util.Set ; 26 import javax.swing.JMenu ; 27 import javax.swing.JMenuItem ; 28 import javax.swing.JRadioButtonMenuItem ; 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 40 public class ModuleNodeActions { 41 42 public static class EnableDisableAction extends NodeAction { 43 private String name; 44 45 protected void performAction (final Node[] activatedNodes) { 46 assert activatedNodes != null : "Cannot performAction when activatedNodes is null"; 47 final Boolean b = ModuleNodeUtils.isEnableCandidate (activatedNodes); 48 assert b != null : "Cannot performAction if mixed statuses on activatedNodes " + Arrays.asList (activatedNodes); 49 50 ModuleBean.AllModulesBean.getDefault ().pause (); 52 Set <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 <ModuleNode.Item> items = ModuleNodeUtils.getAllSelectedModuleItems(activatedNodes); 65 66 boolean allowed = ! items.isEmpty (); 67 String moduleName = ""; 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 Boolean b = ModuleNodeUtils.isEnableCandidate (activatedNodes); 79 80 if (activatedNodes.length == 1 && ! (activatedNodes [0] instanceof ModuleNode.Item)) { 82 name = NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_format", b == null || b.booleanValue () ? NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_enable") : NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_disable"), activatedNodes[0].getDisplayName ()); } else { 87 name = NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_format", b == null || b.booleanValue () ? NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_enable") : NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_disable"), items.size () > 1 ? NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableDisableAction_many") : moduleName); } 92 93 return allowed && (ModuleNodeUtils.isEnableCandidate (activatedNodes) != null); 94 } 95 96 public String getName() { 97 return name; 98 } 99 100 public HelpCtx getHelpCtx() { 101 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 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 return activatedNodes != null && activatedNodes.length > 0; 128 } 129 130 public String getName() { 131 return NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_EnableAllAction"); } 133 134 public HelpCtx getHelpCtx() { 135 return null; 137 } 138 139 protected boolean asynchronous() { 140 return true; 141 } 142 } 143 144 public static class UninstallAction extends NodeAction { 145 private String 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 getName() { 157 return name; 158 } 159 160 public HelpCtx getHelpCtx() { 161 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 JMenu subMenu; 174 JRadioButtonMenuItem sortByCategory; 175 JRadioButtonMenuItem sortByName; 176 JRadioButtonMenuItem sortByCluster; 177 178 protected void performAction (Node[] activatedNodes) { 179 } 180 181 protected boolean enable (Node[] activatedNodes) { 182 return true; 183 } 184 185 public String getName () { 186 return NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_SortAction"); } 188 189 public HelpCtx getHelpCtx () { 190 return null; 191 } 192 193 public JMenuItem getPopupPresenter() { 194 return getSubmenuPopupPresenter(); 195 } 196 197 public JMenuItem getSubmenuPopupPresenter() { 198 if (subMenu == null) { 199 200 subMenu = new JMenu (); 201 Mnemonics.setLocalizedText (subMenu, NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_SortAction")); 203 sortByCategory = new JRadioButtonMenuItem (); 204 Mnemonics.setLocalizedText (sortByCategory, NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_SortByCategory")); sortByCategory.addActionListener (new MenuListener ()); 206 subMenu.add (sortByCategory); 207 208 sortByName = new JRadioButtonMenuItem (); 209 Mnemonics.setLocalizedText (sortByName, NbBundle.getMessage (ModuleNodeActions.class, "CTL_ModuleNodeActions_SortByName")); sortByName.addActionListener (new MenuListener ()); 211 subMenu.add (sortByName); 212 213 sortByCluster = new JRadioButtonMenuItem (); 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 { 226 227 public void actionPerformed (ActionEvent e) { 228 JMenuItem source = (JMenuItem )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 |