Index: ListViewer.java =================================================================== retrieving revision 1.7 diff -u -r1.7 ListViewer.java --- ListViewer.java 28 Oct 2003 20:50:48 -0000 1.7 +++ ListViewer.java 11 Feb 2004 02:32:53 -0000 @@ -11,16 +11,11 @@ package org.eclipse.jface.viewers; -import java.util.ArrayList; -import java.util.List; - +import org.eclipse.jface.util.Assert; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Widget; - -import org.eclipse.jface.util.Assert; /** * A concrete viewer based on an SWT List control. @@ -36,17 +31,13 @@ * * @see TableViewer */ -public class ListViewer extends StructuredViewer { +public class ListViewer extends AbstractListViewer { /** * This viewer's list control. */ private org.eclipse.swt.widgets.List list; - /** - * A list of viewer elements (element type: Object). - */ - private java.util.List listMap = new ArrayList(); /** * Creates a list viewer on a newly-created list control under the given parent. * The list control is created using the SWT style bits MULTI, H_SCROLL, V_SCROLL, and BORDER. @@ -81,79 +72,7 @@ this.list = list; hookControl(list); } -/** - * Adds the given elements to this list viewer. - * If this viewer does not have a sorter, the elements are added at the end - * in the order given; otherwise the elements are inserted at appropriate positions. - *

- * This method should be called (by the content provider) when elements - * have been added to the model, in order to cause the viewer to accurately - * reflect the model. This method only affects the viewer, not the model. - *

- * - * @param elements the elements to add - */ -public void add(Object[] elements) { - assertElementsNotNull(elements); - Object[] filtered = filter(elements); - ILabelProvider labelProvider = (ILabelProvider) getLabelProvider(); - for (int i = 0; i < filtered.length; i++){ - Object element = filtered[i]; - int ix = indexForElement(element); - list.add(labelProvider.getText(element), ix); - listMap.add(ix, element); - mapElement(element, list); // must map it, since findItem only looks in map, if enabled - } -} -/** - * Adds the given element to this list viewer. - * If this viewer does not have a sorter, the element is added at the end; - * otherwise the element is inserted at the appropriate position. - *

- * This method should be called (by the content provider) when a single element - * has been added to the model, in order to cause the viewer to accurately - * reflect the model. This method only affects the viewer, not the model. - * Note that there is another method for efficiently processing the simultaneous - * addition of multiple elements. - *

- * - * @param element the element - */ -public void add(Object element) { - add(new Object[] { element }); -} -/* (non-Javadoc) - * Method declared on StructuredViewer. - * Since SWT.List doesn't use items we always return the List itself. - */ -protected Widget doFindInputItem(Object element) { - if (element != null && equals(element, getRoot())) - return getList(); - return null; -} -/* (non-Javadoc) - * Method declared on StructuredViewer. - * Since SWT.List doesn't use items we always return the List itself. - */ -protected Widget doFindItem(Object element) { - if (element != null) { - if (listMap.contains(element)) - return getList(); - } - return null; -} -/* (non-Javadoc) - * Method declared on StructuredViewer. - */ -protected void doUpdateItem(Widget data, Object element, boolean fullMap) { - if (element != null) { - int ix = listMap.indexOf(element); - if (ix >= 0) { - ILabelProvider labelProvider = (ILabelProvider) getLabelProvider(); - list.setItem(ix, labelProvider.getText(element)); - } - } -} + /* (non-Javadoc) * Method declared on Viewer. */ @@ -161,27 +80,6 @@ return list; } /** - * Returns the element with the given index from this list viewer. - * Returns null if the index is out of range. - * - * @param index the zero-based index - * @return the element at the given index, or null if the - * index is out of range - */ -public Object getElementAt(int index) { - if (index >= 0 && index < listMap.size()) - return listMap.get(index); - return null; -} -/** - * The list viewer implementation of this Viewer framework - * method returns the label provider, which in the case of list - * viewers will be an instance of ILabelProvider. - */ -public IBaseLabelProvider getLabelProvider() { - return super.getLabelProvider(); -} -/** * Returns this list viewer's list control. * * @return the list control @@ -189,161 +87,14 @@ public org.eclipse.swt.widgets.List getList() { return list; } -/* (non-Javadoc) - * Method declared on Viewer. - */ -/* (non-Javadoc) - * Method declared on StructuredViewer. - */ -protected List getSelectionFromWidget() { - int[] ixs = getList().getSelectionIndices(); - ArrayList list = new ArrayList(ixs.length); - for (int i = 0; i < ixs.length; i++) { - Object e = getElementAt(ixs[i]); - if (e != null) - list.add(e); - } - return list; -} -/* - * Returns the index where the item should be inserted. -*/ -protected int indexForElement(Object element) { - ViewerSorter sorter = getSorter(); - if(sorter == null) - return list.getItemCount(); - int count = list.getItemCount(); - int min = 0, max = count - 1; - while (min <= max) { - int mid = (min + max) / 2; - Object data = listMap.get(mid); - int compare = sorter.compare(this, data, element); - if (compare == 0) { - // find first item > element - while (compare == 0) { - ++mid; - if (mid >= count) { - break; - } - data = listMap.get(mid); - compare = sorter.compare(this, data, element); - } - return mid; - } - if (compare < 0) - min = mid + 1; - else - max = mid - 1; - } - return min; -} -/* (non-Javadoc) - * Method declared on Viewer. - */ -protected void inputChanged(Object input, Object oldInput) { - listMap.clear(); - Object[] children = getSortedChildren(getRoot()); - int size = children.length; - org.eclipse.swt.widgets.List list = getList(); - list.removeAll(); - String[] labels = new String[size]; - for (int i = 0; i < size; i++) { - Object el = children[i]; - labels[i] = ((ILabelProvider) getLabelProvider()).getText(el); - listMap.add(el); - mapElement(el, list); // must map it, since findItem only looks in map, if enabled - } - list.setItems(labels); -} -/* (non-Javadoc) - * Method declared on StructuredViewer. - */ -protected void internalRefresh(Object element) { - if (element == null || equals(element, getRoot())) { - // the parent - if (listMap != null) - listMap.clear(); - unmapAllElements(); - List selection = getSelectionFromWidget(); - list.setRedraw(false); - list.removeAll(); - Object[] children = getSortedChildren(getRoot()); - ILabelProvider labelProvider= (ILabelProvider) getLabelProvider(); - for (int i= 0; i < children.length; i++) { - Object el = children[i]; - list.add(labelProvider.getText(el), i); - listMap.add(el); - mapElement(el, list); // must map it, since findItem only looks in map, if enabled - } - list.setRedraw(true); - setSelectionToWidget(selection, false); - } else { - doUpdateItem(list, element, true); - } -} -/** - * Removes the given elements from this list viewer. - * - * @param elements the elements to remove - */ -private void internalRemove(final Object[] elements) { - Object input = getInput(); - for (int i = 0; i < elements.length; ++i) { - if (equals(elements[i], input)) { - setInput(null); - return; - } - int ix = listMap.indexOf(elements[i]); - if (ix >= 0) { - list.remove(ix); - listMap.remove(ix); - unmapElement(elements[i], list); - } - } -} -/** - * Removes the given elements from this list viewer. - * The selection is updated if required. - *

- * This method should be called (by the content provider) when elements - * have been removed from the model, in order to cause the viewer to accurately - * reflect the model. This method only affects the viewer, not the model. - *

- * - * @param elements the elements to remove - */ -public void remove(final Object[] elements) { - assertElementsNotNull(elements); - preservingSelection(new Runnable() { - public void run() { - internalRemove(elements); - } - }); -} -/** - * Removes the given element from this list viewer. - * The selection is updated if necessary. - *

- * This method should be called (by the content provider) when a single element - * has been removed from the model, in order to cause the viewer to accurately - * reflect the model. This method only affects the viewer, not the model. - * Note that there is another method for efficiently processing the simultaneous - * removal of multiple elements. - *

- * - * @param element the element - */ -public void remove(Object element) { - remove(new Object[] { element }); -} /* * Non-Javadoc. * Method defined on StructuredViewer. */ public void reveal(Object element) { Assert.isNotNull(element); - int index = listMap.indexOf(element); + int index = getElementIndex(element); if (index == -1) return; // algorithm patterned after List.showSelection() @@ -359,39 +110,76 @@ int newTop = Math.min (Math.max (index - (visibleCount / 2), 0), count - 1); list.setTopIndex(newTop); } -/** - * The list viewer implementation of this Viewer framework - * method ensures that the given label provider is an instance - * of ILabelProvider. - */ -public void setLabelProvider(IBaseLabelProvider labelProvider) { - Assert.isTrue(labelProvider instanceof ILabelProvider); - super.setLabelProvider(labelProvider); -} -/* (non-Javadoc) - * Method declared on StructuredViewer. - */ -protected void setSelectionToWidget(List in, boolean reveal) { - org.eclipse.swt.widgets.List list = getList(); - if (in == null || in.size() == 0) { // clear selection - list.deselectAll(); - } else { - int n = in.size(); - int[] ixs = new int[n]; - int count = 0; - for (int i = 0; i < n; ++i) { - Object el = in.get(i); - int ix = listMap.indexOf(el); - if (ix >= 0) - ixs[count++] = ix; - } - if (count < n) { - System.arraycopy(ixs, 0, ixs = new int[count], 0, count); - } - list.setSelection(ixs); - if (reveal) { - list.showSelection(); - } - } + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listAdd(java.lang.String, int) + */ +void listAdd(String string, int index) { + list.add(string, index); +} + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listSetItem(int, java.lang.String) + */ +void listSetItem(int index, String string) { + list.setItem(index, string); +} + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listGetSelectionIndices() + */ +int[] listGetSelectionIndices() { + return list.getSelectionIndices(); +} + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listGetItemCount() + */ +int listGetItemCount() { + return list.getItemCount(); } + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listSetItems(java.lang.String[]) + */ +void listSetItems(String[] labels) { + list.setItems(labels); +} + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listRemoveAll() + */ +void listRemoveAll() { + list.removeAll(); +} + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listRemove(int) + */ +void listRemove(int index) { + list.remove(index); +} + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listSelectAndShow(int[]) + */ +void listSetSelection(int[] ixs) { + list.setSelection(ixs); +} + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listDeselectAll() + */ +void listDeselectAll() { + list.deselectAll(); +} + +/* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listShowSelection() + */ +void listShowSelection() { + list.showSelection(); +} + + } Index: src/org/eclipse/jface/viewers/AbstractListViewer.java =================================================================== RCS file: src/org/eclipse/jface/viewers/AbstractListViewer.java diff -N src/org/eclipse/jface/viewers/AbstractListViewer.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/jface/viewers/AbstractListViewer.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,333 @@ +/******************************************************************************* + * Copyright (c) 2004 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 Corporation - initial API and implementation + *******************************************************************************/ + +package org.eclipse.jface.viewers; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jface.util.Assert; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Widget; + +/** + * @since 3.0 + */ +abstract class AbstractListViewer extends StructuredViewer { + + /** + * A list of viewer elements (element type: Object). + */ + private java.util.List listMap = new ArrayList(); + +abstract void listAdd(String string, int index); +abstract void listSetItem(int index, String string); +abstract int[] listGetSelectionIndices(); +abstract int listGetItemCount(); +abstract void listSetItems(String[] labels); +abstract void listRemoveAll(); +abstract void listRemove(int index); +abstract void listSetSelection(int[] ixs); +abstract void listShowSelection(); +abstract void listDeselectAll(); + +/** + * Adds the given elements to this list viewer. + * If this viewer does not have a sorter, the elements are added at the end + * in the order given; otherwise the elements are inserted at appropriate positions. + *

+ * This method should be called (by the content provider) when elements + * have been added to the model, in order to cause the viewer to accurately + * reflect the model. This method only affects the viewer, not the model. + *

+ * + * @param elements the elements to add + */ +public void add(Object[] elements) { + assertElementsNotNull(elements); + Object[] filtered = filter(elements); + ILabelProvider labelProvider = (ILabelProvider) getLabelProvider(); + for (int i = 0; i < filtered.length; i++){ + Object element = filtered[i]; + int ix = indexForElement(element); + listAdd(labelProvider.getText(element), ix); + listMap.add(ix, element); + mapElement(element, getControl()); // must map it, since findItem only looks in map, if enabled + } +} +/** + * Adds the given element to this list viewer. + * If this viewer does not have a sorter, the element is added at the end; + * otherwise the element is inserted at the appropriate position. + *

+ * This method should be called (by the content provider) when a single element + * has been added to the model, in order to cause the viewer to accurately + * reflect the model. This method only affects the viewer, not the model. + * Note that there is another method for efficiently processing the simultaneous + * addition of multiple elements. + *

+ * + * @param element the element + */ +public void add(Object element) { + add(new Object[] { element }); +} +/* (non-Javadoc) + * Method declared on StructuredViewer. + * Since SWT.List doesn't use items we always return the List itself. + */ +protected Widget doFindInputItem(Object element) { + if (element != null && equals(element, getRoot())) + return getControl(); + return null; +} +/* (non-Javadoc) + * Method declared on StructuredViewer. + * Since SWT.List doesn't use items we always return the List itself. + */ +protected Widget doFindItem(Object element) { + if (element != null) { + if (listMap.contains(element)) + return getControl(); + } + return null; +} +/* (non-Javadoc) + * Method declared on StructuredViewer. + */ +protected void doUpdateItem(Widget data, Object element, boolean fullMap) { + if (element != null) { + int ix = listMap.indexOf(element); + if (ix >= 0) { + ILabelProvider labelProvider = (ILabelProvider) getLabelProvider(); + listSetItem(ix, labelProvider.getText(element)); + } + } +} +/* (non-Javadoc) + * Method declared on Viewer. + */ +public abstract Control getControl(); +/** + * Returns the element with the given index from this list viewer. + * Returns null if the index is out of range. + * + * @param index the zero-based index + * @return the element at the given index, or null if the + * index is out of range + */ +public Object getElementAt(int index) { + if (index >= 0 && index < listMap.size()) + return listMap.get(index); + return null; +} +/** + * The list viewer implementation of this Viewer framework + * method returns the label provider, which in the case of list + * viewers will be an instance of ILabelProvider. + */ +public IBaseLabelProvider getLabelProvider() { + return super.getLabelProvider(); +} + +/* (non-Javadoc) + * Method declared on Viewer. + */ +/* (non-Javadoc) + * Method declared on StructuredViewer. + */ +protected List getSelectionFromWidget() { + int[] ixs = listGetSelectionIndices(); + ArrayList list = new ArrayList(ixs.length); + for (int i = 0; i < ixs.length; i++) { + Object e = getElementAt(ixs[i]); + if (e != null) + list.add(e); + } + return list; +} +/* + * Returns the index where the item should be inserted. +*/ +protected int indexForElement(Object element) { + ViewerSorter sorter = getSorter(); + if(sorter == null) + return listGetItemCount(); + int count = listGetItemCount(); + int min = 0, max = count - 1; + while (min <= max) { + int mid = (min + max) / 2; + Object data = listMap.get(mid); + int compare = sorter.compare(this, data, element); + if (compare == 0) { + // find first item > element + while (compare == 0) { + ++mid; + if (mid >= count) { + break; + } + data = listMap.get(mid); + compare = sorter.compare(this, data, element); + } + return mid; + } + if (compare < 0) + min = mid + 1; + else + max = mid - 1; + } + return min; +} +/* (non-Javadoc) + * Method declared on Viewer. + */ +protected void inputChanged(Object input, Object oldInput) { + listMap.clear(); + Object[] children = getSortedChildren(getRoot()); + int size = children.length; + + listRemoveAll(); + String[] labels = new String[size]; + for (int i = 0; i < size; i++) { + Object el = children[i]; + labels[i] = ((ILabelProvider) getLabelProvider()).getText(el); + listMap.add(el); + mapElement(el, getControl()); // must map it, since findItem only looks in map, if enabled + } + listSetItems(labels); +} + +/* (non-Javadoc) + * Method declared on StructuredViewer. + */ +protected void internalRefresh(Object element) { + + Control list = getControl(); + if (element == null || equals(element, getRoot())) { + // the parent + if (listMap != null) + listMap.clear(); + unmapAllElements(); + List selection = getSelectionFromWidget(); + + list.setRedraw(false); + listRemoveAll(); + Object[] children = getSortedChildren(getRoot()); + ILabelProvider labelProvider= (ILabelProvider) getLabelProvider(); + for (int i= 0; i < children.length; i++) { + Object el = children[i]; + listAdd(labelProvider.getText(el), i); + listMap.add(el); + mapElement(el, list); // must map it, since findItem only looks in map, if enabled + } + list.setRedraw(true); + setSelectionToWidget(selection, false); + } else { + doUpdateItem(list, element, true); + } +} +/** + * Removes the given elements from this list viewer. + * + * @param elements the elements to remove + */ +private void internalRemove(final Object[] elements) { + Object input = getInput(); + for (int i = 0; i < elements.length; ++i) { + if (equals(elements[i], input)) { + setInput(null); + return; + } + int ix = listMap.indexOf(elements[i]); + if (ix >= 0) { + listRemove(ix); + listMap.remove(ix); + unmapElement(elements[i], getControl()); + } + } +} +/** + * Removes the given elements from this list viewer. + * The selection is updated if required. + *

+ * This method should be called (by the content provider) when elements + * have been removed from the model, in order to cause the viewer to accurately + * reflect the model. This method only affects the viewer, not the model. + *

+ * + * @param elements the elements to remove + */ +public void remove(final Object[] elements) { + assertElementsNotNull(elements); + preservingSelection(new Runnable() { + public void run() { + internalRemove(elements); + } + }); +} +/** + * Removes the given element from this list viewer. + * The selection is updated if necessary. + *

+ * This method should be called (by the content provider) when a single element + * has been removed from the model, in order to cause the viewer to accurately + * reflect the model. This method only affects the viewer, not the model. + * Note that there is another method for efficiently processing the simultaneous + * removal of multiple elements. + *

+ * + * @param element the element + */ +public void remove(Object element) { + remove(new Object[] { element }); +} + +/** + * The list viewer implementation of this Viewer framework + * method ensures that the given label provider is an instance + * of ILabelProvider. + */ +public void setLabelProvider(IBaseLabelProvider labelProvider) { + Assert.isTrue(labelProvider instanceof ILabelProvider); + super.setLabelProvider(labelProvider); +} + +/* (non-Javadoc) + * Method declared on StructuredViewer. + */ +protected void setSelectionToWidget(List in, boolean reveal) { + if (in == null || in.size() == 0) { // clear selection + listDeselectAll(); + } else { + int n = in.size(); + int[] ixs = new int[n]; + int count = 0; + for (int i = 0; i < n; ++i) { + Object el = in.get(i); + int ix = listMap.indexOf(el); + if (ix >= 0) + ixs[count++] = ix; + } + if (count < n) { + System.arraycopy(ixs, 0, ixs = new int[count], 0, count); + } + listSetSelection(ixs); + if (reveal) { + listShowSelection(); + } + } +} + +int getElementIndex(Object element) { + return listMap.indexOf(element); +} + +} Index: src/org/eclipse/jface/viewers/ComboViewer.java =================================================================== RCS file: src/org/eclipse/jface/viewers/ComboViewer.java diff -N src/org/eclipse/jface/viewers/ComboViewer.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/jface/viewers/ComboViewer.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2004 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 Corporation - initial API and implementation + *******************************************************************************/ + +package org.eclipse.jface.viewers; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; + +/** + * A concrete viewer based on an SWT Combo control. This class is intended + * as an alternative to the JFace ListViewer, which displays its content + * in a combo box rather than a list. Wherever possible, this class attempts to behave + * like ListViewer.

+ * + * This class is designed to be instantiated with a pre-existing SWT combo control + * and configured with a domain-specific content provider, label provider, element + * filter (optional), and element sorter (optional). + *

+ * + * @see org.eclipse.jface.viewers.ListViewer + * + * @author IBM + */ +public final class ComboViewer extends AbstractListViewer { + + /** + * This viewer's list control. + */ + private org.eclipse.swt.widgets.Combo list; + + /** + * Creates a combo viewer on a newly-created combo control under the given parent. + * The viewer has no input, no content provider, a default label provider, + * no sorter, and no filters. + * + * @param parent the parent control + */ + public ComboViewer(Composite parent) { + this(parent, SWT.READ_ONLY | SWT.BORDER); + } + /** + * Creates a combo viewer on a newly-created combo control under the given parent. + * The combo control is created using the given SWT style bits. + * The viewer has no input, no content provider, a default label provider, + * no sorter, and no filters. + * + * @param parent the parent control + * @param style the SWT style bits + */ + public ComboViewer(Composite parent, int style) { + this(new org.eclipse.swt.widgets.Combo(parent, style)); + } + /** + * Creates a combo viewer on the given combo control. + * The viewer has no input, no content provider, a default label provider, + * no sorter, and no filters. + * + * @param list the combo control + */ + public ComboViewer(org.eclipse.swt.widgets.Combo list) { + this.list = list; + hookControl(list); + } + + protected void listAdd(String string, int index) { + list.add(string, index); + } + + protected void listSetItem(int index, String string) { + list.setItem(index, string); + } + + protected int[] listGetSelectionIndices() { + return new int[] {list.getSelectionIndex()}; + } + + protected int listGetItemCount() { + return list.getItemCount(); + } + + protected void listSetItems(String[] labels) { + list.setItems(labels); + } + + protected void listRemoveAll() { + list.removeAll(); + } + + protected void listRemove(int index) { + list.remove(index); + } + + /* (non-Javadoc) + * Method declared on Viewer. + */ + public Control getControl() { + return list; + } + + /** + * Returns this list viewer's list control. + * + * @return the list control + */ + public org.eclipse.swt.widgets.Combo getCombo() { + return list; + } + + /* + * Do nothing -- combos only display the selected element, so there is no way + * we can ensure that the given element is visible without changing the selection. + * Method defined on StructuredViewer. + */ + public void reveal(Object element) { + return; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listSelectAndShow(int[]) + */ + protected void listSetSelection(int[] ixs) { + for (int idx = 0; idx < ixs.length; idx++) { + list.select(ixs[idx]); + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listDeselectAll() + */ + protected void listDeselectAll() { + list.deselectAll(); + list.clearSelection(); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.AbstractListViewer#listShowSelection() + */ + protected void listShowSelection() { + + } +}