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