KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > checkout > ProjectUtilities


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 package org.netbeans.modules.subversion.ui.checkout;
20
21 import org.netbeans.api.project.Project;
22 import org.netbeans.api.project.ProjectUtils;
23 import org.netbeans.api.project.ProjectManager;
24 import org.netbeans.spi.project.ui.support.CommonProjectActions;
25 import org.netbeans.spi.project.ui.support.ProjectChooser;
26 import org.openide.filesystems.FileUtil;
27 import org.openide.nodes.Node;
28 import org.openide.explorer.ExplorerManager;
29 import org.openide.windows.TopComponent;
30 import org.openide.windows.WindowManager;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.Repository;
33 import org.openide.loaders.DataObject;
34 import org.openide.cookies.InstanceCookie;
35 import org.openide.ErrorManager;
36
37 import javax.swing.*;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.LinkedList JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.Collections JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.File JavaDoc;
45
46 /**
47  * Simpliied nb_all/projects/projectui/src/org/netbeans/modules/project/ui/ProjectUtilities.java,
48  * nb_all/projects/projectui/src/org/netbeans/modules/project/ui/ProjectTab.java and
49  * nb_all/ide/welcome/src/org/netbeans/modules/welcome/ui/TitlePanel.java copy.
50  *
51  * @author Petr Kuzel
52  */

53 final class ProjectUtilities {
54
55     private static final String JavaDoc ProjectTab_ID_LOGICAL = "projectTabLogical_tc"; // NOI18N
56

57     private static final String JavaDoc NEW_PROJECT_ACTION = "Actions/Project/org-netbeans-modules-project-ui-NewProject.instance"; // NOI18N
58

59     private static final String JavaDoc INITIAL_SOURCE_ROOT = "EXISTING_SOURCES_CURRENT_DIRECTORY"; // NOI18N
60

61
62     public static void selectAndExpandProject( final Project p ) {
63
64         // invoke later to select the being opened project if the focus is outside ProjectTab
65
SwingUtilities.invokeLater (new Runnable JavaDoc () {
66
67             final ExplorerManager.Provider ptLogial = findDefault(ProjectTab_ID_LOGICAL);
68
69             public void run () {
70                 Node root = ptLogial.getExplorerManager ().getRootContext ();
71                 // Node projNode = root.getChildren ().findChild( p.getProjectDirectory().getName () );
72
Node projNode = root.getChildren ().findChild( ProjectUtils.getInformation( p ).getName() );
73                 if ( projNode != null ) {
74                     try {
75                         ptLogial.getExplorerManager ().setSelectedNodes( new Node[] { projNode } );
76                     } catch (Exception JavaDoc ignore) {
77                         // may ignore it
78
}
79                 }
80             }
81         });
82
83     }
84
85     /* Singleton accessor. As ProjectTab is persistent singleton this
86      * accessor makes sure that ProjectTab is deserialized by window system.
87      * Uses known unique TopComponent ID TC_ID = "projectTab_tc" to get ProjectTab instance
88      * from window system. "projectTab_tc" is name of settings file defined in module layer.
89      * For example ProjectTabAction uses this method to create instance if necessary.
90      */

91     private static synchronized ExplorerManager.Provider findDefault( String JavaDoc tcID ) {
92         TopComponent tc = WindowManager.getDefault().findTopComponent( tcID );
93         return (ExplorerManager.Provider) tc;
94     }
95
96     /**
97      * Runs <i>New Project...</i> wizard with redefined defaults:
98      * <ul>
99      * <li>default project directory to working folder to
100      * capture creating new project in placeholder
101      * directory prepared by CVS server admin
102      * <li>CommonProjectActions.EXISTING_SOURCES_FOLDER
103      * pointing to working folder to capture
104      * typical <i>... from Existing Sources</i> panel
105      * <i>Add</i> button behaviour.
106      * </ul>
107      */

108     public static void newProjectWizard(File JavaDoc workingDirectory) {
109         Action action = CommonProjectActions.newProjectAction();
110         if (action != null) {
111             File JavaDoc original = ProjectChooser.getProjectsFolder();
112             ProjectChooser.setProjectsFolder(workingDirectory);
113             FileObject workingFolder = FileUtil.toFileObject(workingDirectory);
114             action.putValue(CommonProjectActions.EXISTING_SOURCES_FOLDER, workingFolder);
115             performAction(action);
116             ProjectChooser.setProjectsFolder(original);
117         }
118     }
119
120     /**
121      * Scans given folder (and subfolder into deep 5) for projects.
122      * @return List of {@link Project}s never <code>null</code>.
123      */

124     public static List JavaDoc<Project> scanForProjects(FileObject scanRoot) {
125         return scanForProjectsRecursively(scanRoot, 5);
126     }
127
128     private static List JavaDoc<Project> scanForProjectsRecursively(FileObject scanRoot, int deep) {
129         if (deep <= 0) return Collections.emptyList();
130         List JavaDoc<Project> projects = new LinkedList JavaDoc<Project>();
131         ProjectManager projectManager = ProjectManager.getDefault();
132         if (scanRoot.isFolder() && projectManager.isProject(scanRoot)) {
133             try {
134                 Project prj = projectManager.findProject(scanRoot);
135                 if(prj != null) {
136                     projects.add(prj);
137                 }
138             } catch (IOException JavaDoc e) {
139                 // it happens for all apisupport projects unless
140
// checked out into directory that contains nbbuild and openide folders
141
// apisupport project is valid only if placed in defined directory structure
142
ErrorManager.getDefault().annotate(e, "SVN.PU: ignoring suspicious project folder..."); // NOI18N
143
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
144             }
145         }
146         Enumeration JavaDoc en = scanRoot.getChildren(false);
147         while (en.hasMoreElements()) {
148             FileObject fo = (FileObject) en.nextElement();
149             if (fo.isFolder()) {
150                 List JavaDoc<Project> nested = scanForProjectsRecursively(fo, deep -1); // RECURSION
151
projects.addAll(nested);
152             }
153         }
154         return projects;
155     }
156
157     private static Action findAction (String JavaDoc key) {
158         FileObject fo =
159             Repository.getDefault().getDefaultFileSystem().findResource(key);
160
161         if (fo != null && fo.isValid()) {
162             try {
163                 DataObject dob = DataObject.find (fo);
164                 InstanceCookie ic =
165                     (InstanceCookie) dob.getCookie(InstanceCookie.class);
166
167                 if (ic != null) {
168                     Object JavaDoc instance = ic.instanceCreate();
169                     if (instance instanceof Action) {
170                         Action a = (Action) instance;
171                         // NewProject action reads the properties PRESELECT_CATEGORY and PRESELECT_TEMPLATE
172
a.putValue ("PRESELECT_CATEGORY", "General"); // NOI18N
173
a.putValue ("PRESELECT_TEMPLATE", null); // NOI18N
174
return a;
175                     }
176                 }
177             } catch (Exception JavaDoc e) {
178                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
179                 return null;
180             }
181         }
182         return null;
183     }
184
185
186     private static boolean performAction (Action a) {
187         if (a == null) {
188             return false;
189         }
190         ActionEvent JavaDoc ae = new ActionEvent JavaDoc(ProjectUtilities.class, ActionEvent.ACTION_PERFORMED, "command"); // NOI18N
191
try {
192             a.actionPerformed(ae);
193             return true;
194         } catch (Exception JavaDoc e) {
195             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
196             return false;
197         }
198     }
199
200 }
201
Popular Tags