Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SWTBotDateTime |
|
| 1.0;1 | ||||
SWTBotDateTime$1 |
|
| 1.0;1 | ||||
SWTBotDateTime$2 |
|
| 1.0;1 |
1 | 14 | /******************************************************************************* |
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 | 6 | super(w, description); |
54 | 6 | } |
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 | 7 | return syncExec(new Result<Date>() { |
63 | public Date run() { | |
64 | 7 | int year = widget.getYear(); |
65 | 7 | int month = widget.getMonth(); |
66 | 7 | int day = widget.getDay(); |
67 | ||
68 | 7 | int hours = widget.getHours(); |
69 | 7 | int minutes = widget.getMinutes(); |
70 | 7 | int seconds = widget.getSeconds(); |
71 | 7 | Calendar calendar = Calendar.getInstance(); |
72 | 7 | calendar.setTimeInMillis(0); |
73 | 7 | calendar.set(year, month, day, hours, minutes, seconds); |
74 | 7 | 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 | 6 | log.debug(MessageFormat.format("Setting date on control: {0} to {1}", this, toSet)); //$NON-NLS-1$ |
87 | 6 | waitForEnabled(); |
88 | 6 | syncExec(new VoidResult() { |
89 | @SuppressWarnings("deprecation") | |
90 | public void run() { | |
91 | 6 | widget.setDate(toSet.getYear() + 1900, toSet.getMonth(), toSet.getDate()); |
92 | 6 | widget.setHours(toSet.getHours()); |
93 | 6 | widget.setMinutes(toSet.getMinutes()); |
94 | 6 | widget.setSeconds(toSet.getSeconds()); |
95 | 6 | } |
96 | }); | |
97 | 6 | notify(SWT.Selection); |
98 | 6 | } |
99 | } |