KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > groups > GroupsMenu


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.project.ui.groups;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JCheckBoxMenuItem JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JMenu JavaDoc;
31 import javax.swing.JMenuItem JavaDoc;
32 import javax.swing.JRadioButtonMenuItem JavaDoc;
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 /**
45  * Submenu listing available groups and offering some operations on them.
46  * @author Jesse Glick
47  */

48 public class GroupsMenu extends AbstractAction JavaDoc 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 JavaDoc e) {
57         assert false;
58     }
59
60     public JMenuItem JavaDoc getMenuPresenter() {
61         return new Menu JavaDoc();
62     }
63
64     public JMenuItem JavaDoc getPopupPresenter() {
65         return new Menu JavaDoc();
66     }
67
68     /**
69      * The actual submenu (recreated each time it is displayed).
70      */

71     private static class Menu extends JMenu JavaDoc implements DynamicMenuContent {
72
73         Menu() {
74             Mnemonics.setLocalizedText(this, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.label"));
75         }
76
77         public JComponent JavaDoc[] getMenuPresenters() {
78             // XXX can it wait to add menu items until it is posted?
79
removeAll();
80             final Group active = Group.getActiveGroup();
81             // Create one menu item per group.
82
for (final Group g : Group.allGroups()) {
83                 JRadioButtonMenuItem JavaDoc mi = new JRadioButtonMenuItem JavaDoc(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 JavaDoc() {
91                     public void actionPerformed(ActionEvent JavaDoc e) {
92                         // Could be slow (if needs to load projects); don't block EQ.
93
RP.post(new Runnable JavaDoc() {
94                             public void run() {
95                                 Group.setActiveGroup(g);
96                             }
97                         });
98                     }
99                 });
100                 add(mi);
101             }
102             JMenuItem JavaDoc mi = new JRadioButtonMenuItem JavaDoc();
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 JavaDoc() {
111                 public void actionPerformed(ActionEvent JavaDoc e) {
112                     // Could be slow (if needs to load projects); don't block EQ.
113
RP.post(new Runnable JavaDoc() {
114                         public void run() {
115                             Group.setActiveGroup(null);
116                         }
117                     });
118                 }
119             });
120             add(mi);
121             // Special menu items.
122
addSeparator();
123             mi = new JMenuItem JavaDoc();
124             Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.new_group"));
125             mi.addActionListener(new ActionListener JavaDoc() {
126                 public void actionPerformed(ActionEvent JavaDoc e) {
127                     newGroup();
128                 }
129             });
130             add(mi);
131             if (active != null) {
132                 mi = new JMenuItem JavaDoc();
133                 Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.properties", active.getName()));
134                 mi.addActionListener(new ActionListener JavaDoc() {
135                     public void actionPerformed(ActionEvent JavaDoc e) {
136                         openProperties(active);
137                     }
138                 });
139                 add(mi);
140                 mi = new JMenuItem JavaDoc();
141                 Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.remove", active.getName()));
142                 mi.addActionListener(new ActionListener JavaDoc() {
143                     public void actionPerformed(ActionEvent JavaDoc e) {
144                         active.destroy();
145                     }
146                 });
147                 add(mi);
148             }
149             mi = new JCheckBoxMenuItem JavaDoc();
150             Mnemonics.setLocalizedText(mi, NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.advanced"));
151             mi.setSelected(Group.isAdvancedMode());
152             mi.addActionListener(new ActionListener JavaDoc() {
153                 public void actionPerformed(ActionEvent JavaDoc e) {
154                     Group.setAdvancedMode(!Group.isAdvancedMode());
155                 }
156             });
157             add(mi);
158             return new JComponent JavaDoc[] {this};
159         }
160
161         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
162             return getMenuPresenters();
163         }
164
165     }
166
167     /**
168      * Create (and open) a new group.
169      */

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 JavaDoc create = new JButton JavaDoc(NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.new_create"));
177         create.setDefaultCapable(true);
178         create.setEnabled(panel.isReady());
179         panel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
180             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
181                 if (AbstractNewGroupPanel.PROP_READY.equals(evt.getPropertyName())) {
182                     create.setEnabled(panel.isReady());
183                 }
184             }
185         });
186         JButton JavaDoc cancel = new JButton JavaDoc(NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.new_cancel"));
187         dd.setOptions(new Object JavaDoc[] {create, cancel});
188         Object JavaDoc result = DialogDisplayer.getDefault().notify(dd);
189         if (result.equals(create)) {
190             final Group g = panel.create();
191             RP.post(new Runnable JavaDoc() {
192                 public void run() {
193                     Group.setActiveGroup(g);
194                 }
195             });
196         }
197     }
198
199     /**
200      * Open a properties dialog for the group, according to its type.
201      */

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 JavaDoc result = DialogDisplayer.getDefault().notify(dd);
209         if (result.equals(NotifyDescriptor.OK_OPTION)) {
210             panel.applyChanges();
211         }
212     }
213
214 }
215
Popular Tags