1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package net.sf.tm.plugin;
34
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.List;
38
39 import net.sf.tm.ContextManagerException;
40 import net.sf.tm.WebContext;
41 import net.sf.tm.tomcat.TomcatContextManager;
42 import net.sf.tm.tomcat.TomcatServer;
43
44 import org.apache.commons.lang.builder.CompareToBuilder;
45 import org.eclipse.core.resources.ResourcesPlugin;
46 import org.eclipse.jface.viewers.IStructuredContentProvider;
47 import org.eclipse.jface.viewers.ITreeContentProvider;
48 import org.eclipse.jface.viewers.TreeViewer;
49 import org.eclipse.jface.viewers.Viewer;
50
51 /***
52 * @author Andreas Pataki
53 * @version $Id: WebContextContentProvider.java,v 1.7 2004/02/23 19:18:49 apataki Exp $
54 */
55 public class WebContextContentProvider
56 implements IStructuredContentProvider, ITreeContentProvider {
57
58 /***
59 * The tree this provider is bound to
60 */
61 private TreeViewer tree;
62
63 /***
64 * Instance of the server persistence store used by this provider
65 */
66 private ServerStore store;
67
68 /***
69 *
70 */
71 public WebContextContentProvider() {
72 store = ServerStore.getInstance();
73 }
74
75 /***
76 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
77 */
78 public Object[] getElements(Object parent) {
79 if (parent.equals(ResourcesPlugin.getWorkspace())) {
80
81 TreeNode[] servers = new TreeNode[store.getSize()];
82 for (int i = 0; i < store.getSize(); i++) {
83 servers[i] = new TreeNode(store.get(i));
84 }
85
86 return servers;
87 } else {
88 return getChildren(parent);
89 }
90 }
91
92 /***
93 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
94 */
95 public void dispose() {
96 }
97
98 /***
99 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
100 */
101 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
102 tree = (TreeViewer) viewer;
103 }
104
105 /***
106 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
107 */
108 public Object[] getChildren(Object parent) {
109 if (parent instanceof TreeNode
110 && ((TreeNode) parent).getContent() instanceof TomcatServer) {
111
112
113 TomcatServer server =
114 (TomcatServer) ((TreeNode) parent).getContent();
115 TomcatContextManager manager = new TomcatContextManager(server);
116
117 try {
118
119 List contextList = manager.getAll();
120
121 sortContextList(contextList);
122
123 TreeNode[] children = new TreeNode[contextList.size()];
124 for (int i = 0; i < contextList.size(); i++) {
125 children[i] =
126 new TreeNode(contextList.get(i), (TreeNode) parent);
127 }
128
129 return children;
130 } catch (ContextManagerException e) {
131
132 tree.collapseToLevel(parent, TreeViewer.ALL_LEVELS);
133
134 ContextManagerPlugin.getDefault().showError(
135 ContextManagerPlugin.getDefault().getLocalizedString(
136 "error.getChildren",
137 server.getHost() + ":" + server.getPort()),
138 e);
139
140 Logger.error("error while getting context's for " + parent, e);
141
142 }
143 }
144 return null;
145 }
146
147 /***
148 * Sorts the list of context objects alphabetically
149 *
150 * @param contextList The unsorted list
151 */
152 private void sortContextList(List contextList) {
153 Collections.sort(contextList, new Comparator() {
154
155 public int compare(Object o1, Object o2) {
156
157 return new CompareToBuilder()
158 .append(
159 ((WebContext) o1).getPath(),
160 ((WebContext) o2).getPath())
161 .toComparison();
162 }
163
164 });
165 }
166
167 /***
168 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
169 */
170 public Object getParent(Object node) {
171 if (node instanceof TreeNode) {
172 return ((TreeNode) node).getParent();
173 }
174 return null;
175 }
176
177 /***
178 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
179 */
180 public boolean hasChildren(Object node) {
181 return (
182 node instanceof TreeNode
183 && ((TreeNode) node).getContent() instanceof TomcatServer);
184 }
185
186 }