1 19 package org.netbeans.modules.versioning.system.cvss.ui.actions.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 ; 39 import java.util.List ; 40 import java.util.LinkedList ; 41 import java.util.Enumeration ; 42 import java.util.Collections ; 43 import java.io.IOException ; 44 import java.io.File ; 45 46 53 final class ProjectUtilities { 54 55 private static final String ProjectTab_ID_LOGICAL = "projectTabLogical_tc"; 57 private static final String NEW_PROJECT_ACTION = "Actions/Project/org-netbeans-modules-project-ui-NewProject.instance"; 59 private static final String INITIAL_SOURCE_ROOT = "EXISTING_SOURCES_CURRENT_DIRECTORY"; 61 62 public static void selectAndExpandProject( final Project p ) { 63 64 SwingUtilities.invokeLater (new Runnable () { 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( ProjectUtils.getInformation( p ).getName() ); 73 if ( projNode != null ) { 74 try { 75 ptLogial.getExplorerManager ().setSelectedNodes( new Node[] { projNode } ); 76 } catch (Exception ignore) { 77 } 79 } 80 } 81 }); 82 83 } 84 85 91 private static synchronized ExplorerManager.Provider findDefault( String tcID ) { 92 TopComponent tc = WindowManager.getDefault().findTopComponent( tcID ); 93 return (ExplorerManager.Provider) tc; 94 } 95 96 108 public static void newProjectWizard(File workingDirectory) { 109 Action action = CommonProjectActions.newProjectAction(); 110 if (action != null) { 111 File 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 124 public static List scanForProjects(FileObject scanRoot) { 125 return scanForProjectsRecursively(scanRoot, 5); 126 } 127 128 private static List scanForProjectsRecursively(FileObject scanRoot, int deep) { 129 if (deep <= 0) return Collections.EMPTY_LIST; 130 List projects = new LinkedList (); 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 e) { 139 ErrorManager.getDefault().annotate(e, "CVS.PU: ignoring suspicious project folder..."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 144 } 145 } 146 Enumeration en = scanRoot.getChildren(false); 147 while (en.hasMoreElements()) { 148 FileObject fo = (FileObject) en.nextElement(); 149 if (fo.isFolder()) { 150 List nested = scanForProjectsRecursively(fo, deep -1); projects.addAll(nested); 152 } 153 } 154 return projects; 155 } 156 157 private static Action findAction (String 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 instance = ic.instanceCreate(); 169 if (instance instanceof Action) { 170 Action a = (Action) instance; 171 a.putValue ("PRESELECT_CATEGORY", "General"); a.putValue ("PRESELECT_TEMPLATE", null); return a; 175 } 176 } 177 } catch (Exception 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 ae = new ActionEvent (ProjectUtilities.class, ActionEvent.ACTION_PERFORMED, "command"); try { 192 a.actionPerformed(ae); 193 return true; 194 } catch (Exception e) { 195 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 196 return false; 197 } 198 } 199 200 } 201 | Popular Tags |