?? list.java
字號:
* @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (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_CANNOT_GET_ITEM - if the operation fails because of an operating system failure</li> * </ul> */public String getItem (int index) { checkWidget (); int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, index, 0); if (length != OS.LB_ERR) { TCHAR buffer = new TCHAR (getCodePage (), length + 1); int result = OS.SendMessage (handle, OS.LB_GETTEXT, index, buffer); if (result != OS.LB_ERR) return buffer.toString (0, length); } int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); if (0 <= index && index < count) { error (SWT.ERROR_CANNOT_GET_ITEM); } else { error (SWT.ERROR_INVALID_RANGE); } return null;}/** * Returns the number of items contained in the receiver. * * @return the number of items * * @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_CANNOT_GET_COUNT - if the operation fails because of an operating system failure</li> * </ul> */public int getItemCount () { checkWidget (); int result = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); if (result == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_COUNT); return result;}/** * Returns the height of the area which would be used to * display <em>one</em> of the items in the tree. * * @return the height of one 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> * @exception SWTError <ul> * <li>ERROR_CANNOT_GET_ITEM_HEIGHT - if the operation fails because of an operating system failure</li> * </ul> */public int getItemHeight () { checkWidget (); int result = OS.SendMessage (handle, OS.LB_GETITEMHEIGHT, 0, 0); if (result == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_ITEM_HEIGHT); return result;}/** * Returns an array of <code>String</code>s which are the items * in the receiver. * <p> * Note: This is not the actual structure used by the receiver * to maintain its list of items, so modifying the array will * not affect the receiver. * </p> * * @return the items in the receiver's list * * @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_CANNOT_GET_ITEM - if the operation fails because of an operating system failure while getting an item</li> * <li>ERROR_CANNOT_GET_COUNT - if the operation fails because of an operating system failure while getting the item count</li> * </ul> */public String [] getItems () { checkWidget (); int count = getItemCount (); String [] result = new String [count]; for (int i=0; i<count; i++) result [i] = getItem (i); return result;}/** * Returns an array of <code>String</code>s that are currently * selected in the receiver. An empty array indicates that no * items are selected. * <p> * Note: This is not the actual structure used by the receiver * to maintain its selection, so modifying the array will * not affect the receiver. * </p> * @return an array representing the selection * * @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_CANNOT_GET_SELECTION - if the operation fails because of an operating system failure while getting the selection</li> * <li>ERROR_CANNOT_GET_ITEM - if the operation fails because of an operating system failure while getting an item</li> * </ul> */public String [] getSelection () { checkWidget (); int [] indices = getSelectionIndices (); String [] result = new String [indices.length]; for (int i=0; i<indices.length; i++) { result [i] = getItem (indices [i]); } return result;}/** * Returns the number of selected items contained in the receiver. * * @return the number of selected items * * @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_CANNOT_GET_COUNT - if the operation fails because of an operating system failure</li> * </ul> */public int getSelectionCount () { checkWidget (); if ((style & SWT.SINGLE) != 0) { int result = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); if (result == OS.LB_ERR) return 0; return 1; } int result = OS.SendMessage (handle, OS.LB_GETSELCOUNT, 0, 0); if (result == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_COUNT); return result;}/** * Returns the zero-relative index of the item which is currently * selected in the receiver, or -1 if no item is selected. * * @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> * @exception SWTError <ul> * <li>ERROR_CANNOT_GET_SELECTION - if the operation fails because of an operating system failure</li> * </ul> */public int getSelectionIndex () { checkWidget (); if ((style & SWT.SINGLE) != 0) { return OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); } int count = OS.SendMessage (handle, OS.LB_GETSELCOUNT, 0, 0); if (count == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_SELECTION); if (count == 0) return -1; int index = OS.SendMessage (handle, OS.LB_GETCARETINDEX, 0, 0); int result = OS.SendMessage (handle, OS.LB_GETSEL, index, 0); if (result == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_SELECTION); if (result != 0) return index; int [] buffer = new int [1]; result = OS.SendMessage (handle, OS.LB_GETSELITEMS, 1, buffer); if (result != 1) error (SWT.ERROR_CANNOT_GET_SELECTION); return buffer [0];}/** * Returns the zero-relative indices of the items which are currently * selected in the receiver. The array is empty if no items are selected. * <p> * Note: This is not the actual structure used by the receiver * to maintain its selection, so modifying the array will * not affect the receiver. * </p> * @return the array of indices of the selected items * * @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_CANNOT_GET_SELECTION - if the operation fails because of an operating system failure</li> * </ul> */public int [] getSelectionIndices () { checkWidget (); if ((style & SWT.SINGLE) != 0) { int result = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); if (result == OS.LB_ERR) return new int [0]; return new int [] {result}; } int length = OS.SendMessage (handle, OS.LB_GETSELCOUNT, 0, 0); if (length == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_SELECTION); int [] indices = new int [length]; int result = OS.SendMessage (handle, OS.LB_GETSELITEMS, length, indices); if (result != length) error (SWT.ERROR_CANNOT_GET_SELECTION); return indices;}/** * Returns the zero-relative index of the item which is currently * at the top of the receiver. This index can change when items are * scrolled or new items are added or removed. * * @return the index of the top 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 getTopIndex () { checkWidget (); return OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0);}/** * Gets the index of an item. * <p> * The list is searched starting at 0 until an * item is found that is equal to the search item. * If no item is found, -1 is returned. Indexing * is zero based. * * @param string the search item * @return the index of the 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> */public int indexOf (String string) { return indexOf (string, 0);}/** * Searches the receiver's list starting at the given, * zero-relative index until an item is found that is equal * to the argument, and returns the index of that item. If * no item is found or the starting index is out of range, * returns -1. * * @param string the search item * @param start the zero-relative index at which to start the search * @return the index of the 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_CANNOT_GET_COUNT - if the operation fails because of an operating system failure while getting the item count</li> * <li>ERROR_CANNOT_GET_ITEM - if the operation fails because of an operating system failure while getting an item</li> * </ul> */public int indexOf (String string, int start) { checkWidget (); if (string == null) error (SWT.ERROR_NULL_ARGUMENT); /* * Bug in Windows. For some reason, LB_FINDSTRINGEXACT * will not find empty strings even though it is legal * to insert an empty string into a list. The fix is * to search the list, an item at a time. */ if (string.length () == 0) { int count = getItemCount (); for (int i=start; i<count; i++) { if (string.equals (getItem (i))) return i; } return -1; } /* Use LB_FINDSTRINGEXACT to search for the item */ int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); if (!(0 <= start && start < count)) return -1; int index = start - 1, last; TCHAR buffer = new TCHAR (getCodePage (), string, true); do { index = OS.SendMessage (handle, OS.LB_FINDSTRINGEXACT, last = index, buffer); if (index == OS.LB_ERR || index <= last) return -1; } while (!string.equals (getItem (index))); return index;}/** * Returns <code>true</code> if the item is selected, * and <code>false</code> otherwise. Indices out of * range are ignored. * * @param index the index of the item * @return the visibility state of the item at the index * * @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 boolean isSelected (int index) { checkWidget (); int result = OS.SendMessage (handle, OS.LB_GETSEL, index, 0); return (result != 0) && (result != OS.LB_ERR);}/** * Removes the items from the receiver at the given * zero-relative indices. * * @param indices the array of indices of the items * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li> * <li>ERROR_NULL_ARGUMENT - if the indices array 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_REMOVED - if the operation fails because of an operating system failure</li> * </ul> */public void remove (int [] indices) { checkWidget (); if (indices == null) error (SWT.ERROR_NULL_ARGUMENT); if (indices.length == 0) return; int [] newIndices = new int [indices.length]; System.arraycopy (indices, 0, newIndices, 0, indices.length); sort (newIndices); int start = newIndices [newIndices.length - 1], end = newIndices [0]; int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); if (!(0 <= start && start <= end && end < count)) { error (SWT.ERROR_INVALID_RANGE); } int topIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); RECT rect = null; int hDC = 0, oldFont = 0, newFont = 0, newWidth = 0; if ((style & SWT.H_SCROLL) != 0) { rect = new RECT (); hDC = OS.GetDC (handle); newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); } int cp = getCodePage (); int i = 0, topCount = 0, last = -1; while (i < newIndices.length) { int index = newIndices [i]; if (index != last) { TCHAR buffer = null; if ((style & SWT.H_SCROLL) != 0) { int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, index, 0); if (length == OS.LB_ERR) break; buffer = new TCHAR (cp, length + 1); int result = OS.SendMessage (handle, OS.LB_GETTEXT, index, buffer); if (result == OS.LB_ERR) break; } int result = OS.SendMessage (handle, OS.LB_DELETESTRING, index, 0); if (result == OS.LB_ERR) break; if ((style & SWT.H_SCROLL) != 0) { int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; OS.DrawText (hDC, buffer, -1, rect, flags); newWidth = Math.max (newWidth, rect.right - rect.left); } if (index < topIndex) topCount++; last = index; } i++; } if ((style & SWT.H_SCROLL) != 0) { if (newFont != 0) OS.SelectObject (hDC, oldFont); OS.ReleaseDC (handle, hDC);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -