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

📄 list.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************* * 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.widgets;import org.eclipse.swt.internal.win32.*;import org.eclipse.swt.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.events.*;/**  * Instances of this class represent a selectable user interface * object that displays a list of strings and issues notificiation * when a string selected.  A list may be single or multi select. * <p> * <dl> * <dt><b>Styles:</b></dt> * <dd>SINGLE, MULTI</dd> * <dt><b>Events:</b></dt> * <dd>Selection, DefaultSelection</dd> * </dl> * <p> * Note: Only one of SINGLE and MULTI may be specified. * </p><p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> */public class List extends Scrollable {	static final int ListProc;	static final TCHAR ListClass = new TCHAR (0, "LISTBOX", true);	static {		WNDCLASS lpWndClass = new WNDCLASS ();		OS.GetClassInfo (0, ListClass, lpWndClass);		ListProc = lpWndClass.lpfnWndProc;	}/** * 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 composite control which will be the parent of the new instance (cannot be null) * @param style the style of control 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> *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> * </ul> * * @see SWT#SINGLE * @see SWT#MULTI * @see Widget#checkSubclass * @see Widget#getStyle */public List (Composite parent, int style) {	super (parent, checkStyle (style));}/** * Adds the argument to the end of the receiver's list. * * @param string the new item * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the string is null</li> * </ul> * @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> * @exception SWTError <ul> *    <li>ERROR_ITEM_NOT_ADDED - if the operation fails because of an operating system failure</li> * </ul> * * @see #add(String,int) */public void add (String string) {	checkWidget ();	if (string == null) error (SWT.ERROR_NULL_ARGUMENT);	TCHAR buffer = new TCHAR (getCodePage (), string, true);	int result = OS.SendMessage (handle, OS.LB_ADDSTRING, 0, buffer);	if (result == OS.LB_ERR) error (SWT.ERROR_ITEM_NOT_ADDED);	if (result == OS.LB_ERRSPACE) error (SWT.ERROR_ITEM_NOT_ADDED);	if ((style & SWT.H_SCROLL) != 0) setScrollWidth (buffer, true);}/** * Adds the argument to the receiver's list at the given * zero-relative index. * <p> * Note: To add an item at the end of the list, use the * result of calling <code>getItemCount()</code> as the * index or use <code>add(String)</code>. * </p> * * @param string the new item * @param index the index for the item * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the string is null</li> *    <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list (inclusive)</li> * </ul> * @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> * @exception SWTError <ul> *    <li>ERROR_ITEM_NOT_ADDED - if the operation fails because of an operating system failure</li> * </ul> * * @see #add(String) */public void add (String string, int index) {	checkWidget ();	if (string == null) error (SWT.ERROR_NULL_ARGUMENT);	if (index == -1) error (SWT.ERROR_INVALID_RANGE);	TCHAR buffer = new TCHAR (getCodePage (), string, true);	int result = OS.SendMessage (handle, OS.LB_INSERTSTRING, index, buffer);	if (result == OS.LB_ERRSPACE) error (SWT.ERROR_ITEM_NOT_ADDED);	if (result == OS.LB_ERR) {		int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0);		if (0 <= index && index <= count) {			error (SWT.ERROR_ITEM_NOT_ADDED);		} else {			error (SWT.ERROR_INVALID_RANGE);		}	}	if ((style & SWT.H_SCROLL) != 0) setScrollWidth (buffer, true);}/** * Adds the listener to the collection of listeners who will * be notified when the receiver's selection changes, by sending * it one of the messages defined in the <code>SelectionListener</code> * interface. * <p> * <code>widgetSelected</code> is called when the selection changes. * <code>widgetDefaultSelected</code> is typically called when an item is double-clicked. * </p> * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @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> * * @see SelectionListener * @see #removeSelectionListener * @see SelectionEvent */public void addSelectionListener(SelectionListener listener) {	checkWidget ();	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener (listener);	addListener (SWT.Selection,typedListener);	addListener (SWT.DefaultSelection,typedListener);}int callWindowProc (int msg, int wParam, int lParam) {	if (handle == 0) return 0;	return OS.CallWindowProc (ListProc, handle, msg, wParam, lParam);}static int checkStyle (int style) {	return checkBits (style, SWT.SINGLE, SWT.MULTI, 0, 0, 0, 0);}public Point computeSize (int wHint, int hHint, boolean changed) {	checkWidget ();	int width = 0, height = 0;	if (wHint == SWT.DEFAULT) {		if ((style & SWT.H_SCROLL) != 0) {			width = OS.SendMessage (handle, OS.LB_GETHORIZONTALEXTENT, 0, 0);		} else {			int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0);			int newFont, oldFont = 0;			int hDC = OS.GetDC (handle);			newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);			if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);			RECT rect = new RECT ();			int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX;			int cp = getCodePage ();			TCHAR buffer = new TCHAR (cp, 64 + 1);			for (int i=0; i<count; i++) {				int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, i, 0);				if (length != OS.LB_ERR) {					if (length + 1 > buffer.length ()) {						buffer = new TCHAR (cp, length + 1);					}					int result = OS.SendMessage (handle, OS.LB_GETTEXT, i, buffer);					if (result != OS.LB_ERR) {						OS.DrawText (hDC, buffer, length, rect, flags);						width = Math.max (width, rect.right - rect.left);					}				}			}				if (newFont != 0) OS.SelectObject (hDC, oldFont);			OS.ReleaseDC (handle, hDC);		}	}	if (hHint == SWT.DEFAULT) {		int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0);		int itemHeight = OS.SendMessage (handle, OS.LB_GETITEMHEIGHT, 0, 0);	 	height = count * itemHeight;	}	if (width == 0) width = DEFAULT_WIDTH;	if (height == 0) height = DEFAULT_HEIGHT;	if (wHint != SWT.DEFAULT) width = wHint;	if (hHint != SWT.DEFAULT) height = hHint;	int border = getBorderWidth ();	width += border * 2 + 3;	height += border * 2;	if ((style & SWT.V_SCROLL) != 0) {		width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);	}	if ((style & SWT.H_SCROLL) != 0) {		height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);	}	return new Point (width, height);}int defaultBackground () {	return OS.GetSysColor (OS.COLOR_WINDOW);}/** * Deselects the items at the given zero-relative indices in the receiver. * If the item at the given zero-relative index in the receiver  * is selected, it is deselected.  If the item at the index * was not selected, it remains deselected. Indices that are out * of range and duplicate indices are ignored. * * @param indices the array of indices for the items to deselect * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the set of indices is null</li> * </ul> * @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> */public void deselect (int [] indices) {	checkWidget ();	if (indices == null) error (SWT.ERROR_NULL_ARGUMENT);	if (indices.length == 0) return;	if ((style & SWT.SINGLE) != 0) {		int oldIndex = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0);		if (oldIndex == OS.LB_ERR) return;		for (int i=0; i<indices.length; i++) {			if (oldIndex == indices [i]) {				OS.SendMessage (handle, OS.LB_SETCURSEL, -1, 0);				return;			}		}		return;	}	for (int i=0; i<indices.length; i++) {		int index = indices [i];		if (index != -1) {			OS.SendMessage (handle, OS.LB_SETSEL, 0, index);		}	}}/** * Deselects the item at the given zero-relative index in the receiver. * If the item at the index was already deselected, it remains * deselected. Indices that are out of range are ignored. * * @param index the index of the item to deselect * * @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> */public void deselect (int index) {	checkWidget ();	if (index == -1) return;	if ((style & SWT.SINGLE) != 0) {		int oldIndex = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0);		if (oldIndex == OS.LB_ERR) return;		if (oldIndex == index) OS.SendMessage (handle, OS.LB_SETCURSEL, -1, 0);		return;	} 	OS.SendMessage (handle, OS.LB_SETSEL, 0, index);}/** * Deselects the items at the given zero-relative indices in the receiver. * If the item at the given zero-relative index in the receiver  * is selected, it is deselected.  If the item at the index * was not selected, it remains deselected.  The range of the * indices is inclusive. Indices that are out of range are ignored. * * @param start the start index of the items to deselect * @param end the end index of the items to deselect * * @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> */public void deselect (int start, int end) {	checkWidget ();	if (start > end) return;	if ((style & SWT.SINGLE) != 0) {		int oldIndex = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0);		if (oldIndex == OS.LB_ERR) return;		if (start <= oldIndex && oldIndex <= end) {			OS.SendMessage (handle, OS.LB_SETCURSEL, -1, 0);		}		return;	}	/*	* Ensure that at least one item is contained in	* the range from start to end.  Note that when	* start = end, LB_SELITEMRANGEEX deselects the	* item.	*/	int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0);	if (start < 0 && end < 0) return;	if (start >= count && end >= count) return;	start = Math.min (count - 1, Math.max (0, start));	end = Math.min (count - 1, Math.max (0, end));	OS.SendMessage (handle, OS.LB_SELITEMRANGEEX, end, start);}/** * Deselects all selected items in the receiver. * * @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> */public void deselectAll () {	checkWidget ();	if ((style & SWT.SINGLE) != 0) {		OS.SendMessage (handle, OS.LB_SETCURSEL, -1, 0);	} else {		OS.SendMessage (handle, OS.LB_SETSEL, 0, -1);	}}/** * Returns the zero-relative index of the item which currently * has the focus in the receiver, or -1 if no item has focus. * * @return the index of the selected item * * @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> */public int getFocusIndex () {	checkWidget ();	int result = OS.SendMessage (handle, OS.LB_GETCARETINDEX, 0, 0);	if (result == 0) {		int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0);		if (count == 0) return -1;	}	return result;}/** * Returns the item at the given, zero-relative index in the * receiver. Throws an exception if the index is out of range. * * @param index the index of the item to return * @return the item at the given index *

⌨️ 快捷键说明

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