2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.ui
.mvc
.element
;
12 import java
.awt
.FontMetrics
;
13 import java
.util
.Calendar
;
14 import java
.util
.ConcurrentModificationException
;
15 import java
.util
.List
;
17 import org
.eclipse
.core
.runtime
.Assert
;
18 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
19 import org
.eclipse
.jface
.viewers
.CellEditor
.LayoutData
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.DisposeEvent
;
22 import org
.eclipse
.swt
.events
.DisposeListener
;
23 import org
.eclipse
.swt
.events
.SelectionAdapter
;
24 import org
.eclipse
.swt
.events
.SelectionEvent
;
25 import org
.eclipse
.swt
.graphics
.GC
;
26 import org
.eclipse
.swt
.graphics
.Image
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Button
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.DateTime
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swt
.widgets
.Label
;
34 import org
.eclipse
.swt
.widgets
.Shell
;
35 import org
.eclipse
.swt
.widgets
.Text
;
36 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
37 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
38 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
39 //import org.joda.time.DateTime;
41 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
42 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
44 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
45 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
46 import eu
.etaxonomy
.taxeditor
.ui
.element
.IPropertyChangeEmitter
;
47 import eu
.etaxonomy
.taxeditor
.ui
.mvc
.AbstractCdmComposite
;
54 public class DateElement
extends AbstractCdmComposite
<DateElementController
> {
56 private final FormToolkit toolkit
= new FormToolkit(Display
.getCurrent());
57 private org
.joda
.time
.DateTime initialDateTime
;
58 private Text textDate
;
59 private final Button openDateDialog
;
61 private static final String pattern
= "yyyy-MM-dd HH:mm:ss";
65 * Create the composite.
69 public DateElement(Composite parent
, org
.joda
.time
.DateTime initialDateTime
, int style
, boolean editableText
) {
71 this.initialDateTime
= initialDateTime
;
72 addDisposeListener(new DisposeListener() {
74 public void widgetDisposed(DisposeEvent e
) {
79 toolkit
.paintBordersFor(this);
81 TableWrapLayout tableWrapLayout
= new TableWrapLayout();
82 tableWrapLayout
.verticalSpacing
= 0;
83 tableWrapLayout
.topMargin
= 0;
84 tableWrapLayout
.rightMargin
= 0;
85 tableWrapLayout
.leftMargin
= 0;
86 tableWrapLayout
.horizontalSpacing
= 0;
87 tableWrapLayout
.bottomMargin
= 0;
88 setLayout(tableWrapLayout
);
91 Composite composite
= new Composite (this, SWT
.NONE
);
92 GridLayout layout
= new GridLayout ();
94 layout
.numColumns
= 2;
96 composite
.setLayout(layout
);
97 composite
.setSize(400, composite
.getSize().y
);
98 int width
= composite
.getSize().x
;
100 // textDate = new Text(composite, SWT.BORDER|SWT.FILL);
101 textDate
= toolkit
.createText(composite
, "yyyy-mm-dd HH:mm:ss", style
);
102 textDate
.setEditable(editableText
);
104 GridData gridData
= new GridData();
105 gridData
.minimumWidth
= 200;
108 textDate
.setLayoutData(gridData
);
109 openDateDialog
= new Button (composite
, SWT
.PUSH
);
110 Image image
= ImageResources
.getImage(ImageResources
.DATE
);
111 // openDateDialog.setText ("Open Date Dialog");
112 openDateDialog
.setImage(image
);
114 openDateDialog
.addSelectionListener (new SelectionAdapter() {
116 public void widgetSelected(SelectionEvent e
){
117 final Shell dialog
= new Shell (parent
.getShell(), SWT
.DIALOG_TRIM
);
119 dialog
.setLayout (new GridLayout (3, false));
121 final DateTime calendar
= new DateTime (dialog
, SWT
.CALENDAR
| SWT
.BORDER
);
122 final DateTime time
= new DateTime (dialog
, SWT
.TIME
| SWT
.SHORT
);
124 new Label (dialog
, SWT
.NONE
);
125 new Label (dialog
, SWT
.NONE
);
126 Button ok
= new Button (dialog
, SWT
.PUSH
);
128 ok
.setLayoutData(new GridData (SWT
.FILL
, SWT
.CENTER
, false, false));
129 ok
.addSelectionListener (new SelectionAdapter() {
131 public void widgetSelected(SelectionEvent e
){
132 setData(makeJodaFromSWT(calendar
, time
));
136 dialog
.setDefaultButton (ok
);
142 toolkit
.adapt(textDate
, true, true);
146 * @see eu.etaxonomy.taxeditor.ui.mvc.AbstractCdmComposite#initInternalController(eu.etaxonomy.taxeditor.ui.element.CdmFormFactory, eu.etaxonomy.taxeditor.ui.element.ICdmFormElement)
149 protected void initInternalController(CdmFormFactory formFactory
, ICdmFormElement parentElement
) {
150 controller
= new DateElementController(this, formFactory
, parentElement
, initialDateTime
);
151 formFactory
.adapt(controller
);
153 public Text
getTextDate() {
160 public static org
.joda
.time
.DateTime
makeJodaFromSWT(
161 org
.eclipse
.swt
.widgets
.DateTime widget
, org
.eclipse
.swt
.widgets
.DateTime dateTime
) {
162 return new org
.joda
.time
.DateTime(widget
.getYear(),
166 dateTime
.getMinutes(),
167 dateTime
.getSeconds());
170 public static void updateSWTwithJoda(
171 org
.eclipse
.swt
.widgets
.DateTime widget
,
172 org
.joda
.time
.DateTime dateTime
) {
173 widget
.setYear(dateTime
.getYear());
174 widget
.setMonth(dateTime
.getMonthOfYear());
175 widget
.setDay(dateTime
.getDayOfMonth());
176 widget
.setHours(dateTime
.getHourOfDay());
177 widget
.setMinutes(dateTime
.getMinuteOfHour());
178 widget
.setSeconds(dateTime
.getSecondOfMinute());
181 public void setData (org
.joda
.time
.DateTime data
) {
182 this.initialDateTime
= data
;
183 this.controller
.setDateTime(data
);
184 this.textDate
.setText(initialDateTime
.toString(pattern
));