⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccombo.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html *  * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.custom;import org.eclipse.swt.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.events.*;import org.eclipse.swt.widgets.*;import org.eclipse.swt.accessibility.*;/** * The CCombo class represents a selectable user interface object * that combines a text field and a list and issues notificiation * when an item is selected from the list. * <p> * Note that although this class is a subclass of <code>Composite</code>, * it does not make sense to add children to it, or set a layout on it. * </p> * <dl> * <dt><b>Styles:</b> * <dd>BORDER, READ_ONLY, FLAT</dd> * <dt><b>Events:</b> * <dd>Selection</dd> * </dl> */public final class CCombo extends Composite {	Text text;	List list;	int visibleItemCount = 5;	Shell popup;	Button arrow;	boolean hasFocus;	Listener listener, filter;	Color foreground, background;	Font font;	/** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. * <p> * The style value is either one of the style constants defined in * class <code>SWT</code> which is applicable to instances of this * class, or must be built by <em>bitwise OR</em>'ing together  * (that is, using the <code>int</code> "|" operator) two or more * of those <code>SWT</code> style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. * </p> * * @param parent a widget which will be the parent of the new instance (cannot be null) * @param style the style of widget to construct * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> * </ul> * * @see SWT#BORDER * @see SWT#READ_ONLY * @see SWT#FLAT * @see Widget#getStyle() */public CCombo (Composite parent, int style) {	super (parent, style = checkStyle (style));		int textStyle = SWT.SINGLE;	if ((style & SWT.READ_ONLY) != 0) textStyle |= SWT.READ_ONLY;	if ((style & SWT.FLAT) != 0) textStyle |= SWT.FLAT;	text = new Text (this, textStyle);	int arrowStyle = SWT.ARROW | SWT.DOWN;	if ((style & SWT.FLAT) != 0) arrowStyle |= SWT.FLAT;	arrow = new Button (this, arrowStyle);	listener = new Listener () {		public void handleEvent (Event event) {			if (popup == event.widget) {				popupEvent (event);				return;			}			if (text == event.widget) {				textEvent (event);				return;			}			if (list == event.widget) {				listEvent (event);				return;			}			if (arrow == event.widget) {				arrowEvent (event);				return;			}			if (CCombo.this == event.widget) {				comboEvent (event);				return;			}			if (getShell () == event.widget) {				handleFocus (SWT.FocusOut);			}		}	};	filter = new Listener() {		public void handleEvent(Event event) {			Shell shell = ((Control)event.widget).getShell ();			if (shell == CCombo.this.getShell ()) {				handleFocus (SWT.FocusOut);			}		}	};		int [] comboEvents = {SWT.Dispose, SWT.Move, SWT.Resize};	for (int i=0; i<comboEvents.length; i++) this.addListener (comboEvents [i], listener);		int [] textEvents = {SWT.KeyDown, SWT.KeyUp, SWT.Modify, SWT.MouseDown, SWT.MouseUp, SWT.Traverse, SWT.FocusIn};	for (int i=0; i<textEvents.length; i++) text.addListener (textEvents [i], listener);		int [] arrowEvents = {SWT.Selection, SWT.FocusIn};	for (int i=0; i<arrowEvents.length; i++) arrow.addListener (arrowEvents [i], listener);		createPopup(null, -1);	initAccessible();}static int checkStyle (int style) {	int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;	return style & mask;}/*** Adds an item.* <p>* The item is placed at the end of the list.* Indexing is zero based.** @param string the new item** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed* @exception SWTError(ERROR_NULL_ARGUMENT)*	when the string is null* @exception SWTError(ERROR_ITEM_NOT_ADDED)*	when the item cannot be added*/public void add (String string) {	checkWidget();	if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);	list.add (string);}/*** Adds an item at an index.* <p>* The item is placed at an index in the list.* Indexing is zero based.** This operation will fail when the index is* out of range.** @param string the new item* @param index the index for the item** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed* @exception SWTError(ERROR_NULL_ARGUMENT)*	when the string is null* @exception SWTError(ERROR_ITEM_NOT_ADDED)*	when the item cannot be added*/public void add (String string, int index) {	checkWidget();	if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);	list.add (string, index);}/**	 * Adds the listener to receive events.* <p>** @param listener the listener** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed* @exception SWTError(ERROR_NULL_ARGUMENT)*	when listener is null*/public void addModifyListener (ModifyListener listener) {	checkWidget();	if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener (listener);	addListener (SWT.Modify, typedListener);}/**	 * Adds the listener to receive events.* <p>** @param listener the listener** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed* @exception SWTError(ERROR_NULL_ARGUMENT)*	when listener is null*/public void addSelectionListener(SelectionListener listener) {	checkWidget();	if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener (listener);	addListener (SWT.Selection,typedListener);	addListener (SWT.DefaultSelection,typedListener);}void arrowEvent (Event event) {	switch (event.type) {		case SWT.FocusIn: {			handleFocus (SWT.FocusIn);			break;		}		case SWT.Selection: {			dropDown (!isDropped ());			break;		}	}}/*** Clears the current selection.* <p>** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed*/public void clearSelection () {	checkWidget();	text.clearSelection ();	list.deselectAll ();}void comboEvent (Event event) {	switch (event.type) {		case SWT.Dispose:			if (popup != null && !popup.isDisposed ()) {				list.removeListener(SWT.Dispose, listener);				popup.dispose ();			}			Shell shell = getShell ();			shell.removeListener(SWT.Deactivate, listener);			Display display = getDisplay();			display.removeFilter(SWT.FocusIn, filter);			popup = null;  			text = null;  			list = null;  			arrow = null;			break;		case SWT.Move:			dropDown(false);			break;		case SWT.Resize:			internalLayout();			break;	}}public Point computeSize (int wHint, int hHint, boolean changed) {	checkWidget();	int width = 0, height = 0;	Point textSize = text.computeSize (wHint, SWT.DEFAULT, changed);	Point arrowSize = arrow.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);	Point listSize = list.computeSize (wHint, SWT.DEFAULT, changed);	int borderWidth = getBorderWidth();		height = Math.max (hHint, Math.max(textSize.y, arrowSize.y)  + 2*borderWidth);	width = Math.max (wHint, Math.max(textSize.x + arrowSize.x + 2*borderWidth, listSize.x + 2)  );	return new Point (width, height);}void createPopup(String[] items, int selectionIndex) {				// create shell and list		popup = new Shell (getShell(), SWT.NO_TRIM | SWT.ON_TOP);		int style = getStyle();		int listStyle = SWT.SINGLE | SWT.V_SCROLL;		if ((style & SWT.FLAT) != 0) listStyle |= SWT.FLAT;		if ((style & SWT.RIGHT_TO_LEFT) != 0) listStyle |= SWT.RIGHT_TO_LEFT;		if ((style & SWT.LEFT_TO_RIGHT) != 0) listStyle |= SWT.LEFT_TO_RIGHT;		list = new List (popup, listStyle);		if (font != null) list.setFont(font);		if (foreground != null) list.setForeground(foreground);		if (background != null) list.setBackground(background);				int [] popupEvents = {SWT.Close, SWT.Paint, SWT.Deactivate};		for (int i=0; i<popupEvents.length; i++) popup.addListener (popupEvents [i], listener);		int [] listEvents = {SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp, SWT.FocusIn, SWT.Dispose};		for (int i=0; i<listEvents.length; i++) list.addListener (listEvents [i], listener);				if (items != null) list.setItems(items);		if (selectionIndex != -1) list.setSelection(selectionIndex);}/*** Deselects an item.* <p>* If the item at an index is selected, it is* deselected.  If the item at an index is not* selected, it remains deselected.  Indices* that are out of range are ignored.  Indexing* is zero based.** @param index the index of the item** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed*/public void deselect (int index) {	checkWidget();	list.deselect (index);}/*** Deselects all items.* <p>** If an item is selected, it is deselected.* If an item is not selected, it remains unselected.** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed*/public void deselectAll () {	checkWidget();	list.deselectAll ();}void dropDown (boolean drop) {	if (drop == isDropped ()) return;	if (!drop) {		popup.setVisible (false);		text.setFocus();		return;	}	if (getShell() != popup.getParent()) {		String[] items = list.getItems();		int selectionIndex = list.getSelectionIndex();		list.removeListener(SWT.Dispose, listener);		popup.dispose();		popup = null;		list = null;		createPopup(items, selectionIndex);	}		Point size = getSize();	int itemCount = list.getItemCount();	itemCount = (itemCount == 0) ? visibleItemCount : Math.min(visibleItemCount, itemCount);	int itemHeight = list.getItemHeight () * itemCount;	Point listSize = list.computeSize (SWT.DEFAULT, itemHeight);	list.setBounds (1, 1, Math.max (size.x - 2, listSize.x), listSize.y);		int index = list.getSelectionIndex ();	if (index != -1) list.setTopIndex (index);	Display display = getDisplay ();	Rectangle listRect = list.getBounds ();	Rectangle parentRect = display.map (getParent (), null, getBounds());	Point comboSize = getSize ();	Rectangle displayRect = getMonitor().getClientArea();	int width = Math.max (comboSize.x, listRect.width + 2);	int height = listRect.height + 2;	int x = parentRect.x;	int y = parentRect.y + comboSize.y;	if (y + height > displayRect.y + displayRect.height) y = parentRect.y - height;	popup.setBounds (x, y, width, height);	popup.setVisible (true);	list.setFocus();}/*  * Return the Label immediately preceding the receiver in the z-order,  * or null if none.  */Label getAssociatedLabel () {	Control[] siblings = getParent().getChildren();	for (int i = 0; i < siblings.length; i++) {		if (siblings[i] == CCombo.this) {			if (i > 0 && siblings[i-1] instanceof Label) {				return (Label) siblings[i-1];			}		}	}	return null;}public Control [] getChildren () {	checkWidget();	return new Control [0];}/** * Gets the editable state. *  * @return true if the contents can be edited * * @exception SWTException <ul> *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @since 3.0 * */public boolean getEditable () {	checkWidget ();	return text.getEditable();}/*** Gets an item at an index.* <p>* Indexing is zero based.** This operation will fail when the index is out* of range or an item could not be queried from* the OS.** @param index the index of the item* @return the item** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed* @exception SWTError(ERROR_CANNOT_GET_ITEM)*	when the operation fails*/public String getItem (int index) {	checkWidget();	return list.getItem (index);}/*** Gets the number of items.* <p>* This operation will fail if the number of* items could not be queried from the OS.** @return the number of items in the widget** @exception SWTError(ERROR_THREAD_INVALID_ACCESS)*	when called from the wrong thread* @exception SWTError(ERROR_WIDGET_DISPOSED)*	when the widget has been disposed

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -