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.io.IOException;
047: import java.util.Iterator;
048: import java.util.LinkedList;
049: import java.util.List;
050: import java.util.Set;
051: import java.util.prefs.Preferences;
052: import javax.swing.Icon;
053: import javax.swing.ImageIcon;
054: import javax.swing.SwingUtilities;
055: import org.netbeans.api.project.Project;
056: import org.netbeans.api.project.ProjectManager;
057: import org.netbeans.modules.project.ui.NewProjectWizard;
058: import org.netbeans.modules.project.ui.OpenProjectList;
059: import org.netbeans.modules.project.ui.OpenProjectListSettings;
060: import org.netbeans.modules.project.ui.ProjectUtilities;
061: import org.netbeans.spi.project.ui.support.CommonProjectActions;
062: import org.openide.ErrorManager;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.Repository;
065: import org.openide.loaders.DataObject;
066: import org.openide.loaders.DataObjectNotFoundException;
067: import org.openide.util.NbBundle;
068: import org.openide.util.NbPreferences;
069: import org.openide.util.RequestProcessor;
070: import org.openide.util.Utilities;
071:
072: public class NewProject extends BasicAction {
073:
074: private static final Icon ICON = new ImageIcon(
075: Utilities
076: .loadImage("org/netbeans/modules/project/ui/resources/newProject.png")); //NOI18N
077: private static final String NAME = NbBundle.getMessage(
078: NewProject.class, "LBL_NewProjectAction_Name"); // NOI18N
079: private static final String _SHORT_DESCRIPTION = NbBundle
080: .getMessage(NewProject.class,
081: "LBL_NewProjectAction_Tooltip"); // NOI18N
082:
083: private boolean isPreselect = false;
084:
085: private RequestProcessor.Task bodyTask;
086:
087: public NewProject() {
088: super (NAME, ICON);
089: putValue("iconBase",
090: "org/netbeans/modules/project/ui/resources/newProject.png"); //NOI18N
091: putValue(SHORT_DESCRIPTION, _SHORT_DESCRIPTION);
092: bodyTask = new RequestProcessor("NewProjectBody")
093: .create(new Runnable() { // NOI18N
094: public void run() {
095: doPerform();
096: }
097: });
098: }
099:
100: public static NewProject newSample() {
101: NewProject np = new NewProject();
102: np.setDisplayName("New Sample");
103: np.isPreselect = true;
104: return np;
105: }
106:
107: public void actionPerformed(ActionEvent evt) {
108: bodyTask.schedule(0);
109:
110: if ("waitFinished".equals(evt.getActionCommand())) {
111: bodyTask.waitFinished();
112: }
113: }
114:
115: /*T9Y*/NewProjectWizard prepareWizardDescriptor(FileObject fo) {
116: NewProjectWizard wizard = new NewProjectWizard(fo);
117:
118: if (isPreselect) {
119: // XXX make the properties public ?
120: wizard.putProperty("PRESELECT_CATEGORY",
121: getValue("PRESELECT_CATEGORY"));
122: wizard.putProperty("PRESELECT_TEMPLATE",
123: getValue("PRESELECT_TEMPLATE"));
124: } else {
125: wizard.putProperty("PRESELECT_CATEGORY", null);
126: wizard.putProperty("PRESELECT_TEMPLATE", null);
127: }
128:
129: FileObject folder = (FileObject) getValue(CommonProjectActions.EXISTING_SOURCES_FOLDER);
130: if (folder != null) {
131: wizard.putProperty(
132: CommonProjectActions.EXISTING_SOURCES_FOLDER,
133: folder);
134: }
135: return wizard;
136: }
137:
138: private void doPerform() {
139:
140: FileObject fo = Repository.getDefault().getDefaultFileSystem()
141: .findResource("Templates/Project"); //NOI18N
142: final NewProjectWizard wizard = prepareWizardDescriptor(fo);
143:
144: SwingUtilities.invokeLater(new Runnable() {
145:
146: public void run() {
147: try {
148:
149: Set newObjects = wizard.instantiate();
150: // #75960 - test if any folder was created during the wizard and if yes and it's empty delete it
151: Preferences prefs = NbPreferences
152: .forModule(OpenProjectListSettings.class);
153: String nbPrjDirPath = prefs
154: .get(
155: OpenProjectListSettings.PROP_CREATED_PROJECTS_FOLDER,
156: null);
157: prefs
158: .remove(OpenProjectListSettings.PROP_CREATED_PROJECTS_FOLDER);
159: if (nbPrjDirPath != null) {
160: File prjDir = new File(nbPrjDirPath);
161: if (prjDir.exists() && prjDir.isDirectory()
162: && prjDir.listFiles() != null
163: && prjDir.listFiles().length == 0) {
164: prjDir.delete();
165: }
166: }
167:
168: Object mainProperty = wizard
169: .getProperty( /* XXX Define somewhere */"setAsMain"); // NOI18N
170: boolean setFirstMain = true;
171: if (mainProperty instanceof Boolean) {
172: setFirstMain = ((Boolean) mainProperty)
173: .booleanValue();
174: }
175: final boolean setFirstMainFinal = setFirstMain;
176:
177: //#69618: the non-project cache may contain a project folder listed in newObjects:
178: ProjectManager.getDefault().clearNonProjectCache();
179: ProjectUtilities.WaitCursor.show();
180:
181: if (newObjects != null && !newObjects.isEmpty()) {
182: // First. Open all returned projects in the GUI.
183:
184: LinkedList<DataObject> filesToOpen = new LinkedList<DataObject>();
185: List<Project> projectsToOpen = new LinkedList<Project>();
186:
187: for (Iterator it = newObjects.iterator(); it
188: .hasNext();) {
189: Object obj = it.next();
190: FileObject newFo;
191: DataObject newDo;
192: if (obj instanceof DataObject) {
193: newDo = (DataObject) obj;
194: newFo = newDo.getPrimaryFile();
195: } else if (obj instanceof FileObject) {
196: newFo = (FileObject) obj;
197: try {
198: newDo = DataObject.find(newFo);
199: } catch (DataObjectNotFoundException e) {
200: ErrorManager.getDefault().notify(
201: ErrorManager.INFORMATIONAL,
202: e);
203: continue;
204: }
205: } else {
206: ErrorManager
207: .getDefault()
208: .log(
209: ErrorManager.WARNING,
210: "Found unrecognized object "
211: + obj
212: + " in result set from instantiate()");
213: continue;
214: }
215: // check if it's a project directory
216: if (newFo.isFolder()) {
217: try {
218: Project p = ProjectManager
219: .getDefault().findProject(
220: newFo);
221: if (p != null) {
222: // It is a project, so schedule it to open:
223: projectsToOpen.add(p);
224: } else {
225: // Just a folder to expand
226: filesToOpen.add(newDo);
227: }
228: } catch (IOException e) {
229: ErrorManager.getDefault().notify(
230: ErrorManager.INFORMATIONAL,
231: e);
232: continue;
233: }
234: } else {
235: filesToOpen.add(newDo);
236: }
237: }
238:
239: Project lastProject = projectsToOpen.size() > 0 ? (Project) projectsToOpen
240: .get(0)
241: : null;
242:
243: OpenProjectList.getDefault().open(
244: projectsToOpen.toArray(new Project[0]),
245: false, true);
246:
247: if (setFirstMainFinal && lastProject != null) {
248: OpenProjectList.getDefault()
249: .setMainProject(lastProject);
250: }
251:
252: // Show the project tab to show the user we did something
253: if (!Boolean
254: .getBoolean("project.tab.no.selection")) { //NOI18N
255: ProjectUtilities
256: .makeProjectTabVisible(true);
257: }
258:
259: if (lastProject != null) {
260: // Just select and expand the project node
261: ProjectUtilities
262: .selectAndExpandProject(lastProject);
263: }
264: // Second open the files
265: for (Iterator it = filesToOpen.iterator(); it
266: .hasNext();) { // Open the files
267: ProjectUtilities
268: .openAndSelectNewObject((DataObject) it
269: .next());
270: }
271:
272: }
273: ProjectUtilities.WaitCursor.hide();
274: } catch (IOException e) {
275: ErrorManager.getDefault().notify(
276: ErrorManager.INFORMATIONAL, e);
277: }
278: }
279:
280: });
281:
282: }
283:
284: }
|