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.event.ActionEvent;
045: import java.io.IOException;
046: import java.util.Arrays;
047: import java.util.HashSet;
048: import java.util.Set;
049: import javax.swing.AbstractAction;
050: import javax.swing.Action;
051: import javax.swing.JComponent;
052: import javax.swing.JMenuItem;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.ProjectManager;
055: import org.netbeans.api.project.ProjectUtils;
056: import org.netbeans.modules.project.ui.OpenProjectList;
057: import org.openide.ErrorManager;
058: import org.openide.awt.DynamicMenuContent;
059: import org.openide.loaders.DataFolder;
060: import org.openide.util.ContextAwareAction;
061: import org.openide.util.Lookup;
062: import org.openide.util.NbBundle;
063: import org.openide.util.RequestProcessor;
064: import org.openide.util.actions.Presenter;
065:
066: /**
067: * Action to open the project(s) corresponding to selected folder(s).
068: * Enabled only for projects which are not already open, and thus useful
069: * for opening projects encountered in the Favorites tab, as well as nested
070: * projects found beneath open projects in the Files tab.
071: * @see "#54122"
072: * @author Jesse Glick
073: */
074: public final class OpenProjectFolderAction extends AbstractAction
075: implements ContextAwareAction {
076:
077: public OpenProjectFolderAction() {
078: // Label not likely displayed in GUI.
079: super (NbBundle.getMessage(OpenProjectFolderAction.class,
080: "OpenProjectFolderAction.LBL_action"));
081: }
082:
083: public void actionPerformed(ActionEvent e) {
084: // Cannot be invoked without any context.
085: assert false;
086: }
087:
088: public Action createContextAwareInstance(Lookup context) {
089: return new ContextAction(context);
090: }
091:
092: private final class ContextAction extends AbstractAction implements
093: Presenter.Popup {
094:
095: /** Projects to be opened. */
096: private final Set<Project> projects;
097:
098: public ContextAction(Lookup context) {
099: projects = new HashSet<Project>();
100: // Collect projects corresponding to selected folders.
101: for (DataFolder d : context.lookupAll(DataFolder.class)) {
102: Project p = null;
103: try {
104: p = ProjectManager.getDefault().findProject(
105: d.getPrimaryFile());
106: } catch (IOException e) {
107: ErrorManager.getDefault().notify(
108: ErrorManager.INFORMATIONAL, e);
109: }
110: if (p != null) {
111: projects.add(p);
112: }
113: // Ignore folders not corresponding to projects (will not disable action if some correspond to projects).
114: }
115: // Ignore projects which are already open (will not disable action if some can be opened).
116: projects.removeAll(Arrays.asList(OpenProjectList
117: .getDefault().getOpenProjects()));
118: int size = projects.size();
119: if (size == 1) {
120: String name = ProjectUtils.getInformation(
121: projects.iterator().next()).getDisplayName();
122: putValue(Action.NAME, NbBundle.getMessage(
123: OpenProjectFolderAction.class,
124: "OpenProjectFolderAction.LBL_menu_one", name));
125: } else if (size > 1) {
126: putValue(Action.NAME, NbBundle.getMessage(
127: OpenProjectFolderAction.class,
128: "OpenProjectFolderAction.LBL_menu_multiple",
129: new Integer(size)));
130: }
131: }
132:
133: public void actionPerformed(ActionEvent e) {
134: // Run asynch so that UI is not blocked; might show progress dialog (?).
135: RequestProcessor.getDefault().post(new Runnable() {
136: public void run() {
137: OpenProjectList.getDefault().open(
138: projects.toArray(new Project[projects
139: .size()]), false, true);
140: }
141: });
142: }
143:
144: public JMenuItem getPopupPresenter() {
145: class Presenter extends JMenuItem implements
146: DynamicMenuContent {
147: public Presenter() {
148: super (ContextAction.this );
149: }
150:
151: public JComponent[] getMenuPresenters() {
152: if (!projects.isEmpty()) {
153: return new JComponent[] { this , null };
154: } else {
155: // Disabled, so do not display at all.
156: return new JComponent[0];
157: }
158: }
159:
160: public JComponent[] synchMenuPresenters(
161: JComponent[] items) {
162: return getMenuPresenters();
163: }
164: }
165: return new Presenter();
166: }
167:
168: }
169:
170: }
|