1 19 20 package org.netbeans.modules.project.ui.groups; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import javax.swing.AbstractAction ; 27 import javax.swing.JButton ; 28 import javax.swing.JCheckBoxMenuItem ; 29 import javax.swing.JComponent ; 30 import javax.swing.JMenu ; 31 import javax.swing.JMenuItem ; 32 import javax.swing.JRadioButtonMenuItem ; 33 import org.netbeans.api.project.ui.OpenProjects; 34 import org.openide.DialogDescriptor; 35 import org.openide.DialogDisplayer; 36 import org.openide.NotifyDescriptor; 37 import org.openide.awt.DynamicMenuContent; 38 import org.openide.awt.Mnemonics; 39 import org.openide.util.HelpCtx; 40 import org.openide.util.NbBundle; 41 import org.openide.util.RequestProcessor; 42 import org.openide.util.actions.Presenter; 43 44 48 public class GroupsMenu extends AbstractAction implements Presenter.Menu, Presenter.Popup { 49 50 private static final RequestProcessor RP = new RequestProcessor(GroupsMenu.class.getName()); 51 52 public GroupsMenu() { 53 super(NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.label")); 54 } 55 56 public void actionPerformed(ActionEvent e) { 57 assert false; 58 } 59 60 public JMenuItem getMenuPresenter() { 61 return new Menu (); 62 } 63 64 public JMenuItem getPopupPresenter() { 65 return new Menu (); 66 } 67 68 71 private static class Menu extends JMenu implements DynamicMenuContent { 72 73 Menu() { 74 Mnemonics.setLocalizedText(this, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.label")); 75 } 76 77 public JComponent [] getMenuPresenters() { 78 removeAll(); 80 final Group active = Group.getActiveGroup(); 81 for (final Group g : Group.allGroups()) { 83 JRadioButtonMenuItem mi = new JRadioButtonMenuItem (g.getName()); 84 if (g.equals(active)) { 85 mi.setSelected(true); 86 if (g.isPristine() && Group.isAdvancedMode()) { 87 mi.setEnabled(false); 88 } 89 } 90 mi.addActionListener(new ActionListener () { 91 public void actionPerformed(ActionEvent e) { 92 RP.post(new Runnable () { 94 public void run() { 95 Group.setActiveGroup(g); 96 } 97 }); 98 } 99 }); 100 add(mi); 101 } 102 JMenuItem mi = new JRadioButtonMenuItem (); 103 Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.no_group")); 104 if (active == null) { 105 mi.setSelected(true); 106 if (OpenProjects.getDefault().getOpenProjects().length == 0 && Group.isAdvancedMode()) { 107 mi.setEnabled(false); 108 } 109 } 110 mi.addActionListener(new ActionListener () { 111 public void actionPerformed(ActionEvent e) { 112 RP.post(new Runnable () { 114 public void run() { 115 Group.setActiveGroup(null); 116 } 117 }); 118 } 119 }); 120 add(mi); 121 addSeparator(); 123 mi = new JMenuItem (); 124 Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.new_group")); 125 mi.addActionListener(new ActionListener () { 126 public void actionPerformed(ActionEvent e) { 127 newGroup(); 128 } 129 }); 130 add(mi); 131 if (active != null) { 132 mi = new JMenuItem (); 133 Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.properties", active.getName())); 134 mi.addActionListener(new ActionListener () { 135 public void actionPerformed(ActionEvent e) { 136 openProperties(active); 137 } 138 }); 139 add(mi); 140 mi = new JMenuItem (); 141 Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.remove", active.getName())); 142 mi.addActionListener(new ActionListener () { 143 public void actionPerformed(ActionEvent e) { 144 active.destroy(); 145 } 146 }); 147 add(mi); 148 } 149 mi = new JCheckBoxMenuItem (); 150 Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.advanced")); 151 mi.setSelected(Group.isAdvancedMode()); 152 mi.addActionListener(new ActionListener () { 153 public void actionPerformed(ActionEvent e) { 154 Group.setAdvancedMode(!Group.isAdvancedMode()); 155 } 156 }); 157 add(mi); 158 return new JComponent [] {this}; 159 } 160 161 public JComponent [] synchMenuPresenters(JComponent [] items) { 162 return getMenuPresenters(); 163 } 164 165 } 166 167 170 private static void newGroup() { 171 final AbstractNewGroupPanel panel = Group.isAdvancedMode() ? new NewGroupPanel() : new NewGroupPanelBasic(); 172 DialogDescriptor dd = new DialogDescriptor(panel, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.new_title")); 173 dd.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION); 174 dd.setModal(true); 175 dd.setHelpCtx(new HelpCtx(GroupsMenu.class)); 176 final JButton create = new JButton (NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.new_create")); 177 create.setDefaultCapable(true); 178 create.setEnabled(panel.isReady()); 179 panel.addPropertyChangeListener(new PropertyChangeListener () { 180 public void propertyChange(PropertyChangeEvent evt) { 181 if (AbstractNewGroupPanel.PROP_READY.equals(evt.getPropertyName())) { 182 create.setEnabled(panel.isReady()); 183 } 184 } 185 }); 186 JButton cancel = new JButton (NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.new_cancel")); 187 dd.setOptions(new Object [] {create, cancel}); 188 Object result = DialogDisplayer.getDefault().notify(dd); 189 if (result.equals(create)) { 190 final Group g = panel.create(); 191 RP.post(new Runnable () { 192 public void run() { 193 Group.setActiveGroup(g); 194 } 195 }); 196 } 197 } 198 199 202 private static void openProperties(Group g) { 203 GroupEditPanel panel = g.createPropertiesPanel(); 204 DialogDescriptor dd = new DialogDescriptor(panel, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.properties_title")); 205 dd.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION); 206 dd.setModal(true); 207 dd.setHelpCtx(new HelpCtx(GroupsMenu.class)); 208 Object result = DialogDisplayer.getDefault().notify(dd); 209 if (result.equals(NotifyDescriptor.OK_OPTION)) { 210 panel.applyChanges(); 211 } 212 } 213 214 } 215 | Popular Tags |