Refactoring of selection elements. Additional minor refactoring. Fixed a bug with...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / DateDetailSection.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.element;
12
13 import org.eclipse.jface.util.PropertyChangeEvent;
14 import org.eclipse.swt.widgets.Text;
15 import org.joda.time.Partial;
16
17 import eu.etaxonomy.cdm.common.CdmUtils;
18 import eu.etaxonomy.cdm.model.common.TimePeriod;
19
20 /**
21 * <p>
22 * DateDetailSection class.
23 * </p>
24 *
25 * @author n.hoffmann
26 * @created Mar 31, 2010
27 * @version 1.0
28 */
29 public class DateDetailSection extends AbstractFormSection<TimePeriod> {
30
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;
36
37 /**
38 * <p>
39 * Constructor for DateDetailSection.
40 * </p>
41 *
42 * @param formFactory
43 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
44 * object.
45 * @param parentElement
46 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
47 * object.
48 * @param style
49 * a int.
50 */
51 protected DateDetailSection(CdmFormFactory formFactory,
52 ICdmFormElement parentElement, int style) {
53 super(formFactory, parentElement, style);
54
55 partialElement_start = formFactory.createPartialElement(this,
56 "Start: ", null, style);
57 partialElement_end = formFactory.createPartialElement(this, "End: ",
58 null, style);
59
60 text_parseText = formFactory.createTextWithLabelElement(this, "Parse",
61 null, style);
62 text_parseText.getMainControl().setLayoutData(
63 LayoutConstants.FILL_HORIZONTALLY(6, 1));
64
65 text_freeText = formFactory.createTextWithLabelElement(this,
66 "Freetext", null, style);
67 text_freeText.getMainControl().setLayoutData(
68 LayoutConstants.FILL_HORIZONTALLY(6, 1));
69
70 formFactory.addPropertyChangeListener(this);
71 }
72
73 @Override
74 public TimePeriod getEntity() {
75 if(super.getEntity() == null){
76 setEntity(TimePeriod.NewInstance());
77 }
78 return super.getEntity();
79 }
80
81 /**
82 * <p>
83 * Setter for the field <code>timePeriod</code>.
84 * </p>
85 *
86 * @param timePeriod
87 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
88 */
89 @Override
90 public void setEntity(TimePeriod timePeriod) {
91 setEntityInternally(timePeriod);
92 updateTitle();
93 text_parseText.setText(timePeriod.toString());
94 }
95
96 /**
97 * When setting the entity through parsing we do not want to alter the parse field
98 * @param timePeriod
99 */
100 private void setEntityInternally(TimePeriod timePeriod){
101 Partial start = timePeriod.getStart();
102 partialElement_start.setPartial(start);
103 Partial end = timePeriod.getEnd();
104 partialElement_end.setPartial(end);
105
106 ((Text) text_parseText.getMainControl()).setSelection(cursorPosition);
107 text_freeText.setText(timePeriod.getFreeText());
108
109 super.setEntity(timePeriod);
110 }
111
112 /** {@inheritDoc} */
113 @Override
114 public void propertyChange(PropertyChangeEvent event) {
115 if (event == null) {
116 return;
117 }
118 Object eventSource = event.getSource();
119
120 if (getElements().contains(eventSource)) {
121 if (event instanceof CdmPropertyChangeEvent) {
122 if (((CdmPropertyChangeEvent) event).hasException()) {
123 handleException((CdmPropertyChangeEvent) event);
124 return;
125 }
126 }
127 handleEvent(eventSource);
128 }
129 }
130
131 /**
132 * @param event
133 */
134 private void handleException(CdmPropertyChangeEvent event) {
135 firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
136 event.getException()));
137 }
138
139 private void handleEvent(Object eventSource) {
140 if (eventSource == partialElement_start) {
141 Partial start = partialElement_start.getPartial();
142 getEntity().setStart(start);
143 } else if (eventSource == partialElement_end) {
144 Partial end = partialElement_end.getPartial();
145 getEntity().setEnd(end);
146 } else if (eventSource == text_parseText) {
147 cursorPosition = ((Text) text_parseText.getMainControl())
148 .getCaretPosition();
149 setEntityInternally(TimePeriod.parseString(text_parseText.getText()));
150 } else if (eventSource == text_freeText) {
151 getEntity().setFreeText(text_freeText.getText());
152 }
153 updateTitle();
154 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
155 }
156
157 private void updateTitle(){
158 String title = CdmUtils.Nz(getEntity().toString());
159 this.setText(title);
160 layout();
161 }
162
163 /*
164 * (non-Javadoc)
165 *
166 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
167 */
168 /** {@inheritDoc} */
169 @Override
170 public void dispose() {
171 formFactory.removePropertyChangeListener(this);
172 super.dispose();
173 }
174
175 }