001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brad Reynolds - bug 141435
011: * Tom Schindl <tom.schindl@bestsolution.at> - bug 157309, 177619
012: *******************************************************************************/package org.eclipse.jface.viewers;
013:
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.graphics.Rectangle;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Control;
021:
022: /**
023: * A concrete viewer based on an SWT <code>List</code> control.
024: * <p>
025: * This class is not intended to be subclassed. It is designed to be
026: * instantiated with a pre-existing SWT <code>List</code> control and configured
027: * with a domain-specific content provider, label provider, element filter (optional),
028: * and element sorter (optional).
029: * <p>
030: * Note that the SWT <code>List</code> control only supports the display of strings, not icons.
031: * If you need to show icons for items, use <code>TableViewer</code> instead.
032: * </p>
033: *
034: * @see TableViewer
035: */
036: public class ListViewer extends AbstractListViewer {
037:
038: /**
039: * This viewer's list control.
040: */
041: private org.eclipse.swt.widgets.List list;
042:
043: /**
044: * Creates a list viewer on a newly-created list control under the given parent.
045: * The list control is created using the SWT style bits <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>.
046: * The viewer has no input, no content provider, a default label provider,
047: * no sorter, and no filters.
048: *
049: * @param parent the parent control
050: */
051: public ListViewer(Composite parent) {
052: this (parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL
053: | SWT.BORDER);
054: }
055:
056: /**
057: * Creates a list viewer on a newly-created list control under the given parent.
058: * The list control is created using the given SWT style bits.
059: * The viewer has no input, no content provider, a default label provider,
060: * no sorter, and no filters.
061: *
062: * @param parent the parent control
063: * @param style the SWT style bits
064: */
065: public ListViewer(Composite parent, int style) {
066: this (new org.eclipse.swt.widgets.List(parent, style));
067: }
068:
069: /**
070: * Creates a list viewer on the given list control.
071: * The viewer has no input, no content provider, a default label provider,
072: * no sorter, and no filters.
073: *
074: * @param list the list control
075: */
076: public ListViewer(org.eclipse.swt.widgets.List list) {
077: this .list = list;
078: hookControl(list);
079: }
080:
081: /* (non-Javadoc)
082: * Method declared on Viewer.
083: */
084: public Control getControl() {
085: return list;
086: }
087:
088: /**
089: * Returns this list viewer's list control.
090: *
091: * @return the list control
092: */
093: public org.eclipse.swt.widgets.List getList() {
094: return list;
095: }
096:
097: /*
098: * Non-Javadoc.
099: * Method defined on StructuredViewer.
100: */
101: public void reveal(Object element) {
102: Assert.isNotNull(element);
103: int index = getElementIndex(element);
104: if (index == -1) {
105: return;
106: }
107: // algorithm patterned after List.showSelection()
108: int count = list.getItemCount();
109: if (count == 0) {
110: return;
111: }
112: int height = list.getItemHeight();
113: Rectangle rect = list.getClientArea();
114: int topIndex = list.getTopIndex();
115: int visibleCount = Math.max(rect.height / height, 1);
116: int bottomIndex = Math.min(topIndex + visibleCount, count) - 1;
117: if ((topIndex <= index) && (index <= bottomIndex)) {
118: return;
119: }
120: int newTop = Math.min(Math.max(index - (visibleCount / 2), 0),
121: count - 1);
122: list.setTopIndex(newTop);
123: }
124:
125: /* (non-Javadoc)
126: * @see org.eclipse.jface.viewers.AbstractListViewer#listAdd(java.lang.String, int)
127: */
128: protected void listAdd(String string, int index) {
129: list.add(string, index);
130: }
131:
132: /* (non-Javadoc)
133: * @see org.eclipse.jface.viewers.AbstractListViewer#listSetItem(int, java.lang.String)
134: */
135: protected void listSetItem(int index, String string) {
136: list.setItem(index, string);
137: }
138:
139: /* (non-Javadoc)
140: * @see org.eclipse.jface.viewers.AbstractListViewer#listGetSelectionIndices()
141: */
142: protected int[] listGetSelectionIndices() {
143: return list.getSelectionIndices();
144: }
145:
146: /* (non-Javadoc)
147: * @see org.eclipse.jface.viewers.AbstractListViewer#listGetItemCount()
148: */
149: protected int listGetItemCount() {
150: return list.getItemCount();
151: }
152:
153: /* (non-Javadoc)
154: * @see org.eclipse.jface.viewers.AbstractListViewer#listSetItems(java.lang.String[])
155: */
156: protected void listSetItems(String[] labels) {
157: list.setItems(labels);
158: }
159:
160: /* (non-Javadoc)
161: * @see org.eclipse.jface.viewers.AbstractListViewer#listRemoveAll()
162: */
163: protected void listRemoveAll() {
164: list.removeAll();
165: }
166:
167: /* (non-Javadoc)
168: * @see org.eclipse.jface.viewers.AbstractListViewer#listRemove(int)
169: */
170: protected void listRemove(int index) {
171: list.remove(index);
172: }
173:
174: /* (non-Javadoc)
175: * @see org.eclipse.jface.viewers.AbstractListViewer#listSelectAndShow(int[])
176: */
177: protected void listSetSelection(int[] ixs) {
178: list.setSelection(ixs);
179: }
180:
181: /* (non-Javadoc)
182: * @see org.eclipse.jface.viewers.AbstractListViewer#listDeselectAll()
183: */
184: protected void listDeselectAll() {
185: list.deselectAll();
186: }
187:
188: /* (non-Javadoc)
189: * @see org.eclipse.jface.viewers.AbstractListViewer#listShowSelection()
190: */
191: protected void listShowSelection() {
192: list.showSelection();
193: }
194:
195: /* (non-Javadoc)
196: * @see org.eclipse.jface.viewers.AbstractListViewer#listGetTopIndex()
197: */
198: protected int listGetTopIndex() {
199: return list.getTopIndex();
200: }
201:
202: /*
203: * (non-Javadoc)
204: * @see org.eclipse.jface.viewers.AbstractListViewer#listSetTopIndex(int)
205: */
206: protected void listSetTopIndex(int index) {
207: list.setTopIndex(index);
208: }
209:
210: /* (non-Javadoc)
211: * @see org.eclipse.jface.viewers.AbstractListViewer#setSelectionToWidget(java.util.List, boolean)
212: */
213: protected void setSelectionToWidget(List in, boolean reveal) {
214: if (reveal) {
215: super .setSelectionToWidget(in, reveal);
216: } else {
217: if (in == null || in.size() == 0) { // clear selection
218: list.deselectAll();
219: } else {
220: int n = in.size();
221: int[] ixs = new int[n];
222: int count = 0;
223: for (int i = 0; i < n; ++i) {
224: Object el = in.get(i);
225: int ix = getElementIndex(el);
226: if (ix >= 0) {
227: ixs[count++] = ix;
228: }
229: }
230: if (count < n) {
231: System.arraycopy(ixs, 0, ixs = new int[count], 0,
232: count);
233: }
234: list.deselectAll();
235: list.select(ixs);
236: }
237: }
238: }
239:
240: }
|