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