KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > NewProject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.project.ui.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.Icon JavaDoc;
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.modules.project.ui.NewProjectWizard;
34 import org.netbeans.modules.project.ui.OpenProjectList;
35 import org.netbeans.modules.project.ui.ProjectUtilities;
36 import org.netbeans.spi.project.ui.support.CommonProjectActions;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.Repository;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectNotFoundException;
42 import org.openide.util.NbBundle;
43 import org.openide.util.RequestProcessor;
44 import org.openide.util.Utilities;
45
46 public class NewProject extends BasicAction {
47         
48     private static final Icon JavaDoc ICON = new ImageIcon JavaDoc( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/newProject.gif" ) ); //NOI18N
49
private static final String JavaDoc NAME = NbBundle.getMessage( NewProject.class, "LBL_NewProjectAction_Name" ); // NOI18N
50
private static final String JavaDoc _SHORT_DESCRIPTION = NbBundle.getMessage( NewProject.class, "LBL_NewProjectAction_Tooltip" ); // NOI18N
51

52     private boolean isPreselect = false;
53     
54     private RequestProcessor.Task bodyTask;
55
56     public NewProject() {
57         super( NAME, ICON );
58         putValue("iconBase","org/netbeans/modules/project/ui/resources/newProject.gif"); //NOI18N
59
putValue(SHORT_DESCRIPTION, _SHORT_DESCRIPTION);
60         bodyTask = new RequestProcessor( "NewProjectBody" ).create( new Runnable JavaDoc () { // NOI18N
61
public void run () {
62                 doPerform ();
63             }
64         });
65     }
66     
67     public static NewProject newSample() {
68         NewProject np = new NewProject();
69         np.setDisplayName( "New Sample" );
70         np.isPreselect = true;
71         return np;
72     }
73
74     public void actionPerformed( ActionEvent JavaDoc evt ) {
75         bodyTask.schedule( 0 );
76         
77         if ( "waitFinished".equals( evt.getActionCommand() ) ) {
78             bodyTask.waitFinished();
79         }
80     }
81         
82     /*T9Y*/ NewProjectWizard prepareWizardDescriptor(FileObject fo) {
83         NewProjectWizard wizard = new NewProjectWizard(fo);
84             
85         if ( isPreselect ) {
86             // XXX make the properties public ?
87
wizard.putProperty( "PRESELECT_CATEGORY", getValue ("PRESELECT_CATEGORY"));
88             wizard.putProperty( "PRESELECT_TEMPLATE", getValue ("PRESELECT_TEMPLATE"));
89         }
90         else {
91             wizard.putProperty( "PRESELECT_CATEGORY", null );
92             wizard.putProperty( "PRESELECT_TEMPLATE", null );
93         }
94
95         FileObject folder = (FileObject) getValue(CommonProjectActions.EXISTING_SOURCES_FOLDER);
96         if (folder != null) {
97             wizard.putProperty(CommonProjectActions.EXISTING_SOURCES_FOLDER, folder);
98         }
99         return wizard;
100     }
101     
102     private void doPerform () {
103         
104         FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Project" ); //NOI18N
105
final NewProjectWizard wizard = prepareWizardDescriptor(fo);
106         
107         
108         SwingUtilities.invokeLater( new Runnable JavaDoc() {
109             
110             public void run() {
111                 try {
112                     
113                     Set JavaDoc newObjects = wizard.instantiate();
114                     Object JavaDoc mainProperty = wizard.getProperty( /* XXX Define somewhere */ "setAsMain" ); // NOI18N
115
boolean setFirstMain = true;
116                     if ( mainProperty instanceof Boolean JavaDoc ) {
117                         setFirstMain = ((Boolean JavaDoc)mainProperty).booleanValue();
118                     }
119                     final boolean setFirstMainFinal = setFirstMain;
120                     
121                     //#69618: the non-project cache may contain a project folder listed in newObjects:
122
ProjectManager.getDefault().clearNonProjectCache();
123                     ProjectUtilities.WaitCursor.show();
124                     
125                     if ( newObjects != null && !newObjects.isEmpty() ) {
126                         // First. Open all returned projects in the GUI.
127

128                         LinkedList JavaDoc<DataObject> filesToOpen = new LinkedList JavaDoc<DataObject>();
129                         List JavaDoc<Project> projectsToOpen = new LinkedList JavaDoc<Project>();
130
131                         for( Iterator JavaDoc it = newObjects.iterator(); it.hasNext(); ) {
132                             Object JavaDoc obj = it.next();
133                             FileObject newFo;
134                             DataObject newDo;
135                             if (obj instanceof DataObject) {
136                                 newDo = (DataObject) obj;
137                                 newFo = newDo.getPrimaryFile();
138                             } else if (obj instanceof FileObject) {
139                                 newFo = (FileObject) obj;
140                                 try {
141                                     newDo = DataObject.find(newFo);
142                                 } catch (DataObjectNotFoundException e) {
143                                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
144                                     continue;
145                                 }
146                             } else {
147                                 ErrorManager.getDefault().log(ErrorManager.WARNING, "Found unrecognized object " + obj + " in result set from instantiate()");
148                                 continue;
149                             }
150                             // check if it's a project directory
151
if (newFo.isFolder()) {
152                                 try {
153                                     Project p = ProjectManager.getDefault().findProject(newFo);
154                                     if (p != null) {
155                                         // It is a project, so schedule it to open:
156
projectsToOpen.add(p);
157                                     } else {
158                                         // Just a folder to expand
159
filesToOpen.add(newDo);
160                                     }
161                                 } catch (IOException JavaDoc e) {
162                                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
163                                     continue;
164                                 }
165                             } else {
166                                 filesToOpen.add(newDo);
167                             }
168                         }
169                         
170                         Project lastProject = projectsToOpen.size() > 0 ? (Project) projectsToOpen.get(0) : null;
171                         
172                         OpenProjectList.getDefault().open( projectsToOpen.toArray(new Project[0]), false, true);
173                         
174                         if (setFirstMainFinal && lastProject != null) {
175                             OpenProjectList.getDefault().setMainProject(lastProject);
176                         }
177                         
178                         // Show the project tab to show the user we did something
179
if (! Boolean.getBoolean("project.tab.no.selection")) { //NOI18N
180
ProjectUtilities.makeProjectTabVisible( true );
181                         }
182                         
183                         // Second open the files
184
if (filesToOpen.isEmpty() && lastProject != null) {
185                             // Just select and expand the project node
186
ProjectUtilities.selectAndExpandProject(lastProject);
187                         } else {
188                             for( Iterator JavaDoc it = filesToOpen.iterator(); it.hasNext(); ) { // Open the files
189
ProjectUtilities.openAndSelectNewObject( (DataObject)it.next() );
190                             }
191                         }
192                         
193                     }
194                     ProjectUtilities.WaitCursor.hide();
195                 } catch ( IOException JavaDoc e ) {
196                     ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e );
197                 }
198             }
199             
200         } );
201         
202         
203         
204     }
205     
206 }
207
Popular Tags