javauis/eswt_qt/org.eclipse.swt/Eclipse SWT/qt/org/eclipse/ercp/swt/mobile/ListView.java
changeset 21 2a9601315dfc
child 35 85266cc22c7f
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2004, 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved. This program and the accompanying materials 
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  * 
       
     8  * Contributors:
       
     9  *     Yu You (Nokia Corp.)- initial API specification 
       
    10  *     Nokia Corporation - S60 implementation
       
    11  *     Nokia Corporation - QT implementation
       
    12  *******************************************************************************/
       
    13 package org.eclipse.ercp.swt.mobile;
       
    14 
       
    15 import java.util.Vector;
       
    16 
       
    17 import org.eclipse.swt.SWT;
       
    18 import org.eclipse.swt.events.SelectionListener;
       
    19 import org.eclipse.swt.graphics.Font;
       
    20 import org.eclipse.swt.graphics.Image;
       
    21 import org.eclipse.swt.graphics.Point;
       
    22 import org.eclipse.swt.graphics.Rectangle;
       
    23 import org.eclipse.swt.internal.qt.ListUtils;
       
    24 import org.eclipse.swt.internal.qt.OS;
       
    25 import org.eclipse.swt.internal.qt.PackageProxy;
       
    26 import org.eclipse.swt.internal.qt.WidgetState;
       
    27 import org.eclipse.swt.widgets.Composite;
       
    28 import org.eclipse.swt.widgets.Internal_PackageSupport;
       
    29 import org.eclipse.swt.widgets.Scrollable;
       
    30 import org.eclipse.swt.widgets.TypedListener;
       
    31 
       
    32 /**
       
    33  * 
       
    34  * A widget that allows the user to select one or more items from a collection
       
    35  * of items that can be displayed in a multi-column way with different styles.
       
    36  * 
       
    37  * <p>
       
    38  * ListView lays out its children items in one or more columns from top to
       
    39  * bottom. If a layout orientation hint is not specified, the implementation
       
    40  * chooses the orientation. If there is only enough screen width for one column,
       
    41  * the list scrolls vertically. If there is room to display multiple columns
       
    42  * within the widget, then the list scrolls horizontally. The list never scrolls
       
    43  * in more than one direction. The layout orientation may be set at runtime by
       
    44  * calling method <code>setLayout(int)</code>.
       
    45  * <p>
       
    46  * The item density hint determines the size and positioning of items in order
       
    47  * to fit more or less within the widget. Applications can query the preferred
       
    48  * sizes of the icons for each density level. Note that the sizes may be diverse
       
    49  * in different platforms. When the given icons do not match the prefered size,
       
    50  * the implementation may adjust icon sizes without throwing any exception.
       
    51  * Applications can change the item density level at runtime by calling method
       
    52  * <code>setLayoutDensity(int)</code>. Some platforms may use context-sensitive
       
    53  * device keys to allow the user to change the ListView's density level at
       
    54  * run-time. For example, by activating "zoom in/out" device keys when the
       
    55  * ListView is focused.
       
    56  * <p>
       
    57  * Applications can query the current layout and density values by calling the
       
    58  * corresponding "get" methods.
       
    59  * 
       
    60  * <dl>
       
    61  * <dt><b>SWT styles: </b></dt>
       
    62  * <dd>SINGLE, MULTI: single or multiple selection</dd>
       
    63  * <dd>VERTICAL, HORIZONTAL: defines the layout orientation</dd>
       
    64  * <dt><b>ListView density levels: </b></dt>
       
    65  * <dd>HIGH, MEDIUM (default), LOW: defines the item density levels that
       
    66  * determine the numbers of visible list items per screen</dd>
       
    67  * <dt><b>Events: </b></dt>
       
    68  * <dd>Selection, DefaultSelection</dd>
       
    69  * </dl>
       
    70  * <p>
       
    71  * Note: If styles not applicable to ListView are specified, they are ignored.
       
    72  * </p>
       
    73  * <p>
       
    74  * Note: ListView is a subclass of Scrollable. Changing the scrollbar values (
       
    75  * <code>ScrollBar.setSelection(int)</code>) will affect the scrolling position
       
    76  * of the ListView control. However, changing the range of scrollbars, such as
       
    77  * <code>setMaximum(int)</code> and <code>setMinimum(int)</code>, will not
       
    78  * affect the ListView at all.
       
    79  * </p>
       
    80  * <p>
       
    81  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
       
    82  * </p>
       
    83  */
       
    84 public class ListView extends Scrollable {
       
    85 private Vector icons = new Vector();
       
    86 private int density;
       
    87 private int spacing;
       
    88 private boolean ignoreSelectionEvent;
       
    89 
       
    90 /**
       
    91  * A low density level.
       
    92  * 
       
    93  * <P>
       
    94  * Value <code>1</code> is assigned to <code>LOW</code>.
       
    95  * </P>
       
    96  * 
       
    97  */
       
    98 public static final int LOW = 1;
       
    99 
       
   100 /**
       
   101  * A medium density level.
       
   102  * 
       
   103  * <P>
       
   104  * Value <code>2</code> is assigned to <code>MEDIUM</code>.
       
   105  * </P>
       
   106  * 
       
   107  */
       
   108 public static final int MEDIUM = 2;
       
   109 
       
   110 /**
       
   111  * A high density level.
       
   112  * <P>
       
   113  * Value <code>3</code> is assigned to <code>HIGH</code>.
       
   114  * </P>
       
   115  * </DL>
       
   116  * 
       
   117  */
       
   118 public static final int HIGH = 3;
       
   119 
       
   120 static final class ListViewPackageProxy extends PackageProxy {
       
   121     public void createHandle(int index) {
       
   122         ((ListView)w).createHandle(index);
       
   123     }
       
   124     public void hookEvents() {
       
   125         ((ListView)w).hookEvents();
       
   126     }
       
   127     public void qt_signal_list_itemDoubleClicked(int selectionHandle) {
       
   128         ((ListView)w).qt_signal_list_itemDoubleClicked(selectionHandle);
       
   129     }
       
   130     public void qt_signal_list_itemSelectionChanged() {
       
   131         ((ListView)w).qt_signal_list_itemSelectionChanged();
       
   132     }
       
   133     public void setTraversalFlags(int type, int key, int modifier, int character) {
       
   134         ((ListView)w).setTraversalFlags(type, key, modifier, character);
       
   135     }
       
   136     public Point getPreferredClientAreaSize() {
       
   137         return ((ListView)w).getPreferredClientAreaSize();
       
   138     }
       
   139 }
       
   140     
       
   141 /**
       
   142  * Constructs a new instance of this class given its parent and a style
       
   143  * value describing its behavior and appearance.
       
   144  * <p>
       
   145  * The style value is either one of the style constants defined in class
       
   146  * <code>SWT</code> which is applicable to instances of this class, or must
       
   147  * be built by <em>bitwise OR</em> 'ing together (that is, using the
       
   148  * <code>int</code> "|" operator) two or more of those <code>SWT</code>
       
   149  * style constants. The class description lists the style constants that are
       
   150  * applicable to the class. Style bits are also inherited from superclasses.
       
   151  * </p>
       
   152  * 
       
   153  * @param parent
       
   154  *            a widget which will be the parent of the new instance (cannot
       
   155  *            be null)
       
   156  * @param style
       
   157  *            the style value of the widget to construct. See <a
       
   158  *            href="#description">Description </a> for details.
       
   159  * 
       
   160  * @exception IllegalArgumentException
       
   161  *                <ul>
       
   162  *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
       
   163  *                </ul>
       
   164  * @exception SWTException
       
   165  *                <ul>
       
   166  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   167  *                thread that created the parent</li>
       
   168  *                <li>ERROR_INVALID_SUBCLASS - if this class is not an
       
   169  *                allowed subclass</li>
       
   170  *                </ul>
       
   171  * 
       
   172  * @see SWT#SINGLE
       
   173  * @see SWT#MULTI
       
   174  * @see SWT#HORIZONTAL
       
   175  * @see SWT#VERTICAL
       
   176  */
       
   177 public ListView(Composite parent, int style) {
       
   178     super(parent, checkStyle(style), 0, new ListViewPackageProxy(), false);
       
   179 }
       
   180 
       
   181 /**
       
   182  * Constructs a new instance of this class given its parent and a style
       
   183  * value describing its behavior and appearance styles.
       
   184  * <p>
       
   185  * The style value is either one of the style constants defined in class
       
   186  * <code>SWT</code> which is applicable to instances of this class, or must
       
   187  * be built by <em>bitwise OR</em> 'ing together (that is, using the
       
   188  * <code>int</code> "|" operator) two or more of those <code>SWT</code>
       
   189  * style constants. The class description lists the style constants that are
       
   190  * applicable to the class. Style bits are also inherited from superclasses.
       
   191  * </p>
       
   192  * 
       
   193  * <p>
       
   194  * The density style is one of HIGH, MEDIUM and LOW.
       
   195  * </p>
       
   196  * 
       
   197  * @param parent
       
   198  *            a widget which will be the parent of the new instance (cannot
       
   199  *            be null)
       
   200  * @param style
       
   201  *            the style value of the widget to construct. See <a
       
   202  *            href="#description">Description </a> for details.
       
   203  * @param density
       
   204  *            the density style value.
       
   205  * 
       
   206  * @exception IllegalArgumentException
       
   207  *                <ul>
       
   208  *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
       
   209  *                <li>ERROR_INVALID_ARGUMENT - if the density is invalid</li>
       
   210  *                </ul>
       
   211  * @exception SWTException
       
   212  *                <ul>
       
   213  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   214  *                thread that created the parent</li>
       
   215  *                <li>ERROR_INVALID_SUBCLASS - if this class is not an
       
   216  *                allowed subclass</li>
       
   217  *                </ul>
       
   218  * 
       
   219  * @see SWT#SINGLE
       
   220  * @see SWT#MULTI
       
   221  * @see SWT#HORIZONTAL
       
   222  * @see SWT#VERTICAL
       
   223  * @see #HIGH
       
   224  * @see #MEDIUM
       
   225  * @see #LOW
       
   226  * @see #getLayoutDensity
       
   227  */
       
   228 public ListView(Composite parent, int style, int density) {
       
   229     super(parent, style, 0, new ListViewPackageProxy(), false);
       
   230     if (density != LOW && density != MEDIUM && density != HIGH) {
       
   231         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
       
   232     }
       
   233     this.density = density;
       
   234 }
       
   235 
       
   236 /**
       
   237  * Adds the string item and an optional icon to the end of the receiver's
       
   238  * list.
       
   239  * 
       
   240  * @param item
       
   241  *            the new item text
       
   242  * @param icon
       
   243  *            the icon of the item, or null.
       
   244  * 
       
   245  * @exception IllegalArgumentException
       
   246  *                <ul>
       
   247  *                <li>ERROR_NULL_ARGUMENT - if the item is null</li>
       
   248  *                </ul>
       
   249  * @exception SWTException
       
   250  *                <ul>
       
   251  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   252  *                disposed</li>
       
   253  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   254  *                thread that created the receiver</li>
       
   255  *                </ul>
       
   256  * @exception SWTError
       
   257  *                <ul>
       
   258  *                <li>ERROR_ITEM_NOT_ADDED - if the operation fails because
       
   259  *                of an operating system failure</li>
       
   260  *                </ul>
       
   261  * 
       
   262  * @see #add(String,Image, int)
       
   263  */
       
   264 public void add(String item, Image icon) {
       
   265     checkWidget();
       
   266     ListUtils.add(topHandle(), item, icon);
       
   267     icons.addElement(icon);
       
   268     resetGeometry(getItemCount() - 1);
       
   269 }
       
   270 
       
   271 /**
       
   272  * Adds the string item and an optional icon to the receiver's list at the
       
   273  * given zero-relative index.
       
   274  * <p>
       
   275  * Note: To add an item at the end of the list, use the result of calling
       
   276  * <code>getItemCount()</code> as the index or use
       
   277  * <code>add(String, Image)</code>.
       
   278  * </p>
       
   279  * 
       
   280  * @param string
       
   281  *            the new item
       
   282  * @param icon
       
   283  *            the icon part of the item, or null.
       
   284  * @param index
       
   285  *            the index for the item
       
   286  * 
       
   287  * @exception IllegalArgumentException
       
   288  *                <ul>
       
   289  *                <li>ERROR_NULL_ARGUMENT - if the string is null</li>
       
   290  *                <li>ERROR_INVALID_RANGE - if the index is not between 0
       
   291  *                and the number of elements in the list (inclusive)</li>
       
   292  *                </ul>
       
   293  * @exception SWTException
       
   294  *                <ul>
       
   295  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   296  *                disposed</li>
       
   297  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   298  *                thread that created the receiver</li>
       
   299  *                </ul>
       
   300  * @exception SWTError
       
   301  *                <ul>
       
   302  *                <li>ERROR_ITEM_NOT_ADDED - if the operation fails because
       
   303  *                of an operating system failure</li>
       
   304  *                </ul>
       
   305  * 
       
   306  * @see #add(String, Image)
       
   307  */
       
   308 public void add(String item, Image icon, int index) {
       
   309     checkWidget();
       
   310     ListUtils.add(topHandle(), item, icon, index);
       
   311     icons.insertElementAt(icon, index);
       
   312     resetGeometry(index);
       
   313 }
       
   314 
       
   315 /**
       
   316  * Adds the listener to the collection of listeners who will be notified
       
   317  * when the receiver's selection changes. The listener receives the messages
       
   318  * defined in the <code>SelectionListener</code> interface.
       
   319  * <p>
       
   320  * <code>widgetSelected</code> is called when the selection changes.
       
   321  * <code>widgetDefaultSelected</code> is typically called when an item is
       
   322  * double-clicked.
       
   323  * </p>
       
   324  * 
       
   325  * @param listener
       
   326  *            the listener which should be notified
       
   327  * 
       
   328  * @exception IllegalArgumentException
       
   329  *                <ul>
       
   330  *                <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
       
   331  *                </ul>
       
   332  * @exception SWTException
       
   333  *                <ul>
       
   334  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   335  *                disposed</li>
       
   336  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   337  *                thread that created the receiver</li>
       
   338  *                </ul>
       
   339  * 
       
   340  * @see SelectionListener
       
   341  * @see #removeSelectionListener
       
   342  * @see org.eclipse.swt.events.SelectionEvent
       
   343  */
       
   344 public void addSelectionListener(SelectionListener listener) {
       
   345     checkWidget();
       
   346     if (listener == null) {
       
   347         SWT.error(SWT.ERROR_NULL_ARGUMENT);
       
   348     }
       
   349     TypedListener typedListener = new TypedListener(listener);
       
   350     addListener(SWT.Selection, typedListener);
       
   351     addListener(SWT.DefaultSelection, typedListener);
       
   352 }
       
   353 
       
   354 /**
       
   355  * Deselects the item at the given zero-relative index in the receiver. If
       
   356  * the item at the index was already deselected, it remains deselected.
       
   357  * Indices that are out of range are ignored.
       
   358  * 
       
   359  * @param index
       
   360  *            the index of the item to deselect
       
   361  * 
       
   362  * @exception SWTException
       
   363  *                <ul>
       
   364  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   365  *                disposed</li>
       
   366  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   367  *                thread that created the receiver</li>
       
   368  *                </ul>
       
   369  */
       
   370 public void deselect(int index) {
       
   371     checkWidget();
       
   372     ignoreSelectionEvent = true;
       
   373     ListUtils.deselect(topHandle(), index);
       
   374     ignoreSelectionEvent = false;
       
   375 }
       
   376 
       
   377 /**
       
   378  * Deselects the items at the given zero-relative indices in the receiver.
       
   379  * If the items at the given zero-relative indexes in the receiver are
       
   380  * selected, they are deselected. If they were not selected, they remain
       
   381  * deselected. Indices that are out of range and duplicate indices are
       
   382  * ignored.
       
   383  * 
       
   384  * @param indices
       
   385  *            the array of indices for the items to deselect
       
   386  * 
       
   387  * @exception IllegalArgumentException
       
   388  *                <ul>
       
   389  *                <li>ERROR_NULL_ARGUMENT - if the set of indices is null</li>
       
   390  *                </ul>
       
   391  * @exception SWTException
       
   392  *                <ul>
       
   393  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   394  *                disposed</li>
       
   395  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   396  *                thread that created the receiver</li>
       
   397  *                </ul>
       
   398  */
       
   399 public void deselect(int[] indices) {
       
   400     checkWidget();
       
   401     ignoreSelectionEvent = true;
       
   402     ListUtils.deselect(topHandle(), indices);
       
   403     ignoreSelectionEvent = false;
       
   404 }
       
   405 
       
   406 /**
       
   407  * Deselects the items at the given zero-relative indices in the receiver.
       
   408  * If the items at the given zero-relative indexes in the receiver were
       
   409  * selected, they are deselected. If they were not selected, they remain
       
   410  * deselected. The range of the indices is inclusive. Indices that are out
       
   411  * of range are ignored.
       
   412  * 
       
   413  * @param start
       
   414  *            the start index of the items to deselect
       
   415  * @param end
       
   416  *            the end index of the items to deselect
       
   417  * 
       
   418  * @exception SWTException
       
   419  *                <ul>
       
   420  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   421  *                disposed</li>
       
   422  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   423  *                thread that created the receiver</li>
       
   424  *                </ul>
       
   425  */
       
   426 public void deselect(int start, int end) {
       
   427     checkWidget();
       
   428     ignoreSelectionEvent = true;
       
   429     ListUtils.deselect(topHandle(), start, end);
       
   430     ignoreSelectionEvent = false;
       
   431 }
       
   432 
       
   433 /**
       
   434  * Deselects all selected items in the receiver.
       
   435  * 
       
   436  * @exception SWTException
       
   437  *                <ul>
       
   438  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   439  *                disposed</li>
       
   440  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   441  *                thread that created the receiver</li>
       
   442  *                </ul>
       
   443  */
       
   444 public void deselectAll() {
       
   445     checkWidget();
       
   446     ignoreSelectionEvent = true;
       
   447     ListUtils.deselectAll(topHandle());
       
   448     ignoreSelectionEvent = false;
       
   449 }
       
   450 
       
   451 /**
       
   452  * Returns the zero-relative index of the item which currently has the focus
       
   453  * in the receiver, or -1 if no item has focus.
       
   454  * 
       
   455  * @return the index of the selected item
       
   456  * 
       
   457  * @exception SWTException
       
   458  *                <ul>
       
   459  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   460  *                disposed</li>
       
   461  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   462  *                thread that created the receiver</li>
       
   463  *                </ul>
       
   464  */
       
   465 public int getFocusIndex() {
       
   466     checkWidget();
       
   467     return ListUtils.getFocusIndex(topHandle());
       
   468 }
       
   469 
       
   470 /**
       
   471  * Returns the item icon at the given, zero-relative index in the receiver.
       
   472  * Throws an exception if the index is out of range.
       
   473  * 
       
   474  * @param index
       
   475  *            the index of the item to return
       
   476  * @return the associated icon at the given index. Null means no icon
       
   477  *         defined.
       
   478  * 
       
   479  * @exception IllegalArgumentException
       
   480  *                <ul>
       
   481  *                <li>ERROR_INVALID_RANGE - if the index is not between 0
       
   482  *                and the number of elements in the list minus 1 (inclusive)
       
   483  *                </li>
       
   484  *                </ul>
       
   485  * @exception SWTException
       
   486  *                <ul>
       
   487  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   488  *                disposed</li>
       
   489  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   490  *                thread that created the receiver</li>
       
   491  *                </ul>
       
   492  * @exception SWTError
       
   493  *                <ul>
       
   494  *                <li>ERROR_CANNOT_GET_ITEM - if the operation fails because
       
   495  *                of an operating system failure</li>
       
   496  *                </ul>
       
   497  */
       
   498 public Image getIcon(int index) {
       
   499     checkWidget();
       
   500     if (index < 0 || index >= icons.size()) {
       
   501         SWT.error(SWT.ERROR_INVALID_RANGE);
       
   502     }
       
   503     return (Image) icons.elementAt(index);
       
   504 }
       
   505 
       
   506 /**
       
   507  * Gets the preferred icon size that matches the density style. The style
       
   508  * must be one of SWT.HIGH, SWT.MEDIUM, or or SWT.LOW.
       
   509  * 
       
   510  * @param style
       
   511  *            the size style. Only SWT.HIGH, SWT.MEDIUM, and SWT.LOW can be
       
   512  *            used.
       
   513  * 
       
   514  * @return Point the best icon size in pixel (width and height). Must be
       
   515  *         positive values.
       
   516  * @exception IllegalArgumentException
       
   517  *                <ul>
       
   518  *                <li>ERROR_INVALID_ARGUMENT - if the style value is invalid
       
   519  *                </li>
       
   520  *                </ul>
       
   521  * @exception SWTException
       
   522  *                <ul>
       
   523  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   524  *                disposed</li>
       
   525  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   526  *                thread that created the receiver</li>
       
   527  *                </ul>
       
   528  * 
       
   529  */
       
   530 public Point getIconSize(int style) {
       
   531     checkWidget();
       
   532     if (style != LOW && style != MEDIUM && style != HIGH) {
       
   533         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
       
   534     }
       
   535     return OS.QAbstractItemView_iconSize(topHandle());
       
   536 }
       
   537 
       
   538 /**
       
   539  * Returns the item at the given, zero-relative index in the receiver.
       
   540  * Throws an exception if the index is out of range.
       
   541  * 
       
   542  * @param index
       
   543  *            the index of the item to return
       
   544  * @return the item at the given index
       
   545  * 
       
   546  * @exception IllegalArgumentException
       
   547  *                <ul>
       
   548  *                <li>ERROR_INVALID_RANGE - if the index is not between 0
       
   549  *                and the number of elements in the list minus 1 (inclusive)
       
   550  *                </li>
       
   551  *                </ul>
       
   552  * @exception SWTException
       
   553  *                <ul>
       
   554  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   555  *                disposed</li>
       
   556  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   557  *                thread that created the receiver</li>
       
   558  *                </ul>
       
   559  * @exception SWTError
       
   560  *                <ul>
       
   561  *                <li>ERROR_CANNOT_GET_ITEM - if the operation fails because
       
   562  *                of an operating system failure</li>
       
   563  *                </ul>
       
   564  */
       
   565 public String getItem(int index) {
       
   566     checkWidget();
       
   567     return ListUtils.getItem(topHandle(), index);
       
   568 }
       
   569 
       
   570 /**
       
   571  * Returns the number of items contained in the receiver.
       
   572  * 
       
   573  * @return the number of items
       
   574  * 
       
   575  * @exception SWTException
       
   576  *                <ul>
       
   577  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   578  *                disposed</li>
       
   579  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   580  *                thread that created the receiver</li>
       
   581  *                </ul>
       
   582  * @exception SWTError
       
   583  *                <ul>
       
   584  *                <li>ERROR_CANNOT_GET_COUNT - if the operation fails
       
   585  *                because of an operating system failure</li>
       
   586  *                </ul>
       
   587  */
       
   588 public int getItemCount() {
       
   589     checkWidget();
       
   590     return ListUtils.getItemCount(topHandle());
       
   591 }
       
   592 
       
   593 /**
       
   594  * Returns an array of <code>String</code> s which are the items in the
       
   595  * receiver.
       
   596  * <p>
       
   597  * Note: This is not the actual structure used by the receiver to maintain
       
   598  * its list of items, so modifying the array will not affect the receiver.
       
   599  * </p>
       
   600  * 
       
   601  * @return the items in the receiver's list
       
   602  * 
       
   603  * @exception SWTException
       
   604  *                <ul>
       
   605  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   606  *                disposed</li>
       
   607  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   608  *                thread that created the receiver</li>
       
   609  *                </ul>
       
   610  * @exception SWTError
       
   611  *                <ul>
       
   612  *                <li>ERROR_CANNOT_GET_ITEM - if the operation fails because
       
   613  *                of an operating system failure while getting an item</li>
       
   614  *                <li>ERROR_CANNOT_GET_COUNT - if the operation fails
       
   615  *                because of an operating system failure while getting the
       
   616  *                item count</li>
       
   617  *                </ul>
       
   618  */
       
   619 public String[] getItems() {
       
   620     checkWidget();
       
   621     return ListUtils.getItems(topHandle());
       
   622 }
       
   623 
       
   624 /**
       
   625  * Returns the value of the layout orientation.
       
   626  * 
       
   627  * 
       
   628  * @return the scrolling orientation value. One of SWT.VERTICAL and
       
   629  *         SWT.HORIZONTAL
       
   630  * @exception SWTException
       
   631  *                <ul>
       
   632  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   633  *                disposed</li>
       
   634  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   635  *                thread that created the receiver</li>
       
   636  *                </ul>
       
   637  * 
       
   638  * @see SWT#HORIZONTAL
       
   639  * @see SWT#VERTICAL
       
   640  */
       
   641 public int getLayout() {
       
   642     checkWidget();
       
   643     if (OS.QT_LISTVIEW_FLOW_LEFTTORIGHT == OS.QListView_flow(topHandle())) {
       
   644         // 0 1 2
       
   645         // 3 4 5
       
   646         // 6 7 8
       
   647         return SWT.HORIZONTAL;
       
   648     }
       
   649     else {
       
   650         // 0 3 6
       
   651         // 1 4 7
       
   652         // 2 5 8
       
   653         return SWT.VERTICAL;
       
   654     }
       
   655 }
       
   656 
       
   657 /**
       
   658  * Returns the value of the item density level.
       
   659  * 
       
   660  * @return the density level. One of HIGH, MEDIUM, or LOW.
       
   661  * @exception SWTException
       
   662  *                <ul>
       
   663  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   664  *                disposed</li>
       
   665  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   666  *                thread that created the receiver</li>
       
   667  *                </ul>
       
   668  * 
       
   669  * @see ListView#LOW
       
   670  * @see ListView#MEDIUM
       
   671  * @see ListView#HIGH
       
   672  */
       
   673 public int getLayoutDensity() {
       
   674     checkWidget();
       
   675     return density;
       
   676 }
       
   677 
       
   678 /**
       
   679  * Returns an array of <code>String</code> s that are currently selected in
       
   680  * the receiver. An empty array indicates that no items are selected.
       
   681  * <p>
       
   682  * Note: This is not the actual structure used by the receiver to maintain
       
   683  * its selection, so modifying the array will not affect the receiver.
       
   684  * </p>
       
   685  * 
       
   686  * @return an array representing the selection
       
   687  * 
       
   688  * @exception SWTException
       
   689  *                <ul>
       
   690  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   691  *                disposed</li>
       
   692  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   693  *                thread that created the receiver</li>
       
   694  *                </ul>
       
   695  * @exception SWTError
       
   696  *                <ul>
       
   697  *                <li>ERROR_CANNOT_GET_SELECTION - if the operation fails
       
   698  *                because of an operating system failure while getting the
       
   699  *                selection</li>
       
   700  *                <li>ERROR_CANNOT_GET_ITEM - if the operation fails because
       
   701  *                of an operating system failure while getting an item</li>
       
   702  *                </ul>
       
   703  */
       
   704 public String[] getSelection() {
       
   705     checkWidget();
       
   706     return ListUtils.getSelection(topHandle());
       
   707 }
       
   708 
       
   709 /**
       
   710  * Returns the number of selected items contained in the receiver.
       
   711  * 
       
   712  * @return the number of selected items
       
   713  * 
       
   714  * @exception SWTException
       
   715  *                <ul>
       
   716  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   717  *                disposed</li>
       
   718  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   719  *                thread that created the receiver</li>
       
   720  *                </ul>
       
   721  * @exception SWTError
       
   722  *                <ul>
       
   723  *                <li>ERROR_CANNOT_GET_COUNT - if the operation fails
       
   724  *                because of an operating system failure</li>
       
   725  *                </ul>
       
   726  */
       
   727 public int getSelectionCount() {
       
   728     checkWidget();
       
   729     return ListUtils.getSelectionCount(topHandle());
       
   730 }
       
   731 
       
   732 /**
       
   733  * Returns the zero-relative indices of the items which are currently
       
   734  * selected in the receiver. The array is empty if no items are selected.
       
   735  * <p>
       
   736  * Note: This is not the actual structure used by the receiver to maintain
       
   737  * its selection, so modifying the array will not affect the receiver.
       
   738  * </p>
       
   739  * 
       
   740  * @return the array of indices of the selected items
       
   741  * 
       
   742  * @exception SWTException
       
   743  *                <ul>
       
   744  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   745  *                disposed</li>
       
   746  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   747  *                thread that created the receiver</li>
       
   748  *                </ul>
       
   749  * @exception SWTError
       
   750  *                <ul>
       
   751  *                <li>ERROR_CANNOT_GET_SELECTION - if the operation fails
       
   752  *                because of an operating system failure</li>
       
   753  *                </ul>
       
   754  */
       
   755 public int[] getSelectionIndices() {
       
   756     checkWidget();
       
   757     return ListUtils.getSelectionIndices(topHandle());
       
   758 }
       
   759 
       
   760 /**
       
   761  * Returns the zero-relative index of the item which is currently at the top
       
   762  * of the receiver. This index can change when items are scrolled or new
       
   763  * items are added or removed.
       
   764  * 
       
   765  * @return the index of the top item
       
   766  * 
       
   767  * @exception SWTException
       
   768  *                <ul>
       
   769  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   770  *                disposed</li>
       
   771  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
       
   772  *                thread that created the receiver</li>
       
   773  *                </ul>
       
   774  */
       
   775 public int getTopIndex() {
       
   776     checkWidget();
       
   777     return OS.QAbstractItemView_swt_indexAt(topHandle(), 1, 1);
       
   778 }
       
   779 
       
   780     /**
       
   781  * Gets the index of an item.
       
   782  * <p>
       
   783  * The list is searched starting at 0 until an item is found that is equal
       
   784  * to the search item. If no item is found, -1 is returned. Indexing is zero
       
   785  * based.
       
   786  * 
       
   787  * @param string
       
   788  *            the search item
       
   789  * @return the index of the item
       
   790  * 
       
   791  * @exception IllegalArgumentException
       
   792  *                <ul>
       
   793  *                <li>ERROR_NULL_ARGUMENT - if the string is null</li>
       
   794  *                </ul>
       
   795  * @exception SWTException
       
   796  *                <ul>
       
   797  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   798  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
   799  *                called from the thread that created the receiver</li>
       
   800  *                </ul>
       
   801  */
       
   802 public int indexOf(String string) {
       
   803     return indexOf(string, 0);
       
   804 }
       
   805 
       
   806 /**
       
   807  * Searches the receiver's list starting at the given, zero-relative index
       
   808  * until an item is found that is equal to the argument, and returns the
       
   809  * index of that item. If no item is found or the starting index is out of
       
   810  * range, returns -1.
       
   811  * 
       
   812  * @param string
       
   813  *            the search item
       
   814  * @param start
       
   815  *            the zero-relative index at which to start the search
       
   816  * @return the index of the item
       
   817  * 
       
   818  * @exception IllegalArgumentException
       
   819  *                <ul>
       
   820  *                <li>ERROR_NULL_ARGUMENT - if the string is null</li>
       
   821  *                </ul>
       
   822  * @exception SWTException
       
   823  *                <ul>
       
   824  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   825  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
   826  *                called from the thread that created the receiver</li>
       
   827  *                </ul>
       
   828  * @exception SWTError
       
   829  *                <ul>
       
   830  *                <li>ERROR_CANNOT_GET_COUNT - if the operation fails
       
   831  *                because of an operating system failure while getting the
       
   832  *                item count</li> <li>ERROR_CANNOT_GET_ITEM - if the
       
   833  *                operation fails because of an operating system failure
       
   834  *                while getting an item</li>
       
   835  *                </ul>
       
   836  */
       
   837 public int indexOf(String string, int start) {
       
   838     checkWidget();
       
   839     return ListUtils.indexOf(topHandle(), string, start);
       
   840 }
       
   841 
       
   842 /**
       
   843  * Returns <code>true</code> if the item is selected, and <code>false</code>
       
   844  * otherwise. Indices out of range are ignored.
       
   845  * 
       
   846  * @param index
       
   847  *            the index of the item
       
   848  * @return the visibility state of the item at the index
       
   849  * 
       
   850  * @exception SWTException
       
   851  *                <ul>
       
   852  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   853  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
   854  *                called from the thread that created the receiver</li>
       
   855  *                </ul>
       
   856  */
       
   857 public boolean isSelected(int index) {
       
   858     checkWidget();
       
   859     return ListUtils.isSelected(topHandle(), index);
       
   860 }
       
   861 
       
   862 /**
       
   863  * Removes the item from the receiver at the given zero-relative index.
       
   864  * 
       
   865  * @param index
       
   866  *            the index for the item
       
   867  * 
       
   868  * @exception IllegalArgumentException
       
   869  *                <ul>
       
   870  *                <li>ERROR_INVALID_RANGE - if the index is not between 0
       
   871  *                and the number of elements in the list minus 1 (inclusive)
       
   872  *                </li>
       
   873  *                </ul>
       
   874  * @exception SWTException
       
   875  *                <ul>
       
   876  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   877  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
   878  *                called from the thread that created the receiver</li>
       
   879  *                </ul>
       
   880  * @exception SWTError
       
   881  *                <ul>
       
   882  *                <li>ERROR_ITEM_NOT_REMOVED - if the operation fails
       
   883  *                because of an operating system failure</li>
       
   884  *                </ul>
       
   885  */
       
   886 public void remove(int index) {
       
   887     checkWidget();
       
   888     ListUtils.remove(topHandle(), index);
       
   889     icons.removeElementAt(index);
       
   890     resetGeometry();
       
   891 }
       
   892 
       
   893 /**
       
   894  * Removes the items from the receiver which are between the given
       
   895  * zero-relative start and end indices (inclusive).
       
   896  * 
       
   897  * @param start
       
   898  *            the start of the range
       
   899  * @param end
       
   900  *            the end of the range
       
   901  * 
       
   902  * @exception IllegalArgumentException
       
   903  *                <ul>
       
   904  *                <li>ERROR_INVALID_RANGE - if either the start or end are
       
   905  *                not between 0 and the number of elements in the list minus
       
   906  *                1 (inclusive)</li>
       
   907  *                </ul>
       
   908  * @exception SWTException
       
   909  *                <ul>
       
   910  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   911  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
   912  *                called from the thread that created the receiver</li>
       
   913  *                </ul>
       
   914  * @exception SWTError
       
   915  *                <ul>
       
   916  *                <li>ERROR_ITEM_NOT_REMOVED - if the operation fails
       
   917  *                because of an operating system failure</li>
       
   918  *                </ul>
       
   919  */
       
   920 public void remove(int start, int end) {
       
   921     checkWidget();
       
   922     ListUtils.remove(topHandle(), start, end);
       
   923     for (int i = end; i >= start; i--) {
       
   924         icons.removeElementAt(i);
       
   925     }
       
   926     resetGeometry();
       
   927 }
       
   928 
       
   929 /**
       
   930  * Searches the receiver's list starting at the first item until an item is
       
   931  * found that is equal to the argument, and removes that item from the list.
       
   932  * 
       
   933  * @param string
       
   934  *            the item to remove
       
   935  * 
       
   936  * @exception IllegalArgumentException
       
   937  *                <ul>
       
   938  *                <li>ERROR_NULL_ARGUMENT - if the string is null</li> <li>
       
   939  *                ERROR_INVALID_ARGUMENT - if the string is not found in the
       
   940  *                list</li>
       
   941  *                </ul>
       
   942  * @exception SWTException
       
   943  *                <ul>
       
   944  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   945  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
   946  *                called from the thread that created the receiver</li>
       
   947  *                </ul>
       
   948  * @exception SWTError
       
   949  *                <ul>
       
   950  *                <li>ERROR_ITEM_NOT_REMOVED - if the operation fails
       
   951  *                because of an operating system failure</li>
       
   952  *                </ul>
       
   953  */
       
   954 public void remove(String string) {
       
   955     if (string == null) {
       
   956         SWT.error(SWT.ERROR_NULL_ARGUMENT);
       
   957     }
       
   958     int index = indexOf(string, 0);
       
   959     if (index == -1) {
       
   960         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
       
   961     }
       
   962     remove(index);
       
   963 }
       
   964 
       
   965 /**
       
   966  * Removes the items from the receiver at the given zero-relative indices.
       
   967  * 
       
   968  * @param indices
       
   969  *            the array of indices of the items
       
   970  * 
       
   971  * @exception IllegalArgumentException
       
   972  *                <ul>
       
   973  *                <li>ERROR_INVALID_RANGE - if the index is not between 0
       
   974  *                and the number of elements in the list minus 1 (inclusive)
       
   975  *                </li> <li>ERROR_NULL_ARGUMENT - if the indices array is
       
   976  *                null </li>
       
   977  *                </ul>
       
   978  * @exception SWTException
       
   979  *                <ul>
       
   980  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
   981  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
   982  *                called from the thread that created the receiver</li>
       
   983  *                </ul>
       
   984  * @exception SWTError
       
   985  *                <ul>
       
   986  *                <li>ERROR_ITEM_NOT_REMOVED - if the operation fails
       
   987  *                because of an operating system failure</li>
       
   988  *                </ul>
       
   989  */
       
   990 public void remove(int[] indices) {
       
   991     checkWidget();
       
   992     int[] sortedIdxs = ListUtils.remove(topHandle(), indices);
       
   993     int prevIndex = -1;
       
   994     for (int i = sortedIdxs.length - 1; i >= 0; i--) {
       
   995         if (prevIndex != sortedIdxs[i]) {
       
   996             icons.removeElementAt(sortedIdxs[i]);
       
   997         }
       
   998         prevIndex = sortedIdxs[i];
       
   999     }
       
  1000     resetGeometry();
       
  1001 }
       
  1002 
       
  1003 /**
       
  1004  * Removes all of the items from the receiver.
       
  1005  * <p>
       
  1006  * 
       
  1007  * @exception SWTException
       
  1008  *                <ul>
       
  1009  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1010  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1011  *                called from the thread that created the receiver</li>
       
  1012  *                </ul>
       
  1013  */
       
  1014 public void removeAll() {
       
  1015     checkWidget();
       
  1016     ListUtils.removeAll(topHandle());
       
  1017     resetGeometry();
       
  1018 }
       
  1019 
       
  1020 /**
       
  1021  * Removes the listener from the collection of listeners who will be
       
  1022  * notified when the receiver's selection changes.
       
  1023  * 
       
  1024  * @param listener
       
  1025  *            the listener which should no longer be notified
       
  1026  * 
       
  1027  * @exception IllegalArgumentException
       
  1028  *                <ul>
       
  1029  *                <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
       
  1030  *                </ul>
       
  1031  * @exception SWTException
       
  1032  *                <ul>
       
  1033  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1034  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1035  *                called from the thread that created the receiver</li>
       
  1036  *                </ul>
       
  1037  * 
       
  1038  * @see SelectionListener
       
  1039  * @see #addSelectionListener
       
  1040  */
       
  1041 public void removeSelectionListener(SelectionListener listener) {
       
  1042     removeListener(SWT.Selection, listener);
       
  1043     removeListener(SWT.DefaultSelection, listener);
       
  1044 }
       
  1045 
       
  1046 /**
       
  1047  * Selects the item at the given zero-relative index in the receiver's list.
       
  1048  * If the item at the index was already selected, it remains selected.
       
  1049  * Indices that are out of range are ignored.
       
  1050  * 
       
  1051  * @param index
       
  1052  *            the index of the item to select
       
  1053  * 
       
  1054  * @exception SWTException
       
  1055  *                <ul>
       
  1056  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1057  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1058  *                called from the thread that created the receiver</li>
       
  1059  *                </ul>
       
  1060  */
       
  1061 public void select(int index) {
       
  1062     checkWidget();
       
  1063     ignoreSelectionEvent = true;
       
  1064     ListUtils.select(topHandle(), index, (SWT.MULTI & getStyle()) != 0);
       
  1065     ignoreSelectionEvent = false;
       
  1066 }
       
  1067 
       
  1068 /**
       
  1069  * Selects the items in the range specified by the given zero-relative
       
  1070  * indices in the receiver. The range of indices is inclusive. The current
       
  1071  * selection is not cleared before the new items are selected.
       
  1072  * <p>
       
  1073  * If an item in the given range is not selected, it is selected. If an item
       
  1074  * in the given range was already selected, it remains selected. Indices
       
  1075  * that are out of range are ignored and no items will be selected if start
       
  1076  * is greater than end. If the receiver is single-select and there is more
       
  1077  * than one item in the given range, then all indices are ignored.
       
  1078  * 
       
  1079  * @param start
       
  1080  *            the start of the range
       
  1081  * @param end
       
  1082  *            the end of the range
       
  1083  * 
       
  1084  * @exception SWTException
       
  1085  *                <ul>
       
  1086  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1087  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1088  *                called from the thread that created the receiver</li>
       
  1089  *                </ul>
       
  1090  * 
       
  1091  * @see List#setSelection(int,int)
       
  1092  */
       
  1093 public void select(int start, int end) {
       
  1094     checkWidget();
       
  1095     ignoreSelectionEvent = true;
       
  1096     ListUtils.select(topHandle(), start, end, (SWT.MULTI & getStyle()) != 0);
       
  1097     ignoreSelectionEvent = false;
       
  1098 }
       
  1099 
       
  1100 /**
       
  1101  * Selects the items at the given zero-relative indices in the receiver. The
       
  1102  * current selection is not cleared before the new items are selected.
       
  1103  * <p>
       
  1104  * If the item at a given index is not selected, it is selected. If the item
       
  1105  * at a given index was already selected, it remains selected. Indices that
       
  1106  * are out of range and duplicate indices are ignored. If the receiver is
       
  1107  * single-select and multiple indices are specified, then all indices are
       
  1108  * ignored.
       
  1109  * 
       
  1110  * @param indices
       
  1111  *            the array of indices for the items to select
       
  1112  * 
       
  1113  * @exception IllegalArgumentException
       
  1114  *                <ul>
       
  1115  *                <li>ERROR_NULL_ARGUMENT - if the array of indices is null
       
  1116  *                </li>
       
  1117  *                </ul>
       
  1118  * @exception SWTException
       
  1119  *                <ul>
       
  1120  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1121  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1122  *                called from the thread that created the receiver</li>
       
  1123  *                </ul>
       
  1124  * 
       
  1125  * @see List#setSelection(int[])
       
  1126  */
       
  1127 public void select(int[] indices) {
       
  1128     checkWidget();
       
  1129     ignoreSelectionEvent = true;
       
  1130     ListUtils.select(topHandle(), indices, (SWT.MULTI & getStyle()) != 0);
       
  1131     ignoreSelectionEvent = false;
       
  1132 }
       
  1133 
       
  1134 /**
       
  1135  * Selects all of the items in the receiver.
       
  1136  * <p>
       
  1137  * If the receiver is single-select, do nothing.
       
  1138  * 
       
  1139  * @exception SWTException
       
  1140  *                <ul>
       
  1141  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1142  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1143  *                called from the thread that created the receiver</li>
       
  1144  *                </ul>
       
  1145  */
       
  1146 public void selectAll() {
       
  1147     checkWidget();
       
  1148     ignoreSelectionEvent = true;
       
  1149     ListUtils.selectAll(topHandle(), (SWT.MULTI & getStyle()) != 0);
       
  1150     ignoreSelectionEvent = false;
       
  1151 }
       
  1152 
       
  1153 /**
       
  1154  * Sets the text and icon of the item in the receiver's list at the given
       
  1155  * zero-relative index to the string argument. This is equivalent to
       
  1156  * <code>remove</code> 'ing the old item at the index, and then
       
  1157  * <code>add</code> 'ing the new item at that index.
       
  1158  * 
       
  1159  * @param index
       
  1160  *            the index for the item
       
  1161  * @param string
       
  1162  *            the new text for the item
       
  1163  * @param icon
       
  1164  *            the icon image for the item, can be Null.
       
  1165  * 
       
  1166  * @exception IllegalArgumentException
       
  1167  *                <ul>
       
  1168  *                <li>ERROR_INVALID_RANGE - if the index is not between 0
       
  1169  *                and the number of elements in the list minus 1 (inclusive)
       
  1170  *                </li> <li>ERROR_NULL_ARGUMENT - if the string is null</li>
       
  1171  *                </ul>
       
  1172  * @exception SWTException
       
  1173  *                <ul>
       
  1174  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1175  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1176  *                called from the thread that created the receiver</li>
       
  1177  *                </ul>
       
  1178  * @exception SWTError
       
  1179  *                <ul>
       
  1180  *                <li>ERROR_ITEM_NOT_REMOVED - if the remove operation fails
       
  1181  *                because of an operating system failure</li> <li>
       
  1182  *                ERROR_ITEM_NOT_ADDED - if the add operation fails because
       
  1183  *                of an operating system failure</li>
       
  1184  *                </ul>
       
  1185  */
       
  1186 public void setItem(int index, String string, Image icon) {
       
  1187     checkWidget();
       
  1188     if (string == null) {
       
  1189         SWT.error(SWT.ERROR_NULL_ARGUMENT);
       
  1190     }
       
  1191     boolean isSelected = isSelected(index);
       
  1192     // Disable redrawing to prevent flickering. Ensure that redrawing is
       
  1193     // restored by using the try-finally block.
       
  1194     setRedraw(false);
       
  1195     try {
       
  1196         remove(index);
       
  1197         add(string, icon, index);
       
  1198         if (isSelected) {
       
  1199             select(index);
       
  1200         }
       
  1201         resetGeometry();
       
  1202     } finally {
       
  1203         setRedraw(true);
       
  1204     }
       
  1205 }
       
  1206 
       
  1207 /**
       
  1208  * Sets the receiver's items to be the given array of items and icons.
       
  1209  * 
       
  1210  * @param items
       
  1211  *            the array of items
       
  1212  * @param icons
       
  1213  *            the array of icons. Can be NULL.
       
  1214  * 
       
  1215  * @exception IllegalArgumentException
       
  1216  *                <ul>
       
  1217  *                <li>ERROR_NULL_ARGUMENT - if the <code>items</code> array
       
  1218  *                is null</li> <li>ERROR_INVALID_RANGE - if number of
       
  1219  *                elements in <code>icons</code> does not match the number
       
  1220  *                in <code>items</code></li>
       
  1221  *                </ul>
       
  1222  * @exception SWTException
       
  1223  *                <ul>
       
  1224  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1225  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1226  *                called from the thread that created the receiver</li>
       
  1227  *                </ul>
       
  1228  * @exception SWTError
       
  1229  *                <ul>
       
  1230  *                <li>ERROR_ITEM_NOT_ADDED - if the operation fails because
       
  1231  *                of an operating system failure</li>
       
  1232  *                </ul>
       
  1233  */
       
  1234 public void setItems(String[] items, Image[] icons) {
       
  1235     checkWidget();
       
  1236     ListUtils.setItems(topHandle(), items, icons);
       
  1237     this.icons.removeAllElements(); 
       
  1238     for (int i = 0; i < items.length; i++) {
       
  1239         this.icons.addElement((icons == null) ? null : icons[i]);
       
  1240     }
       
  1241     resetGeometry();
       
  1242 }
       
  1243 
       
  1244 /**
       
  1245  * Changes the widget layout orientation, which affects which way the widget
       
  1246  * scrolls.
       
  1247  * 
       
  1248  * @param style
       
  1249  *            the orientation style. Only SWT.VERTICAL and SWT.HORIZONTAL
       
  1250  *            can be used.
       
  1251  * 
       
  1252  * @exception IllegalArgumentException
       
  1253  *                <ul>
       
  1254  *                <li>ERROR_INVALID_ARGUMENT - if the style value is invalid
       
  1255  *                </li>
       
  1256  *                </ul>
       
  1257  * @exception SWTException
       
  1258  *                <ul>
       
  1259  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1260  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1261  *                called from the thread that created the receiver</li>
       
  1262  *                </ul>
       
  1263  * 
       
  1264  * @see SWT#HORIZONTAL
       
  1265  * @see SWT#VERTICAL
       
  1266  */
       
  1267 public void setLayout(int layout) {
       
  1268     checkWidget();
       
  1269     if (layout != SWT.HORIZONTAL && layout != SWT.VERTICAL) {
       
  1270         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
       
  1271     }
       
  1272     
       
  1273     // ListView gets the other scrollbar created always when the layout is changed.
       
  1274     setStyle(style() & ~SWT.VERTICAL);
       
  1275     setStyle(style() & ~SWT.HORIZONTAL);
       
  1276     int scrollAreaHandle = Internal_PackageSupport.scrollAreaHandle(this);
       
  1277     if (layout == SWT.HORIZONTAL) {
       
  1278         // 0 1 2
       
  1279         // 3 4 5
       
  1280         // 6 7 8
       
  1281         setStyle(style() | SWT.V_SCROLL);
       
  1282         if (getVerticalBar() == null) {
       
  1283             Internal_PackageSupport.setVerticalBar(this, Internal_PackageSupport.createScrollBar(this, SWT.V_SCROLL));
       
  1284         }
       
  1285         OS.QAbstractScrollArea_setVerticalScrollBarPolicy(scrollAreaHandle, OS.QT_SCROLLBARALWAYSON);
       
  1286         OS.QAbstractScrollArea_setHorizontalScrollBarPolicy(scrollAreaHandle, OS.QT_SCROLLBARALWAYSOFF);
       
  1287         OS.QListView_setFlow(topHandle(), OS.QT_LISTVIEW_FLOW_LEFTTORIGHT);
       
  1288     }
       
  1289     else {
       
  1290         // 0 3 6
       
  1291         // 1 4 7
       
  1292         // 2 5 8
       
  1293         setStyle(style() | SWT.H_SCROLL);
       
  1294         if (getHorizontalBar() == null) {
       
  1295             Internal_PackageSupport.setHorizontalBar(this, Internal_PackageSupport.createScrollBar(this, SWT.H_SCROLL));
       
  1296         }
       
  1297         OS.QAbstractScrollArea_setHorizontalScrollBarPolicy(scrollAreaHandle, OS.QT_SCROLLBARALWAYSON);
       
  1298         OS.QAbstractScrollArea_setVerticalScrollBarPolicy(scrollAreaHandle, OS.QT_SCROLLBARALWAYSOFF);
       
  1299         OS.QListView_setFlow(topHandle(), OS.QT_LISTVIEW_FLOW_TOPTOBOTTOM);
       
  1300     }
       
  1301 }
       
  1302 
       
  1303 /**
       
  1304  * Changes the item density level.
       
  1305  * 
       
  1306  * @param style
       
  1307  *            the density level. Only HIGH, MEDIUM, LOW can be used.
       
  1308  * 
       
  1309  * @exception IllegalArgumentException
       
  1310  *                <ul>
       
  1311  *                <li>ERROR_INVALID_ARGUMENT - if the style value is invalid
       
  1312  *                </li>
       
  1313  *                </ul>
       
  1314  * @exception SWTException
       
  1315  *                <ul>
       
  1316  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1317  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1318  *                called from the thread that created the receiver</li>
       
  1319  *                </ul>
       
  1320  * 
       
  1321  * @see ListView#LOW
       
  1322  * @see ListView#MEDIUM
       
  1323  * @see ListView#HIGH
       
  1324  */
       
  1325 public void setLayoutDensity(int style) {
       
  1326     checkWidget();
       
  1327     if (style != LOW && style != MEDIUM && style != HIGH)
       
  1328         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
       
  1329     this.density = style;
       
  1330     resetGeometry();
       
  1331 }
       
  1332 
       
  1333 /**
       
  1334  * Selects the item at the given zero-relative index in the receiver. If the
       
  1335  * item at the index was already selected, it remains selected. The current
       
  1336  * selected is first cleared, then the new items are selected. Indices that
       
  1337  * are out of range are ignored.
       
  1338  * 
       
  1339  * @param index
       
  1340  *            the index of the item to select
       
  1341  * 
       
  1342  * @exception SWTException
       
  1343  *                <ul>
       
  1344  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1345  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1346  *                called from the thread that created the receiver</li>
       
  1347  *                </ul>
       
  1348  * @see List#deselectAll()
       
  1349  * @see List#select(int)
       
  1350  */
       
  1351 public void setSelection(int index) {
       
  1352     checkWidget();
       
  1353     ignoreSelectionEvent = true;
       
  1354     ListUtils.setSelection(topHandle(), index);
       
  1355     ignoreSelectionEvent = false;
       
  1356 }
       
  1357 
       
  1358 /**
       
  1359  * Selects the items in the range specified by the given zero-relative
       
  1360  * indices in the receiver. The range of indices is inclusive. The current
       
  1361  * selection is cleared before the new items are selected.
       
  1362  * <p>
       
  1363  * Indices that are out of range are ignored and no items will be selected
       
  1364  * if start is greater than end. If the receiver is single-select and there
       
  1365  * is more than one item in the given range, then all indices are ignored.
       
  1366  * 
       
  1367  * @param start
       
  1368  *            the start index of the items to select
       
  1369  * @param end
       
  1370  *            the end index of the items to select
       
  1371  * 
       
  1372  * @exception SWTException
       
  1373  *                <ul>
       
  1374  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1375  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1376  *                called from the thread that created the receiver</li>
       
  1377  *                </ul>
       
  1378  * 
       
  1379  * @see List#deselectAll()
       
  1380  * @see List#select(int,int)
       
  1381  */
       
  1382 public void setSelection(int start, int end) {
       
  1383     checkWidget();
       
  1384     ignoreSelectionEvent = true;
       
  1385     ListUtils.setSelection(topHandle(), start, end, (SWT.MULTI & getStyle()) != 0);
       
  1386     ignoreSelectionEvent = false;
       
  1387 }
       
  1388 
       
  1389 /**
       
  1390  * Selects the items at the given zero-relative indices in the receiver. The
       
  1391  * current selection is cleared before the new items are selected.
       
  1392  * <p>
       
  1393  * Indices that are out of range and duplicate indices are ignored. If the
       
  1394  * receiver is single-select and multiple indices are specified, then all
       
  1395  * indices are ignored.
       
  1396  * 
       
  1397  * @param indices
       
  1398  *            the indices of the items to select
       
  1399  * 
       
  1400  * @exception IllegalArgumentException
       
  1401  *                <ul>
       
  1402  *                <li>ERROR_NULL_ARGUMENT - if the array of indices is null
       
  1403  *                </li>
       
  1404  *                </ul>
       
  1405  * @exception SWTException
       
  1406  *                <ul>
       
  1407  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1408  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1409  *                called from the thread that created the receiver</li>
       
  1410  *                </ul>
       
  1411  * 
       
  1412  * @see List#deselectAll()
       
  1413  * @see List#select(int[])
       
  1414  */
       
  1415 public void setSelection(int[] indices) {
       
  1416     checkWidget();
       
  1417     ignoreSelectionEvent = true;
       
  1418     ListUtils.setSelection(topHandle(), indices, (SWT.MULTI & getStyle()) != 0);
       
  1419     ignoreSelectionEvent = false;
       
  1420 }
       
  1421 
       
  1422 /**
       
  1423  * Sets the receiver's selection to be the given array of items. The current
       
  1424  * selection is cleared before the new items are selected.
       
  1425  * <p>
       
  1426  * Items that are not in the receiver are ignored. If the receiver is
       
  1427  * single-select and multiple items are specified, then all items are
       
  1428  * ignored.
       
  1429  * 
       
  1430  * @param items
       
  1431  *            the array of items
       
  1432  * 
       
  1433  * @exception IllegalArgumentException
       
  1434  *                <ul>
       
  1435  *                <li>ERROR_NULL_ARGUMENT - if the array of items is null
       
  1436  *                </li>
       
  1437  *                </ul>
       
  1438  * @exception SWTException
       
  1439  *                <ul>
       
  1440  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1441  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1442  *                called from the thread that created the receiver</li>
       
  1443  *                </ul>
       
  1444  * 
       
  1445  * @see List#deselectAll()
       
  1446  * @see List#select(int[])
       
  1447  * @see List#setSelection(int[])
       
  1448  */
       
  1449 public void setSelection(String[] items) {
       
  1450     checkWidget();
       
  1451     ignoreSelectionEvent = true;
       
  1452     ListUtils.setSelection(topHandle(), items, (SWT.MULTI & getStyle()) != 0);
       
  1453     ignoreSelectionEvent = false;
       
  1454 }
       
  1455 
       
  1456 /**
       
  1457  * Shows the selection. If the selection is already showing in the receiver,
       
  1458  * this method simply returns. Otherwise, the items are scrolled until the
       
  1459  * selection is visible. When multiple items are selected, the selection of
       
  1460  * which item becomes visible is implementation-dependent.
       
  1461  * 
       
  1462  * @exception SWTException
       
  1463  *                <ul>
       
  1464  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
       
  1465  *                disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not
       
  1466  *                called from the thread that created the receiver</li>
       
  1467  *                </ul>
       
  1468  */
       
  1469 public void showSelection() {
       
  1470     checkWidget();
       
  1471     ListUtils.showSelection(topHandle());
       
  1472 }
       
  1473 
       
  1474 void createHandle(int index) {
       
  1475     Internal_PackageSupport.createHandle(this, index);
       
  1476     int scrollAreaHandle = OS.QListWidget_new();
       
  1477     Internal_PackageSupport.setScrollAreaHandle(this, scrollAreaHandle);
       
  1478     Internal_PackageSupport.setFrameHandle(this, scrollAreaHandle);
       
  1479     Internal_PackageSupport.setHandle(this, OS.QAbstractScrollArea_viewPort(scrollAreaHandle));
       
  1480     Internal_PackageSupport.setTopHandle(this, scrollAreaHandle);
       
  1481     
       
  1482     OS.QListView_setMovement(scrollAreaHandle, OS.QT_LISTVIEW_MOVEMENT_STATIC);
       
  1483     OS.QListView_setViewMode(scrollAreaHandle, OS.QT_LISTVIEW_VIEWMODE_ICON);
       
  1484     OS.QListView_setResizeMode(scrollAreaHandle, OS.QT_LISTVIEW_RESIZEMODE_ADJUST);
       
  1485     OS.QAbstractItemView_setSelectionBehavior(scrollAreaHandle, OS.QT_ABSTRACTITEMVIEW_SELECTIONBEHAVIOR_ITEMS);
       
  1486     if ((getStyle() & SWT.MULTI) != 0) {
       
  1487         OS.QAbstractItemView_setSelectionMode(scrollAreaHandle, OS.QT_ABSTRACTITEMVIEW_SELECTIONMODE_MULTI);
       
  1488     }
       
  1489     else {
       
  1490         OS.QAbstractItemView_setSelectionMode(scrollAreaHandle, OS.QT_ABSTRACTITEMVIEW_SELECTIONMODE_SINGLE);
       
  1491     }
       
  1492     if ((getStyle() & SWT.H_SCROLL) != 0) {
       
  1493         setLayout(SWT.VERTICAL);
       
  1494     }
       
  1495     else {
       
  1496         setLayout(SWT.HORIZONTAL);
       
  1497     }
       
  1498     spacing = OS.QListView_spacing(scrollAreaHandle);
       
  1499     density = MEDIUM;
       
  1500     
       
  1501     Internal_PackageSupport.setState(this, Internal_PackageSupport.state(this) | WidgetState.HANDLE);
       
  1502 }
       
  1503 
       
  1504 void hookEvents () {
       
  1505     Internal_PackageSupport.hookEvents(this);
       
  1506     int handle = topHandle();
       
  1507     int signalProxySel = OS.SignalHandler_new(handle, Internal_PackageSupport.display(this), 
       
  1508             OS.QSIGNAL_LIST_ITEMSELECTION_CHANGED);
       
  1509     OS.QObject_connectOrThrow(handle, "itemSelectionChanged()", 
       
  1510             signalProxySel, "widgetSignal()", OS.QT_AUTOCONNECTION);
       
  1511     int signalProxyDefSel = OS.SignalHandler_new(handle, Internal_PackageSupport.display(this), 
       
  1512             OS.QSIGNAL_LIST_ITEM_DOUBLECLICKED);
       
  1513     OS.QObject_connectOrThrow(handle, "itemDoubleClicked(QListWidgetItem*)", 
       
  1514             signalProxyDefSel, "widgetSignal(QListWidgetItem*)", OS.QT_AUTOCONNECTION);
       
  1515 }
       
  1516 
       
  1517 void qt_signal_list_itemSelectionChanged(){
       
  1518     if (!ignoreSelectionEvent) {
       
  1519         Internal_PackageSupport.sendEvent(this, SWT.Selection);
       
  1520     }
       
  1521 }
       
  1522 
       
  1523 void qt_signal_list_itemDoubleClicked(int selectionHandle){
       
  1524     if (!ignoreSelectionEvent) {
       
  1525         Internal_PackageSupport.sendEvent(this, SWT.DefaultSelection);
       
  1526     }
       
  1527 }
       
  1528 
       
  1529 public void setFont(Font font) {
       
  1530     super.setFont(font);
       
  1531     resetGeometry();
       
  1532 }
       
  1533 
       
  1534 void setTraversalFlags(int type, int key, int modifier, int character) {
       
  1535     Internal_PackageSupport.setTraverseDoIt(this, false);
       
  1536     Internal_PackageSupport.setTraverseCancel(this, false);
       
  1537     
       
  1538     int handle = topHandle();
       
  1539     
       
  1540     switch (key) {    
       
  1541     case OS.QT_KEY_UP: {
       
  1542         // ^  ^  ^  ^       ^  ^  ^  ^  
       
  1543         // 4  5  6  7       1  5  9  13
       
  1544         // 8  9  10 11      2  6  10
       
  1545         // 12 13            3  7  11
       
  1546         if (getVisualRow(ListUtils.getFocusIndex(handle)) == 0) {
       
  1547             Internal_PackageSupport.setTraverseDoIt(this, true);
       
  1548             Internal_PackageSupport.setTraverseCancel(this, true);
       
  1549         }
       
  1550         break;
       
  1551     }    
       
  1552     case OS.QT_KEY_LEFT: {
       
  1553         // <  1  2  3       <  4  8  12
       
  1554         // <  5  6  7       <  5  9  13
       
  1555         // <  9  10 11      <  6  10
       
  1556         // <  13            <  7  11
       
  1557         if (getVisualCol(ListUtils.getFocusIndex(handle)) == 0) {
       
  1558             Internal_PackageSupport.setTraverseDoIt(this, true);
       
  1559             Internal_PackageSupport.setTraverseCancel(this, true);
       
  1560         }
       
  1561         break;
       
  1562     }
       
  1563     case OS.QT_KEY_DOWN: {
       
  1564         int focusIndex = ListUtils.getFocusIndex(handle);
       
  1565         int itemCount = ListUtils.getItemCount(handle);
       
  1566         int rowCount = getVisualRowCount();
       
  1567         int focusRow = getVisualRow(focusIndex);
       
  1568         if (getLayout() == SWT.HORIZONTAL) {
       
  1569             // 0  1  2  3
       
  1570             // 4  5  6  7
       
  1571             // 8  9  v  v
       
  1572             // v  v
       
  1573             if ((focusRow == rowCount - 1)
       
  1574                 || ((focusRow == rowCount - 2) 
       
  1575                     && (getVisualCol(focusIndex) > getVisualCol(itemCount - 1)))) {
       
  1576                 Internal_PackageSupport.setTraverseDoIt(this, true);
       
  1577                 Internal_PackageSupport.setTraverseCancel(this, true);
       
  1578             }
       
  1579         }
       
  1580         else {
       
  1581             // 0  4  8  12
       
  1582             // 1  5  9  v
       
  1583             // 2  6  10
       
  1584             // v  v  v
       
  1585             if ((focusIndex == itemCount - 1)
       
  1586                 || (focusRow == rowCount - 1)) {
       
  1587                 Internal_PackageSupport.setTraverseDoIt(this, true);
       
  1588                 Internal_PackageSupport.setTraverseCancel(this, true);
       
  1589             }
       
  1590         }
       
  1591         break;
       
  1592     }
       
  1593     case OS.QT_KEY_RIGHT: {
       
  1594         int focusIndex = ListUtils.getFocusIndex(handle);
       
  1595         int itemCount = ListUtils.getItemCount(handle);
       
  1596         int colCount = getVisualColCount();
       
  1597         int focusCol = getVisualCol(focusIndex);
       
  1598         if (getLayout() == SWT.HORIZONTAL) {
       
  1599             // 0  1  2  >
       
  1600             // 4  5  6  > 
       
  1601             // 8  9  10 >
       
  1602             // 12 >
       
  1603             if ((focusIndex == itemCount - 1)
       
  1604                 || (focusCol == getVisualColCount() - 1)) {
       
  1605                 Internal_PackageSupport.setTraverseDoIt(this, true);
       
  1606                 Internal_PackageSupport.setTraverseCancel(this, true);
       
  1607             }
       
  1608         }
       
  1609         else {
       
  1610             // 0  4  8  >
       
  1611             // 1  5  9  >
       
  1612             // 2  6  >
       
  1613             // 3  7  >
       
  1614             if ((focusCol == colCount - 1)
       
  1615                 || ((focusCol == colCount - 2) 
       
  1616                     && (getVisualRow(focusIndex) > getVisualRow(itemCount - 1)))) {
       
  1617                 Internal_PackageSupport.setTraverseDoIt(this, true);
       
  1618                 Internal_PackageSupport.setTraverseCancel(this, true);
       
  1619             }
       
  1620         }
       
  1621         break;
       
  1622     }    
       
  1623     // All other traverse keys can traverse
       
  1624     default: {
       
  1625         Internal_PackageSupport.setTraverseDoIt(this, true);
       
  1626         Internal_PackageSupport.setTraverseCancel(this, true);
       
  1627         break;
       
  1628     }
       
  1629     }
       
  1630 }
       
  1631 
       
  1632 Point getPreferredClientAreaSize() {
       
  1633     int count = getItemCount();
       
  1634     if (count > 0) {
       
  1635         int x = 0;
       
  1636         int y = 0;
       
  1637         Point gridSize = OS.QListView_gridSize(topHandle());
       
  1638         if (getLayout() == SWT.HORIZONTAL) {
       
  1639             x += gridSize.x * count;
       
  1640             y += gridSize.y;
       
  1641             x += 16;
       
  1642         } else {
       
  1643             x += gridSize.x;
       
  1644             y += gridSize.y * count;
       
  1645             y += 16;
       
  1646         }
       
  1647         
       
  1648         return new Point(x, y);
       
  1649     } 
       
  1650     else {
       
  1651         return OS.QWidget_sizeHint(topHandle());
       
  1652     }
       
  1653 }
       
  1654 
       
  1655 /*
       
  1656  * Style filter
       
  1657  */
       
  1658 private static int checkStyle(int style) {
       
  1659     // VERTICAL layout goes with H_SCROLL, HORIZONTAL layout goes with V_SCROLL
       
  1660     // Default layout: HORIZONTAL
       
  1661     int layoutStyle = Internal_PackageSupport.checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
       
  1662     style &= ~SWT.VERTICAL;
       
  1663     style &= ~SWT.HORIZONTAL;
       
  1664     if ((layoutStyle & SWT.VERTICAL) != 0) {
       
  1665         style |= SWT.H_SCROLL;
       
  1666     }
       
  1667     else {
       
  1668         style |= SWT.V_SCROLL;
       
  1669     }
       
  1670     return Internal_PackageSupport.checkBits (style, SWT.SINGLE, SWT.MULTI, 0, 0, 0, 0);
       
  1671 }
       
  1672 
       
  1673 /*
       
  1674  * Get spacing based on density level
       
  1675  */
       
  1676 private int getSpacing(int density) {
       
  1677     switch(density) {
       
  1678     case LOW:
       
  1679         if (spacing != 0) {
       
  1680             return spacing * 2;
       
  1681         }
       
  1682         else {
       
  1683 //          Rectangle dispRect = getDisplay().getBounds(); 
       
  1684 //          return Math.max(dispRect.height,dispRect.width) / 50;
       
  1685           return 20;
       
  1686         }
       
  1687     case HIGH: {
       
  1688           return 0;
       
  1689         }
       
  1690     case MEDIUM:
       
  1691     // fall trough
       
  1692     default:
       
  1693         return spacing;
       
  1694     }
       
  1695 }
       
  1696 
       
  1697 /*
       
  1698  * Visual column index for index 
       
  1699  */
       
  1700 private int getVisualCol(int index) {
       
  1701     if (getLayout() == SWT.HORIZONTAL) {
       
  1702         return index % getVisualColCount();
       
  1703     }
       
  1704     else {
       
  1705         return (int)Math.floor(index/ getVisualRowCount());
       
  1706     }
       
  1707 }
       
  1708 
       
  1709 /*
       
  1710  * Visual row index for index 
       
  1711  */
       
  1712 private int getVisualRow(int index) {
       
  1713     if (getLayout() == SWT.HORIZONTAL) {
       
  1714         return (int)Math.floor(index / getVisualColCount());
       
  1715     }
       
  1716     else {
       
  1717         return index % getVisualRowCount();
       
  1718     }
       
  1719 }
       
  1720 
       
  1721 /*
       
  1722  * Count visual columns 
       
  1723  */
       
  1724 private int getVisualColCount() {
       
  1725     int handle = topHandle();
       
  1726     int res = 1;
       
  1727     int count = ListUtils.getItemCount(handle);
       
  1728     if (getLayout() == SWT.HORIZONTAL) {
       
  1729         int y = OS.QAbstractItemView_visualRect(handle, 0, 0).y;
       
  1730         int i = 1;
       
  1731         while (i < count) {
       
  1732             int yTemp = y;
       
  1733             y = OS.QAbstractItemView_visualRect(handle, i, 0).y;
       
  1734             if (yTemp != y) {
       
  1735                 break;
       
  1736             }
       
  1737             res++;
       
  1738             i++;
       
  1739         }
       
  1740     }
       
  1741     else {
       
  1742         int rowCount = getVisualRowCount();
       
  1743         res = (int)Math.floor(count / rowCount);
       
  1744         if (count % rowCount > 0) {
       
  1745             res++;
       
  1746         }
       
  1747     }
       
  1748     return res;
       
  1749 }
       
  1750 
       
  1751 /*
       
  1752  * Count visual rows 
       
  1753  */
       
  1754 private int getVisualRowCount() {
       
  1755     int handle = topHandle();
       
  1756     int res = 1;
       
  1757     int count = ListUtils.getItemCount(handle);
       
  1758     if (getLayout() == SWT.HORIZONTAL) {
       
  1759         int colCount = getVisualColCount();
       
  1760         res = (int)Math.floor(count / colCount);
       
  1761         if (count % colCount > 0) {
       
  1762             res++;
       
  1763         }
       
  1764     }
       
  1765     else {
       
  1766         int x = OS.QAbstractItemView_visualRect(handle, 0, 0).x;
       
  1767         int i = 1;
       
  1768         while (i < count) {
       
  1769             int xTemp = x;
       
  1770             x = OS.QAbstractItemView_visualRect(handle, i, 0).x;
       
  1771             if (xTemp != x) {
       
  1772                 break;
       
  1773             }
       
  1774             res++;
       
  1775             i++;
       
  1776         }
       
  1777     }
       
  1778     return res;
       
  1779 }
       
  1780 
       
  1781 /*
       
  1782  * Recalculates the geometry of the list (cell size, icon size, etc.).
       
  1783  */
       
  1784 private void resetGeometry() {
       
  1785     resetGeometry(-1);
       
  1786 }
       
  1787 
       
  1788 /*
       
  1789  * Recalculates the geometry of the list (cell size, icon size, etc.). Optimized. 
       
  1790  */
       
  1791 private void resetGeometry(int lastAddedItemIndex) {
       
  1792     // In this order
       
  1793     resetIconSize(lastAddedItemIndex);
       
  1794     resetGridSize(lastAddedItemIndex);
       
  1795 }
       
  1796 
       
  1797 /*
       
  1798  * Resets the cell size. Optimized if lastAddedItemIndex not -1.
       
  1799  */
       
  1800 private void resetGridSize(int lastAddedItemIndex) {
       
  1801     int handle = topHandle();
       
  1802     Point size;
       
  1803     if (lastAddedItemIndex > -1 ) {
       
  1804         size = OS.QListView_gridSize(handle);
       
  1805         Point s = OS.QAbstractItemView_sizeHintForIndex(handle, lastAddedItemIndex, 0);
       
  1806         size.x = Math.max(size.x, s.x);
       
  1807         size.y = Math.max(size.y, s.y);        
       
  1808     }
       
  1809     else {
       
  1810         size = new Point(0, 0);
       
  1811         for (int i = 0; i < getItemCount(); i++) {
       
  1812             Point s = OS.QAbstractItemView_sizeHintForIndex(handle, i, 0);
       
  1813             size.x = Math.max(size.x, s.x);
       
  1814             size.y = Math.max(size.y, s.y);
       
  1815         }
       
  1816     }
       
  1817     int extra = getSpacing(density);
       
  1818     OS.QListView_setGridSize(handle, size.x + extra, size.y + extra);
       
  1819 }
       
  1820 
       
  1821 /*
       
  1822  * Resets the icon size. Optimized if lastAddedItemIndex not -1.
       
  1823  */
       
  1824 private void resetIconSize(int lastAddedItemIndex) {
       
  1825     int handle = topHandle();
       
  1826     Point size;
       
  1827     if (lastAddedItemIndex > -1 && getIcon(lastAddedItemIndex) != null) {
       
  1828         size = OS.QAbstractItemView_iconSize(handle);
       
  1829         Rectangle iconRect = getIcon(lastAddedItemIndex).getBounds();
       
  1830         size.x = Math.max(size.x, iconRect.width);
       
  1831         size.y = Math.max(size.y, iconRect.height);        
       
  1832     }
       
  1833     else {
       
  1834         size = new Point(0, 0);
       
  1835         for (int i = 0; i < getItemCount(); i++) {
       
  1836             Image icon = getIcon(i);
       
  1837             if (icon != null) {
       
  1838                 Rectangle iconRect = icon.getBounds();
       
  1839                 size.x = Math.max(size.x, iconRect.width);
       
  1840                 size.y = Math.max(size.y, iconRect.height);
       
  1841             }
       
  1842         }
       
  1843     }
       
  1844     OS.QAbstractItemView_setIconSize(handle, size.x, size.y);
       
  1845 }
       
  1846 
       
  1847 private final int topHandle() {
       
  1848     return Internal_PackageSupport.topHandle(this);
       
  1849 }
       
  1850 
       
  1851 private final int style() {
       
  1852     return Internal_PackageSupport.style(this);
       
  1853 }
       
  1854 
       
  1855 private final void setStyle(int style) {
       
  1856     Internal_PackageSupport.setStyle(this, style);
       
  1857 }
       
  1858 }