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
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal.view;
13 import java.util.List;
14 import java.util.ArrayList;
15 import java.util.Iterator;
17 import net.sourceforge.phpdt.monitor.core.IRequest;
18 import net.sourceforge.phpdt.monitor.core.MonitorCore;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
23 * Content provider for the monitor server view.
25 public class MonitorTreeContentProvider implements ITreeContentProvider {
26 protected static final String ROOT = "root";
28 protected boolean sortByResponseTime;
31 * ProxyTreeContentProvider constructor comment.
33 public MonitorTreeContentProvider() {
38 * Disposes of this content provider.
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).
48 public void dispose() { }
51 * Returns an iterator over the child elements of the given element.
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
61 * [Issue: Don't know what above is trying to say.
62 * See IStructuredContentProvider.getElements.
66 * @param element the element
67 * @return an iterator over the child elements
68 * (element type: <code>Object</code>)
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())
80 if (sortByResponseTime)
81 sortByResponseTime(list);
82 return list.toArray();
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.
92 * [Issue: Should return Object[] rather than Iterator.
96 * [Issue: Not clear what "belonging to the given element"
97 * means. See ITreeContentProvider.getChildren.
101 * @param element the element
102 * @return an iterator over the elements
103 * (element type: <code>Object</code>)
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))
116 if (sortByResponseTime)
117 sortByResponseTime(list);
119 return list.toArray();
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.
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
134 public Object getParent(Object element) {
135 if (element != null) {
136 if (element instanceof Integer)
139 IRequest call = (IRequest) element;
140 return new Integer(call.getLocalPort());
147 * Returns true if the elements are currently being sorted by response time.
150 public boolean getSortByResponseTime() {
151 return sortByResponseTime;
155 * Returns whether the given element has children.
157 * [Issue: This method may not be warranted if getChildren
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
166 public boolean hasChildren(Object element) {
167 return (element instanceof Integer);
171 * Notifies this content provider that the given viewer's input
172 * has been switched to a different element.
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.
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
186 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
189 * Sets the sort by response time option.
192 public void setSortByResponseTime(boolean b) {
193 sortByResponseTime = b;
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()) {