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