1 /*******************************************************************************
2 * Copyright (c) 2000, 2017 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.events.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
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).
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.
30 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
32 public class PopupList {
37 * Creates a PopupList above the specified shell.
39 * @param parent a Shell control which will be the parent of the new instance (cannot be null)
41 public PopupList(Shell parent) {
45 * Creates a PopupList above the specified shell.
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
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;
56 shell = new Shell(parent, checkStyle(style));
58 list = new List(shell, listStyle);
60 // close dialog if user selects outside of the shell
61 shell.addListener(SWT.Deactivate, e -> shell.setVisible (false));
63 // resize shell when list resizes
64 shell.addControlListener(ControlListener.controlResizedAdapter(e -> {
65 Rectangle shellSize = shell.getClientArea();
66 list.setSize(shellSize.width, shellSize.height);
69 // return list selection on Mouse Up or Carriage Return
70 list.addMouseListener(new MouseListener() {
72 public void mouseDoubleClick(MouseEvent e){}
74 public void mouseDown(MouseEvent e){}
76 public void mouseUp(MouseEvent e){
77 shell.setVisible (false);
80 list.addKeyListener(new KeyListener() {
82 public void keyReleased(KeyEvent e){}
84 public void keyPressed(KeyEvent e){
85 if (e.character == '\r'){
86 shell.setVisible (false);
92 private static int checkStyle (int style) {
93 int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
97 * Gets the widget font.
99 * @return the widget font
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>
106 public Font getFont () {
107 return list.getFont();
112 * This operation will fail if the items cannot
113 * be queried from the OS.
115 * @return the items in the widget
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>
122 public String[] getItems () {
123 return list.getItems();
126 * Gets the minimum width of the list.
128 * @return the minimum width of the list
130 public int getMinimumWidth () {
134 * Launches the Popup List, waits for an item to be selected and then closes the PopupList.
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
139 * @return the text of the selected item or null if no item is selected
141 public String open (Rectangle rect) {
143 Point listSize = list.computeSize (rect.width, SWT.DEFAULT, false);
144 Rectangle screenSize = shell.getDisplay().getBounds();
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;
151 if (spaceAbove > spaceBelow && listSize.y > spaceBelow) {
152 // place popup list above table cell
153 if (listSize.y > spaceAbove){
154 listSize.y = spaceAbove;
158 y = rect.y - listSize.y;
161 // place popup list below table cell
162 if (listSize.y > spaceBelow){
163 listSize.y = spaceBelow;
167 y = rect.y + rect.height;
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;
176 // Align right side of dialog with right side of cell
177 int x = rect.x + rect.width - listSize.x;
179 shell.setBounds(x, y, listSize.x, listSize.y);
184 Display display = shell.getDisplay();
185 while (!shell.isDisposed () && shell.isVisible ()) {
186 if (!display.readAndDispatch()) display.sleep();
189 String result = null;
190 if (!shell.isDisposed ()) {
191 String [] strings = list.getSelection ();
193 if (strings.length != 0) result = strings [0];
198 * Selects an item with text that starts with specified String.
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.
204 * @param string the text of the item
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>
211 public void select(String string) {
212 String[] items = list.getItems();
214 // find the first entry in the list that starts with the
217 for (int i = 0; i < items.length; i++) {
218 if (items[i].startsWith(string)){
219 int index = list.indexOf(items[i]);
227 * Sets the widget font.
229 * When new font is null, the font reverts
230 * to the default system font for the widget.
232 * @param font the new font (or null)
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>
239 public void setFont (Font font) {
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.
250 * @param strings the array of items
252 * This operation will fail when an item is null
253 * or could not be added in the OS.
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>
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>
264 public void setItems (String[] strings) {
265 list.setItems(strings);
268 * Sets the minimum width of the list.
270 * @param width the minimum width of the list
272 public void setMinimumWidth (int width) {
274 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
276 minimumWidth = width;