155
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwt.custom.PopupList;
|
|
14
|
|
15
|
|
16 import dwt.DWT;
|
|
17 import dwt.DWTException;
|
|
18 import dwt.events.ControlEvent;
|
|
19 import dwt.events.ControlListener;
|
|
20 import dwt.events.KeyEvent;
|
|
21 import dwt.events.KeyListener;
|
|
22 import dwt.events.MouseEvent;
|
|
23 import dwt.events.MouseListener;
|
|
24 import dwt.graphics.Font;
|
|
25 import dwt.graphics.Point;
|
|
26 import dwt.graphics.Rectangle;
|
|
27 import dwt.widgets.Display;
|
|
28 import dwt.widgets.Event;
|
|
29 import dwt.widgets.List;
|
|
30 import dwt.widgets.Listener;
|
|
31 import dwt.widgets.Shell;
|
|
32 /**
|
|
33 * A PopupList is a list of selectable items that appears in its own shell positioned above
|
|
34 * its parent shell. It is used for selecting items when editing a Table cell (similar to the
|
|
35 * list that appears when you open a Combo box).
|
|
36 *
|
|
37 * The list will be positioned so that it does not run off the screen and the largest number of items
|
|
38 * are visible. It may appear above the current cursor location or below it depending how close you
|
|
39 * are to the edge of the screen.
|
|
40 */
|
|
41 public class PopupList {
|
|
42 Shell shell;
|
|
43 List list;
|
|
44 int minimumWidth;
|
|
45 /**
|
|
46 * Creates a PopupList above the specified shell.
|
|
47 *
|
|
48 * @param parent a Shell control which will be the parent of the new instance (cannot be null)
|
|
49 */
|
|
50 public this(Shell parent) {
|
|
51 this (parent, 0);
|
|
52 }
|
|
53 /**
|
|
54 * Creates a PopupList above the specified shell.
|
|
55 *
|
|
56 * @param parent a widget which will be the parent of the new instance (cannot be null)
|
|
57 * @param style the style of widget to construct
|
|
58 *
|
|
59 * @since 3.0
|
|
60 */
|
|
61 public this(Shell parent, int style) {
|
|
62 shell = new Shell(parent, checkStyle(style));
|
|
63
|
|
64 list = new List(shell, DWT.SINGLE | DWT.V_SCROLL);
|
|
65
|
|
66 // close dialog if user selects outside of the shell
|
|
67 shell.addListener(DWT.Deactivate, new class() Listener {
|
|
68 public void handleEvent(Event e){
|
|
69 shell.setVisible (false);
|
|
70 }
|
|
71 });
|
|
72
|
|
73 // resize shell when list resizes
|
|
74 shell.addControlListener(new class() ControlListener {
|
|
75 public void controlMoved(ControlEvent e){}
|
|
76 public void controlResized(ControlEvent e){
|
|
77 Rectangle shellSize = shell.getClientArea();
|
|
78 list.setSize(shellSize.width, shellSize.height);
|
|
79 }
|
|
80 });
|
|
81
|
|
82 // return list selection on Mouse Up or Carriage Return
|
|
83 list.addMouseListener(new class() MouseListener {
|
|
84 public void mouseDoubleClick(MouseEvent e){}
|
|
85 public void mouseDown(MouseEvent e){}
|
|
86 public void mouseUp(MouseEvent e){
|
|
87 shell.setVisible (false);
|
|
88 }
|
|
89 });
|
|
90 list.addKeyListener(new class() KeyListener {
|
|
91 public void keyReleased(KeyEvent e){}
|
|
92 public void keyPressed(KeyEvent e){
|
|
93 if (e.character is '\r'){
|
|
94 shell.setVisible (false);
|
|
95 }
|
|
96 }
|
|
97 });
|
|
98
|
|
99 }
|
|
100 private static int checkStyle (int style) {
|
|
101 int mask = DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT;
|
|
102 return style & mask;
|
|
103 }
|
|
104 /**
|
|
105 * Gets the widget font.
|
|
106 * <p>
|
|
107 * @return the widget font
|
|
108 *
|
|
109 * @exception DWTException <ul>
|
|
110 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
111 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
112 * </ul>
|
|
113 */
|
|
114 public Font getFont () {
|
|
115 return list.getFont();
|
|
116 }
|
|
117 /**
|
|
118 * Gets the items.
|
|
119 * <p>
|
|
120 * This operation will fail if the items cannot
|
|
121 * be queried from the OS.
|
|
122 *
|
|
123 * @return the items in the widget
|
|
124 *
|
|
125 * @exception DWTException <ul>
|
|
126 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
127 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
128 * </ul>
|
|
129 */
|
|
130 public char[][] getItems () {
|
|
131 return list.getItems();
|
|
132 }
|
|
133 /**
|
|
134 * Gets the minimum width of the list.
|
|
135 *
|
|
136 * @return the minimum width of the list
|
|
137 */
|
|
138 public int getMinimumWidth () {
|
|
139 return minimumWidth;
|
|
140 }
|
|
141 /**
|
|
142 * Launches the Popup List, waits for an item to be selected and then closes the PopupList.
|
|
143 *
|
|
144 * @param rect the initial size and location of the PopupList; the dialog will be
|
|
145 * positioned so that it does not run off the screen and the largest number of items are visible
|
|
146 *
|
|
147 * @return the text of the selected item or null if no item is selected
|
|
148 */
|
|
149 public char[] open (Rectangle rect) {
|
|
150
|
|
151 Point listSize = list.computeSize (rect.width, DWT.DEFAULT, false);
|
|
152 Rectangle screenSize = shell.getDisplay().getBounds();
|
|
153
|
|
154 // Position the dialog so that it does not run off the screen and the largest number of items are visible
|
|
155 int spaceBelow = screenSize.height - (rect.y + rect.height) - 30;
|
|
156 int spaceAbove = rect.y - 30;
|
|
157
|
|
158 int y = 0;
|
|
159 if (spaceAbove > spaceBelow && listSize.y > spaceBelow) {
|
|
160 // place popup list above table cell
|
|
161 if (listSize.y > spaceAbove){
|
|
162 listSize.y = spaceAbove;
|
|
163 } else {
|
|
164 listSize.y += 2;
|
|
165 }
|
|
166 y = rect.y - listSize.y;
|
|
167
|
|
168 } else {
|
|
169 // place popup list below table cell
|
|
170 if (listSize.y > spaceBelow){
|
|
171 listSize.y = spaceBelow;
|
|
172 } else {
|
|
173 listSize.y += 2;
|
|
174 }
|
|
175 y = rect.y + rect.height;
|
|
176 }
|
|
177
|
|
178 // Make dialog as wide as the cell
|
|
179 listSize.x = rect.width;
|
|
180 // dialog width should not be less than minimumWidth
|
|
181 if (listSize.x < minimumWidth)
|
|
182 listSize.x = minimumWidth;
|
|
183
|
|
184 // Align right side of dialog with right side of cell
|
|
185 int x = rect.x + rect.width - listSize.x;
|
|
186
|
|
187 shell.setBounds(x, y, listSize.x, listSize.y);
|
|
188
|
|
189 shell.open();
|
|
190 list.setFocus();
|
|
191
|
|
192 Display display = shell.getDisplay();
|
|
193 while (!shell.isDisposed () && shell.isVisible ()) {
|
|
194 if (!display.readAndDispatch()) display.sleep();
|
|
195 }
|
|
196
|
|
197 char[] result = null;
|
|
198 if (!shell.isDisposed ()) {
|
|
199 char[] [] strings = list.getSelection ();
|
|
200 shell.dispose();
|
|
201 if (strings.length !is 0) result = strings [0];
|
|
202 }
|
|
203 return result;
|
|
204 }
|
|
205 /**
|
|
206 * Selects an item with text that starts with specified char[].
|
|
207 * <p>
|
|
208 * If the item is not currently selected, it is selected.
|
|
209 * If the item at an index is selected, it remains selected.
|
|
210 * If the string is not matched, it is ignored.
|
|
211 *
|
|
212 * @param string the text of the item
|
|
213 *
|
|
214 * @exception DWTException <ul>
|
|
215 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
216 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
217 * </ul>
|
|
218 */
|
|
219 public void select(char[] string) {
|
|
220 char[][] items = list.getItems();
|
|
221
|
|
222 // find the first entry in the list that starts with the
|
|
223 // specified string
|
|
224 if (string !is null){
|
|
225 for (int i = 0; i < items.length; i++) {
|
|
226 if ( tango.text.Util.locatePattern( items[i], string) is 0 ){
|
|
227 int index = list.indexOf(items[i]);
|
|
228 list.select(index);
|
|
229 break;
|
|
230 }
|
|
231 }
|
|
232 }
|
|
233 }
|
|
234 /**
|
|
235 * Sets the widget font.
|
|
236 * <p>
|
|
237 * When new font is null, the font reverts
|
|
238 * to the default system font for the widget.
|
|
239 *
|
|
240 * @param font the new font (or null)
|
|
241 *
|
|
242 * @exception DWTException <ul>
|
|
243 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
244 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
245 * </ul>
|
|
246 */
|
|
247 public void setFont (Font font) {
|
|
248 list.setFont(font);
|
|
249 }
|
|
250 /**
|
|
251 * Sets all items.
|
|
252 * <p>
|
|
253 * The previous selection is cleared.
|
|
254 * The previous items are deleted.
|
|
255 * The new items are added.
|
|
256 * The top index is set to 0.
|
|
257 *
|
|
258 * @param strings the array of items
|
|
259 *
|
|
260 * This operation will fail when an item is null
|
|
261 * or could not be added in the OS.
|
|
262 *
|
|
263 * @exception IllegalArgumentException <ul>
|
|
264 * <li>ERROR_NULL_ARGUMENT - if the items array is null</li>
|
|
265 * <li>ERROR_INVALID_ARGUMENT - if an item in the items array is null</li>
|
|
266 * </ul>
|
|
267 * @exception DWTException <ul>
|
|
268 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
269 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
270 * </ul>
|
|
271 */
|
|
272 public void setItems (char[][] strings) {
|
|
273 list.setItems(strings);
|
|
274 }
|
|
275 /**
|
|
276 * Sets the minimum width of the list.
|
|
277 *
|
|
278 * @param width the minimum width of the list
|
|
279 */
|
|
280 public void setMinimumWidth (int width) {
|
|
281 if (width < 0)
|
|
282 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
283
|
|
284 minimumWidth = width;
|
|
285 }
|
|
286 }
|