3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.ui
.forms
;
13 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
14 import org
.eclipse
.swt
.widgets
.Text
;
15 import org
.joda
.time
.Partial
;
17 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
18 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
22 * DateDetailSection class.
26 * @created Mar 31, 2010
29 public class DateDetailSection
extends AbstractFormSection
<TimePeriod
> {
31 private final TextWithLabelElement text_freeText
;
32 private final PartialElement partialElement_start
;
33 private final PartialElement partialElement_end
;
34 private final TextWithLabelElement text_parseText
;
35 private int cursorPosition
;
39 * Constructor for DateDetailSection.
43 * a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory}
45 * @param parentElement
46 * a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
51 protected DateDetailSection(CdmFormFactory formFactory
,
52 ICdmFormElement parentElement
, int style
) {
53 super(formFactory
, null, parentElement
, style
);
55 partialElement_start
= formFactory
.createPartialElement(this,
56 "Start: ", null, style
);
57 partialElement_end
= formFactory
.createPartialElement(this, "End: ",
60 text_parseText
= formFactory
.createTextWithLabelElement(this, "Parse",
62 text_parseText
.getMainControl().setLayoutData(
63 CdmFormFactory
.FILL_HORIZONTALLY(6, 1));
65 text_freeText
= formFactory
.createTextWithLabelElement(this,
66 "Freetext", null, style
);
67 text_freeText
.getMainControl().setLayoutData(
68 CdmFormFactory
.FILL_HORIZONTALLY(6, 1));
70 formFactory
.addPropertyChangeListener(this);
75 * Setter for the field <code>timePeriod</code>.
79 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
82 public void setEntity(TimePeriod timePeriod
) {
83 setEntityInternally(timePeriod
);
84 text_parseText
.setText(timePeriod
.toString());
85 super.setEntity(timePeriod
);
89 * When setting the entity through parsing we do not want to alter the parse field
92 private void setEntityInternally(TimePeriod timePeriod
){
93 Partial start
= timePeriod
.getStart();
94 partialElement_start
.setPartial(start
);
95 Partial end
= timePeriod
.getEnd();
96 partialElement_end
.setPartial(end
);
98 ((Text
) text_parseText
.getMainControl()).setSelection(cursorPosition
);
99 text_freeText
.setText(timePeriod
.getFreeText());
101 this.setText(CdmUtils
.Nz(timePeriod
.toString()));
106 public void propertyChange(PropertyChangeEvent event
) {
110 Object eventSource
= event
.getSource();
112 if (getElements().contains(eventSource
)) {
113 if (event
instanceof CdmPropertyChangeEvent
) {
114 if (((CdmPropertyChangeEvent
) event
).hasException()) {
115 handleException((CdmPropertyChangeEvent
) event
);
119 handleEvent(eventSource
);
126 private void handleException(CdmPropertyChangeEvent event
) {
127 firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
128 event
.getException()));
131 private void handleEvent(Object eventSource
) {
132 if (eventSource
== partialElement_start
) {
133 Partial start
= partialElement_start
.getPartial();
134 getEntity().setStart(start
);
135 } else if (eventSource
== partialElement_end
) {
136 Partial end
= partialElement_end
.getPartial();
137 getEntity().setEnd(end
);
138 } else if (eventSource
== text_parseText
) {
139 cursorPosition
= ((Text
) text_parseText
.getMainControl())
141 setEntityInternally(TimePeriod
.parseString(text_parseText
.getText()));
142 } else if (eventSource
== text_freeText
) {
143 getEntity().setFreeText(text_freeText
.getText());
145 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
151 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
155 public void dispose() {
156 formFactory
.removePropertyChangeListener(this);