inital plugin from webtools project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.ui / src / net / sourceforge / phpdt / monitor / ui / internal / view / MonitorTreeContentProvider.java
1 /**********************************************************************
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal.view;
12
13 import java.util.List;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16
17 import net.sourceforge.phpdt.monitor.core.IRequest;
18 import net.sourceforge.phpdt.monitor.core.MonitorCore;
19
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
22 /**
23  * Content provider for the monitor server view.
24  */
25 public class MonitorTreeContentProvider implements ITreeContentProvider {
26         protected static final String ROOT = "root";
27
28         protected boolean sortByResponseTime;
29
30         /**
31          * ProxyTreeContentProvider constructor comment.
32          */
33         public MonitorTreeContentProvider() {
34                 super();
35         }
36         
37         /**
38          * Disposes of this content provider.
39          * <p>
40          * [Issue: This method should be changed to take a Viewer,
41          * renamed and repurposed to disconnect a content provider from
42          * a viewer. This is over and above what inputChanged does,
43          * which is disconnecting a content provider from the viewer's
44          * input (but not the viewer itself).
45          * ]
46          * </p>
47          */
48         public void dispose() { }
49         
50         /**
51          * Returns an iterator over the child elements of the given element.
52          * <p>
53          * Note: The difference between this method and 
54          * <code>IStructuredContentProvider.getElements</code> is
55          * that <code>getElements</code> is called to obtain the 
56          * tree viewer's root elements, whereas <code>getChildren</code> is used
57          * to obtain the children of a given node in the tree
58          * (including a root).
59          * </p>
60          * <p>
61          * [Issue: Don't know what above is trying to say.
62          *  See IStructuredContentProvider.getElements.
63          * ]
64          * </p>
65          *
66          * @param element the element
67          * @return an iterator over the child elements 
68          *    (element type: <code>Object</code>)
69          */
70         public Object[] getChildren(Object element) {
71                 if (element instanceof Integer) {
72                         Integer in = (Integer) element;
73                         List list = new ArrayList();
74                         Iterator iterator = MonitorCore.getRequests().iterator();
75                         while (iterator.hasNext()) {
76                                 IRequest call = (IRequest) iterator.next();
77                                 if (call.getLocalPort() == in.intValue())
78                                         list.add(call);
79                         }
80                         if (sortByResponseTime)
81                                 sortByResponseTime(list);
82                         return list.toArray();
83                 }
84                 return null;
85         }
86         
87         /**
88          * Returns an iterator over the elements belonging to the
89          * given element. These elements can be presented as rows in a table,
90          * items in a list, etc.
91          * <p>
92          * [Issue: Should return Object[] rather than Iterator.
93          * ]
94          * </p>
95          * <p>
96          * [Issue: Not clear what "belonging to the given element"
97          *  means. See ITreeContentProvider.getChildren.
98          * ]
99          * </p>
100          *
101          * @param element the element
102          * @return an iterator over the elements 
103          *    (element type: <code>Object</code>)
104          */
105         public Object[] getElements(Object element) {
106                 if (ROOT.equals(element)) {
107                         List list = new ArrayList();
108                         Iterator iterator = MonitorCore.getRequests().iterator();
109                         while (iterator.hasNext()) {
110                                 IRequest call = (IRequest) iterator.next();
111                                 Integer in = new Integer(call.getLocalPort());
112                                 if (!list.contains(in))
113                                         list.add(in);
114                         }
115         
116                         if (sortByResponseTime)
117                                 sortByResponseTime(list);
118         
119                         return list.toArray();
120                 } else
121                         return null;
122         }
123         
124         /**
125          * Returns the parent for the given element, or <code>null</code> 
126          * indicating that the parent can't be computed. 
127          * In this case the tree-structured viewer can't expand
128          * a given node correctly if requested.
129          *
130          * @param element the element
131          * @return the parent element, or <code>null</code> if it
132          *   has none or if the parent cannot be computed
133          */
134         public Object getParent(Object element) {
135                 if (element != null) {
136                         if (element instanceof Integer)
137                                 return ROOT;
138                         else {
139                                 IRequest call = (IRequest) element;
140                                 return new Integer(call.getLocalPort());
141                         }
142                 } else
143                         return null;
144         }
145         
146         /**
147          * Returns true if the elements are currently being sorted by response time.
148          * @return boolean
149          */
150         public boolean getSortByResponseTime() {
151                 return sortByResponseTime;
152         }
153         
154         /**
155          * Returns whether the given element has children.
156          * <p>
157          * [Issue: This method may not be warranted if getChildren
158          *  return Object[].
159          * ]
160          * </p>
161          *
162          * @param element the element
163          * @return <code>true</code> if the given element has children,
164          *  and <code>false</code> if it has no children
165          */
166         public boolean hasChildren(Object element) {
167                 return (element instanceof Integer);
168         }
169         
170         /**
171          * Notifies this content provider that the given viewer's input
172          * has been switched to a different element.
173          * <p>
174          * A typical use for this method is registering the content provider as a listener
175          * to changes on the new input (using model-specific means), and deregistering the viewer 
176          * from the old input. In response to these change notifications, the content provider
177          * propagates the changes to the viewer.
178          * </p>
179          *
180          * @param viewer the viewer
181          * @param oldInput the old input element, or <code>null</code> if the viewer
182          *   did not previously have an input
183          * @param newInput the new input element, or <code>null</code> if the viewer
184          *   does not have an input
185          */
186         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
187         
188         /**
189          * Sets the sort by response time option.
190          * @param b boolean
191          */
192         public void setSortByResponseTime(boolean b) {
193                 sortByResponseTime = b;
194         }
195         
196         /**
197          * 
198          */
199         protected void sortByResponseTime(List list) {
200                 int size = list.size();
201                 for (int i = 0; i < size - 1; i++) {
202                         for (int j = i + 1; j < size; j++) {
203                                 IRequest c1 = (IRequest) list.get(i);
204                                 IRequest c2 = (IRequest) list.get(j);
205                                 if (c1.getResponseTime() < c2.getResponseTime()) {
206                                         list.set(i, c2);
207                                         list.set(j, c1);
208                                 }
209                         }
210                 }
211                 
212         }
213 }