001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ui.groups;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import javax.swing.AbstractAction;
049: import javax.swing.JButton;
050: import javax.swing.JComponent;
051: import javax.swing.JMenu;
052: import javax.swing.JMenuItem;
053: import javax.swing.JRadioButtonMenuItem;
054: import org.openide.DialogDescriptor;
055: import org.openide.DialogDisplayer;
056: import org.openide.NotifyDescriptor;
057: import org.openide.awt.DynamicMenuContent;
058: import org.openide.awt.Mnemonics;
059: import org.openide.util.HelpCtx;
060: import org.openide.util.NbBundle;
061: import org.openide.util.RequestProcessor;
062: import org.openide.util.actions.Presenter;
063:
064: /**
065: * Submenu listing available groups and offering some operations on them.
066: * @author Jesse Glick
067: */
068: public class GroupsMenu extends AbstractAction implements
069: Presenter.Menu, Presenter.Popup {
070:
071: private static final RequestProcessor RP = new RequestProcessor(
072: GroupsMenu.class.getName());
073:
074: public GroupsMenu() {
075: super (NbBundle.getMessage(GroupsMenu.class, "GroupsMenu.label"));
076: }
077:
078: public void actionPerformed(ActionEvent e) {
079: assert false;
080: }
081:
082: public JMenuItem getMenuPresenter() {
083: return new Menu();
084: }
085:
086: public JMenuItem getPopupPresenter() {
087: return new Menu();
088: }
089:
090: /**
091: * The actual submenu (recreated each time it is displayed).
092: */
093: private static class Menu extends JMenu implements
094: DynamicMenuContent {
095:
096: Menu() {
097: Mnemonics.setLocalizedText(this , NbBundle.getMessage(
098: GroupsMenu.class, "GroupsMenu.label"));
099: }
100:
101: public JComponent[] getMenuPresenters() {
102: // XXX can it wait to add menu items until it is posted?
103: removeAll();
104: final Group active = Group.getActiveGroup();
105: // Create one menu item per group.
106: for (final Group g : Group.allGroups()) {
107: JRadioButtonMenuItem mi = new JRadioButtonMenuItem(g
108: .getName());
109: if (g.equals(active)) {
110: mi.setSelected(true);
111: /* Was disliked by UI people:
112: if (g.isPristine()) {
113: mi.setEnabled(false);
114: }
115: */
116: }
117: mi.addActionListener(new ActionListener() {
118: public void actionPerformed(ActionEvent e) {
119: // Could be slow (if needs to load projects); don't block EQ.
120: RP.post(new Runnable() {
121: public void run() {
122: Group.setActiveGroup(g);
123: }
124: });
125: }
126: });
127: add(mi);
128: }
129: JMenuItem mi = new JRadioButtonMenuItem();
130: Mnemonics.setLocalizedText(mi, NbBundle.getMessage(
131: GroupsMenu.class, "GroupsMenu.no_group"));
132: if (active == null) {
133: mi.setSelected(true);
134: /* Was disliked by UI people:
135: if (OpenProjects.getDefault().getOpenProjects().length == 0) {
136: mi.setEnabled(false);
137: }
138: */
139: }
140: mi.addActionListener(new ActionListener() {
141: public void actionPerformed(ActionEvent e) {
142: // Could be slow (if needs to load projects); don't block EQ.
143: RP.post(new Runnable() {
144: public void run() {
145: Group.setActiveGroup(null);
146: }
147: });
148: }
149: });
150: add(mi);
151: // Special menu items.
152: addSeparator();
153: mi = new JMenuItem();
154: Mnemonics.setLocalizedText(mi, NbBundle.getMessage(
155: GroupsMenu.class, "GroupsMenu.new_group"));
156: mi.addActionListener(new ActionListener() {
157: public void actionPerformed(ActionEvent e) {
158: newGroup();
159: }
160: });
161: add(mi);
162: if (active != null) {
163: mi = new JMenuItem();
164: Mnemonics.setLocalizedText(mi, NbBundle.getMessage(
165: GroupsMenu.class, "GroupsMenu.properties",
166: active.getName()));
167: mi.addActionListener(new ActionListener() {
168: public void actionPerformed(ActionEvent e) {
169: openProperties(active);
170: }
171: });
172: add(mi);
173: mi = new JMenuItem();
174: Mnemonics.setLocalizedText(mi, NbBundle.getMessage(
175: GroupsMenu.class, "GroupsMenu.remove", active
176: .getName()));
177: mi.addActionListener(new ActionListener() {
178: public void actionPerformed(ActionEvent e) {
179: active.destroy();
180: }
181: });
182: add(mi);
183: }
184: return new JComponent[] { this };
185: }
186:
187: public JComponent[] synchMenuPresenters(JComponent[] items) {
188: return getMenuPresenters();
189: }
190:
191: }
192:
193: /**
194: * Create (and open) a new group.
195: */
196: private static void newGroup() {
197: final NewGroupPanel panel = new NewGroupPanel();
198: DialogDescriptor dd = new DialogDescriptor(panel, NbBundle
199: .getMessage(GroupsMenu.class, "GroupsMenu.new_title"));
200: dd.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION);
201: dd.setModal(true);
202: dd.setHelpCtx(new HelpCtx(GroupsMenu.class));
203: final JButton create = new JButton(NbBundle.getMessage(
204: GroupsMenu.class, "GroupsMenu.new_create"));
205: create.setDefaultCapable(true);
206: create.setEnabled(panel.isReady());
207: panel.addPropertyChangeListener(new PropertyChangeListener() {
208: public void propertyChange(PropertyChangeEvent evt) {
209: if (NewGroupPanel.PROP_READY.equals(evt
210: .getPropertyName())) {
211: create.setEnabled(panel.isReady());
212: }
213: }
214: });
215: JButton cancel = new JButton(NbBundle.getMessage(
216: GroupsMenu.class, "GroupsMenu.new_cancel"));
217: dd.setOptions(new Object[] { create, cancel });
218: Object result = DialogDisplayer.getDefault().notify(dd);
219: if (result.equals(create)) {
220: final Group g = panel.create();
221: RP.post(new Runnable() {
222: public void run() {
223: Group.setActiveGroup(g);
224: }
225: });
226: }
227: }
228:
229: /**
230: * Open a properties dialog for the group, according to its type.
231: */
232: private static void openProperties(Group g) {
233: GroupEditPanel panel = g.createPropertiesPanel();
234: DialogDescriptor dd = new DialogDescriptor(panel, NbBundle
235: .getMessage(GroupsMenu.class,
236: "GroupsMenu.properties_title"));
237: dd.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION);
238: dd.setModal(true);
239: dd.setHelpCtx(new HelpCtx(GroupsMenu.class));
240: Object result = DialogDisplayer.getDefault().notify(dd);
241: if (result.equals(NotifyDescriptor.OK_OPTION)) {
242: panel.applyChanges();
243: }
244: }
245:
246: }
|