comparison dwtx/jface/viewers/ListViewer.d @ 10:b6c35faf97c8

Viewers
author Frank Benoit <benoit@tionex.de>
date Mon, 31 Mar 2008 00:47:19 +0200
parents
children ea8ff534f622
comparison
equal deleted inserted replaced
9:6c14e54dfc11 10:b6c35faf97c8
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 * Port to the D programming language:
13 * Frank Benoit <benoit@tionex.de>
14 *******************************************************************************/
15
16 module dwtx.jface.viewers.ListViewer;
17
18 import dwtx.jface.viewers.AbstractListViewer;
19
20 import tango.util.collection.model.SeqView;
21
22 import dwt.DWT;
23 import dwt.graphics.Rectangle;
24 import dwt.widgets.Composite;
25 import dwt.widgets.Control;
26 static import dwt.widgets.List;
27 import dwtx.core.runtime.Assert;
28
29 import dwt.dwthelper.utils;
30
31 /**
32 * A concrete viewer based on an DWT <code>List</code> control.
33 * <p>
34 * This class is not intended to be subclassed. It is designed to be
35 * instantiated with a pre-existing DWT <code>List</code> control and configured
36 * with a domain-specific content provider, label provider, element filter (optional),
37 * and element sorter (optional).
38 * <p>
39 * Note that the DWT <code>List</code> control only supports the display of strings, not icons.
40 * If you need to show icons for items, use <code>TableViewer</code> instead.
41 * </p>
42 *
43 * @see TableViewer
44 */
45 public class ListViewer : AbstractListViewer {
46
47 /**
48 * This viewer's list control.
49 */
50 private dwt.widgets.List.List list;
51
52 /**
53 * Creates a list viewer on a newly-created list control under the given parent.
54 * The list control is created using the DWT style bits <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>.
55 * The viewer has no input, no content provider, a default label provider,
56 * no sorter, and no filters.
57 *
58 * @param parent the parent control
59 */
60 public this(Composite parent) {
61 this(parent, DWT.MULTI | DWT.H_SCROLL | DWT.V_SCROLL | DWT.BORDER);
62 }
63
64 /**
65 * Creates a list viewer on a newly-created list control under the given parent.
66 * The list control is created using the given DWT style bits.
67 * The viewer has no input, no content provider, a default label provider,
68 * no sorter, and no filters.
69 *
70 * @param parent the parent control
71 * @param style the DWT style bits
72 */
73 public this(Composite parent, int style) {
74 this(new dwt.widgets.List.List(parent, style));
75 }
76
77 /**
78 * Creates a list viewer on the given list control.
79 * The viewer has no input, no content provider, a default label provider,
80 * no sorter, and no filters.
81 *
82 * @param list the list control
83 */
84 public this(dwt.widgets.List.List list) {
85 this.list = list;
86 hookControl(list);
87 }
88
89 /* (non-Javadoc)
90 * Method declared on Viewer.
91 */
92 public Control getControl() {
93 return list;
94 }
95
96 /**
97 * Returns this list viewer's list control.
98 *
99 * @return the list control
100 */
101 public dwt.widgets.List.List getList() {
102 return list;
103 }
104
105 /*
106 * Non-Javadoc.
107 * Method defined on StructuredViewer.
108 */
109 public void reveal(Object element) {
110 Assert.isNotNull(element);
111 int index = getElementIndex(element);
112 if (index is -1) {
113 return;
114 }
115 // algorithm patterned after List.showSelection()
116 int count = list.getItemCount();
117 if (count is 0) {
118 return;
119 }
120 int height = list.getItemHeight();
121 Rectangle rect = list.getClientArea();
122 int topIndex = list.getTopIndex();
123 int visibleCount = Math.max(rect.height / height, 1);
124 int bottomIndex = Math.min(topIndex + visibleCount, count) - 1;
125 if ((topIndex <= index) && (index <= bottomIndex)) {
126 return;
127 }
128 int newTop = Math.min(Math.max(index - (visibleCount / 2), 0),
129 count - 1);
130 list.setTopIndex(newTop);
131 }
132
133 /* (non-Javadoc)
134 * @see dwtx.jface.viewers.AbstractListViewer#listAdd(java.lang.String, int)
135 */
136 protected void listAdd(String string, int index) {
137 list.add(string, index);
138 }
139
140 /* (non-Javadoc)
141 * @see dwtx.jface.viewers.AbstractListViewer#listSetItem(int, java.lang.String)
142 */
143 protected void listSetItem(int index, String string) {
144 list.setItem(index, string);
145 }
146
147 /* (non-Javadoc)
148 * @see dwtx.jface.viewers.AbstractListViewer#listGetSelectionIndices()
149 */
150 protected int[] listGetSelectionIndices() {
151 return list.getSelectionIndices();
152 }
153
154 /* (non-Javadoc)
155 * @see dwtx.jface.viewers.AbstractListViewer#listGetItemCount()
156 */
157 protected int listGetItemCount() {
158 return list.getItemCount();
159 }
160
161 /* (non-Javadoc)
162 * @see dwtx.jface.viewers.AbstractListViewer#listSetItems(java.lang.String[])
163 */
164 protected void listSetItems(String[] labels) {
165 list.setItems(labels);
166 }
167
168 /* (non-Javadoc)
169 * @see dwtx.jface.viewers.AbstractListViewer#listRemoveAll()
170 */
171 protected void listRemoveAll() {
172 list.removeAll();
173 }
174
175 /* (non-Javadoc)
176 * @see dwtx.jface.viewers.AbstractListViewer#listRemove(int)
177 */
178 protected void listRemove(int index) {
179 list.remove(index);
180 }
181
182 /* (non-Javadoc)
183 * @see dwtx.jface.viewers.AbstractListViewer#listSelectAndShow(int[])
184 */
185 protected void listSetSelection(int[] ixs) {
186 list.setSelection(ixs);
187 }
188
189 /* (non-Javadoc)
190 * @see dwtx.jface.viewers.AbstractListViewer#listDeselectAll()
191 */
192 protected void listDeselectAll() {
193 list.deselectAll();
194 }
195
196 /* (non-Javadoc)
197 * @see dwtx.jface.viewers.AbstractListViewer#listShowSelection()
198 */
199 protected void listShowSelection() {
200 list.showSelection();
201 }
202
203 /* (non-Javadoc)
204 * @see dwtx.jface.viewers.AbstractListViewer#listGetTopIndex()
205 */
206 protected int listGetTopIndex() {
207 return list.getTopIndex();
208 }
209
210 /*
211 * (non-Javadoc)
212 * @see dwtx.jface.viewers.AbstractListViewer#listSetTopIndex(int)
213 */
214 protected void listSetTopIndex(int index) {
215 list.setTopIndex(index);
216 }
217
218 /* (non-Javadoc)
219 * @see dwtx.jface.viewers.AbstractListViewer#setSelectionToWidget(java.util.List, bool)
220 */
221 protected void setSelectionToWidget(SeqView!(Object) in_, bool reveal) {
222 if( reveal ) {
223 super.setSelectionToWidget(in_, reveal);
224 } else {
225 if (in_ is null || in_.size() is 0) { // clear selection
226 list.deselectAll();
227 } else {
228 int n = in_.size();
229 int[] ixs = new int[n];
230 int count = 0;
231 for (int i = 0; i < n; ++i) {
232 Object el = in_.get(i);
233 int ix = getElementIndex(el);
234 if (ix >= 0) {
235 ixs[count++] = ix;
236 }
237 }
238 if (count < n) {
239 System.arraycopy(ixs, 0, ixs = new int[count], 0, count);
240 }
241 list.deselectAll();
242 list.select(ixs);
243 }
244 }
245 }
246
247
248 }