KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > ListViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Brad Reynolds - bug 141435
11  * Tom Schindl <tom.schindl@bestsolution.at> - bug 157309, 177619
12  *******************************************************************************/

13
14 package org.eclipse.jface.viewers;
15
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23
24 /**
25  * A concrete viewer based on an SWT <code>List</code> control.
26  * <p>
27  * This class is not intended to be subclassed. It is designed to be
28  * instantiated with a pre-existing SWT <code>List</code> control and configured
29  * with a domain-specific content provider, label provider, element filter (optional),
30  * and element sorter (optional).
31  * <p>
32  * Note that the SWT <code>List</code> control only supports the display of strings, not icons.
33  * If you need to show icons for items, use <code>TableViewer</code> instead.
34  * </p>
35  *
36  * @see TableViewer
37  */

38 public class ListViewer extends AbstractListViewer {
39
40     /**
41      * This viewer's list control.
42      */

43     private org.eclipse.swt.widgets.List list;
44
45     /**
46      * Creates a list viewer on a newly-created list control under the given parent.
47      * The list control is created using the SWT style bits <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>.
48      * The viewer has no input, no content provider, a default label provider,
49      * no sorter, and no filters.
50      *
51      * @param parent the parent control
52      */

53     public ListViewer(Composite parent) {
54         this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
55     }
56
57     /**
58      * Creates a list viewer on a newly-created list control under the given parent.
59      * The list control is created using the given SWT style bits.
60      * The viewer has no input, no content provider, a default label provider,
61      * no sorter, and no filters.
62      *
63      * @param parent the parent control
64      * @param style the SWT style bits
65      */

66     public ListViewer(Composite parent, int style) {
67         this(new org.eclipse.swt.widgets.List(parent, style));
68     }
69
70     /**
71      * Creates a list viewer on the given list control.
72      * The viewer has no input, no content provider, a default label provider,
73      * no sorter, and no filters.
74      *
75      * @param list the list control
76      */

77     public ListViewer(org.eclipse.swt.widgets.List list) {
78         this.list = list;
79         hookControl(list);
80     }
81
82     /* (non-Javadoc)
83      * Method declared on Viewer.
84      */

85     public Control getControl() {
86         return list;
87     }
88
89     /**
90      * Returns this list viewer's list control.
91      *
92      * @return the list control
93      */

94     public org.eclipse.swt.widgets.List getList() {
95         return list;
96     }
97
98     /*
99      * Non-Javadoc.
100      * Method defined on StructuredViewer.
101      */

102     public void reveal(Object JavaDoc element) {
103         Assert.isNotNull(element);
104         int index = getElementIndex(element);
105         if (index == -1) {
106             return;
107         }
108         // algorithm patterned after List.showSelection()
109
int count = list.getItemCount();
110         if (count == 0) {
111             return;
112         }
113         int height = list.getItemHeight();
114         Rectangle rect = list.getClientArea();
115         int topIndex = list.getTopIndex();
116         int visibleCount = Math.max(rect.height / height, 1);
117         int bottomIndex = Math.min(topIndex + visibleCount, count) - 1;
118         if ((topIndex <= index) && (index <= bottomIndex)) {
119             return;
120         }
121         int newTop = Math.min(Math.max(index - (visibleCount / 2), 0),
122                 count - 1);
123         list.setTopIndex(newTop);
124     }
125
126     /* (non-Javadoc)
127      * @see org.eclipse.jface.viewers.AbstractListViewer#listAdd(java.lang.String, int)
128      */

129     protected void listAdd(String JavaDoc string, int index) {
130         list.add(string, index);
131     }
132
133     /* (non-Javadoc)
134      * @see org.eclipse.jface.viewers.AbstractListViewer#listSetItem(int, java.lang.String)
135      */

136     protected void listSetItem(int index, String JavaDoc string) {
137         list.setItem(index, string);
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.jface.viewers.AbstractListViewer#listGetSelectionIndices()
142      */

143     protected int[] listGetSelectionIndices() {
144         return list.getSelectionIndices();
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.jface.viewers.AbstractListViewer#listGetItemCount()
149      */

150     protected int listGetItemCount() {
151         return list.getItemCount();
152     }
153
154     /* (non-Javadoc)
155      * @see org.eclipse.jface.viewers.AbstractListViewer#listSetItems(java.lang.String[])
156      */

157     protected void listSetItems(String JavaDoc[] labels) {
158         list.setItems(labels);
159     }
160
161     /* (non-Javadoc)
162      * @see org.eclipse.jface.viewers.AbstractListViewer#listRemoveAll()
163      */

164     protected void listRemoveAll() {
165         list.removeAll();
166     }
167
168     /* (non-Javadoc)
169      * @see org.eclipse.jface.viewers.AbstractListViewer#listRemove(int)
170      */

171     protected void listRemove(int index) {
172         list.remove(index);
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.jface.viewers.AbstractListViewer#listSelectAndShow(int[])
177      */

178     protected void listSetSelection(int[] ixs) {
179         list.setSelection(ixs);
180     }
181
182     /* (non-Javadoc)
183      * @see org.eclipse.jface.viewers.AbstractListViewer#listDeselectAll()
184      */

185     protected void listDeselectAll() {
186         list.deselectAll();
187     }
188
189     /* (non-Javadoc)
190      * @see org.eclipse.jface.viewers.AbstractListViewer#listShowSelection()
191      */

192     protected void listShowSelection() {
193         list.showSelection();
194     }
195     
196     /* (non-Javadoc)
197      * @see org.eclipse.jface.viewers.AbstractListViewer#listGetTopIndex()
198      */

199     protected int listGetTopIndex() {
200         return list.getTopIndex();
201     }
202     
203     /*
204      * (non-Javadoc)
205      * @see org.eclipse.jface.viewers.AbstractListViewer#listSetTopIndex(int)
206      */

207     protected void listSetTopIndex(int index) {
208         list.setTopIndex(index);
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.jface.viewers.AbstractListViewer#setSelectionToWidget(java.util.List, boolean)
213      */

214     protected void setSelectionToWidget(List JavaDoc in, boolean reveal) {
215         if( reveal ) {
216             super.setSelectionToWidget(in, reveal);
217         } else {
218             if (in == null || in.size() == 0) { // clear selection
219
list.deselectAll();
220             } else {
221                 int n = in.size();
222                 int[] ixs = new int[n];
223                 int count = 0;
224                 for (int i = 0; i < n; ++i) {
225                     Object JavaDoc el = in.get(i);
226                     int ix = getElementIndex(el);
227                     if (ix >= 0) {
228                         ixs[count++] = ix;
229                     }
230                 }
231                 if (count < n) {
232                     System.arraycopy(ixs, 0, ixs = new int[count], 0, count);
233                 }
234                 list.deselectAll();
235                 list.select(ixs);
236             }
237         }
238     }
239     
240     
241 }
242
Popular Tags