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.File;
046: import java.util.ArrayList;
047: import javax.swing.ImageIcon;
048: import javax.swing.JFileChooser;
049: import javax.swing.SwingUtilities;
050: import org.netbeans.api.project.FileOwnerQuery;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.modules.project.ui.OpenProjectList;
053: import org.netbeans.modules.project.ui.OpenProjectListSettings;
054: import org.netbeans.modules.project.ui.ProjectChooserAccessory;
055: import org.netbeans.modules.project.ui.ProjectTab;
056: import org.openide.DialogDisplayer;
057: import org.openide.NotifyDescriptor;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.loaders.DataObject;
060: import org.openide.nodes.Node;
061: import org.openide.util.NbBundle;
062: import org.openide.util.Utilities;
063: import org.openide.windows.WindowManager;
064:
065: public class OpenProject extends BasicAction {
066:
067: private static final String DISPLAY_NAME = NbBundle.getMessage(
068: OpenProject.class, "LBL_OpenProjectAction_Name"); // NOI18N
069: private static final String _SHORT_DESCRIPTION = NbBundle
070: .getMessage(OpenProject.class,
071: "LBL_OpenProjectAction_Tooltip"); // NOI18N
072:
073: /** Creates a new instance of BrowserAction */
074: public OpenProject() {
075: super (
076: DISPLAY_NAME,
077: new ImageIcon(
078: Utilities
079: .loadImage("org/netbeans/modules/project/ui/resources/openProject.png")));
080: putValue("iconBase",
081: "org/netbeans/modules/project/ui/resources/openProject.png"); //NOI18N
082: putValue(SHORT_DESCRIPTION, _SHORT_DESCRIPTION);
083: }
084:
085: public void actionPerformed(ActionEvent evt) {
086: JFileChooser chooser = ProjectChooserAccessory
087: .createProjectChooser(true); // Create the jFileChooser
088: chooser.setMultiSelectionEnabled(true);
089:
090: // Check to see if the current selection matches a file/folder owned by a non-open project;
091: // if so, use that as the starting directory, as a convenience in case that is what should be opened.
092: // XXX may also want to check lookup for FileObject
093: for (DataObject d : Utilities.actionsGlobalContext().lookupAll(
094: DataObject.class)) {
095: Project selected = FileOwnerQuery.getOwner(d
096: .getPrimaryFile());
097: if (selected != null
098: && !OpenProjectList.getDefault().isOpen(selected)) {
099: File dir = FileUtil.toFile(selected
100: .getProjectDirectory());
101: if (dir != null) {
102: chooser.setCurrentDirectory(dir.getParentFile());
103: chooser.setSelectedFiles(new File[] { dir });
104: break;
105: }
106: }
107: }
108:
109: OpenProjectListSettings opls = OpenProjectListSettings
110: .getInstance();
111:
112: while (true) { // Cycle while users does some reasonable action e.g.
113: // select project dir or cancel the chooser
114:
115: int option = chooser.showOpenDialog(WindowManager
116: .getDefault().getMainWindow()); // Sow the chooser
117:
118: if (option == JFileChooser.APPROVE_OPTION) {
119:
120: final File[] projectDirs;
121: if (chooser.isMultiSelectionEnabled()) {
122: projectDirs = chooser.getSelectedFiles();
123: } else {
124: projectDirs = new File[] { chooser
125: .getSelectedFile() };
126: }
127:
128: // Project project = OpenProjectList.fileToProject( projectDir );
129: ArrayList<Project> projects = new ArrayList<Project>(
130: projectDirs.length);
131: for (File d : projectDirs) {
132: Project p = OpenProjectList.fileToProject(FileUtil
133: .normalizeFile(d));
134: if (p != null) {
135: projects.add(p);
136: }
137: }
138:
139: if (projects.isEmpty()) {
140: DialogDisplayer.getDefault().notify(
141: new NotifyDescriptor.Message(NbBundle
142: .getMessage(OpenProject.class,
143: "MSG_notProjectDir"), // NOI18N
144: NotifyDescriptor.WARNING_MESSAGE));
145: } else {
146: Project projectsArray[] = new Project[projects
147: .size()];
148: projects.toArray(projectsArray);
149: OpenProjectList.getDefault().open(projectsArray, // Put the project into OpenProjectList
150: opls.isOpenSubprojects(), // And optionaly open subprojects
151: true); // open asynchronously
152: if (opls.isOpenAsMain()
153: && projectsArray.length == 1) {
154: // Set main project if selected
155: OpenProjectList.getDefault().setMainProject(
156: projectsArray[0]);
157: }
158: final ProjectTab ptLogial = ProjectTab
159: .findDefault(ProjectTab.ID_LOGICAL);
160:
161: // invoke later to select the being opened project if the focus is outside ProjectTab
162: SwingUtilities.invokeLater(new Runnable() {
163: public void run() {
164: Node root = ptLogial.getExplorerManager()
165: .getRootContext();
166:
167: ArrayList<Node> nodes = new ArrayList<Node>(
168: projectDirs.length);
169: for (int i = 0; i < projectDirs.length; i++) {
170: Node projNode = root.getChildren()
171: .findChild(
172: projectDirs[i]
173: .getName());
174: if (projNode != null) {
175: nodes.add(projNode);
176: }
177: }
178: try {
179: Node[] nodesArray = new Node[nodes
180: .size()];
181: nodes.toArray(nodesArray);
182: ptLogial.getExplorerManager()
183: .setSelectedNodes(nodesArray);
184: if (!Boolean
185: .getBoolean("project.tab.no.selection")) { //NOI18N
186: ptLogial.open();
187: ptLogial.requestActive();
188: }
189: } catch (Exception ignore) {
190: // may ignore it
191: }
192: }
193: });
194: break; // and exit the loop
195: }
196: } else {
197: return; // OK user changed his mind and won't open anything
198: // Don't remeber the last selected dir
199: }
200: }
201:
202: opls.setLastOpenProjectDir(chooser.getCurrentDirectory()
203: .getPath());
204:
205: }
206:
207: }
|