Mercurial > projects > dwt-addons
comparison dwtx/jface/viewers/ComboViewer.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) 2004-2006 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 * Sebastian Davids - bug 69254 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 | |
15 module dwtx.jface.viewers.ComboViewer; | |
16 | |
17 import dwtx.jface.viewers.AbstractListViewer; | |
18 | |
19 import dwt.DWT; | |
20 import dwt.custom.CCombo; | |
21 import dwt.widgets.Combo; | |
22 import dwt.widgets.Composite; | |
23 import dwt.widgets.Control; | |
24 import dwtx.core.runtime.Assert; | |
25 | |
26 import dwt.dwthelper.utils; | |
27 | |
28 /** | |
29 * A concrete viewer based either on an DWT <code>Combo</code> control or <code>CCombo</code> | |
30 * control. This class is intended as an alternative to the JFace <code>ListViewer</code>, which displays | |
31 * its content in a combo box rather than a list. Wherever possible, this class attempts to behave | |
32 * like ListViewer. <p> | |
33 * | |
34 * This class is designed to be instantiated with a pre-existing DWT combo control | |
35 * and configured with a domain-specific content provider, label provider, element | |
36 * filter (optional), and element sorter (optional). | |
37 * </p> | |
38 * | |
39 * @see dwtx.jface.viewers.ListViewer | |
40 * @since 3.0 | |
41 */ | |
42 public final class ComboViewer : AbstractListViewer { | |
43 | |
44 /** | |
45 * This viewer's list control if this viewer is instantiated with a combo control; otherwise | |
46 * <code>null</code>. | |
47 * | |
48 * @see #ComboViewer(Combo) | |
49 */ | |
50 private Combo combo; | |
51 | |
52 /** | |
53 * This viewer's list control if this viewer is instantiated with a CCombo control; otherwise | |
54 * <code>null</code>. | |
55 * | |
56 * @see #ComboViewer(CCombo) | |
57 * @since 3.3 | |
58 */ | |
59 private CCombo ccombo; | |
60 | |
61 /** | |
62 * Creates a combo viewer on a newly-created combo control under the given parent. | |
63 * The viewer has no input, no content provider, a default label provider, | |
64 * no sorter, and no filters. | |
65 * | |
66 * @param parent the parent control | |
67 */ | |
68 public this(Composite parent) { | |
69 this(parent, DWT.READ_ONLY | DWT.BORDER); | |
70 } | |
71 | |
72 /** | |
73 * Creates a combo viewer on a newly-created combo control under the given parent. | |
74 * The combo control is created using the given DWT style bits. | |
75 * The viewer has no input, no content provider, a default label provider, | |
76 * no sorter, and no filters. | |
77 * | |
78 * @param parent the parent control | |
79 * @param style the DWT style bits | |
80 */ | |
81 public this(Composite parent, int style) { | |
82 this(new Combo(parent, style)); | |
83 } | |
84 | |
85 /** | |
86 * Creates a combo viewer on the given combo control. | |
87 * The viewer has no input, no content provider, a default label provider, | |
88 * no sorter, and no filters. | |
89 * | |
90 * @param list the combo control | |
91 */ | |
92 public this(Combo list) { | |
93 this.combo = list; | |
94 hookControl(list); | |
95 } | |
96 | |
97 /** | |
98 * Creates a combo viewer on the given CCombo control. | |
99 * The viewer has no input, no content provider, a default label provider, | |
100 * no sorter, and no filters. | |
101 * | |
102 * @param list the CCombo control | |
103 * @since 3.3 | |
104 */ | |
105 public this(CCombo list) { | |
106 this.ccombo = list; | |
107 hookControl(list); | |
108 } | |
109 | |
110 protected void listAdd(String string, int index) { | |
111 if (combo is null) { | |
112 ccombo.add(string, index); | |
113 } else { | |
114 combo.add(string, index); | |
115 } | |
116 } | |
117 | |
118 protected void listSetItem(int index, String string) { | |
119 if (combo is null) { | |
120 ccombo.setItem(index, string); | |
121 } else { | |
122 combo.setItem(index, string); | |
123 } | |
124 } | |
125 | |
126 protected int[] listGetSelectionIndices() { | |
127 if (combo is null) { | |
128 return [ ccombo.getSelectionIndex() ]; | |
129 } else { | |
130 return [ combo.getSelectionIndex() ]; | |
131 } | |
132 } | |
133 | |
134 protected int listGetItemCount() { | |
135 if (combo is null) { | |
136 return ccombo.getItemCount(); | |
137 } else { | |
138 return combo.getItemCount(); | |
139 } | |
140 } | |
141 | |
142 protected void listSetItems(String[] labels) { | |
143 if (combo is null) { | |
144 ccombo.setItems(labels); | |
145 } else { | |
146 combo.setItems(labels); | |
147 } | |
148 } | |
149 | |
150 protected void listRemoveAll() { | |
151 if (combo is null) { | |
152 ccombo.removeAll(); | |
153 } else { | |
154 combo.removeAll(); | |
155 } | |
156 } | |
157 | |
158 protected void listRemove(int index) { | |
159 if (combo is null) { | |
160 ccombo.remove(index); | |
161 } else { | |
162 combo.remove(index); | |
163 } | |
164 } | |
165 | |
166 /* (non-Javadoc) | |
167 * Method declared on Viewer. | |
168 */ | |
169 public Control getControl() { | |
170 if (combo is null) { | |
171 return ccombo; | |
172 } else { | |
173 return combo; | |
174 } | |
175 } | |
176 | |
177 /** | |
178 * Returns this list viewer's list control. If the viewer was not created on | |
179 * a CCombo control, some kind of unchecked exception is thrown. | |
180 * | |
181 * @return the list control | |
182 * @since 3.3 | |
183 */ | |
184 public CCombo getCCombo() { | |
185 Assert.isNotNull(ccombo); | |
186 return ccombo; | |
187 } | |
188 | |
189 /** | |
190 * Returns this list viewer's list control. If the viewer was not created on | |
191 * a Combo control, some kind of unchecked exception is thrown. | |
192 * | |
193 * @return the list control | |
194 */ | |
195 public Combo getCombo() { | |
196 Assert.isNotNull(combo); | |
197 return combo; | |
198 } | |
199 | |
200 /* | |
201 * Do nothing -- combos only display the selected element, so there is no way | |
202 * we can ensure that the given element is visible without changing the selection. | |
203 * Method defined on StructuredViewer. | |
204 */ | |
205 public void reveal(Object element) { | |
206 } | |
207 | |
208 /* (non-Javadoc) | |
209 * @see dwtx.jface.viewers.AbstractListViewer#listSetSelection(int[]) | |
210 */ | |
211 protected void listSetSelection(int[] ixs) { | |
212 if (combo is null) { | |
213 for (int idx = 0; idx < ixs.length; idx++) { | |
214 ccombo.select(ixs[idx]); | |
215 } | |
216 } else { | |
217 for (int idx = 0; idx < ixs.length; idx++) { | |
218 combo.select(ixs[idx]); | |
219 } | |
220 } | |
221 } | |
222 | |
223 /* (non-Javadoc) | |
224 * @see dwtx.jface.viewers.AbstractListViewer#listDeselectAll() | |
225 */ | |
226 protected void listDeselectAll() { | |
227 if (combo is null) { | |
228 ccombo.deselectAll(); | |
229 ccombo.clearSelection(); | |
230 } else { | |
231 combo.deselectAll(); | |
232 combo.clearSelection(); | |
233 } | |
234 } | |
235 | |
236 /* (non-Javadoc) | |
237 * @see dwtx.jface.viewers.AbstractListViewer#listShowSelection() | |
238 */ | |
239 protected void listShowSelection() { | |
240 } | |
241 } |