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);
74 public TimePeriod
getEntity() {
75 if(super.getEntity() == null){
76 setEntity(TimePeriod
.NewInstance());
78 return super.getEntity();
83 * Setter for the field <code>timePeriod</code>.
87 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
90 public void setEntity(TimePeriod timePeriod
) {
91 setEntityInternally(timePeriod
);
92 text_parseText
.setText(timePeriod
.toString());
96 * When setting the entity through parsing we do not want to alter the parse field
99 private void setEntityInternally(TimePeriod timePeriod
){
100 Partial start
= timePeriod
.getStart();
101 partialElement_start
.setPartial(start
);
102 Partial end
= timePeriod
.getEnd();
103 partialElement_end
.setPartial(end
);
105 ((Text
) text_parseText
.getMainControl()).setSelection(cursorPosition
);
106 text_freeText
.setText(timePeriod
.getFreeText());
108 super.setEntity(timePeriod
);
113 public void propertyChange(PropertyChangeEvent event
) {
117 Object eventSource
= event
.getSource();
119 if (getElements().contains(eventSource
)) {
120 if (event
instanceof CdmPropertyChangeEvent
) {
121 if (((CdmPropertyChangeEvent
) event
).hasException()) {
122 handleException((CdmPropertyChangeEvent
) event
);
126 handleEvent(eventSource
);
133 private void handleException(CdmPropertyChangeEvent event
) {
134 firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
135 event
.getException()));
138 private void handleEvent(Object eventSource
) {
139 if (eventSource
== partialElement_start
) {
140 Partial start
= partialElement_start
.getPartial();
141 getEntity().setStart(start
);
142 } else if (eventSource
== partialElement_end
) {
143 Partial end
= partialElement_end
.getPartial();
144 getEntity().setEnd(end
);
145 } else if (eventSource
== text_parseText
) {
146 cursorPosition
= ((Text
) text_parseText
.getMainControl())
148 setEntityInternally(TimePeriod
.parseString(text_parseText
.getText()));
149 } else if (eventSource
== text_freeText
) {
150 getEntity().setFreeText(text_freeText
.getText());
153 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
156 private void updateTitle(){
157 String title
= CdmUtils
.Nz(getEntity().toString());
165 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
169 public void dispose() {
170 formFactory
.removePropertyChangeListener(this);