KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28 import javax.swing.JFileChooser JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
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 JavaDoc NAME = NbBundle.getMessage( OpenProject.class, "LBL_OpenProjectAction_Name" ); // NOI18N
49
private static final String JavaDoc _SHORT_DESCRIPTION = NbBundle.getMessage( OpenProject.class, "LBL_OpenProjectAction_Tooltip" ); // NOI18N
50

51     /** Creates a new instance of BrowserAction */
52     public OpenProject() {
53         super( NAME, new ImageIcon JavaDoc( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/openProject.gif" ) ) );
54         putValue("iconBase","org/netbeans/modules/project/ui/resources/openProject.gif"); //NOI18N
55
putValue(SHORT_DESCRIPTION, _SHORT_DESCRIPTION);
56     }
57
58     public void actionPerformed( ActionEvent JavaDoc evt ) {
59         JFileChooser JavaDoc chooser = ProjectChooserAccessory.createProjectChooser( true ); // Create the jFileChooser
60
chooser.setMultiSelectionEnabled( true );
61         
62         // Check to see if the current selection matches a file/folder owned by a non-open project;
63
// if so, use that as the starting directory, as a convenience in case that is what should be opened.
64
Iterator JavaDoc it = Utilities.actionsGlobalContext().lookupAll(DataObject.class).iterator();
65         while (it.hasNext()) {
66             // XXX may also want to check lookup for FileObject
67
DataObject d = (DataObject) it.next();
68             Project selected = FileOwnerQuery.getOwner(d.getPrimaryFile());
69             if (selected != null && !OpenProjectList.getDefault().isOpen(selected)) {
70                 File JavaDoc dir = FileUtil.toFile(selected.getProjectDirectory());
71                 if (dir != null) {
72                     chooser.setCurrentDirectory(dir.getParentFile());
73                     chooser.setSelectedFiles(new File JavaDoc[] {dir});
74                     break;
75                 }
76             }
77         }
78         
79         OpenProjectListSettings opls = OpenProjectListSettings.getInstance();
80         
81         while( true ) { // Cycle while users does some reasonable action e.g.
82
// select project dir or cancel the chooser
83

84             int option = chooser.showOpenDialog( WindowManager.getDefault().getMainWindow() ); // Sow the chooser
85

86             if ( option == JFileChooser.APPROVE_OPTION ) {
87
88                 final File JavaDoc[] projectDirs;
89                 if ( chooser.isMultiSelectionEnabled() ) {
90                     projectDirs = chooser.getSelectedFiles();
91                 }
92                 else {
93                     projectDirs = new File JavaDoc[] { chooser.getSelectedFile() };
94                 }
95                 
96                 // Project project = OpenProjectList.fileToProject( projectDir );
97
ArrayList JavaDoc<Project> projects = new ArrayList JavaDoc<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"), // NOI18N
108
NotifyDescriptor.WARNING_MESSAGE));
109                 }
110                 else {
111                     Project projectsArray[] = new Project[ projects.size() ];
112                     projects.toArray( projectsArray );
113                     OpenProjectList.getDefault().open(
114                         projectsArray, // Put the project into OpenProjectList
115
opls.isOpenSubprojects(), // And optionaly open subprojects
116
true); // open asynchronously
117
if ( opls.isOpenAsMain() && projectsArray.length == 1 ) {
118                         // Set main project if selected
119
OpenProjectList.getDefault().setMainProject( projectsArray[0] );
120                     }
121                     final ProjectTab ptLogial = ProjectTab.findDefault (ProjectTab.ID_LOGICAL);
122                     
123                     // invoke later to select the being opened project if the focus is outside ProjectTab
124
SwingUtilities.invokeLater (new Runnable JavaDoc () {
125                         public void run () {
126                             Node root = ptLogial.getExplorerManager ().getRootContext ();
127                             
128                             ArrayList JavaDoc<Node> nodes = new ArrayList JavaDoc<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")) { //NOI18N
140
ptLogial.open ();
141                                     ptLogial.requestActive ();
142                                 }
143                             } catch (Exception JavaDoc ignore) {
144                                 // may ignore it
145
}
146                         }
147                     });
148                     break; // and exit the loop
149
}
150             }
151             else {
152                 return ; // OK user changed his mind and won't open anything
153
// Don't remeber the last selected dir
154
}
155         }
156         
157         opls.setLastOpenProjectDir( chooser.getCurrentDirectory().getPath() );
158         
159     }
160     
161         
162 }
163
Popular Tags