1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.swing.Icon ; 29 import javax.swing.ImageIcon ; 30 import javax.swing.SwingUtilities ; 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 ICON = new ImageIcon ( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/newProject.gif" ) ); private static final String NAME = NbBundle.getMessage( NewProject.class, "LBL_NewProjectAction_Name" ); private static final String _SHORT_DESCRIPTION = NbBundle.getMessage( NewProject.class, "LBL_NewProjectAction_Tooltip" ); 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"); putValue(SHORT_DESCRIPTION, _SHORT_DESCRIPTION); 60 bodyTask = new RequestProcessor( "NewProjectBody" ).create( new Runnable () { 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 evt ) { 75 bodyTask.schedule( 0 ); 76 77 if ( "waitFinished".equals( evt.getActionCommand() ) ) { 78 bodyTask.waitFinished(); 79 } 80 } 81 82 NewProjectWizard prepareWizardDescriptor(FileObject fo) { 83 NewProjectWizard wizard = new NewProjectWizard(fo); 84 85 if ( isPreselect ) { 86 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" ); final NewProjectWizard wizard = prepareWizardDescriptor(fo); 106 107 108 SwingUtilities.invokeLater( new Runnable () { 109 110 public void run() { 111 try { 112 113 Set newObjects = wizard.instantiate(); 114 Object mainProperty = wizard.getProperty( "setAsMain" ); boolean setFirstMain = true; 116 if ( mainProperty instanceof Boolean ) { 117 setFirstMain = ((Boolean )mainProperty).booleanValue(); 118 } 119 final boolean setFirstMainFinal = setFirstMain; 120 121 ProjectManager.getDefault().clearNonProjectCache(); 123 ProjectUtilities.WaitCursor.show(); 124 125 if ( newObjects != null && !newObjects.isEmpty() ) { 126 128 LinkedList <DataObject> filesToOpen = new LinkedList <DataObject>(); 129 List <Project> projectsToOpen = new LinkedList <Project>(); 130 131 for( Iterator it = newObjects.iterator(); it.hasNext(); ) { 132 Object 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 if (newFo.isFolder()) { 152 try { 153 Project p = ProjectManager.getDefault().findProject(newFo); 154 if (p != null) { 155 projectsToOpen.add(p); 157 } else { 158 filesToOpen.add(newDo); 160 } 161 } catch (IOException 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 if (! Boolean.getBoolean("project.tab.no.selection")) { ProjectUtilities.makeProjectTabVisible( true ); 181 } 182 183 if (filesToOpen.isEmpty() && lastProject != null) { 185 ProjectUtilities.selectAndExpandProject(lastProject); 187 } else { 188 for( Iterator it = filesToOpen.iterator(); it.hasNext(); ) { ProjectUtilities.openAndSelectNewObject( (DataObject)it.next() ); 190 } 191 } 192 193 } 194 ProjectUtilities.WaitCursor.hide(); 195 } catch ( IOException e ) { 196 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e ); 197 } 198 } 199 200 } ); 201 202 203 204 } 205 206 } 207 | Popular Tags |