1
|
package eu.etaxonomy.taxeditor.ui.element;
|
2
|
|
3
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
4
|
import org.eclipse.swt.widgets.Text;
|
5
|
import org.joda.time.Partial;
|
6
|
|
7
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
8
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
9
|
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
|
10
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
11
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
12
|
|
13
|
public class DateDetailSection<T extends TimePeriod>
|
14
|
extends AbstractFormSection<T> {
|
15
|
protected TextWithLabelElement text_freeText;
|
16
|
private PartialElement partialElement_start;
|
17
|
private PartialElement partialElement_end;
|
18
|
private TextWithLabelElement text_parseText;
|
19
|
private TextWithLabelElement text_VerbatimDate = null;
|
20
|
private int cursorPosition;
|
21
|
private boolean includeVerbatim;
|
22
|
|
23
|
public int getCursorPosition() {
|
24
|
return cursorPosition;
|
25
|
}
|
26
|
|
27
|
public void setCursorPosition(int cursorPosition) {
|
28
|
this.cursorPosition = cursorPosition;
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* <p>
|
33
|
* Constructor for DateDetailSection.
|
34
|
* </p>
|
35
|
*
|
36
|
* @param formFactory
|
37
|
* a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
|
38
|
* object.
|
39
|
* @param parentElement
|
40
|
* a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
|
41
|
* object.
|
42
|
* @param style
|
43
|
* a int.
|
44
|
*/
|
45
|
protected DateDetailSection(CdmFormFactory formFactory,
|
46
|
ICdmFormElement parentElement, boolean includeVerbatim, int style) {
|
47
|
super(formFactory, parentElement, style);
|
48
|
|
49
|
this.includeVerbatim = includeVerbatim;
|
50
|
text_parseText = formFactory.createTextWithLabelElement(this, "Parse",
|
51
|
null, style);
|
52
|
text_parseText.getMainControl().setLayoutData(
|
53
|
LayoutConstants.FILL_HORIZONTALLY(6, 1));
|
54
|
text_parseText.getMainControl().setToolTipText(Messages.DateDetail_parseText_tooltip);
|
55
|
partialElement_start = formFactory.createPartialElement(this,
|
56
|
"Start ", null, style);
|
57
|
partialElement_end = formFactory.createPartialElement(this, "End ",
|
58
|
null, style);
|
59
|
if (includeVerbatim){
|
60
|
text_VerbatimDate = formFactory.createTextWithLabelElement(this,
|
61
|
"Verbatim Date", null, style);
|
62
|
|
63
|
text_VerbatimDate.getMainControl().setLayoutData(
|
64
|
LayoutConstants.FILL_HORIZONTALLY(6, 1));
|
65
|
}
|
66
|
|
67
|
|
68
|
|
69
|
text_freeText = formFactory.createTextWithLabelElement(this,
|
70
|
"Freetext", null, style);
|
71
|
text_freeText.getMainControl().setLayoutData(
|
72
|
LayoutConstants.FILL_HORIZONTALLY(6, 1));
|
73
|
|
74
|
|
75
|
formFactory.addPropertyChangeListener(this);
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public final T getEntity() {
|
80
|
if(super.getEntity() == null){
|
81
|
T newInstance = newInstance();
|
82
|
this.setEntity(newInstance);
|
83
|
}
|
84
|
return super.getEntity();
|
85
|
}
|
86
|
|
87
|
@SuppressWarnings("unchecked")
|
88
|
protected T newInstance(){
|
89
|
if (includeVerbatim){
|
90
|
return (T)VerbatimTimePeriod.NewVerbatimInstance();
|
91
|
}else{
|
92
|
return (T)TimePeriod.NewInstance();
|
93
|
}
|
94
|
}
|
95
|
|
96
|
@SuppressWarnings("unchecked")
|
97
|
protected T parseNewInstance() {
|
98
|
if (includeVerbatim){
|
99
|
T result = (T)TimePeriodParser.parseStringVerbatim(getText_parseText().getText());
|
100
|
return result;
|
101
|
}else{
|
102
|
T result = (T)TimePeriodParser.parseString(getText_parseText().getText());
|
103
|
return result;
|
104
|
}
|
105
|
}
|
106
|
|
107
|
|
108
|
/**
|
109
|
* <p>
|
110
|
* Setter for the field <code>timePeriod</code>.
|
111
|
* </p>
|
112
|
*
|
113
|
* @param timePeriod
|
114
|
* a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
|
115
|
*/
|
116
|
@Override
|
117
|
public void setEntity(T timePeriod) {
|
118
|
setEntityInternally(timePeriod);
|
119
|
updateTitle();
|
120
|
getText_parseText().setText(timePeriod.toString());
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* When setting the entity through parsing we do not want to alter the parse field
|
125
|
* @param timePeriod
|
126
|
*/
|
127
|
protected void setEntityInternally(T timePeriod){
|
128
|
Partial start = timePeriod.getStart();
|
129
|
partialElement_start.setPartial(start);
|
130
|
Partial end = timePeriod.getEnd();
|
131
|
partialElement_end.setPartial(end);
|
132
|
|
133
|
((Text) getText_parseText().getMainControl()).setSelection(cursorPosition);
|
134
|
text_freeText.setText(timePeriod.getFreeText());
|
135
|
if (includeVerbatim){
|
136
|
((Text) text_VerbatimDate.getMainControl()).setSelection(cursorPosition);
|
137
|
text_VerbatimDate.setText(((VerbatimTimePeriod)timePeriod).getVerbatimDate());
|
138
|
}
|
139
|
|
140
|
super.setEntity(timePeriod);
|
141
|
}
|
142
|
|
143
|
/** {@inheritDoc} */
|
144
|
@Override
|
145
|
public void propertyChange(PropertyChangeEvent event) {
|
146
|
if (event == null) {
|
147
|
return;
|
148
|
}
|
149
|
Object eventSource = event.getSource();
|
150
|
|
151
|
if (getElements().contains(eventSource)) {
|
152
|
if (event instanceof CdmPropertyChangeEvent) {
|
153
|
if (((CdmPropertyChangeEvent) event).hasException()) {
|
154
|
handleException((CdmPropertyChangeEvent) event);
|
155
|
return;
|
156
|
}
|
157
|
}
|
158
|
handleEvent(eventSource);
|
159
|
}
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* @param event
|
164
|
*/
|
165
|
private void handleException(CdmPropertyChangeEvent event) {
|
166
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
|
167
|
event.getException()));
|
168
|
}
|
169
|
|
170
|
protected void handleEvent(Object eventSource) {
|
171
|
if (eventSource == partialElement_start) {
|
172
|
Partial start = partialElement_start.getPartial();
|
173
|
getEntity().setStart(start);
|
174
|
} else if (eventSource == partialElement_end) {
|
175
|
Partial end = partialElement_end.getPartial();
|
176
|
getEntity().setEnd(end);
|
177
|
} else if (eventSource == getText_parseText()) {
|
178
|
cursorPosition = ((Text) getText_parseText().getMainControl())
|
179
|
.getCaretPosition();
|
180
|
T newInstance = parseNewInstance();
|
181
|
setEntityInternally(newInstance);
|
182
|
} else if (eventSource == text_freeText) {
|
183
|
getEntity().setFreeText(text_freeText.getText());
|
184
|
}else if (eventSource == text_VerbatimDate) {
|
185
|
VerbatimTimePeriod entity = (VerbatimTimePeriod)getEntity();
|
186
|
entity.setVerbatimDate(text_VerbatimDate.getText());
|
187
|
}
|
188
|
updateTitle();
|
189
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
|
190
|
}
|
191
|
|
192
|
// protected abstract T parseNewInstance() ;
|
193
|
|
194
|
protected void updateTitle(){
|
195
|
String title = CdmUtils.Nz(getEntity().toString());
|
196
|
this.setText(title);
|
197
|
layout();
|
198
|
}
|
199
|
|
200
|
/** {@inheritDoc} */
|
201
|
@Override
|
202
|
public void dispose() {
|
203
|
formFactory.removePropertyChangeListener(this);
|
204
|
super.dispose();
|
205
|
}
|
206
|
|
207
|
public TextWithLabelElement getText_parseText() {
|
208
|
return text_parseText;
|
209
|
}
|
210
|
|
211
|
public void setText_parseText(TextWithLabelElement text_parseText) {
|
212
|
this.text_parseText = text_parseText;
|
213
|
}
|
214
|
}
|