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.actions;
043:
044: import java.awt.Dialog;
045: import java.awt.Toolkit;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.MouseEvent;
048: import java.beans.PropertyChangeEvent;
049: import java.beans.PropertyChangeListener;
050: import java.util.Arrays;
051: import javax.swing.Icon;
052: import javax.swing.JButton;
053: import javax.swing.event.ChangeEvent;
054: import javax.swing.event.ChangeListener;
055: import org.netbeans.api.project.Project;
056: import org.netbeans.modules.project.ui.NoMainProjectWarning;
057: import org.netbeans.modules.project.ui.OpenProjectList;
058: import org.netbeans.spi.project.ActionProvider;
059: import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
060: import org.openide.DialogDescriptor;
061: import org.openide.DialogDisplayer;
062: import org.openide.awt.Actions;
063: import org.openide.awt.MouseUtils;
064: import org.openide.util.Lookup;
065: import org.openide.util.Mutex;
066: import org.openide.util.NbBundle;
067: import org.openide.util.WeakListeners;
068:
069: /** Invokes command on the main project.
070: *
071: * @author Pet Hrebejk
072: */
073: public class MainProjectAction extends BasicAction implements
074: PropertyChangeListener {
075:
076: private String command;
077: private ProjectActionPerformer performer;
078: private String name;
079:
080: public MainProjectAction(ProjectActionPerformer performer,
081: String name, Icon icon) {
082: this (null, performer, name, icon);
083: }
084:
085: public MainProjectAction(String command, String name, Icon icon) {
086: this (command, null, name, icon);
087: }
088:
089: public MainProjectAction(String command,
090: ProjectActionPerformer performer, String name, Icon icon) {
091:
092: this .command = command;
093: this .performer = performer;
094: this .name = name;
095:
096: setDisplayName(name);
097: if (icon != null) {
098: setSmallIcon(icon);
099: }
100:
101: refreshView();
102: // Start listening on open projects list to correctly enable the action
103: OpenProjectList.getDefault().addPropertyChangeListener(
104: WeakListeners.propertyChange(this , OpenProjectList
105: .getDefault()));
106: // XXX #47160: listen to changes in supported commands on current project, when that becomes possible
107: }
108:
109: public void actionPerformed(ActionEvent e) {
110:
111: Project p = OpenProjectList.getDefault().getMainProject();
112:
113: // if no main project than show warning and allow choose a main project
114: if (p == null) {
115: // show warning, if cancel then return
116: if (showNoMainProjectWarning(OpenProjectList.getDefault()
117: .getOpenProjects(), name)) {
118: return;
119: }
120: p = OpenProjectList.getDefault().getMainProject();
121: }
122:
123: if (command != null) {
124: ActionProvider ap = p.getLookup().lookup(
125: ActionProvider.class);
126: if (ap != null) {
127: if (Arrays.asList(ap.getSupportedActions()).contains(
128: command)) {
129: ap.invokeAction(command, Lookup.EMPTY);
130: } else {
131: // #47160: was a supported command (e.g. on a freeform project) but was then removed.
132: Toolkit.getDefaultToolkit().beep();
133: refreshView();
134: }
135: }
136: } else {
137: performer.perform(p);
138: }
139: }
140:
141: // Private methods ---------------------------------------------------------
142:
143: // Implementation of PropertyChangeListener --------------------------------
144:
145: public void propertyChange(PropertyChangeEvent evt) {
146: if (evt.getPropertyName() == OpenProjectList.PROPERTY_MAIN_PROJECT
147: || evt.getPropertyName() == OpenProjectList.PROPERTY_OPEN_PROJECTS) {
148: Mutex.EVENT.readAccess(new Runnable() {
149: public void run() {
150: refreshView();
151: }
152: });
153: }
154: }
155:
156: private void refreshView() {
157:
158: Project p = OpenProjectList.getDefault().getMainProject();
159: boolean noOpenProject = OpenProjectList.getDefault()
160: .getOpenProjects().length == 0;
161:
162: if (command == null) {
163: setEnabled(performer.enable(p));
164: } else {
165: if (p == null) {
166: setEnabled(!noOpenProject);
167: } else if (ActionsUtil.commandSupported(p, command,
168: Lookup.EMPTY)) {
169: setEnabled(!noOpenProject);
170: } else {
171: setEnabled(false);
172: }
173: }
174: }
175:
176: private boolean showNoMainProjectWarning(Project[] projects,
177: String action) {
178: boolean canceled;
179: final JButton okButton = new JButton(NbBundle.getMessage(
180: NoMainProjectWarning.class,
181: "LBL_NoMainClassWarning_ChooseMainProject_OK")); // NOI18N
182: okButton.getAccessibleContext().setAccessibleDescription(
183: NbBundle.getMessage(NoMainProjectWarning.class,
184: "AD_NoMainClassWarning_ChooseMainProject_OK"));
185:
186: // no main project set => warning
187: final NoMainProjectWarning panel = new NoMainProjectWarning(
188: projects);
189:
190: Object[] options = new Object[] { okButton,
191: DialogDescriptor.CANCEL_OPTION };
192:
193: panel.addChangeListener(new ChangeListener() {
194: public void stateChanged(ChangeEvent e) {
195: if (e.getSource() instanceof MouseEvent
196: && MouseUtils.isDoubleClick(((MouseEvent) e
197: .getSource()))) {
198: // click button and the finish dialog with selected class
199: if (panel.getSelectedProject() != null) {
200: okButton.doClick();
201: }
202: } else {
203: okButton
204: .setEnabled(panel.getSelectedProject() != null);
205: }
206: }
207: });
208:
209: okButton.setEnabled(panel.getSelectedProject() != null);
210:
211: DialogDescriptor desc = new DialogDescriptor(panel,
212: action == null ? NbBundle.getMessage(
213: NoMainProjectWarning.class,
214: "CTL_NoMainProjectWarning_Title") : Actions
215: .cutAmpersand(action), true, options,
216: options[0], DialogDescriptor.DEFAULT_ALIGN, null, null);
217: desc.setMessageType(DialogDescriptor.INFORMATION_MESSAGE);
218: Dialog dlg = DialogDisplayer.getDefault().createDialog(desc);
219: dlg.setVisible(true);
220: if (desc.getValue() != options[0]) {
221: canceled = true;
222: } else {
223: Project mainProject = panel.getSelectedProject();
224: OpenProjectList.getDefault().setMainProject(mainProject);
225: canceled = false;
226: }
227: dlg.dispose();
228:
229: return canceled;
230: }
231:
232: /* Backed out; see issue #105664 for discussion:
233: public Component getToolbarPresenter() {
234: final JButton button = DropDownButtonFactory.createDropDownButton(
235: new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)), null); // image replaced anyway
236: Actions.connect(button, this);
237: final PropertyChangeListener[] weakPCL = {null};
238: PropertyChangeListener pcl = new PropertyChangeListener() {
239: public void propertyChange(PropertyChangeEvent evt) {
240: String prop = evt != null ? evt.getPropertyName() : null;
241: if (prop == null || prop.equals(OpenProjectList.PROPERTY_MAIN_PROJECT) ||
242: prop.equals(ProjectConfigurationProvider.PROP_CONFIGURATIONS)) {
243: Mutex.EVENT.readAccess(new Runnable() {
244: public void run() {
245: JPopupMenu menu = null;
246: final Project p = OpenProjectList.getDefault().getMainProject();
247: if (p != null) {
248: ActionProvider ap = p.getLookup().lookup(ActionProvider.class);
249: if (ap != null) {
250: if (Arrays.asList(ap.getSupportedActions()).contains(command)) {
251: final ProjectConfigurationProvider<?> pcp =
252: p.getLookup().lookup(ProjectConfigurationProvider.class);
253: if (pcp != null) {
254: pcp.removePropertyChangeListener(weakPCL[0]);
255: pcp.addPropertyChangeListener(weakPCL[0]);
256: if (pcp.configurationsAffectAction(command) && pcp.getConfigurations().size() > 1) {
257: menu = new JPopupMenu();
258: for (final ProjectConfiguration config : pcp.getConfigurations()) {
259: JMenuItem item = new JMenuItem(config.getDisplayName());
260: menu.add(item);
261: item.addActionListener(new ActionListener() {
262: public void actionPerformed(ActionEvent e) {
263: p.getLookup().lookup(ActionProvider.class).invokeAction(
264: command, Lookups.singleton(config));
265: }
266: });
267: }
268: }
269: }
270: }
271: }
272: }
273: button.putClientProperty(DropDownButtonFactory.PROP_DROP_DOWN_MENU, menu);
274: }
275: });
276: }
277: }
278: };
279: // avoid premature GC:
280: button.putClientProperty("listener", pcl); // NOI18N
281: weakPCL[0] = WeakListeners.propertyChange(pcl, null);
282: OpenProjectList.getDefault().addPropertyChangeListener(weakPCL[0]);
283: pcl.propertyChange(null);
284: return button;
285: }
286: */
287:
288: }
|