Renamed constants, local variables and member variables using "hg" to "git".
[nbgit.git] / src / org / netbeans / modules / git / ui / clone / ProjectUtilities.java
blob30db3490522b67d9a70e493f8b6d7dac62091533
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Contributor(s):
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.netbeans.modules.git.ui.clone;
44 import java.awt.event.ActionEvent;
45 import java.io.File;
46 import java.io.IOException;
47 import java.util.Collections;
48 import java.util.Enumeration;
49 import java.util.LinkedList;
50 import java.util.List;
51 import java.util.logging.Level;
52 import javax.swing.Action;
53 import javax.swing.SwingUtilities;
54 import org.netbeans.api.project.Project;
55 import org.netbeans.api.project.ProjectManager;
56 import org.netbeans.api.project.ProjectUtils;
57 import org.netbeans.modules.git.Git;
58 import org.netbeans.spi.project.ui.support.CommonProjectActions;
59 import org.netbeans.spi.project.ui.support.ProjectChooser;
60 import org.openide.explorer.ExplorerManager;
61 import org.openide.filesystems.FileObject;
62 import org.openide.filesystems.FileUtil;
63 import org.openide.nodes.Node;
64 import org.openide.windows.TopComponent;
65 import org.openide.windows.WindowManager;
67 /**
68 * Simpliied nb_all/projects/projectui/src/org/netbeans/modules/project/ui/ProjectUtilities.java,
69 * nb_all/projects/projectui/src/org/netbeans/modules/project/ui/ProjectTab.java and
70 * nb_all/ide/welcome/src/org/netbeans/modules/welcome/ui/TitlePanel.java copy.
72 * @author Petr Kuzel
74 final class ProjectUtilities {
76 private static final String ProjectTab_ID_LOGICAL = "projectTabLogical_tc"; // NOI18N
78 public static void selectAndExpandProject( final Project p ) {
80 // invoke later to select the being opened project if the focus is outside ProjectTab
81 SwingUtilities.invokeLater (new Runnable () {
83 final ExplorerManager.Provider ptLogial = findDefault(ProjectTab_ID_LOGICAL);
85 public void run () {
86 Node root = ptLogial.getExplorerManager ().getRootContext ();
87 // Node projNode = root.getChildren ().findChild( p.getProjectDirectory().getName () );
88 Node projNode = root.getChildren ().findChild( ProjectUtils.getInformation( p ).getName() );
89 if ( projNode != null ) {
90 try {
91 ptLogial.getExplorerManager ().setSelectedNodes( new Node[] { projNode } );
92 } catch (Exception ignore) {
93 // may ignore it
97 });
101 /* Singleton accessor. As ProjectTab is persistent singleton this
102 * accessor makes sure that ProjectTab is deserialized by window system.
103 * Uses known unique TopComponent ID TC_ID = "projectTab_tc" to get ProjectTab instance
104 * from window system. "projectTab_tc" is name of settings file defined in module layer.
105 * For example ProjectTabAction uses this method to create instance if necessary.
107 private static synchronized ExplorerManager.Provider findDefault( String tcID ) {
108 TopComponent tc = WindowManager.getDefault().findTopComponent( tcID );
109 return (ExplorerManager.Provider) tc;
113 * Runs <i>New Project...</i> wizard with redefined defaults:
114 * <ul>
115 * <li>default project directory to working folder to
116 * capture creating new project in placeholder
117 * directory prepared by CVS server admin
118 * <li>CommonProjectActions.EXISTING_SOURCES_FOLDER
119 * pointing to working folder to capture
120 * typical <i>... from Existing Sources</i> panel
121 * <i>Add</i> button behaviour.
122 * </ul>
124 public static void newProjectWizard(File workingDirectory) {
125 if(workingDirectory == null) return;
127 Action action = CommonProjectActions.newProjectAction();
128 if (action != null) {
129 File original = ProjectChooser.getProjectsFolder();
130 ProjectChooser.setProjectsFolder(workingDirectory);
131 FileObject workingFolder = FileUtil.toFileObject(workingDirectory);
132 action.putValue(CommonProjectActions.EXISTING_SOURCES_FOLDER, workingFolder);
133 performAction(action);
134 try {
135 ProjectChooser.setProjectsFolder(original);
136 } catch (IllegalArgumentException e) {
137 // it seems the original folder is invalid, ignore this
143 * Scans given folder (and subfolder into deep 5) for projects.
144 * @return List of {@link Project}s never <code>null</code>.
146 public static List<Project> scanForProjects(FileObject scanRoot) {
147 ProjectManager.getDefault().clearNonProjectCache();
148 return scanForProjectsRecursively(scanRoot, 5);
151 private static List<Project> scanForProjectsRecursively(FileObject scanRoot, int deep) {
152 if (scanRoot == null || deep <= 0) return Collections.emptyList();
153 List<Project> projects = new LinkedList<Project>();
154 ProjectManager projectManager = ProjectManager.getDefault();
155 if (scanRoot.isFolder() && projectManager.isProject(scanRoot)) {
156 try {
157 Project prj = projectManager.findProject(scanRoot);
158 if(prj != null) {
159 projects.add(prj);
161 } catch (IOException e) {
162 // it happens for all apisupport projects unless
163 // checked out into directory that contains nbbuild and openide folders
164 // apisupport project is valid only if placed in defined directory structure
165 Throwable cause = new Throwable("GIT.PU: ignoring suspicious project folder..."); // NOI18N
166 e.initCause(cause);
167 Git.LOG.log(Level.INFO, null, e);
170 Enumeration en = scanRoot.getChildren(false);
171 while (en.hasMoreElements()) {
172 FileObject fo = (FileObject) en.nextElement();
173 if (fo.isFolder()) {
174 List<Project> nested = scanForProjectsRecursively(fo, deep -1); // RECURSION
175 projects.addAll(nested);
178 return projects;
181 private static boolean performAction (Action a) {
182 if (a == null) {
183 return false;
185 ActionEvent ae = new ActionEvent(ProjectUtilities.class, ActionEvent.ACTION_PERFORMED, "command"); // NOI18N
186 try {
187 a.actionPerformed(ae);
188 return true;
189 } catch (Exception e) {
190 Git.LOG.log(Level.WARNING, null, e);
191 return false;