1 20 21 package net.java.dev.cvsrootselector; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.Collection ; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.api.project.ProjectUtils; 28 import org.netbeans.api.project.SourceGroup; 29 import org.netbeans.api.project.Sources; 30 import org.openide.ErrorManager; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.loaders.DataObject; 34 import org.openide.loaders.DataShadow; 35 import org.openide.nodes.Node; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.Lookup; 38 import org.openide.util.actions.NodeAction; 39 40 public final class CvsRootSelectorAction extends NodeAction { 41 42 public CvsRootSelectorAction() { 43 setIcon(null); 44 putValue("noIconInMenu", Boolean.TRUE); } 46 47 public String getName() { 48 return "Change CVSROOT..."; 49 } 50 51 public HelpCtx getHelpCtx() { 52 return HelpCtx.DEFAULT_HELP; 53 } 54 55 protected boolean asynchronous() { 56 return false; 57 } 58 59 protected boolean enable(Node[] node) { 60 if (node.length != 1) { 61 return false; 62 } 63 64 try { 65 File file = getFile(node[0]); 66 67 if (file == null) { 68 return false; 69 } 70 71 return CvsRootRewriter.getCvsRootFile(file) != null; 72 } catch (IOException ex) { 73 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 74 return false; 75 } 76 } 77 78 protected void performAction(Node[] node) { 79 try { 80 new CvsRootSelectorPanel(getFile(node[0])).display(); 81 } catch (IOException ex) { 82 ErrorManager.getDefault().notify(ErrorManager.USER, ex); 83 } 84 } 85 86 private File getFile(Node node) throws IOException { 87 File file = null; 88 89 FileObject fo = null; 90 Collection fileObjects = node.getLookup().lookup( 91 new Lookup.Template(FileObject.class)).allInstances(); 92 93 if (fileObjects.size() > 0) { 94 fo = (FileObject) fileObjects.iterator().next(); 95 } else { 96 DataObject dataObject = (DataObject) node.getCookie(DataObject.class); 97 if (dataObject instanceof DataShadow) { 98 dataObject = ((DataShadow) dataObject).getOriginal(); 99 } 100 if (dataObject != null) { 101 fo = dataObject.getPrimaryFile(); 102 } 103 } 104 105 if (fo != null) { 106 File f = FileUtil.toFile(fo); 107 if (f != null && f.isDirectory()) { 108 file = f; 109 } 110 } else { 111 Project project = (Project)node.getLookup().lookup(Project.class); 112 113 if (project != null) { 114 Sources sources = ProjectUtils.getSources(project); 115 SourceGroup[] groups = sources.getSourceGroups(Sources.TYPE_GENERIC); 116 117 if (groups.length == 1) { 118 FileObject root = groups[0].getRootFolder(); 119 file = FileUtil.toFile(root); 120 } else { 121 File versioned = null; 122 boolean multiple = false; 123 124 for (int i = 0; i < groups.length; i++) { 125 FileObject root = groups[0].getRootFolder(); 126 File f = FileUtil.toFile(root); 127 128 System.out.println(f); 129 if (f != null && CvsRootRewriter.getCvsRootFile(f) != null) { 130 if (versioned != null && !versioned.equals(f)) { 131 multiple = true; 132 } 133 134 versioned = f; 135 } 136 } 137 138 file = (multiple || versioned == null) ? 139 FileUtil.toFile(project.getProjectDirectory()) : 140 versioned; 141 } 142 } 143 } 144 145 return file; 146 } 147 } | Popular Tags |