View Javadoc

1   /*
2    * Copyright (c) 2003, Andreas Pataki 
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.
11   * 
12   * Redistributions in binary form must reproduce the above copyright notice,
13   * this list of conditions and the following disclaimer in the documentation
14   * and/or other materials provided with the distribution.
15   * 
16   * Neither the name of the author nor the names of its contributors
17   * may be used to endorse or promote products derived from this software
18   * without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS
21   * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
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              // root requested, build server 
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             // create manager
113             TomcatServer server =
114                 (TomcatServer) ((TreeNode) parent).getContent();
115             TomcatContextManager manager = new TomcatContextManager(server);
116 
117             try {
118                 // create tree nodes from context list
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 }