Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/ListViewer.d @ 90:7ffeace6c47f
Update 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 06 Jul 2008 23:30:07 +0200 |
parents | ea8ff534f622 |
children | 04b47443bb01 |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
90 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
10 | 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 | |
90 | 44 * @noextend This class is not intended to be subclassed by clients. |
10 | 45 */ |
46 public class ListViewer : AbstractListViewer { | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
47 alias AbstractListViewer.setSelectionToWidget setSelectionToWidget; |
10 | 48 |
49 /** | |
50 * This viewer's list control. | |
51 */ | |
52 private dwt.widgets.List.List list; | |
53 | |
54 /** | |
55 * Creates a list viewer on a newly-created list control under the given parent. | |
56 * The list control is created using the DWT style bits <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. | |
57 * The viewer has no input, no content provider, a default label provider, | |
58 * no sorter, and no filters. | |
59 * | |
60 * @param parent the parent control | |
61 */ | |
62 public this(Composite parent) { | |
63 this(parent, DWT.MULTI | DWT.H_SCROLL | DWT.V_SCROLL | DWT.BORDER); | |
64 } | |
65 | |
66 /** | |
67 * Creates a list viewer on a newly-created list control under the given parent. | |
68 * The list control is created using the given DWT style bits. | |
69 * The viewer has no input, no content provider, a default label provider, | |
70 * no sorter, and no filters. | |
71 * | |
72 * @param parent the parent control | |
73 * @param style the DWT style bits | |
74 */ | |
75 public this(Composite parent, int style) { | |
76 this(new dwt.widgets.List.List(parent, style)); | |
77 } | |
78 | |
79 /** | |
80 * Creates a list viewer on the given list control. | |
81 * The viewer has no input, no content provider, a default label provider, | |
82 * no sorter, and no filters. | |
83 * | |
84 * @param list the list control | |
85 */ | |
86 public this(dwt.widgets.List.List list) { | |
87 this.list = list; | |
88 hookControl(list); | |
89 } | |
90 | |
91 /* (non-Javadoc) | |
92 * Method declared on Viewer. | |
93 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
94 public override Control getControl() { |
10 | 95 return list; |
96 } | |
97 | |
98 /** | |
99 * Returns this list viewer's list control. | |
100 * | |
101 * @return the list control | |
102 */ | |
103 public dwt.widgets.List.List getList() { | |
104 return list; | |
105 } | |
106 | |
107 /* | |
108 * Non-Javadoc. | |
109 * Method defined on StructuredViewer. | |
110 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
111 public override void reveal(Object element) { |
10 | 112 Assert.isNotNull(element); |
113 int index = getElementIndex(element); | |
114 if (index is -1) { | |
115 return; | |
116 } | |
117 // algorithm patterned after List.showSelection() | |
118 int count = list.getItemCount(); | |
119 if (count is 0) { | |
120 return; | |
121 } | |
122 int height = list.getItemHeight(); | |
123 Rectangle rect = list.getClientArea(); | |
124 int topIndex = list.getTopIndex(); | |
125 int visibleCount = Math.max(rect.height / height, 1); | |
126 int bottomIndex = Math.min(topIndex + visibleCount, count) - 1; | |
127 if ((topIndex <= index) && (index <= bottomIndex)) { | |
128 return; | |
129 } | |
130 int newTop = Math.min(Math.max(index - (visibleCount / 2), 0), | |
131 count - 1); | |
132 list.setTopIndex(newTop); | |
133 } | |
134 | |
135 /* (non-Javadoc) | |
136 * @see dwtx.jface.viewers.AbstractListViewer#listAdd(java.lang.String, int) | |
137 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
138 protected override void listAdd(String string, int index) { |
10 | 139 list.add(string, index); |
140 } | |
141 | |
142 /* (non-Javadoc) | |
143 * @see dwtx.jface.viewers.AbstractListViewer#listSetItem(int, java.lang.String) | |
144 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
145 protected override void listSetItem(int index, String string) { |
10 | 146 list.setItem(index, string); |
147 } | |
148 | |
149 /* (non-Javadoc) | |
150 * @see dwtx.jface.viewers.AbstractListViewer#listGetSelectionIndices() | |
151 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
152 protected override int[] listGetSelectionIndices() { |
10 | 153 return list.getSelectionIndices(); |
154 } | |
155 | |
156 /* (non-Javadoc) | |
157 * @see dwtx.jface.viewers.AbstractListViewer#listGetItemCount() | |
158 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
159 protected override int listGetItemCount() { |
10 | 160 return list.getItemCount(); |
161 } | |
162 | |
163 /* (non-Javadoc) | |
164 * @see dwtx.jface.viewers.AbstractListViewer#listSetItems(java.lang.String[]) | |
165 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
166 protected override void listSetItems(String[] labels) { |
10 | 167 list.setItems(labels); |
168 } | |
169 | |
170 /* (non-Javadoc) | |
171 * @see dwtx.jface.viewers.AbstractListViewer#listRemoveAll() | |
172 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
173 protected override void listRemoveAll() { |
10 | 174 list.removeAll(); |
175 } | |
176 | |
177 /* (non-Javadoc) | |
178 * @see dwtx.jface.viewers.AbstractListViewer#listRemove(int) | |
179 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
180 protected override void listRemove(int index) { |
10 | 181 list.remove(index); |
182 } | |
183 | |
184 /* (non-Javadoc) | |
185 * @see dwtx.jface.viewers.AbstractListViewer#listSelectAndShow(int[]) | |
186 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
187 protected override void listSetSelection(int[] ixs) { |
10 | 188 list.setSelection(ixs); |
189 } | |
190 | |
191 /* (non-Javadoc) | |
192 * @see dwtx.jface.viewers.AbstractListViewer#listDeselectAll() | |
193 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
194 protected override void listDeselectAll() { |
10 | 195 list.deselectAll(); |
196 } | |
197 | |
198 /* (non-Javadoc) | |
199 * @see dwtx.jface.viewers.AbstractListViewer#listShowSelection() | |
200 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
201 protected override void listShowSelection() { |
10 | 202 list.showSelection(); |
203 } | |
204 | |
205 /* (non-Javadoc) | |
206 * @see dwtx.jface.viewers.AbstractListViewer#listGetTopIndex() | |
207 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
208 protected override int listGetTopIndex() { |
10 | 209 return list.getTopIndex(); |
210 } | |
211 | |
212 /* | |
213 * (non-Javadoc) | |
214 * @see dwtx.jface.viewers.AbstractListViewer#listSetTopIndex(int) | |
215 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
216 protected override void listSetTopIndex(int index) { |
10 | 217 list.setTopIndex(index); |
218 } | |
219 | |
220 /* (non-Javadoc) | |
221 * @see dwtx.jface.viewers.AbstractListViewer#setSelectionToWidget(java.util.List, bool) | |
222 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
223 protected override void setSelectionToWidget(SeqView!(Object) in_, bool reveal) { |
10 | 224 if( reveal ) { |
225 super.setSelectionToWidget(in_, reveal); | |
226 } else { | |
227 if (in_ is null || in_.size() is 0) { // clear selection | |
228 list.deselectAll(); | |
229 } else { | |
230 int n = in_.size(); | |
231 int[] ixs = new int[n]; | |
232 int count = 0; | |
233 for (int i = 0; i < n; ++i) { | |
234 Object el = in_.get(i); | |
235 int ix = getElementIndex(el); | |
236 if (ix >= 0) { | |
237 ixs[count++] = ix; | |
238 } | |
239 } | |
240 if (count < n) { | |
241 System.arraycopy(ixs, 0, ixs = new int[count], 0, count); | |
242 } | |
243 list.deselectAll(); | |
244 list.select(ixs); | |
245 } | |
246 } | |
247 } | |
248 | |
249 | |
250 } |