KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.nodes.AbstractNode;
23 import org.openide.nodes.Children;
24 import org.openide.nodes.Node;
25 import org.openide.util.Lookup;
26 import org.openide.util.RequestProcessor;
27 import org.openide.util.lookup.Lookups;
28 import org.netbeans.lib.cvsclient.CVSRoot;
29 import org.netbeans.lib.cvsclient.Client;
30 import org.netbeans.lib.cvsclient.connection.AuthenticationException;
31 import org.netbeans.lib.cvsclient.command.CommandException;
32
33 import javax.swing.*;
34 import java.util.Collections JavaDoc;
35 import java.util.List JavaDoc;
36 import java.awt.*;
37 import java.beans.BeanInfo JavaDoc;
38
39 /**
40  * Represents a path in repository, its children
41  * subfolders.
42  *
43  * <p>Lookup contains a string identifing rpresented path.
44  *
45  * @author Petr Kuzel
46  */

47 public class RepositoryPathNode extends AbstractNode {
48
49     public static RepositoryPathNode create(Client.Factory clientFactory, CVSRoot root, String JavaDoc path) {
50
51         assert path.startsWith("/") == false : path; // NOI18N
52

53         RepositoryPathChildren kids = new RepositoryPathChildren(clientFactory, root, path);
54         Lookup lookup = Lookups.singleton(path);
55         RepositoryPathNode node = new RepositoryPathNode(kids, lookup);
56
57         String JavaDoc name = root.getRepository();
58         if (path.equals("") == false) { // NOI18N
59
String JavaDoc[] atoms = path.split("/"); // NOI18N
60
if (atoms.length > 0) {
61                 name = atoms[atoms.length -1];
62             }
63         }
64         node.setDisplayName(name);
65         return node;
66     }
67
68     private RepositoryPathNode(Children children, Lookup lookup) {
69         super(children, lookup);
70         setIconBaseWithExtension("org/netbeans/modules/versioning/system/cvss/ui/selectors/defaultFolder.gif"); // NOI18N
71
}
72
73     public Image getIcon(int type) {
74         Image img = null;
75         if (type == BeanInfo.ICON_COLOR_16x16) {
76             img = (Image)UIManager.get("Nb.Explorer.Folder.icon"); // NOI18N
77
}
78         if (img == null) {
79             img = super.getIcon(type);
80         }
81         return img;
82     }
83
84     public Image getOpenedIcon(int type) {
85         Image img = null;
86         if (type == BeanInfo.ICON_COLOR_16x16) {
87             img = (Image)UIManager.get("Nb.Explorer.Folder.openedIcon"); // NOI18N
88
}
89         if (img == null) {
90             img = super.getIcon(type);
91         }
92         return img;
93     }
94
95     static class RepositoryPathChildren extends Children.Keys implements Runnable JavaDoc {
96
97         private final Client.Factory clientFactory;
98         private final CVSRoot root;
99         private final String JavaDoc path;
100         private RequestProcessor.Task task;
101
102         public RepositoryPathChildren(Client.Factory client, CVSRoot root, String JavaDoc path) {
103             this.clientFactory = client;
104             this.root = root;
105             this.path = path;
106         }
107
108         protected void addNotify() {
109             super.addNotify();
110             AbstractNode waitNode = new WaitNode(org.openide.util.NbBundle.getMessage(RepositoryPathNode.class, "BK2024"));
111             setKeys(Collections.singleton(waitNode));
112             RequestProcessor rp = RequestProcessor.getDefault();
113             task = rp.post(this);
114         }
115
116         protected void removeNotify() {
117             task.cancel();
118             setKeys(Collections.EMPTY_SET);
119             super.removeNotify();
120         }
121
122         protected Node[] createNodes(Object JavaDoc key) {
123             if (key instanceof Node) {
124                 return new Node[] {(Node)key};
125             }
126
127             String JavaDoc relPath = path.equals("") ? (String JavaDoc) key : path + "/" + key; // NOI18N
128
Node pathNode = RepositoryPathNode.create(clientFactory, root, relPath);
129             return new Node[] {pathNode};
130         }
131
132         public void run() {
133             try {
134                 List JavaDoc keys = ModuleSelector.listRepositoryPath(clientFactory.createClient(), root, path);
135                 setKeys(keys);
136             } catch (CommandException e) {
137                 setKeys(Collections.singleton(errorNode(e)));
138             } catch (AuthenticationException e) {
139                 setKeys(Collections.singleton(errorNode(e)));
140             }
141         }
142
143         private Node errorNode(Exception JavaDoc ex) {
144             AbstractNode errorNode = new AbstractNode(Children.LEAF);
145             errorNode.setDisplayName(org.openide.util.NbBundle.getMessage(RepositoryPathNode.class, "BK2025"));
146             errorNode.setShortDescription(ex.getLocalizedMessage());
147             return errorNode;
148         }
149     }
150
151 }
152
Popular Tags