KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > selectors > BranchSelector


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 NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui.selectors;
21
22 import org.netbeans.lib.cvsclient.CVSRoot;
23 import org.netbeans.lib.cvsclient.Client;
24 import org.netbeans.lib.cvsclient.connection.*;
25 import org.netbeans.lib.cvsclient.event.*;
26 import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
27 import org.netbeans.lib.cvsclient.command.GlobalOptions;
28 import org.netbeans.lib.cvsclient.command.CommandException;
29 import org.netbeans.lib.cvsclient.command.log.LogCommand;
30 import org.netbeans.lib.cvsclient.command.log.LogInformation;
31 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
32 import org.netbeans.modules.versioning.system.cvss.FileInformation;
33 import org.netbeans.modules.versioning.system.cvss.util.Utils;
34 import org.openide.util.RequestProcessor;
35 import org.openide.util.UserCancelException;
36 import org.openide.util.HelpCtx;
37 import org.openide.ErrorManager;
38 import org.openide.nodes.Node;
39 import org.openide.nodes.AbstractNode;
40 import org.openide.nodes.NodeAcceptor;
41
42 import java.util.*;
43 import java.util.List JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 /**
48  * Allows to select branches for given repository path,
49  *
50  * @author Petr Kuzel
51  */

52 public final class BranchSelector implements Runnable JavaDoc {
53
54     private CVSRoot root;
55
56     private String JavaDoc module;
57
58     private File JavaDoc file;
59
60     private Node rootNode;
61     private BranchNodeChildren rootKids;
62
63     /**
64 s * Selects tag or branch for versioned files. Shows modal UI.
65      *
66      * @param file versioned file or folder
67      * @return selected tag or <code>null</code> on cancel.
68      */

69     public String JavaDoc selectTag(File JavaDoc file) {
70
71         this.file = file;
72
73         return showSelector();
74     }
75
76     /**
77      * Selects tag or branch for not yet locally checked out files.
78      *
79      * @param root repository
80      * @param module hint where to look for the first cvs_loggable file
81      * <ul>
82      * <li><code>null</code> is not allowed
83      * <li><code>"."</code> stays for any modules
84      * </ul>
85      * @return selected tag or <code>null</code> on cancel.
86      */

87     public String JavaDoc selectTag(CVSRoot root, String JavaDoc module) {
88
89         this.root = root;
90         this.module = module;
91
92         return showSelector();
93     }
94
95     private String JavaDoc showSelector() {
96
97         rootKids = new BranchNodeChildren();
98         rootNode = new AbstractNode(rootKids);
99
100         // load on background
101
RequestProcessor.getDefault().post(this);
102
103         try {
104             NodeOperation2 op = new NodeOperation2();
105             op.setIconsVisible(false);
106             op.setRootVisible(false);
107             op.setHelpCtx(new HelpCtx(BranchSelector.class));
108             Node[] selected = op.select(org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2012"),
109                                         org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2013"),
110                                         org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSD_BranchSelect"),
111                                         rootNode,
112                                         org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSN_BranchesTree"),
113                                         org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSD_BranchesTree"),
114                                         new NodeAcceptor() {
115                 public boolean acceptNodes(Node[] nodes) {
116                     if (nodes.length != 1) return false;
117                     return nodes[0].getLookup().lookup(String JavaDoc.class) != null;
118                 }
119             });
120
121             Node node = selected[0];
122             String JavaDoc branch = (String JavaDoc) node.getLookup().lookup(String JavaDoc.class);
123             return branch;
124         } catch (UserCancelException e) {
125             return null;
126         }
127     }
128
129     /** Background runnable*/
130     public void run() {
131
132         GlobalOptions gtx = CvsVersioningSystem.createGlobalOptions();
133         if (root != null) {
134             gtx.setCVSRoot(root.toString()); // XXX why is it needed? Client already knows, who is definitive source of cvs root?
135
}
136         File JavaDoc checkoutFolder = null;
137         try {
138
139             File JavaDoc[] files;
140             File JavaDoc localPath;
141             if (file == null) {
142                 // netbeans.org rlog does not work, we need to create some sort of fake log command
143

144                 checkoutFolder = Kit.createTmpFolder();
145                 if (checkoutFolder == null) {
146                     error(org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2015"));
147                     return;
148                 }
149
150                 CheckoutCommand checkout = new CheckoutCommand();
151                 checkout.setRecursive(false);
152
153                 // non recursive operation doe snot work with "." module
154
// #58208 so here a random one is choosen
155
if (".".equals(module)) { // NOI18N
156
Client client = Kit.createClient(root);
157                     List JavaDoc l = ModuleSelector.listRepositoryPath(client, root, ""); // NOI18N
158
Iterator it = l.iterator();
159                     int max = l.size();
160                     int counter = max;
161                     Random random = new Random();
162                     while (counter-- > 0) {
163                         int rnd = random.nextInt(max);
164                         String JavaDoc path = (String JavaDoc) l.get(rnd);
165                         if ("CVSROOT".equals(path)) continue; // NOI18N
166
module = path;
167                         break;
168                     }
169                 }
170                 checkout.setModule(module);
171                 File JavaDoc[] checkoutFiles = new File JavaDoc[] {checkoutFolder};
172                 checkout.setFiles(checkoutFiles);
173
174                 Client client = Kit.createClient(root);
175                 client.setLocalPath(checkoutFolder.getAbsolutePath());
176                 client.executeCommand(checkout, gtx);
177
178                 files = checkoutFolder.listFiles();
179                 localPath = checkoutFolder;
180
181                 // seek for the first non-administrative file
182
for (int i = 0; i<files.length; i++) {
183                     if (files[i].isFile()) continue;
184                     if ("CVSROOT".equals(files[i].getName())) continue; // NOI18N
185
files = files[i].listFiles();
186                     break;
187                 }
188
189             } else {
190                 if (file.isDirectory()) {
191                     files = file.listFiles();
192                     localPath = file;
193                 } else {
194                     files = new File JavaDoc[] {file};
195                     localPath = file.getParentFile();
196                 }
197             }
198
199             List JavaDoc logFiles = new ArrayList(files.length);
200             fillLogFiles(files, logFiles);
201             if (logFiles.isEmpty()) {
202                 tagsLoaded(Collections.EMPTY_SET, Collections.EMPTY_SET);
203                 return;
204             }
205
206             // extract tags using log
207
LogCommand log = new LogCommand();
208             log.setHeaderOnly(true);
209             File JavaDoc[] cmdFiles = (File JavaDoc[]) logFiles.toArray(new File JavaDoc[logFiles.size()]);
210             log.setFiles(cmdFiles);
211             if (root == null) {
212                 for (int i = 0; i<cmdFiles.length; i++) {
213                     try {
214                         root = CVSRoot.parse(Utils.getCVSRootFor(cmdFiles[i])); // raises exception
215
break;
216                     } catch (IOException JavaDoc e) {
217                         ErrorManager err = ErrorManager.getDefault();
218                         err.annotate(e, "Can not find CVSROOT for " + cmdFiles[i]); // NOI18N
219
err.notify(ErrorManager.INFORMATIONAL, e);
220                     }
221                 }
222             }
223             Client client = Kit.createClient(root);
224             
225             final Set tags = new TreeSet();
226             final Set branches = new TreeSet();
227             EventManager mgr = client.getEventManager();
228             mgr.addCVSListener(new CVSListener() {
229
230                 public void messageSent(MessageEvent e) {
231                 }
232                 public void messageSent(BinaryMessageEvent e) {
233                 }
234                 public void fileAdded(FileAddedEvent e) {
235                 }
236                 public void fileToRemove(FileToRemoveEvent e) {
237                 }
238                 public void fileRemoved(FileRemovedEvent e) {
239                 }
240                 public void fileUpdated(FileUpdatedEvent e) {
241                 }
242                 public void fileInfoGenerated(FileInfoEvent e) {
243                     LogInformation info = (LogInformation) e.getInfoContainer();
244                     List JavaDoc symNames = info.getAllSymbolicNames();
245                     Iterator it = symNames.iterator();
246                     while (it.hasNext()) {
247                         LogInformation.SymName name = (LogInformation.SymName) it.next();
248                         if (name.isBranch()) {
249                             branches.add(name.getName());
250                         } else {
251                             tags.add(name.getName());
252                         }
253                     }
254                 }
255                 public void commandTerminated(TerminationEvent e) {
256                 }
257                 public void moduleExpanded(ModuleExpansionEvent e) {
258                 }
259             });
260             
261             client.setLocalPath(localPath.getAbsolutePath());
262             client.executeCommand(log, gtx);
263             tagsLoaded(branches, tags);
264             Kit.deleteRecursively(checkoutFolder);
265         } catch (CommandException e) {
266             error(org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2016"));
267             ErrorManager err = ErrorManager.getDefault();
268             err.annotate(e, org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2016"));
269             err.notify(e);
270         } catch (AuthenticationException e) {
271             error(org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2016"));
272             ErrorManager err = ErrorManager.getDefault();
273             err.annotate(e, org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2016"));
274             err.notify(e);
275         } finally {
276             Kit.deleteRecursively(checkoutFolder);
277         }
278     }
279
280     /**
281      * Try to recursively locate at least one versioned file.
282      */

283     private void fillLogFiles(File JavaDoc[] files, List JavaDoc logFiles) {
284
285         if (files == null) {
286             return;
287         }
288
289         if (logFiles.isEmpty() == false) {
290             return;
291         }
292
293         for (int i = 0; i<files.length; i++) {
294             if (files[i].isFile()) {
295                 FileInformation info = CvsVersioningSystem.getInstance().getStatusCache().getStatus(files[i]);
296                 if ((info.getStatus() & FileInformation.STATUS_IN_REPOSITORY) != 0) {
297                     logFiles.add(files[i]);
298                 }
299             }
300         }
301
302         for (int i = 0; i<files.length; i++) {
303             if (files[i].isDirectory()) {
304                 fillLogFiles(files[i].listFiles(), logFiles); // RESURSION
305
}
306         }
307
308     }
309
310     /**
311      * @param tags contains ModuleListInformation
312      */

313     private void tagsLoaded(Collection branches, Collection tags) {
314         rootKids.setBranches(branches);
315         rootKids.setTags(tags);
316     }
317
318     private void error(String JavaDoc msg) {
319         rootNode.setDisplayName(msg);
320     }
321
322 }
323
Popular Tags