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