KickJava   Java API By Example, From Geeks To Geeks.

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


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.admin.AdminHandler;
25 import org.netbeans.lib.cvsclient.event.*;
26 import org.netbeans.lib.cvsclient.connection.*;
27 import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
28 import org.netbeans.lib.cvsclient.command.checkout.ModuleListInformation;
29 import org.netbeans.lib.cvsclient.command.GlobalOptions;
30 import org.netbeans.lib.cvsclient.command.CommandException;
31 import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
32 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
33 import org.openide.nodes.*;
34 import org.openide.util.UserCancelException;
35 import org.openide.util.HelpCtx;
36 import org.openide.filesystems.FileUtil;
37
38 import java.util.*;
39 import java.util.List JavaDoc;
40 import java.io.File JavaDoc;
41
42 /**
43  * Prototype impl of defined modules listing.
44  *
45  * @author Petr Kuzel
46  */

47 public final class ModuleSelector {
48
49     /**
50      * Asks user to select which module to checkout. Popups a modal UI,
51      * @param root identifies repository
52      * @return Set of String, possibly empty
53      */

54     public Set selectModules(CVSRoot root) {
55
56         // create top level node that categorizes to aliases and raw browser
57

58         Children.Array kids = new Children.Array();
59         Client.Factory clientFactory = Kit.createClientFactory(root);
60         Node aliasesNode = AliasesNode.create(clientFactory, root);
61         Node pathsNode = RepositoryPathNode.create(clientFactory, root, ""); // NOI18N
62
kids.add(new Node[] {aliasesNode, pathsNode});
63         Node rootNode = new AbstractNode(kids);
64
65         try {
66             NodeOperation2 op = new NodeOperation2();
67             op.setRootVisible(false);
68             op.setHelpCtx(new HelpCtx(ModuleSelector.class));
69             Node[] selected = op.select(org.openide.util.NbBundle.getMessage(ModuleSelector.class, "BK2019"),
70                                         org.openide.util.NbBundle.getMessage(ModuleSelector.class, "BK2020"),
71                                         org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSD_ModuleSelect"),
72                                         rootNode,
73                                         org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSN_ModulesTree"),
74                                         org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSD_ModulesTree"),
75                                         new NodeAcceptor() {
76                 public boolean acceptNodes(Node[] nodes) {
77                     boolean ret = nodes.length > 0;
78                     for (int i = 0; i < nodes.length; i++) {
79                         Node node = nodes[i];
80                         String JavaDoc path = (String JavaDoc) node.getLookup().lookup(String JavaDoc.class);
81                         ret &= path != null;
82                     }
83                     return ret;
84                 }
85             });
86
87             Set modules = new LinkedHashSet();
88             for (int i = 0; i < selected.length; i++) {
89                 Node node = selected[i];
90                 String JavaDoc path = (String JavaDoc) node.getLookup().lookup(String JavaDoc.class);
91                 modules.add(path);
92             }
93             return modules;
94         } catch (UserCancelException e) {
95             return Collections.EMPTY_SET;
96         }
97     }
98
99     /*
100      * Pupup modal UI and let user select repositpry path.
101      *
102      * @param root identifies repository
103      * @param proxy defines which proxy to use or null
104      * to use one from CVsRootSettings.
105      * @return '/' separated path or null on cancel.
106      */

107     public String JavaDoc selectRepositoryPath(CVSRoot root) {
108
109         Client.Factory clientFactory = Kit.createClientFactory(root);
110         Node pathsNode = RepositoryPathNode.create(clientFactory, root, ""); // NOI18N
111

112         try {
113             Node[] selected = NodeOperation.getDefault().select(org.openide.util.NbBundle.getMessage(ModuleSelector.class, "BK2021"), org.openide.util.NbBundle.getMessage(ModuleSelector.class, "BK2022"), pathsNode, new NodeAcceptor() {
114                 public boolean acceptNodes(Node[] nodes) {
115                     if (nodes.length == 1) {
116                         String JavaDoc path = (String JavaDoc) nodes[0].getLookup().lookup(String JavaDoc.class);
117                         return path != null;
118                     }
119                     return false;
120                 }
121             });
122
123             String JavaDoc path = null;
124             if (selected.length == 1) {
125                 path = (String JavaDoc) selected[0].getLookup().lookup(String JavaDoc.class);
126             }
127             return path;
128         } catch (UserCancelException e) {
129             return null;
130         }
131     }
132
133     private static final String JavaDoc MAGIC_START = ": New directory `"; // NOI18N
134
private static final String JavaDoc MAGIC_END = "' -- ignored"; // NOI18N
135

136     /**
137      * Lists subfolders in given repository folder.
138      *
139      * @param client engine to be used
140      * @param root identifies repository
141      * @param path "/" separated repository folder path (e.g. "javacvs/cvsmodule")
142      * @return folders never <code>null</code>
143      */

144     public static List JavaDoc listRepositoryPath(Client client, CVSRoot root, String JavaDoc path) throws CommandException, AuthenticationException {
145
146         final List JavaDoc list = new ArrayList();
147         GlobalOptions gtx = CvsVersioningSystem.createGlobalOptions();
148         gtx.setCVSRoot(root.toString());
149         gtx.setDoNoChanges(true);
150
151         UpdateCommand blindUpdate = new UpdateCommand();
152         blindUpdate.setBuildDirectories(true);
153
154         AdminHandler localEnv = new VirtualAdminHandler(root, path); // NOI18N
155
client.setAdminHandler(localEnv);
156         String JavaDoc tmpDir = System.getProperty("java.io.tmpdir"); // NOI18N
157
File JavaDoc tmp = new File JavaDoc(tmpDir);
158         tmp = FileUtil.normalizeFile(tmp);
159         client.setLocalPath(tmp.getAbsolutePath());
160         EventManager mgr = client.getEventManager();
161         mgr.addCVSListener(new CVSListener() {
162             public void messageSent(MessageEvent e) {
163                 if (e.isError()) {
164                     String JavaDoc message = e.getMessage();
165                     if (message.endsWith(MAGIC_END)) { // NOI18N
166
int start = message.indexOf(MAGIC_START);
167                         if (start != -1) {
168                             int pathStart = start + MAGIC_START.length();
169                             int pathEnd = message.length() - MAGIC_END.length();
170                             String JavaDoc path = message.substring(pathStart, pathEnd);
171                             list.add(path);
172                         }
173                     }
174                 }
175             }
176             public void messageSent(BinaryMessageEvent e) {
177             }
178             public void fileAdded(FileAddedEvent e) {
179             }
180             public void fileToRemove(FileToRemoveEvent e) {
181             }
182             public void fileRemoved(FileRemovedEvent e) {
183             }
184             public void fileUpdated(FileUpdatedEvent e) {
185             }
186             public void fileInfoGenerated(FileInfoEvent e) {
187             }
188             public void commandTerminated(TerminationEvent e) {
189             }
190             public void moduleExpanded(ModuleExpansionEvent e) {
191             }
192         });
193         client.executeCommand(blindUpdate, gtx);
194
195         return list;
196     }
197
198     /**
199      * Lists defined aliases in given repository.
200      *
201      * @return list of ModuleListInformation
202      */

203     public static List JavaDoc listAliases(Client client, CVSRoot root) throws CommandException, AuthenticationException {
204
205         CheckoutCommand checkout = new CheckoutCommand();
206         checkout.setShowModules(true);
207         final List JavaDoc modules = new LinkedList();
208         EventManager mgr = client.getEventManager();
209         mgr.addCVSListener(new CVSListener() {
210             public void messageSent(MessageEvent e) {
211             }
212             public void messageSent(BinaryMessageEvent e) {
213             }
214             public void fileAdded(FileAddedEvent e) {
215             }
216             public void fileToRemove(FileToRemoveEvent e) {
217             }
218             public void fileRemoved(FileRemovedEvent e) {
219             }
220             public void fileUpdated(FileUpdatedEvent e) {
221             }
222             public void fileInfoGenerated(FileInfoEvent e) {
223                 ModuleListInformation moduleList = (ModuleListInformation) e.getInfoContainer();
224                 modules.add(moduleList);
225             }
226             public void commandTerminated(TerminationEvent e) {
227             }
228             public void moduleExpanded(ModuleExpansionEvent e) {
229             }
230         });
231
232         GlobalOptions gtx = CvsVersioningSystem.createGlobalOptions();
233         gtx.setCVSRoot(root.toString()); // XXX why is it needed? Client already knows, who is definitive source of cvs root?
234
client.executeCommand(checkout, gtx);
235
236         return modules;
237     }
238 }
239
Popular Tags