merge-update from trunk
[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 switch(unitType) {
64 case ELEVATION:
65 text_minVal = formFactory.createNumberTextWithLabelElement(this, "Min [m] : ", getMinimum(), style);
66 text_maxVal = formFactory.createNumberTextWithLabelElement(this, "Max [m] : ", getMaximum(), style);
67 break;
68 default:
69 text_minVal = formFactory.createNumberTextWithLabelElement(this, "Min [m] : ", getMinimum(), style);
70 text_maxVal = formFactory.createNumberTextWithLabelElement(this, "Max [m] : ", getMaximum(), style);
71 break;
72 }
73 text_freeText = formFactory.createTextWithLabelElement(this,
74 "Freetext : ", getFreetext(), style);
75
76 formFactory.addPropertyChangeListener(this);
77 }
78
79 /**
80 * <p>
81 * Setter for the field <code>timePeriod</code>.
82 * </p>
83 *
84 * @param timePeriod
85 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
86 */
87 @Override
88 public void setEntity(DerivedUnitFacade gatheringEvent) {
89 super.setEntity(gatheringEvent);
90 updateValues();
91 }
92
93 private void updateValues() {
94 if(getEntity() != null) {
95 switch(unitType) {
96 case ELEVATION:
97 if(getEntity().getAbsoluteElevation() != null) {
98 text_minVal.setNumber(getEntity().getAbsoluteElevation());
99 }
100 if(getEntity().getAbsoluteElevationMaximum() != null) {
101 text_maxVal.setNumber(getEntity().getAbsoluteElevation());
102 }
103 if(getEntity().getAbsoluteElevationText() != null) {
104 text_freeText.setText(getEntity().getAbsoluteElevationText());
105 }
106 break;
107 case DIST_TO_GROUND:
108 if(getEntity().getDistanceToGround() != null) {
109 text_minVal.setNumber(getEntity().getDistanceToGround());
110 }
111 if(getEntity().getDistanceToGroundMax() != null) {
112 text_maxVal.setNumber(getEntity().getDistanceToGroundMax());
113 }
114 if(getEntity().getDistanceToGroundText() != null) {
115 text_freeText.setText(getEntity().getDistanceToGroundText());
116 }
117 break;
118 case DIST_TO_WATER:
119 if(getEntity().getDistanceToWaterSurface() != null) {
120 text_minVal.setNumber(getEntity().getDistanceToWaterSurface());
121 }
122 if(getEntity().getDistanceToWaterSurfaceMax() != null) {
123 text_maxVal.setNumber(getEntity().getDistanceToWaterSurfaceMax());
124 }
125 if(getEntity().getDistanceToWaterSurfaceText() != null) {
126 text_freeText.setText(getEntity().getDistanceToWaterSurfaceText());
127 }
128 break;
129 default:
130 break;
131 }
132
133 }
134 }
135 /** {@inheritDoc} */
136 @Override
137 public void propertyChange(PropertyChangeEvent event) {
138 if (event == null) {
139 return;
140 }
141 Object eventSource = event.getSource();
142
143 if (getElements().contains(eventSource)) {
144 if (event instanceof CdmPropertyChangeEvent) {
145 if (((CdmPropertyChangeEvent) event).hasException()) {
146 handleException((CdmPropertyChangeEvent) event);
147 return;
148 }
149 }
150 handleEvent(eventSource);
151 }
152 }
153
154 /**
155 * @param event
156 */
157 private void handleException(CdmPropertyChangeEvent event) {
158 firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
159 event.getException()));
160 }
161
162 private void handleEvent(Object eventSource) {
163 if (eventSource == text_minVal) {
164 updateMinimum();
165 } else if (eventSource == text_maxVal) {
166 updateMaximum();
167 } else if (eventSource == text_freeText) {
168 updateFreetext();
169 }
170 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
171 }
172
173
174
175 /*
176 * (non-Javadoc)
177 *
178 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
179 */
180 /** {@inheritDoc} */
181 @Override
182 public void dispose() {
183 formFactory.removePropertyChangeListener(this);
184 super.dispose();
185 }
186
187 private void updateMinimum() {
188 switch(unitType) {
189 case ELEVATION:
190 getEntity().setAbsoluteElevation(text_minVal.getDouble().intValue());
191 break;
192 case DIST_TO_GROUND:
193 getEntity().setDistanceToGround(text_minVal.getDouble());
194 break;
195 case DIST_TO_WATER:
196 getEntity().setDistanceToWaterSurface(text_minVal.getDouble());
197 break;
198 }
199 }
200
201 private Number getMinimum() {
202 if(getEntity() == null) {
203 return null;
204 }
205 switch(unitType) {
206 case ELEVATION:
207 return getEntity().getAbsoluteElevation();
208 case DIST_TO_GROUND:
209 return getEntity().getDistanceToGround();
210 case DIST_TO_WATER:
211 return getEntity().getDistanceToWaterSurface();
212 }
213 return null;
214 }
215
216 private void updateMaximum() {
217 switch(unitType) {
218 case ELEVATION:
219 getEntity().setAbsoluteElevationMax(text_maxVal.getDouble().intValue());
220 break;
221 case DIST_TO_GROUND:
222 getEntity().setDistanceToGroundMax(text_maxVal.getDouble());
223 break;
224 case DIST_TO_WATER:
225 getEntity().setDistanceToWaterSurfaceMax(text_maxVal.getDouble());
226 break;
227 }
228 }
229
230 private Number getMaximum() {
231 if(getEntity() == null) {
232 return null;
233 }
234 switch(unitType) {
235 case ELEVATION:
236 return getEntity().getAbsoluteElevationMaximum();
237 case DIST_TO_GROUND:
238 return getEntity().getDistanceToGroundMax();
239 case DIST_TO_WATER:
240 return getEntity().getDistanceToWaterSurfaceMax();
241 }
242 return null;
243 }
244
245 private void updateFreetext() {
246 switch(unitType) {
247 case ELEVATION:
248 getEntity().setAbsoluteElevationText(text_freeText.getText());
249 break;
250 case DIST_TO_GROUND:
251 getEntity().setDistanceToGroundText(text_freeText.getText());
252 break;
253 case DIST_TO_WATER:
254 getEntity().setDistanceToWaterSurfaceText(text_freeText.getText());
255 break;
256 }
257 }
258
259 private String getFreetext() {
260 if(getEntity() == null) {
261 return null;
262 }
263 switch(unitType) {
264 case ELEVATION:
265 return getEntity().getAbsoluteElevationText();
266 case DIST_TO_GROUND:
267 return getEntity().getDistanceToGroundText();
268 case DIST_TO_WATER:
269 return getEntity().getDistanceToWaterSurfaceText();
270 }
271 return null;
272 }
273
274 }
275