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