1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.ui.element;
|
11
|
|
12
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
13
|
import org.eclipse.swt.SWT;
|
14
|
import org.eclipse.swt.graphics.Color;
|
15
|
import org.eclipse.swt.widgets.Display;
|
16
|
import org.eclipse.swt.widgets.Label;
|
17
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
18
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
19
|
import org.joda.time.IllegalFieldValueException;
|
20
|
import org.joda.time.Partial;
|
21
|
|
22
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
23
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
24
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
25
|
|
26
|
/**
|
27
|
* <p>PartialElement class.</p>
|
28
|
*
|
29
|
* @author n.hoffmann
|
30
|
* @created Mar 31, 2010
|
31
|
*/
|
32
|
public class PartialElement extends AbstractCdmFormElement implements ISelectable {
|
33
|
|
34
|
private Label label;
|
35
|
private FloatWithLabelElement number_day;
|
36
|
private FloatWithLabelElement number_month;
|
37
|
private FloatWithLabelElement number_year = null;
|
38
|
private Partial partial;
|
39
|
|
40
|
public PartialElement(CdmFormFactory formFactory,
|
41
|
ICdmFormElement formElement, String labelString, int style){
|
42
|
this(formFactory, formElement, labelString, style, true);
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* <p>Constructor for PartialElement.</p>
|
47
|
*
|
48
|
* @param formFactory a {@link eu.eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
|
49
|
* @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.forms.ICdmFormElement} object.
|
50
|
* @param labelString a {@link java.lang.String} object.
|
51
|
* @param style a int.
|
52
|
*/
|
53
|
public PartialElement(CdmFormFactory formFactory,
|
54
|
ICdmFormElement formElement, String labelString, int style, boolean showYear) {
|
55
|
super(formFactory, formElement);
|
56
|
|
57
|
int rows = 5;
|
58
|
if (showYear){
|
59
|
rows = 7;
|
60
|
}
|
61
|
TableWrapLayout layout = LayoutConstants.LAYOUT(rows, false);
|
62
|
// layout.verticalSpacing = 7;
|
63
|
layout.horizontalSpacing = 7;
|
64
|
|
65
|
formElement.getLayoutComposite().setLayout(layout);
|
66
|
|
67
|
TableWrapData layoutLable = new TableWrapData();
|
68
|
layoutLable.valign = TableWrapData.MIDDLE;
|
69
|
|
70
|
label = formFactory.createLabel(getLayoutComposite(), labelString);
|
71
|
label.setLayoutData(layoutLable);
|
72
|
addControl(label);
|
73
|
|
74
|
if (showYear){
|
75
|
number_year = formFactory.createFloatTextWithLabelElement(this, "Year", null, style);
|
76
|
number_year.setLimits(4, 0, 9999);
|
77
|
}
|
78
|
|
79
|
|
80
|
number_month = formFactory.createFloatTextWithLabelElement(this, "Month", null, style);
|
81
|
number_month.setLimits(2, 1, 12);
|
82
|
|
83
|
number_day = formFactory.createFloatTextWithLabelElement(this, "Day", null, style);
|
84
|
number_day.setLimits(2, 1, 31);
|
85
|
|
86
|
|
87
|
formFactory.addPropertyChangeListener(this);
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* <p>Setter for the field <code>partial</code>.</p>
|
92
|
*
|
93
|
* @param partial a {@link org.joda.time.Partial} object.
|
94
|
*/
|
95
|
public void setPartial(Partial partial){
|
96
|
this.partial = partial;
|
97
|
|
98
|
if(partial != null){
|
99
|
number_day.setNumber(TimePeriod.getPartialValue(partial, TimePeriod.DAY_TYPE));
|
100
|
number_month.setNumber(TimePeriod.getPartialValue(partial, TimePeriod.MONTH_TYPE));
|
101
|
if (number_year != null) {
|
102
|
number_year.setNumber(TimePeriod.getPartialValue(partial, TimePeriod.YEAR_TYPE));
|
103
|
}
|
104
|
}
|
105
|
else{
|
106
|
number_day.setNumber(null);
|
107
|
number_month.setNumber(null);
|
108
|
if (number_year != null) {
|
109
|
number_year.setNumber(null);
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* <p>Getter for the field <code>partial</code>.</p>
|
116
|
*
|
117
|
* @return a {@link org.joda.time.Partial} object.
|
118
|
*/
|
119
|
public Partial getPartial(){
|
120
|
return partial;
|
121
|
}
|
122
|
|
123
|
/** {@inheritDoc} */
|
124
|
@Override
|
125
|
public void propertyChange(PropertyChangeEvent event) {
|
126
|
if(event == null){
|
127
|
return;
|
128
|
}
|
129
|
|
130
|
Object eventSource = event.getSource();
|
131
|
|
132
|
if(getElements().contains(eventSource)){
|
133
|
if(event instanceof CdmPropertyChangeEvent){
|
134
|
if(((CdmPropertyChangeEvent) event).hasException()){
|
135
|
handleException((CdmPropertyChangeEvent) event);
|
136
|
return;
|
137
|
}
|
138
|
}
|
139
|
handleEvent(eventSource);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* @param event
|
145
|
*/
|
146
|
private void handleException(CdmPropertyChangeEvent event) {
|
147
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event.getException()));
|
148
|
}
|
149
|
|
150
|
private void handleEvent(Object eventSource){
|
151
|
if (eventSource instanceof NumberWithLabelElement){
|
152
|
try{
|
153
|
if(eventSource == number_month && number_month.getException()==null){
|
154
|
partial = TimePeriod.setPartialField(partial, number_month.getInteger(), TimePeriod.MONTH_TYPE);
|
155
|
}else if(eventSource == number_day && number_day.getException()==null){
|
156
|
partial = TimePeriod.setPartialField(partial, number_day.getInteger(), TimePeriod.DAY_TYPE);
|
157
|
}else if(eventSource == number_year && number_year.getException()==null){
|
158
|
partial = TimePeriod.setPartialField(partial, number_year.getInteger(), TimePeriod.YEAR_TYPE);
|
159
|
}
|
160
|
} catch (IndexOutOfBoundsException | IllegalFieldValueException e) {
|
161
|
((NumberWithLabelElement)eventSource).text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
|
162
|
}
|
163
|
}
|
164
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
|
165
|
}
|
166
|
|
167
|
/** {@inheritDoc} */
|
168
|
@Override
|
169
|
public void setBackground(Color color) {
|
170
|
label.setBackground(color);
|
171
|
}
|
172
|
|
173
|
@Override
|
174
|
public void setSelected(boolean selected) {
|
175
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
176
|
}
|
177
|
|
178
|
public void setEnabled(boolean enabled){
|
179
|
this.number_day.setEnabled(enabled);
|
180
|
this.number_month.setEnabled(enabled);
|
181
|
if (number_year != null){
|
182
|
this.number_year.setEnabled(enabled);
|
183
|
}
|
184
|
if (!enabled){
|
185
|
if (number_year != null){
|
186
|
this.number_year.getMainControl().setBackground(AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
|
187
|
}
|
188
|
this.number_month.getMainControl().setBackground(AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
|
189
|
this.number_day.getMainControl().setBackground(AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
|
190
|
}else{
|
191
|
if (number_year != null){
|
192
|
this.number_year.getMainControl().setBackground(getPersistentBackground());
|
193
|
}
|
194
|
this.number_month.getMainControl().setBackground(getPersistentBackground());
|
195
|
this.number_day.getMainControl().setBackground(getPersistentBackground());
|
196
|
}
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
*
|
201
|
*/
|
202
|
public void setAllNull() {
|
203
|
this.number_day.setNumber(null);
|
204
|
this.number_month.setNumber(null);
|
205
|
if (number_year != null){
|
206
|
this.number_year.setNumber(null);
|
207
|
}
|
208
|
|
209
|
}
|
210
|
}
|