Adding Git source, NetBeans project files, GPL v2 LICENSE, ant build file.
[nbgit.git] / src / org / netbeans / modules / git / util / GitProjectUtils.java
bloba6699f8ee66a875b02292c3c9fb55e44803c849b
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.util;
44 import java.awt.event.ActionEvent;
45 import java.io.File;
46 import java.util.logging.Level;
47 import javax.swing.Action;
48 import javax.swing.SwingUtilities;
49 import org.netbeans.api.project.Project;
50 import org.netbeans.api.project.ProjectInformation;
51 import org.netbeans.api.project.ProjectManager;
52 import org.netbeans.api.project.ProjectUtils;
53 import org.netbeans.api.project.ui.OpenProjects;
54 import org.netbeans.modules.git.Git;
55 import org.netbeans.spi.project.ui.support.CommonProjectActions;
56 import org.openide.explorer.ExplorerManager;
57 import org.openide.filesystems.FileObject;
58 import org.openide.filesystems.FileUtil;
59 import org.openide.nodes.Node;
60 import org.openide.util.ContextAwareAction;
61 import org.openide.util.Lookup;
62 import org.openide.util.lookup.Lookups;
63 import org.openide.windows.TopComponent;
64 import org.openide.windows.WindowManager;
67 public class GitProjectUtils {
69 private static final String ProjectTab_ID_LOGICAL = "projectTabLogical_tc"; // NOI18N
71 public static void renameProject(Project p, Object caller) {
72 if( p == null) return;
74 ContextAwareAction action = (ContextAwareAction) CommonProjectActions.renameProjectAction();
75 Lookup ctx = Lookups.singleton(p);
76 Action ctxAction = action.createContextAwareInstance(ctx);
77 ctxAction.actionPerformed(new ActionEvent(caller, 0, "")); // NOI18N
80 public static void openProject(Project p, Object caller, boolean setMain) {
81 if (p == null) {
82 return;
84 Project[] projects = new Project[]{p};
85 OpenProjects.getDefault().open(projects, false);
86 if (setMain) {
87 OpenProjects.getDefault().setMainProject(p);
90 // set as main project and expand
91 selectAndExpandProject(p);
94 public static void selectAndExpandProject( final Project p ) {
95 if( p == null) return;
97 // invoke later to select the being opened project if the focus is outside ProjectTab
98 SwingUtilities.invokeLater(new Runnable() {
100 final ExplorerManager.Provider ptLogial = findDefault(ProjectTab_ID_LOGICAL);
102 public void run() {
103 Node root = ptLogial.getExplorerManager().getRootContext();
104 // Node projNode = root.getChildren ().findChild( p.getProjectDirectory().getName () );
105 Node projNode = root.getChildren().findChild( ProjectUtils.getInformation( p ).getName() );
106 if ( projNode != null ) {
107 try {
108 ptLogial.getExplorerManager().setSelectedNodes( new Node[] { projNode } );
109 } catch (Exception ignore) {
110 // may ignore it
117 public static String getProjectName( final File root ) {
118 if(root == null || !root.isDirectory()) return null;
119 final ProjectManager projectManager = ProjectManager.getDefault();
120 FileObject rootFileObj = FileUtil.toFileObject(FileUtil.normalizeFile(root));
121 // This can happen if the root is "ssh://<something>"
122 if (rootFileObj == null || projectManager == null) {
123 return null;
126 String res = null;
127 if (projectManager.isProject(rootFileObj)){
128 try {
129 Project prj = projectManager.findProject(rootFileObj);
131 res = getProjectName(prj);
132 } catch (Exception ex) {
133 Git.LOG.log(Level.FINE, "getProjectName() file: {0} {1}", new Object[] {rootFileObj.getPath(), ex.toString()}); // NOI18N
134 }finally{
135 return res;
137 }else{
138 return res;
142 public static String getProjectName( final Project p ) {
143 if( p == null) return null;
145 ProjectInformation pi = ProjectUtils.getInformation(p);
146 return pi == null ? null : pi.getDisplayName();
149 private static synchronized ExplorerManager.Provider findDefault( String tcID ) {
150 TopComponent tc = WindowManager.getDefault().findTopComponent( tcID );
151 return (ExplorerManager.Provider) tc;
154 // Should not be creating an instance of this class
155 private GitProjectUtils() {