1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import javax.swing.Icon ; 27 import javax.swing.ImageIcon ; 28 import javax.swing.JFileChooser ; 29 import javax.swing.SwingUtilities ; 30 import org.netbeans.api.project.FileOwnerQuery; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.modules.project.ui.OpenProjectList; 33 import org.netbeans.modules.project.ui.OpenProjectListSettings; 34 import org.netbeans.modules.project.ui.ProjectChooserAccessory; 35 import org.netbeans.modules.project.ui.ProjectTab; 36 import org.openide.DialogDisplayer; 37 import org.openide.NotifyDescriptor; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.loaders.DataObject; 40 import org.openide.nodes.Node; 41 import org.openide.util.Lookup; 42 import org.openide.util.NbBundle; 43 import org.openide.util.Utilities; 44 import org.openide.windows.WindowManager; 45 46 public class OpenProject extends BasicAction { 47 48 private static final String NAME = NbBundle.getMessage( OpenProject.class, "LBL_OpenProjectAction_Name" ); private static final String _SHORT_DESCRIPTION = NbBundle.getMessage( OpenProject.class, "LBL_OpenProjectAction_Tooltip" ); 51 52 public OpenProject() { 53 super( NAME, new ImageIcon ( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/openProject.gif" ) ) ); 54 putValue("iconBase","org/netbeans/modules/project/ui/resources/openProject.gif"); putValue(SHORT_DESCRIPTION, _SHORT_DESCRIPTION); 56 } 57 58 public void actionPerformed( ActionEvent evt ) { 59 JFileChooser chooser = ProjectChooserAccessory.createProjectChooser( true ); chooser.setMultiSelectionEnabled( true ); 61 62 Iterator it = Utilities.actionsGlobalContext().lookupAll(DataObject.class).iterator(); 65 while (it.hasNext()) { 66 DataObject d = (DataObject) it.next(); 68 Project selected = FileOwnerQuery.getOwner(d.getPrimaryFile()); 69 if (selected != null && !OpenProjectList.getDefault().isOpen(selected)) { 70 File dir = FileUtil.toFile(selected.getProjectDirectory()); 71 if (dir != null) { 72 chooser.setCurrentDirectory(dir.getParentFile()); 73 chooser.setSelectedFiles(new File [] {dir}); 74 break; 75 } 76 } 77 } 78 79 OpenProjectListSettings opls = OpenProjectListSettings.getInstance(); 80 81 while( true ) { 84 int option = chooser.showOpenDialog( WindowManager.getDefault().getMainWindow() ); 86 if ( option == JFileChooser.APPROVE_OPTION ) { 87 88 final File [] projectDirs; 89 if ( chooser.isMultiSelectionEnabled() ) { 90 projectDirs = chooser.getSelectedFiles(); 91 } 92 else { 93 projectDirs = new File [] { chooser.getSelectedFile() }; 94 } 95 96 ArrayList <Project> projects = new ArrayList <Project>( projectDirs.length ); 98 for( int i = 0; i < projectDirs.length; i++ ) { 99 Project p = OpenProjectList.fileToProject( FileUtil.normalizeFile( projectDirs[i] ) ); 100 if ( p != null ) { 101 projects.add( p ); 102 } 103 } 104 105 if ( projects.isEmpty() ) { 106 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 107 NbBundle.getMessage( OpenProject.class, "MSG_notProjectDir"), NotifyDescriptor.WARNING_MESSAGE)); 109 } 110 else { 111 Project projectsArray[] = new Project[ projects.size() ]; 112 projects.toArray( projectsArray ); 113 OpenProjectList.getDefault().open( 114 projectsArray, opls.isOpenSubprojects(), true); if ( opls.isOpenAsMain() && projectsArray.length == 1 ) { 118 OpenProjectList.getDefault().setMainProject( projectsArray[0] ); 120 } 121 final ProjectTab ptLogial = ProjectTab.findDefault (ProjectTab.ID_LOGICAL); 122 123 SwingUtilities.invokeLater (new Runnable () { 125 public void run () { 126 Node root = ptLogial.getExplorerManager ().getRootContext (); 127 128 ArrayList <Node> nodes = new ArrayList <Node>( projectDirs.length ); 129 for( int i = 0; i < projectDirs.length; i++ ) { 130 Node projNode = root.getChildren ().findChild (projectDirs[i].getName () ); 131 if ( projNode != null ) { 132 nodes.add( projNode ); 133 } 134 } 135 try { 136 Node[] nodesArray = new Node[ nodes.size() ]; 137 nodes.toArray( nodesArray ); 138 ptLogial.getExplorerManager ().setSelectedNodes (nodesArray); 139 if (!Boolean.getBoolean("project.tab.no.selection")) { ptLogial.open (); 141 ptLogial.requestActive (); 142 } 143 } catch (Exception ignore) { 144 } 146 } 147 }); 148 break; } 150 } 151 else { 152 return ; } 155 } 156 157 opls.setLastOpenProjectDir( chooser.getCurrentDirectory().getPath() ); 158 159 } 160 161 162 } 163 | Popular Tags |