/********************************************************************** * Copyright (c) 2003 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html  * * Contributors: * IBM - Initial API and implementation **********************************************************************/ package net.sourceforge.phpdt.monitor.ui.internal.view; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import net.sourceforge.phpdt.monitor.core.IRequest; import net.sourceforge.phpdt.monitor.core.MonitorCore; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ITreeContentProvider; /** * Content provider for the monitor server view. */ public class MonitorTreeContentProvider implements ITreeContentProvider { protected static final String ROOT = "root"; protected boolean sortByResponseTime; /** * ProxyTreeContentProvider constructor comment. */ public MonitorTreeContentProvider() { super(); } /** * Disposes of this content provider. *

* [Issue: This method should be changed to take a Viewer, * renamed and repurposed to disconnect a content provider from * a viewer. This is over and above what inputChanged does, * which is disconnecting a content provider from the viewer's * input (but not the viewer itself). * ] *

*/ public void dispose() { } /** * Returns an iterator over the child elements of the given element. *

* Note: The difference between this method and * IStructuredContentProvider.getElements is * that getElements is called to obtain the * tree viewer's root elements, whereas getChildren is used * to obtain the children of a given node in the tree * (including a root). *

*

* [Issue: Don't know what above is trying to say. * See IStructuredContentProvider.getElements. * ] *

* * @param element the element * @return an iterator over the child elements * (element type: Object) */ public Object[] getChildren(Object element) { if (element instanceof Integer) { Integer in = (Integer) element; List list = new ArrayList(); Iterator iterator = MonitorCore.getRequests().iterator(); while (iterator.hasNext()) { IRequest call = (IRequest) iterator.next(); if (call.getLocalPort() == in.intValue()) list.add(call); } if (sortByResponseTime) sortByResponseTime(list); return list.toArray(); } return null; } /** * Returns an iterator over the elements belonging to the * given element. These elements can be presented as rows in a table, * items in a list, etc. *

* [Issue: Should return Object[] rather than Iterator. * ] *

*

* [Issue: Not clear what "belonging to the given element" * means. See ITreeContentProvider.getChildren. * ] *

* * @param element the element * @return an iterator over the elements * (element type: Object) */ public Object[] getElements(Object element) { if (ROOT.equals(element)) { List list = new ArrayList(); Iterator iterator = MonitorCore.getRequests().iterator(); while (iterator.hasNext()) { IRequest call = (IRequest) iterator.next(); Integer in = new Integer(call.getLocalPort()); if (!list.contains(in)) list.add(in); } if (sortByResponseTime) sortByResponseTime(list); return list.toArray(); } else return null; } /** * Returns the parent for the given element, or null * indicating that the parent can't be computed. * In this case the tree-structured viewer can't expand * a given node correctly if requested. * * @param element the element * @return the parent element, or null if it * has none or if the parent cannot be computed */ public Object getParent(Object element) { if (element != null) { if (element instanceof Integer) return ROOT; else { IRequest call = (IRequest) element; return new Integer(call.getLocalPort()); } } else return null; } /** * Returns true if the elements are currently being sorted by response time. * @return boolean */ public boolean getSortByResponseTime() { return sortByResponseTime; } /** * Returns whether the given element has children. *

* [Issue: This method may not be warranted if getChildren * return Object[]. * ] *

* * @param element the element * @return true if the given element has children, * and false if it has no children */ public boolean hasChildren(Object element) { return (element instanceof Integer); } /** * Notifies this content provider that the given viewer's input * has been switched to a different element. *

* A typical use for this method is registering the content provider as a listener * to changes on the new input (using model-specific means), and deregistering the viewer * from the old input. In response to these change notifications, the content provider * propagates the changes to the viewer. *

* * @param viewer the viewer * @param oldInput the old input element, or null if the viewer * did not previously have an input * @param newInput the new input element, or null if the viewer * does not have an input */ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} /** * Sets the sort by response time option. * @param b boolean */ public void setSortByResponseTime(boolean b) { sortByResponseTime = b; } /** * */ protected void sortByResponseTime(List list) { int size = list.size(); for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size; j++) { IRequest c1 = (IRequest) list.get(i); IRequest c2 = (IRequest) list.get(j); if (c1.getResponseTime() < c2.getResponseTime()) { list.set(i, c2); list.set(j, c1); } } } } }