merged/implemented cdm3.3 model adaptations
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / MinMaxTextSection.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
15 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
16 import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
17
18 /**
19 * <p>
20 * MinMaxTextSection class.
21 * </p>
22 *
23 * @author c.mathew
24 * @created 23 Jul 2013
25 * @version 1.0
26 */
27 public class MinMaxTextSection extends AbstractFormSection<DerivedUnitFacade> {
28
29 private final TextWithLabelElement text_freeText;
30 private final NumberWithLabelElement text_minVal;
31 private final NumberWithLabelElement text_maxVal;
32 private int cursorPosition;
33
34 // unit types handled by this section
35 public enum UnitType {
36 ELEVATION,
37 DIST_TO_GROUND,
38 DIST_TO_WATER
39 }
40
41 private UnitType unitType = UnitType.ELEVATION;
42
43 /**
44 * <p>
45 * Constructor for DateDetailSection.
46 * </p>
47 *
48 * @param formFactory
49 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
50 * object.
51 * @param parentElement
52 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
53 * object.
54 * @param style
55 * a int.
56 */
57 protected MinMaxTextSection(CdmFormFactory formFactory,
58 ICdmFormElement parentElement, UnitType unitType, int style) {
59 super(formFactory, parentElement, style);
60 this.unitType = unitType;
61 getLayoutComposite().setLayout(LayoutConstants.LAYOUT(2, false));
62
63 text_minVal = formFactory.createNumberTextWithLabelElement(this,
64 "Min : ", getMinimum(), style);
65
66 text_maxVal = formFactory.createNumberTextWithLabelElement(this,
67 "Max : ", getMaximum(), style);
68
69 text_freeText = formFactory.createTextWithLabelElement(this,
70 "Freetext : ", getFreetext(), style);
71
72 formFactory.addPropertyChangeListener(this);
73 }
74
75 /**
76 * <p>
77 * Setter for the field <code>timePeriod</code>.
78 * </p>
79 *
80 * @param timePeriod
81 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
82 */
83 @Override
84 public void setEntity(DerivedUnitFacade gatheringEvent) {
85 super.setEntity(gatheringEvent);
86 updateValues();
87 }
88
89 private void updateValues() {
90 if(getEntity() != null) {
91 switch(unitType) {
92 case ELEVATION:
93 if(getEntity().getAbsoluteElevation() != null) {
94 text_minVal.setNumber(getEntity().getAbsoluteElevation());
95 }
96 if(getEntity().getAbsoluteElevationMaximum() != null) {
97 text_maxVal.setNumber(getEntity().getAbsoluteElevation());
98 }
99 if(getEntity().getAbsoluteElevationText() != null) {
100 text_freeText.setText(getEntity().getAbsoluteElevationText());
101 }
102 break;
103 case DIST_TO_GROUND:
104 if(getEntity().getDistanceToGround() != null) {
105 text_minVal.setNumber(getEntity().getDistanceToGround());
106 }
107 if(getEntity().getDistanceToGroundMax() != null) {
108 text_maxVal.setNumber(getEntity().getDistanceToGroundMax());
109 }
110 if(getEntity().getDistanceToGroundText() != null) {
111 text_freeText.setText(getEntity().getDistanceToGroundText());
112 }
113 break;
114 case DIST_TO_WATER:
115 if(getEntity().getDistanceToWaterSurface() != null) {
116 text_minVal.setNumber(getEntity().getDistanceToWaterSurface());
117 }
118 if(getEntity().getDistanceToWaterSurfaceMax() != null) {
119 text_maxVal.setNumber(getEntity().getDistanceToWaterSurfaceMax());
120 }
121 if(getEntity().getDistanceToWaterSurfaceText() != null) {
122 text_freeText.setText(getEntity().getDistanceToWaterSurfaceText());
123 }
124 break;
125 default:
126 break;
127 }
128
129 }
130 }
131 /** {@inheritDoc} */
132 @Override
133 public void propertyChange(PropertyChangeEvent event) {
134 if (event == null) {
135 return;
136 }
137 Object eventSource = event.getSource();
138
139 if (getElements().contains(eventSource)) {
140 if (event instanceof CdmPropertyChangeEvent) {
141 if (((CdmPropertyChangeEvent) event).hasException()) {
142 handleException((CdmPropertyChangeEvent) event);
143 return;
144 }
145 }
146 handleEvent(eventSource);
147 }
148 }
149
150 /**
151 * @param event
152 */
153 private void handleException(CdmPropertyChangeEvent event) {
154 firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
155 event.getException()));
156 }
157
158 private void handleEvent(Object eventSource) {
159 if (eventSource == text_minVal) {
160 updateMinimum();
161 } else if (eventSource == text_maxVal) {
162 updateMaximum();
163 } else if (eventSource == text_freeText) {
164 updateFreetext();
165 }
166 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
167 }
168
169
170
171 /*
172 * (non-Javadoc)
173 *
174 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
175 */
176 /** {@inheritDoc} */
177 @Override
178 public void dispose() {
179 formFactory.removePropertyChangeListener(this);
180 super.dispose();
181 }
182
183 private void updateMinimum() {
184 switch(unitType) {
185 case ELEVATION:
186 getEntity().setAbsoluteElevation(text_minVal.getInteger());
187 break;
188 case DIST_TO_GROUND:
189 getEntity().setDistanceToGround(text_minVal.getDouble());
190 break;
191 case DIST_TO_WATER:
192 getEntity().setDistanceToWaterSurface(text_minVal.getDouble());
193 break;
194 }
195 }
196
197 private Number getMinimum() {
198 if(getEntity() == null) {
199 return null;
200 }
201 switch(unitType) {
202 case ELEVATION:
203 return getEntity().getAbsoluteElevation();
204 case DIST_TO_GROUND:
205 return getEntity().getDistanceToGround();
206 case DIST_TO_WATER:
207 return getEntity().getDistanceToWaterSurface();
208 }
209 return null;
210 }
211
212 private void updateMaximum() {
213 switch(unitType) {
214 case ELEVATION:
215 getEntity().setAbsoluteElevationMax(text_maxVal.getInteger());
216 break;
217 case DIST_TO_GROUND:
218 getEntity().setDistanceToGroundMax(text_maxVal.getDouble());
219 break;
220 case DIST_TO_WATER:
221 getEntity().setDistanceToWaterSurfaceMax(text_maxVal.getDouble());
222 break;
223 }
224 }
225
226 private Number getMaximum() {
227 if(getEntity() == null) {
228 return null;
229 }
230 switch(unitType) {
231 case ELEVATION:
232 return getEntity().getAbsoluteElevationMaximum();
233 case DIST_TO_GROUND:
234 return getEntity().getDistanceToGroundMax();
235 case DIST_TO_WATER:
236 return getEntity().getDistanceToWaterSurfaceMax();
237 }
238 return null;
239 }
240
241 private void updateFreetext() {
242 switch(unitType) {
243 case ELEVATION:
244 getEntity().setAbsoluteElevationText(text_freeText.getText());
245 break;
246 case DIST_TO_GROUND:
247 getEntity().setDistanceToGroundText(text_freeText.getText());
248 break;
249 case DIST_TO_WATER:
250 getEntity().setDistanceToWaterSurfaceText(text_freeText.getText());
251 break;
252 }
253 }
254
255 private String getFreetext() {
256 if(getEntity() == null) {
257 return null;
258 }
259 switch(unitType) {
260 case ELEVATION:
261 return getEntity().getAbsoluteElevationText();
262 case DIST_TO_GROUND:
263 return getEntity().getDistanceToGroundText();
264 case DIST_TO_WATER:
265 return getEntity().getDistanceToWaterSurfaceText();
266 }
267 return null;
268 }
269
270 }
271