Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotExpandItem
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotExpandItem
100%
37/37
100%
2/2
1.286
SWTBotExpandItem$1
100%
3/3
N/A
1.286
SWTBotExpandItem$2
100%
7/7
100%
2/2
1.286
SWTBotExpandItem$3
100%
7/7
100%
2/2
1.286
SWTBotExpandItem$4
100%
3/3
N/A
1.286
 
 1  2
 /*******************************************************************************
 2  
  * Copyright (c) 2011 SWTBot Committers 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  
  *     Toby Weston - initial API and implementation (Bug 259799)
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.widgets;
 12  
 
 13  
 import org.eclipse.swt.SWT;
 14  
 import org.eclipse.swt.widgets.Event;
 15  
 import org.eclipse.swt.widgets.ExpandBar;
 16  
 import org.eclipse.swt.widgets.ExpandItem;
 17  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 18  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 19  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 20  
 import org.eclipse.swtbot.swt.finder.results.WidgetResult;
 21  
 import org.hamcrest.SelfDescribing;
 22  
 
 23  
 /**
 24  
  * Represents an {@link ExpandItem}.
 25  
  * 
 26  
  * @author Toby Weston (Bug 259799)
 27  
  * @version $Id$
 28  
  */
 29  
 public class SWTBotExpandItem extends AbstractSWTBot<ExpandItem> {
 30  
         private ExpandBar        expandBar;
 31  
 
 32  
         /**
 33  
          * Constructs a new instance with the given widget.
 34  
          * 
 35  
          * @param w the widget.
 36  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 37  
          */
 38  
         public SWTBotExpandItem(ExpandItem w) throws WidgetNotFoundException {
 39  2
                 this(w, null);
 40  2
         }
 41  
 
 42  
         /**
 43  
          * Constructs a new instance with the given widget.
 44  
          * 
 45  
          * @param w the widget.
 46  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 47  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 48  
          */
 49  
         public SWTBotExpandItem(final ExpandItem w, SelfDescribing description) throws WidgetNotFoundException {
 50  6
                 super(w, description);
 51  6
                 this.expandBar = syncExec(new WidgetResult<ExpandBar>() {
 52  
                         public ExpandBar run() {
 53  6
                                 return w.getParent();
 54  
                         }
 55  
                 });
 56  6
         }
 57  
 
 58  
         /**
 59  
          * Expand this item and return itself.
 60  
          * 
 61  
          * @return itself.
 62  
          */
 63  
         public SWTBotExpandItem expand() {
 64  2
                 asyncExec(new VoidResult() {
 65  
                         public void run() {
 66  2
                                 if (isExpanded())
 67  1
                                         return;
 68  1
                                 widget.setExpanded(true);
 69  1
                                 expandNotify();
 70  1
                         }
 71  
                 });
 72  2
                 return this;
 73  
         }
 74  
 
 75  
         /**
 76  
          * Collapse this item and return itself.
 77  
          * 
 78  
          * @return itself.
 79  
          */
 80  
         public SWTBotExpandItem collapse() {
 81  2
                 asyncExec(new VoidResult() {
 82  
                         public void run() {
 83  2
                                 if (isCollapsed())
 84  1
                                         return;
 85  1
                                 widget.setExpanded(false);
 86  1
                                 collapseNotify();
 87  1
                         }
 88  
                 });
 89  2
                 return this;
 90  
         }
 91  
 
 92  
         /**
 93  
          * @return <code>true</code> if the item is expanded, <code>false</code> otherwise.
 94  
          * @see #isCollapsed()
 95  
          */
 96  
         public boolean isExpanded() {
 97  12
                 return syncExec(new BoolResult() {
 98  
                         public Boolean run() {
 99  12
                                 return widget.getExpanded();
 100  
                         }
 101  
                 });
 102  
         }
 103  
 
 104  
         /**
 105  
          * @return <code>true</code> if the item is collapsed, <code>false</code> otherwise.
 106  
          * @see SWTBotExpandItem#isExpanded()
 107  
          */
 108  
         public boolean isCollapsed() {
 109  6
                 return !isExpanded();
 110  
         }
 111  
 
 112  1
         private void expandNotify() {
 113  1
                 notifyExpandBar(SWT.MouseMove);
 114  1
                 notifyExpandBar(SWT.Activate);
 115  1
                 notifyExpandBar(SWT.FocusIn);
 116  1
                 notifyExpandBar(SWT.MouseDown);
 117  1
                 notifyExpandBar(SWT.Expand);
 118  1
                 notifyExpandBar(SWT.MeasureItem);
 119  1
                 notifyExpandBar(SWT.Deactivate);
 120  1
                 notifyExpandBar(SWT.FocusOut);
 121  1
         }
 122  
 
 123  1
         private void collapseNotify() {
 124  1
                 notifyExpandBar(SWT.MouseMove);
 125  1
                 notifyExpandBar(SWT.Activate);
 126  1
                 notifyExpandBar(SWT.FocusIn);
 127  1
                 notifyExpandBar(SWT.MouseDown);
 128  1
                 notifyExpandBar(SWT.Collapse);
 129  1
                 notifyExpandBar(SWT.MeasureItem);
 130  1
                 notifyExpandBar(SWT.Deactivate);
 131  1
                 notifyExpandBar(SWT.FocusOut);
 132  1
         }
 133  
 
 134  
         private void notifyExpandBar(int eventType) {
 135  16
                 notify(eventType, createEvent(), expandBar);
 136  16
         }
 137  
 
 138  
         protected Event createEvent() {
 139  16
                 Event e = super.createEvent();
 140  16
                 e.widget = expandBar;
 141  16
                 e.item = widget;
 142  16
                 return e;
 143  
         }
 144  
 
 145  
 }