/******************************************************************************* * Copyright (c) 2000, 2017 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.custom; import org.eclipse.swt.*; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; /** * A PopupList is a list of selectable items that appears in its own shell positioned above * its parent shell. It is used for selecting items when editing a Table cell (similar to the * list that appears when you open a Combo box). * * The list will be positioned so that it does not run off the screen and the largest number of items * are visible. It may appear above the current cursor location or below it depending how close you * are to the edge of the screen. * * @see Sample code and further information */ public class PopupList { Shell shell; List list; int minimumWidth; /** * Creates a PopupList above the specified shell. * * @param parent a Shell control which will be the parent of the new instance (cannot be null) */ public PopupList(Shell parent) { this (parent, 0); } /** * Creates a PopupList above the specified shell. * * @param parent a widget which will be the parent of the new instance (cannot be null) * @param style the style of widget to construct * * @since 3.0 */ public PopupList(Shell parent, int style) { int listStyle = SWT.SINGLE | SWT.V_SCROLL; if ((style & SWT.H_SCROLL) != 0) listStyle |= SWT.H_SCROLL; shell = new Shell(parent, checkStyle(style)); list = new List(shell, listStyle); // close dialog if user selects outside of the shell shell.addListener(SWT.Deactivate, e -> shell.setVisible (false)); // resize shell when list resizes shell.addControlListener(ControlListener.controlResizedAdapter(e -> { Rectangle shellSize = shell.getClientArea(); list.setSize(shellSize.width, shellSize.height); })); // return list selection on Mouse Up or Carriage Return list.addMouseListener(new MouseListener() { @Override public void mouseDoubleClick(MouseEvent e){} @Override public void mouseDown(MouseEvent e){} @Override public void mouseUp(MouseEvent e){ shell.setVisible (false); } }); list.addKeyListener(new KeyListener() { @Override public void keyReleased(KeyEvent e){} @Override public void keyPressed(KeyEvent e){ if (e.character == '\r'){ shell.setVisible (false); } } }); } private static int checkStyle (int style) { int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; return style & mask; } /** * Gets the widget font. *
* @return the widget font * * @exception SWTException
* This operation will fail if the items cannot * be queried from the OS. * * @return the items in the widget * * @exception SWTException
* If the item is not currently selected, it is selected. * If the item at an index is selected, it remains selected. * If the string is not matched, it is ignored. * * @param string the text of the item * * @exception SWTException
* When new font is null, the font reverts * to the default system font for the widget. * * @param font the new font (or null) * * @exception SWTException
* The previous selection is cleared. * The previous items are deleted. * The new items are added. * The top index is set to 0. * * @param strings the array of items * * This operation will fail when an item is null * or could not be added in the OS. * * @exception IllegalArgumentException