📄 table.java
字号:
/** * 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 (); LVITEM lvItem = new LVITEM (); lvItem.mask = OS.LVIF_STATE; lvItem.stateMask = OS.LVIS_SELECTED; lvItem.iItem = index; int result = OS.SendMessage (handle, OS.LVM_GETITEM, 0, lvItem); return (result != 0) && ((lvItem.state & OS.LVIS_SELECTED) != 0);}void releaseWidget () { int hwndHeader = OS.SendMessage (handle, OS.LVM_GETHEADER, 0, 0); int columnCount = OS.SendMessage (hwndHeader, OS.HDM_GETITEMCOUNT, 0, 0); if (columnCount == 1 && columns [0] == null) columnCount = 0; for (int i=0; i<columnCount; i++) { TableColumn column = columns [i]; if (!column.isDisposed ()) column.releaseResources (); } columns = null; int itemCount = OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0); /* * Feature in Windows 98. When there are a large number * of columns and items in a table (>1000) where each * of the subitems in the table has a string, it is much * faster to delete each item with LVM_DELETEITEM rather * than using LVM_DELETEALLITEMS. The fix is to detect * this case and delete the items, one by one. The fact * that the fix is only necessary on Windows 98 was * confirmed using version 5.81 of COMCTL32.DLL on both * Windows 98 and NT. * * NOTE: LVM_DELETEALLITEMS is also sent by the table * when the table is destroyed. */ if (OS.IsWin95 && columnCount > 1) { /* Turn off redraw and leave it off */ OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0); for (int i=itemCount-1; i>=0; --i) { TableItem item = items [i]; ignoreSelect = ignoreShrink = true; OS.SendMessage (handle, OS.LVM_DELETEITEM, i, 0); ignoreSelect = ignoreShrink = false; if (item != null && !item.isDisposed ()) item.releaseResources (); } } else { for (int i=0; i<itemCount; i++) { TableItem item = items [i]; if (item != null && !item.isDisposed ()) item.releaseResources (); } } customDraw = false; items = null; if (imageList != null) { OS.SendMessage (handle, OS.LVM_SETIMAGELIST, OS.LVSIL_SMALL, 0); display.releaseImageList (imageList); } imageList = null; int hOldList = OS.SendMessage (handle, OS.LVM_GETIMAGELIST, OS.LVSIL_STATE, 0); OS.SendMessage (handle, OS.LVM_SETIMAGELIST, OS.LVSIL_STATE, 0); if (hOldList != 0) OS.ImageList_Destroy (hOldList); super.releaseWidget ();}/** * Removes the items from the receiver's list 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.LVM_GETITEMCOUNT, 0, 0); if (!(0 <= start && start <= end && end < count)) { error (SWT.ERROR_INVALID_RANGE); } int last = -1; for (int i=0; i<newIndices.length; i++) { int index = newIndices [i]; if (index != last) { TableItem item = items [index]; ignoreSelect = ignoreShrink = true; int code = OS.SendMessage (handle, OS.LVM_DELETEITEM, index, 0); ignoreSelect = ignoreShrink = false; if (code == 0) error (SWT.ERROR_ITEM_NOT_REMOVED); if (item != null && !item.isDisposed ()) item.releaseResources (); System.arraycopy (items, index + 1, items, index, --count - index); items [count] = null; last = index; } }}/** * Removes the item from the receiver at the given * zero-relative index. * * @param index the index for the item * * @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_ITEM_NOT_REMOVED - if the operation fails because of an operating system failure</li> * </ul> */public void remove (int index) { checkWidget (); int count = OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0); if (!(0 <= index && index < count)) error (SWT.ERROR_INVALID_RANGE); TableItem item = items [index]; ignoreSelect = ignoreShrink = true; int code = OS.SendMessage (handle, OS.LVM_DELETEITEM, index, 0); ignoreSelect = ignoreShrink = false; if (code == 0) error (SWT.ERROR_ITEM_NOT_REMOVED); if (item != null && !item.isDisposed ()) item.releaseResources (); System.arraycopy (items, index + 1, items, index, --count - index); items [count] = null;}/** * Removes the items from the receiver which are * between the given zero-relative start and end * indices (inclusive). * * @param start the start of the range * @param end the end of the range * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_RANGE - if either the start or end are 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_ITEM_NOT_REMOVED - if the operation fails because of an operating system failure</li> * </ul> */public void remove (int start, int end) { checkWidget (); if (start > end) return; int count = OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0); if (!(0 <= start && start <= end && end < count)) { error (SWT.ERROR_INVALID_RANGE); } if (start == 0 && end == count - 1) { removeAll (); } else { int index = start; while (index <= end) { TableItem item = items [index]; ignoreSelect = ignoreShrink = true; int code = OS.SendMessage (handle, OS.LVM_DELETEITEM, start, 0); ignoreSelect = ignoreShrink = false; if (code == 0) break; if (item != null && !item.isDisposed ()) item.releaseResources (); index++; } System.arraycopy (items, index, items, start, count - index); for (int i=count-(index-start); i<count; i++) items [i] = null; if (index <= end) error (SWT.ERROR_ITEM_NOT_REMOVED); }}/** * Removes all of the items from the receiver. * <p> * @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 removeAll () { checkWidget (); int hwndHeader = OS.SendMessage (handle, OS.LVM_GETHEADER, 0, 0); int columnCount = OS.SendMessage (hwndHeader, OS.HDM_GETITEMCOUNT, 0, 0); if (columnCount == 1 && columns [0] == null) columnCount = 0; int itemCount = OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0); /* * Feature in Windows 98. When there are a large number * of columns and items in a table (>1000) where each * of the subitems in the table has a string, it is much * faster to delete each item with LVM_DELETEITEM rather * than using LVM_DELETEALLITEMS. The fix is to detect * this case and delete the items, one by one. The fact * that the fix is only necessary on Windows 98 was * confirmed using version 5.81 of COMCTL32.DLL on both * Windows 98 and NT. * * NOTE: LVM_DELETEALLITEMS is also sent by the table * when the table is destroyed. */ if (OS.IsWin95 && columnCount > 1) { boolean redraw = drawCount == 0 && OS.IsWindowVisible (handle); if (redraw) OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0); int index = itemCount - 1; while (index >= 0) { TableItem item = items [index]; ignoreSelect = ignoreShrink = true; int code = OS.SendMessage (handle, OS.LVM_DELETEITEM, index, 0); ignoreSelect = ignoreShrink = false; if (code == 0) break; if (item != null && !item.isDisposed ()) item.releaseResources (); --index; } if (redraw) { OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0); /* * This code is intentionally commented. The window proc * for the table implements WM_SETREDRAW to invalidate * and erase the table so it is not necessary to do this * again. */// int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;// OS.RedrawWindow (handle, null, 0, flags); } if (index != -1) error (SWT.ERROR_ITEM_NOT_REMOVED); } else { ignoreSelect = ignoreShrink = true; int code = OS.SendMessage (handle, OS.LVM_DELETEALLITEMS, 0, 0); ignoreSelect = ignoreShrink = false; if (code == 0) error (SWT.ERROR_ITEM_NOT_REMOVED); for (int i=0; i<itemCount; i++) { TableItem item = items [i]; if (item != null && !item.isDisposed ()) item.releaseResources (); } } if (imageList != null) { int i = 0; while (i < columnCount) { TableColumn column = columns [i]; if (column.getImage () != null) break; i++; } if (i == columnCount) { OS.SendMessage (handle, OS.LVM_SETIMAGELIST, OS.LVSIL_SMALL, 0); display.releaseImageList (imageList); imageList = null; } } customDraw = false; items = new TableItem [4];}/** * Removes the listener from the collection of listeners who will * be notified when the receiver's selection changes. * * @param listener the listener which should no longer 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 #addSelectionListener(SelectionListener) */public void removeSelectionListener(SelectionListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Selection, listener); eventTable.unhook (SWT.DefaultSelection,listener); }/** * Selects the items at the given zero-relative indices in the receiver. * The current selection is not cleared before the new items are selected. * <p> * If the item at a given index is not selected, it is selected. * If the item at a given index was already selected, it remains selected. * Indices that are out of range and duplicate indices are ignored. * If the receiver is single-select and multiple indices are specified, * then all indices are ignored. * * @param indices the array of indices for the items to select * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the array 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> * * @see Table#setSelection(int[]) */public void select (int [] indices) { checkWidget (); if (indices == null) error (SWT.ERROR_NULL_ARGUMENT); int length = indices.length; if (length == 0 || ((style & SWT.SINGLE) != 0 && length > 1)) return; LVITEM lvItem = new LVITEM (); lvItem.state = OS.LVIS_SELECTED; lvItem.stateMask = OS.LVIS_SELECTED; for (int i=length-1; i>=0; --i) { /* * An index of -1 will apply the change to all * items. Ensure that indices are greater than -1. */ if (indices [i] >= 0) { ignoreSelect = true; OS.SendMessage (handle, OS.LVM_SETITEMSTATE, indices [i], lvItem); ignoreSelect = false; } }}/** * Selects the item at the given zero-relative index in the receiver. * If the item at the index was already selected, it remains * selected. Indices that are out of range are ignored. * * @param index the index of the item to select * * @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 select (int index) { checkWidget (); /* * An index of -1 will apply the change to all * items. Ensure that index is greater than -1. */ if (index < 0) return; LVITEM lvItem = new LVITEM (); lvItem.state = OS.LVIS_SE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -