Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotList
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotList
92%
39/42
100%
4/4
1.08
SWTBotList$1
100%
4/4
N/A
1.08
SWTBotList$10
100%
3/3
N/A
1.08
SWTBotList$11
0%
0/3
N/A
1.08
SWTBotList$2
100%
4/4
N/A
1.08
SWTBotList$3
100%
3/3
N/A
1.08
SWTBotList$4
100%
3/3
N/A
1.08
SWTBotList$5
100%
3/3
N/A
1.08
SWTBotList$6
100%
4/4
N/A
1.08
SWTBotList$7
100%
8/8
75%
3/4
1.08
SWTBotList$8
100%
4/4
N/A
1.08
SWTBotList$9
100%
3/3
N/A
1.08
 
 1  4
 /*******************************************************************************
 2  
  * Copyright (c) 2008 Ketan Padegaonkar and others.
 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  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.widgets;
 12  
 
 13  
 import org.eclipse.swt.SWT;
 14  
 import org.eclipse.swt.widgets.List;
 15  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 16  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 17  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 18  
 import org.eclipse.swtbot.swt.finder.results.ArrayResult;
 19  
 import org.eclipse.swtbot.swt.finder.results.IntResult;
 20  
 import org.eclipse.swtbot.swt.finder.results.StringResult;
 21  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 22  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 23  
 import org.eclipse.swtbot.swt.finder.utils.StringUtils;
 24  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 25  
 import org.hamcrest.SelfDescribing;
 26  
 
 27  
 /**
 28  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 29  
  * @author Joshua Gosse <jlgosse [at] ca [dot] ibm [dot] com>
 30  
  * @version $Id$
 31  
  */
 32  
 @SWTBotWidget(clasz = List.class, preferredName = "list", referenceBy = { ReferenceBy.LABEL })
 33  
 public class SWTBotList extends AbstractSWTBotControl<List> {
 34  
 
 35  
         /**
 36  
          * Constructs an isntance of this with the given list widget.
 37  
          * 
 38  
          * @param list the list.
 39  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 40  
          */
 41  
         public SWTBotList(List list) throws WidgetNotFoundException {
 42  0
                 this(list, null);
 43  0
         }
 44  
 
 45  
         /**
 46  
          * Constructs an isntance of this with the given list widget.
 47  
          * 
 48  
          * @param list the list.
 49  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 50  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 51  
          */
 52  
         public SWTBotList(List list, SelfDescribing description) throws WidgetNotFoundException {
 53  14
                 super(list, description);
 54  14
         }
 55  
 
 56  
         /**
 57  
          * Selects the item matching the given text.
 58  
          * 
 59  
          * @param item the item to select in the list.
 60  
          */
 61  
         public void select(final String item) {
 62  2
                 log.debug(MessageFormat.format("Set selection {0} to text {1}", this, item)); //$NON-NLS-1$
 63  2
                 waitForEnabled();
 64  2
                 final int indexOf = indexOf(item);
 65  2
                 Assert.isTrue(indexOf != -1, "Item `" + item + "' not found in list."); //$NON-NLS-1$ //$NON-NLS-2$
 66  1
                 asyncExec(new VoidResult() {
 67  
                         public void run() {
 68  1
                                 widget.setSelection(indexOf);
 69  1
                         }
 70  
                 });
 71  1
                 notifySelect();
 72  1
         }
 73  
 
 74  
         /**
 75  
          * Selects the given index.
 76  
          * 
 77  
          * @param index the selection index.
 78  
          */
 79  
         public void select(final int index) {
 80  5
                 log.debug(MessageFormat.format("Set selection {0} to index {1}", this, index)); //$NON-NLS-1$
 81  5
                 waitForEnabled();
 82  5
                 int itemCount = itemCount();
 83  10
                 Assert.isTrue(index <= itemCount, java.text.MessageFormat.format(
 84  5
                                 "The index ({0}) is more than the number of items ({1}) in the list.", index, itemCount)); //$NON-NLS-1$
 85  4
                 asyncExec(new VoidResult() {
 86  
                         public void run() {
 87  4
                                 widget.setSelection(index);
 88  4
                         }
 89  
                 });
 90  4
                 notifySelect();
 91  4
         }
 92  
 
 93  
         /**
 94  
          * Gets the item count in the list
 95  
          * 
 96  
          * @return the number of items in the list.
 97  
          */
 98  
         public int itemCount() {
 99  5
                 return syncExec(new IntResult() {
 100  
                         public Integer run() {
 101  5
                                 return widget.getItemCount();
 102  
                         }
 103  
                 });
 104  
         }
 105  
 
 106  
         /**
 107  
          * Gets the selection count.
 108  
          * 
 109  
          * @return the number of selected items in the list.
 110  
          */
 111  
         public int selectionCount() {
 112  6
                 return syncExec(new IntResult() {
 113  
                         public Integer run() {
 114  6
                                 return widget.getSelectionCount();
 115  
                         }
 116  
                 });
 117  
         }
 118  
 
 119  
         /**
 120  
          * Gets the arrray of selected items.
 121  
          * 
 122  
          * @return the selected items in the list.
 123  
          */
 124  
         public String[] selection() {
 125  8
                 return syncExec(new ArrayResult<String>() {
 126  
                         public String[] run() {
 127  8
                                 return widget.getSelection();
 128  
                         }
 129  
                 });
 130  
         }
 131  
 
 132  
         /**
 133  
          * Selects the indexes provided.
 134  
          * 
 135  
          * @param indices the indices to select in the list.
 136  
          */
 137  
         public void select(final int... indices) {
 138  1
                 log.debug(MessageFormat.format("Set selection {0} to indices {1}]", this, StringUtils.join(indices, ", "))); //$NON-NLS-1$ //$NON-NLS-2$
 139  1
                 waitForEnabled();
 140  1
                 asyncExec(new VoidResult() {
 141  
                         public void run() {
 142  1
                                 widget.setSelection(indices);
 143  1
                         }
 144  
 
 145  
                 });
 146  1
                 notifySelect();
 147  1
         }
 148  
 
 149  
         /**
 150  
          * Sets the selection to the given list of items.
 151  
          * 
 152  
          * @param items the items to select in the list.
 153  
          */
 154  
         public void select(final String... items) {
 155  1
                 log.debug(MessageFormat.format("Set selection {0} to items [{1}]", this, StringUtils.join(items, ", "))); //$NON-NLS-1$ //$NON-NLS-2$
 156  1
                 waitForEnabled();
 157  1
                 asyncExec(new VoidResult() {
 158  
                         public void run() {
 159  1
                                 widget.deselectAll();
 160  4
                                 for (String item : items) {
 161  3
                                         int index = widget.indexOf(item);
 162  3
                                         if (index != -1)
 163  3
                                                 widget.select(index);
 164  
                                 }
 165  1
                         }
 166  
                 });
 167  1
                 notifySelect();
 168  1
         }
 169  
 
 170  
         /**
 171  
          * Notifies of a selection.
 172  
          */
 173  
         protected void notifySelect() {
 174  9
                 notify(SWT.MouseDown);
 175  9
                 notify(SWT.Selection);
 176  9
                 notify(SWT.MouseUp);
 177  9
         }
 178  
 
 179  
         /**
 180  
          * Unselects everything.
 181  
          */
 182  
         public void unselect() {
 183  2
                 asyncExec(new VoidResult() {
 184  
                         public void run() {
 185  2
                                 widget.deselectAll();
 186  2
                         }
 187  
                 });
 188  2
                 notifySelect();
 189  2
         }
 190  
 
 191  
         /**
 192  
          * Gets the index of the given item.
 193  
          * 
 194  
          * @param item the search item.
 195  
          * @return the index of the item, or -1 if the item does not exist.
 196  
          */
 197  
         public int indexOf(final String item) {
 198  3
                 return syncExec(new IntResult() {
 199  
                         public Integer run() {
 200  3
                                 return widget.indexOf(item);
 201  
                         }
 202  
                 });
 203  
         }
 204  
 
 205  
         /**
 206  
          * Gets the item at the given index.
 207  
          * 
 208  
          * @param index the zero based index.
 209  
          * @return the item at the specified index.
 210  
          */
 211  
         public String itemAt(final int index) {
 212  1
                 return syncExec(new StringResult() {
 213  
                         public String run() {
 214  1
                                 return widget.getItem(index);
 215  
                         }
 216  
                 });
 217  
         }
 218  
 
 219  
         /**
 220  
          * Gets the array of Strings from the List
 221  
          * 
 222  
          * @return an array of Strings
 223  
          */
 224  
         public String[] getItems() {
 225  0
                 return syncExec(new ArrayResult<String>() {
 226  
                         public String[] run() {
 227  0
                                 return widget.getItems();
 228  
                         }
 229  
                 });
 230  
         }
 231  
 }