Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-swt-dev] Addition to class Table -- invertSelection

Hi,
org.eclipse.swt.widgets.Table has methods for selecting and deselecting
items but no method to invert the selection.

I think it's quite common to invert the selection of a table.

Below is a possible implementation.

I'm not sure if there's an OS-specific message to move to the next item
which is *not* selected ... I took the code from _getSelectionIndices_ and
adjusted it.

Sebastian Davids

@@ Proposed implementation @@

/**
 * Inverts the selection of 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 invertSelection () {
    checkWidget ();
    int selectedCount = OS.SendMessage (handle, OS.LVM_GETSELECTEDCOUNT, 0,
0);
    if (selectedCount == 0)
        selectAll ();
    else {
        int itemCount = OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0);
        if (selectedCount == itemCount)
            deselectAll ();
        else {
            boolean [] selected = new boolean [itemCount];
            int i = -1;
            while ((i = OS.SendMessage (handle, OS.LVM_GETNEXTITEM, i,
OS.LVNI_SELECTED)) != -1) {
                selected[i] = true;
            }
            int invertedSelection [] = new int[itemCount - selectedCount];
            int j = invertedSelection.length;
            for (i = (itemCount - 1); i >= 0; --i) {
                if (!selected[i]) invertedSelection[--j] = i;
            }
            setSelection (invertedSelection);
        }
    }
}

-- 
>>[ Sebastian Davids - Email: <sdavids@xxxxxx> ICQ: 2267 
6231 ]<<
>>[ PGP-FP: 0DF6 5D15 6B32 683D 9CC7 287D 4744 D881 B284 
04FE ]<<

GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net



Back to the top