KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > java > dev > cvsrootselector > CvsRootSelectorAction


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 the CVSROOT Selector (RFE #65366).
16  * The Initial Developer of the Original Software is Michael Nascimento Santos.
17  * Portions created by Michael Nascimento Santos are Copyright (C) 2005.
18  * All Rights Reserved.
19  */

20
21 package net.java.dev.cvsrootselector;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Collection JavaDoc;
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); // NOI18N
45
}
46
47     public String JavaDoc 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 JavaDoc file = getFile(node[0]);
66             
67             if (file == null) {
68                 return false;
69             }
70                         
71             return CvsRootRewriter.getCvsRootFile(file) != null;
72         } catch (IOException JavaDoc 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 JavaDoc ex) {
82             ErrorManager.getDefault().notify(ErrorManager.USER, ex);
83         }
84     }
85     
86     private File JavaDoc getFile(Node node) throws IOException JavaDoc {
87         File JavaDoc file = null;
88         
89         FileObject fo = null;
90         Collection JavaDoc 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 JavaDoc 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 JavaDoc versioned = null;
122                     boolean multiple = false;
123                     
124                     for (int i = 0; i < groups.length; i++) {
125                         FileObject root = groups[0].getRootFolder();
126                         File JavaDoc 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