1 package eu
.etaxonomy
.taxeditor
.ui
.element
;
3 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
4 import org
.eclipse
.swt
.widgets
.Text
;
5 import org
.joda
.time
.Partial
;
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
;
13 public class DateDetailSection
<T
extends TimePeriod
>
14 extends AbstractFormSection
<T
> {
15 protected TextWithLabelElement text_freeText
;
16 protected PartialElement partialElement_start
;
17 protected PartialElement partialElement_end
;
18 protected CheckboxElement period_continue
;
19 protected TextWithLabelElement text_parseText
;
20 protected TextWithLabelElement text_VerbatimDate
= null;
21 protected int cursorPosition
;
22 protected boolean includeVerbatim
;
24 public int getCursorPosition() {
25 return cursorPosition
;
28 public void setCursorPosition(int cursorPosition
) {
29 this.cursorPosition
= cursorPosition
;
34 * Constructor for DateDetailSection.
38 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
40 * @param parentElement
41 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
46 protected DateDetailSection(CdmFormFactory formFactory
,
47 ICdmFormElement parentElement
, boolean includeVerbatim
, int style
) {
48 super(formFactory
, parentElement
, style
);
50 this.includeVerbatim
= includeVerbatim
;
51 text_parseText
= formFactory
.createTextWithLabelElement(this, "Parse",
53 text_parseText
.getMainControl().setLayoutData(
54 LayoutConstants
.FILL_HORIZONTALLY(6, 1));
55 text_parseText
.getMainControl().setToolTipText(Messages
.DateDetail_parseText_tooltip
);
56 partialElement_start
= formFactory
.createPartialElement(this,
57 "Start ", null, style
);
59 period_continue
= formFactory
.createCheckbox(this, "Continue", false, style
);
60 period_continue
.getMainControl().setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(6,1));
61 partialElement_end
= formFactory
.createPartialElement(this, "End ",
65 text_VerbatimDate
= formFactory
.createTextWithLabelElement(this,
66 "Verbatim Date", null, style
);
68 text_VerbatimDate
.getMainControl().setLayoutData(
69 LayoutConstants
.FILL_HORIZONTALLY(6, 1));
74 text_freeText
= formFactory
.createTextWithLabelElement(this,
75 "Freetext", null, style
);
76 text_freeText
.getMainControl().setLayoutData(
77 LayoutConstants
.FILL_HORIZONTALLY(6, 1));
80 formFactory
.addPropertyChangeListener(this);
84 public final T
getEntity() {
85 if(super.getEntity() == null){
86 T newInstance
= newInstance();
87 this.setEntity(newInstance
);
89 return super.getEntity();
92 @SuppressWarnings("unchecked")
93 protected T
newInstance(){
95 return (T
)VerbatimTimePeriod
.NewVerbatimInstance();
97 return (T
)TimePeriod
.NewInstance();
101 @SuppressWarnings("unchecked")
102 protected T
parseNewInstance() {
103 if (includeVerbatim
){
104 T result
= (T
)TimePeriodParser
.parseStringVerbatim(getText_parseText().getText());
107 T result
= (T
)TimePeriodParser
.parseString(getText_parseText().getText());
115 * Setter for the field <code>timePeriod</code>.
119 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
122 public void setEntity(T timePeriod
) {
123 setEntityInternally(timePeriod
);
125 getText_parseText().setText(timePeriod
.toString());
129 * When setting the entity through parsing we do not want to alter the parse field
132 protected void setEntityInternally(T timePeriod
){
133 Partial start
= timePeriod
.getStart();
134 partialElement_start
.setPartial(start
);
135 Partial end
= timePeriod
.getEnd();
136 partialElement_end
.setPartial(end
);
137 period_continue
.setSelection(timePeriod
.isContinued());
138 if (timePeriod
.isContinued()){
139 partialElement_end
.setEnabled(false);
142 ((Text
) getText_parseText().getMainControl()).setSelection(cursorPosition
);
143 text_freeText
.setText(timePeriod
.getFreeText());
144 if (includeVerbatim
){
145 ((Text
) text_VerbatimDate
.getMainControl()).setSelection(cursorPosition
);
146 text_VerbatimDate
.setText(((VerbatimTimePeriod
)timePeriod
).getVerbatimDate());
149 super.setEntity(timePeriod
);
154 public void propertyChange(PropertyChangeEvent event
) {
158 Object eventSource
= event
.getSource();
160 if (getElements().contains(eventSource
)) {
161 if (event
instanceof CdmPropertyChangeEvent
) {
162 if (((CdmPropertyChangeEvent
) event
).hasException()) {
163 handleException((CdmPropertyChangeEvent
) event
);
167 handleEvent(eventSource
);
174 private void handleException(CdmPropertyChangeEvent event
) {
175 firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
176 event
.getException()));
179 protected void handleEvent(Object eventSource
) {
180 if (eventSource
== partialElement_start
) {
181 Partial start
= partialElement_start
.getPartial();
182 getEntity().setStart(start
);
183 } else if (eventSource
== partialElement_end
) {
184 Partial end
= partialElement_end
.getPartial();
185 getEntity().setEnd(end
);
187 } else if (eventSource
== getText_parseText()) {
188 cursorPosition
= ((Text
) getText_parseText().getMainControl())
190 T newInstance
= parseNewInstance();
191 setEntityInternally(newInstance
);
192 } else if (eventSource
== text_freeText
) {
193 getEntity().setFreeText(text_freeText
.getText());
194 }else if (eventSource
== period_continue
) {
195 boolean continued
= period_continue
.getSelection();
196 getEntity().setContinued(continued
);
198 partialElement_end
.setAllNull();
199 partialElement_end
.setEnabled(false);
201 partialElement_end
.setEnabled(true);
203 }else if (eventSource
== text_VerbatimDate
) {
205 ((VerbatimTimePeriod
)getEntity()).setVerbatimDate(text_VerbatimDate
.getText());
208 if (!(eventSource
== getText_parseText())){
212 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
215 // protected abstract T parseNewInstance() ;
217 protected void updateTitle(){
218 String title
= CdmUtils
.Nz(getEntity().toString());
222 protected void updateParseField(){
223 getText_parseText().setText(getEntity().toString());
229 public void dispose() {
230 formFactory
.removePropertyChangeListener(this);
234 public TextWithLabelElement
getText_parseText() {
235 return text_parseText
;
238 public void setText_parseText(TextWithLabelElement text_parseText
) {
239 this.text_parseText
= text_parseText
;