001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.versioning.system.cvss.ui.selectors;
043:
044: import org.netbeans.lib.cvsclient.CVSRoot;
045: import org.netbeans.lib.cvsclient.Client;
046: import org.netbeans.lib.cvsclient.admin.AdminHandler;
047: import org.netbeans.lib.cvsclient.event.*;
048: import org.netbeans.lib.cvsclient.connection.*;
049: import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
050: import org.netbeans.lib.cvsclient.command.checkout.ModuleListInformation;
051: import org.netbeans.lib.cvsclient.command.GlobalOptions;
052: import org.netbeans.lib.cvsclient.command.CommandException;
053: import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
054: import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
055: import org.openide.nodes.*;
056: import org.openide.util.UserCancelException;
057: import org.openide.util.HelpCtx;
058: import org.openide.filesystems.FileUtil;
059:
060: import java.util.*;
061: import java.util.List;
062: import java.io.File;
063:
064: /**
065: * Prototype impl of defined modules listing.
066: *
067: * @author Petr Kuzel
068: */
069: public final class ModuleSelector {
070:
071: /**
072: * Asks user to select which module to checkout. Popups a modal UI,
073: * @param root identifies repository
074: * @return Set of String, possibly empty
075: */
076: public Set selectModules(CVSRoot root) {
077:
078: // create top level node that categorizes to aliases and raw browser
079:
080: Children.Array kids = new Children.Array();
081: Client.Factory clientFactory = Kit.createClientFactory(root);
082: Node aliasesNode = AliasesNode.create(clientFactory, root);
083: Node pathsNode = RepositoryPathNode.create(clientFactory, root,
084: ""); // NOI18N
085: kids.add(new Node[] { aliasesNode, pathsNode });
086: Node rootNode = new AbstractNode(kids);
087:
088: try {
089: NodeOperation2 op = new NodeOperation2();
090: op.setRootVisible(false);
091: op.setHelpCtx(new HelpCtx(ModuleSelector.class));
092: Node[] selected = op.select(org.openide.util.NbBundle
093: .getMessage(ModuleSelector.class, "BK2019"),
094: org.openide.util.NbBundle.getMessage(
095: ModuleSelector.class, "BK2020"),
096: org.openide.util.NbBundle.getMessage(
097: BranchSelector.class, "ACSD_ModuleSelect"),
098: rootNode, org.openide.util.NbBundle.getMessage(
099: BranchSelector.class, "ACSN_ModulesTree"),
100: org.openide.util.NbBundle.getMessage(
101: BranchSelector.class, "ACSD_ModulesTree"),
102: new NodeAcceptor() {
103: public boolean acceptNodes(Node[] nodes) {
104: boolean ret = nodes.length > 0;
105: for (int i = 0; i < nodes.length; i++) {
106: Node node = nodes[i];
107: String path = (String) node.getLookup()
108: .lookup(String.class);
109: ret &= path != null;
110: }
111: return ret;
112: }
113: });
114:
115: Set<String> modules = new LinkedHashSet<String>();
116: for (int i = 0; i < selected.length; i++) {
117: Node node = selected[i];
118: String path = (String) node.getLookup().lookup(
119: String.class);
120: modules.add(path);
121: }
122: return modules;
123: } catch (UserCancelException e) {
124: return Collections.EMPTY_SET;
125: }
126: }
127:
128: /*
129: * Pupup modal UI and let user select repositpry path.
130: *
131: * @param root identifies repository
132: * @param proxy defines which proxy to use or null
133: * to use one from CVsRootSettings.
134: * @return '/' separated path or null on cancel.
135: */
136: public String selectRepositoryPath(CVSRoot root) {
137:
138: Client.Factory clientFactory = Kit.createClientFactory(root);
139: Node pathsNode = RepositoryPathNode.create(clientFactory, root,
140: ""); // NOI18N
141:
142: try {
143: Node[] selected = NodeOperation.getDefault().select(
144: org.openide.util.NbBundle.getMessage(
145: ModuleSelector.class, "BK2021"),
146: org.openide.util.NbBundle.getMessage(
147: ModuleSelector.class, "BK2022"), pathsNode,
148: new NodeAcceptor() {
149: public boolean acceptNodes(Node[] nodes) {
150: if (nodes.length == 1) {
151: String path = (String) nodes[0]
152: .getLookup().lookup(
153: String.class);
154: return path != null;
155: }
156: return false;
157: }
158: });
159:
160: String path = null;
161: if (selected.length == 1) {
162: path = (String) selected[0].getLookup().lookup(
163: String.class);
164: }
165: return path;
166: } catch (UserCancelException e) {
167: return null;
168: }
169: }
170:
171: private static final String MAGIC_START = ": New directory `"; // NOI18N
172: private static final String MAGIC_END = "' -- ignored"; // NOI18N
173:
174: /**
175: * Lists subfolders in given repository folder.
176: *
177: * @param client engine to be used
178: * @param root identifies repository
179: * @param path "/" separated repository folder path (e.g. "javacvs/cvsmodule")
180: * @return folders never <code>null</code>
181: */
182: public static List<String> listRepositoryPath(Client client,
183: CVSRoot root, String path) throws CommandException,
184: AuthenticationException {
185:
186: final List<String> list = new ArrayList<String>();
187: GlobalOptions gtx = CvsVersioningSystem.createGlobalOptions();
188: gtx.setCVSRoot(root.toString());
189: gtx.setDoNoChanges(true);
190:
191: UpdateCommand blindUpdate = new UpdateCommand();
192: blindUpdate.setBuildDirectories(true);
193:
194: AdminHandler localEnv = new VirtualAdminHandler(root, path); // NOI18N
195: client.setAdminHandler(localEnv);
196: String tmpDir = System.getProperty("java.io.tmpdir"); // NOI18N
197: File tmp = new File(tmpDir);
198: tmp = FileUtil.normalizeFile(tmp);
199: client.setLocalPath(tmp.getAbsolutePath());
200: EventManager mgr = client.getEventManager();
201: mgr.addCVSListener(new CVSListener() {
202: public void messageSent(MessageEvent e) {
203: if (e.isError()) {
204: String message = e.getMessage();
205: if (message.endsWith(MAGIC_END)) { // NOI18N
206: int start = message.indexOf(MAGIC_START);
207: if (start != -1) {
208: int pathStart = start
209: + MAGIC_START.length();
210: int pathEnd = message.length()
211: - MAGIC_END.length();
212: String path = message.substring(pathStart,
213: pathEnd);
214: list.add(path);
215: }
216: }
217: }
218: }
219:
220: public void messageSent(BinaryMessageEvent e) {
221: }
222:
223: public void fileAdded(FileAddedEvent e) {
224: }
225:
226: public void fileToRemove(FileToRemoveEvent e) {
227: }
228:
229: public void fileRemoved(FileRemovedEvent e) {
230: }
231:
232: public void fileUpdated(FileUpdatedEvent e) {
233: }
234:
235: public void fileInfoGenerated(FileInfoEvent e) {
236: }
237:
238: public void commandTerminated(TerminationEvent e) {
239: }
240:
241: public void moduleExpanded(ModuleExpansionEvent e) {
242: }
243: });
244: client.executeCommand(blindUpdate, gtx);
245:
246: return list;
247: }
248:
249: /**
250: * Lists defined aliases in given repository.
251: *
252: * @return list of ModuleListInformation
253: */
254: public static List<ModuleListInformation> listAliases(
255: Client client, CVSRoot root) throws CommandException,
256: AuthenticationException {
257:
258: CheckoutCommand checkout = new CheckoutCommand();
259: checkout.setShowModules(true);
260: final List<ModuleListInformation> modules = new LinkedList<ModuleListInformation>();
261: EventManager mgr = client.getEventManager();
262: mgr.addCVSListener(new CVSListener() {
263: public void messageSent(MessageEvent e) {
264: }
265:
266: public void messageSent(BinaryMessageEvent e) {
267: }
268:
269: public void fileAdded(FileAddedEvent e) {
270: }
271:
272: public void fileToRemove(FileToRemoveEvent e) {
273: }
274:
275: public void fileRemoved(FileRemovedEvent e) {
276: }
277:
278: public void fileUpdated(FileUpdatedEvent e) {
279: }
280:
281: public void fileInfoGenerated(FileInfoEvent e) {
282: ModuleListInformation moduleList = (ModuleListInformation) e
283: .getInfoContainer();
284: modules.add(moduleList);
285: }
286:
287: public void commandTerminated(TerminationEvent e) {
288: }
289:
290: public void moduleExpanded(ModuleExpansionEvent e) {
291: }
292: });
293:
294: GlobalOptions gtx = CvsVersioningSystem.createGlobalOptions();
295: gtx.setCVSRoot(root.toString()); // XXX why is it needed? Client already knows, who is definitive source of cvs root?
296: client.executeCommand(checkout, gtx);
297:
298: return modules;
299: }
300: }
|