Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotDateTime
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotDateTime
80%
8/10
N/A
1
SWTBotDateTime$1
100%
12/12
N/A
1
SWTBotDateTime$2
100%
9/9
N/A
1
 
 1  8
 /*******************************************************************************
 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 java.util.Calendar;
 14  
 import java.util.Date;
 15  
 
 16  
 import org.eclipse.swt.SWT;
 17  
 import org.eclipse.swt.widgets.DateTime;
 18  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 19  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 20  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 21  
 import org.eclipse.swtbot.swt.finder.results.Result;
 22  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 23  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 24  
 import org.hamcrest.SelfDescribing;
 25  
 
 26  
 /**
 27  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 28  
  * @version $Id$
 29  
  */
 30  
 @SWTBotWidget(clasz = DateTime.class, preferredName = "dateTime", referenceBy = { ReferenceBy.LABEL })
 31  
 public class SWTBotDateTime extends AbstractSWTBotControl<DateTime> {
 32  
 
 33  
         /**
 34  
          * Constructs an instance of this object with the given widget.
 35  
          *
 36  
          * @param w the widget.
 37  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 38  
          * @since 2.0
 39  
          */
 40  
         public SWTBotDateTime(DateTime w) throws WidgetNotFoundException {
 41  0
                 this(w, null);
 42  0
         }
 43  
 
 44  
         /**
 45  
          * Constructs an instance of this object with the given widget.
 46  
          *
 47  
          * @param w the widget.
 48  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 49  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 50  
          * @since 2.0
 51  
          */
 52  
         public SWTBotDateTime(DateTime w, SelfDescribing description) throws WidgetNotFoundException {
 53  5
                 super(w, description);
 54  5
         }
 55  
 
 56  
         /**
 57  
          * Gets the date of this widget.
 58  
          *
 59  
          * @return the date/time set into the widget.
 60  
          */
 61  
         public Date getDate() {
 62  4
                 return syncExec(new Result<Date>() {
 63  
                         public Date run() {
 64  4
                                 int year = widget.getYear();
 65  4
                                 int month = widget.getMonth();
 66  4
                                 int day = widget.getDay();
 67  
 
 68  4
                                 int hours = widget.getHours();
 69  4
                                 int minutes = widget.getMinutes();
 70  4
                                 int seconds = widget.getSeconds();
 71  4
                                 Calendar calendar = Calendar.getInstance();
 72  4
                                 calendar.setTimeInMillis(0);
 73  4
                                 calendar.set(year, month, day, hours, minutes, seconds);
 74  4
                                 return calendar.getTime();
 75  
                         }
 76  
 
 77  
                 });
 78  
         }
 79  
 
 80  
         /**
 81  
          * Sets the date.
 82  
          *
 83  
          * @param toSet the date to set into the control.
 84  
          */
 85  
         public void setDate(final Date toSet) {
 86  4
                 log.debug(MessageFormat.format("Setting date on control: {0} to {1}", this, toSet)); //$NON-NLS-1$
 87  4
                 waitForEnabled();
 88  4
                 syncExec(new VoidResult() {
 89  
                         @SuppressWarnings("deprecation")
 90  
                         public void run() {
 91  4
                                 widget.setYear(toSet.getYear() + 1900);
 92  4
                                 widget.setMonth(toSet.getMonth());
 93  4
                                 widget.setDay(toSet.getDate());
 94  
 
 95  4
                                 widget.setHours(toSet.getHours());
 96  4
                                 widget.setMinutes(toSet.getMinutes());
 97  4
                                 widget.setSeconds(toSet.getSeconds());
 98  4
                         }
 99  
                 });
 100  4
                 notify(SWT.Selection);
 101  4
         }
 102  
 }