cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / MinMaxTextSection.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.ui.element;
11
12 import org.eclipse.jface.util.PropertyChangeEvent;
13
14 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
15
16 /**
17 * <p>
18 * MinMaxTextSection class.
19 * </p>
20 *
21 * @author c.mathew
22 * @created 23 Jul 2013
23 * @version 1.0
24 */
25 public class MinMaxTextSection extends AbstractFormSection<DerivedUnitFacade> {
26
27 private final TextWithLabelElement text_freeText;
28 private final NumberWithLabelElement text_minVal;
29 private final NumberWithLabelElement text_maxVal;
30 private int cursorPosition;
31
32 // unit types handled by this section
33 public enum UnitType {
34 ELEVATION,
35 DIST_TO_GROUND,
36 DIST_TO_WATER
37 }
38
39 private UnitType unitType = UnitType.ELEVATION;
40
41 /**
42 * <p>
43 * Constructor for DateDetailSection.
44 * </p>
45 *
46 * @param formFactory
47 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
48 * object.
49 * @param parentElement
50 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
51 * object.
52 * @param style
53 * a int.
54 */
55 protected MinMaxTextSection(CdmFormFactory formFactory,
56 ICdmFormElement parentElement, UnitType unitType, int style) {
57 super(formFactory, parentElement, style);
58 this.unitType = unitType;
59 getLayoutComposite().setLayout(LayoutConstants.LAYOUT(2, false));
60
61 switch(unitType) {
62 case ELEVATION:
63 text_minVal = formFactory.createFloatTextWithLabelElement(this, "Min [m] : ", getMinimum(), style);
64 text_maxVal = formFactory.createFloatTextWithLabelElement(this, "Max [m] : ", getMaximum(), style);
65 break;
66 default:
67 text_minVal = formFactory.createFloatTextWithLabelElement(this, "Min [m] : ", getMinimum(), style);
68 text_maxVal = formFactory.createFloatTextWithLabelElement(this, "Max [m] : ", getMaximum(), style);
69 break;
70 }
71 text_freeText = formFactory.createTextWithLabelElement(this,
72 "Freetext : ", getFreetext(), style);
73
74 formFactory.addPropertyChangeListener(this);
75 }
76
77 /**
78 * <p>
79 * Setter for the field <code>timePeriod</code>.
80 * </p>
81 *
82 * @param timePeriod
83 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
84 */
85 @Override
86 public void setEntity(DerivedUnitFacade gatheringEvent) {
87 super.setEntity(gatheringEvent);
88 updateValues();
89 updateTitle();
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().getAbsoluteElevationMaximum());
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 updateTitle();
170 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
171 }
172
173 private void updateTitle(){
174 String title = "";
175 if(text_minVal.getText()!=null){
176 title += text_minVal.getText();
177 }
178 if(text_maxVal.getText()!=null && !text_maxVal.getText().equals("")){
179 if(!title.equals("")){
180 title += " - "+text_maxVal.getText();
181 }
182 }
183 if(title.equals("") && text_freeText.getText()!=null){
184 title = text_freeText.getText();
185 }
186 this.setText(title);
187 layout();
188 }
189
190 /*
191 * (non-Javadoc)
192 *
193 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
194 */
195 /** {@inheritDoc} */
196 @Override
197 public void dispose() {
198 formFactory.removePropertyChangeListener(this);
199 super.dispose();
200 }
201
202 private void updateMinimum() {
203 switch(unitType) {
204 case ELEVATION:
205 getEntity().setAbsoluteElevation(text_minVal.getDouble()!=null ? text_minVal.getDouble().intValue(): null);
206 break;
207 case DIST_TO_GROUND:
208 getEntity().setDistanceToGround(text_minVal.getDouble());
209 break;
210 case DIST_TO_WATER:
211 getEntity().setDistanceToWaterSurface(text_minVal.getDouble());
212 break;
213 }
214 }
215
216 private Number getMinimum() {
217 if(getEntity() == null) {
218 return null;
219 }
220 switch(unitType) {
221 case ELEVATION:
222 return getEntity().getAbsoluteElevation();
223 case DIST_TO_GROUND:
224 return getEntity().getDistanceToGround();
225 case DIST_TO_WATER:
226 return getEntity().getDistanceToWaterSurface();
227 }
228 return null;
229 }
230
231 private void updateMaximum() {
232 switch(unitType) {
233 case ELEVATION:
234 getEntity().setAbsoluteElevationMax(text_maxVal.getDouble() != null ? text_maxVal.getDouble().intValue() : null);
235 break;
236 case DIST_TO_GROUND:
237 getEntity().setDistanceToGroundMax(text_maxVal.getDouble());
238 break;
239 case DIST_TO_WATER:
240 getEntity().setDistanceToWaterSurfaceMax(text_maxVal.getDouble());
241 break;
242 }
243 }
244
245 private Number getMaximum() {
246 if(getEntity() == null) {
247 return null;
248 }
249 switch(unitType) {
250 case ELEVATION:
251 return getEntity().getAbsoluteElevationMaximum();
252 case DIST_TO_GROUND:
253 return getEntity().getDistanceToGroundMax();
254 case DIST_TO_WATER:
255 return getEntity().getDistanceToWaterSurfaceMax();
256 }
257 return null;
258 }
259
260 private void updateFreetext() {
261 switch(unitType) {
262 case ELEVATION:
263 getEntity().setAbsoluteElevationText(text_freeText.getText());
264 break;
265 case DIST_TO_GROUND:
266 getEntity().setDistanceToGroundText(text_freeText.getText());
267 break;
268 case DIST_TO_WATER:
269 getEntity().setDistanceToWaterSurfaceText(text_freeText.getText());
270 break;
271 }
272 }
273
274 private String getFreetext() {
275 if(getEntity() == null) {
276 return null;
277 }
278 switch(unitType) {
279 case ELEVATION:
280 return getEntity().getAbsoluteElevationText();
281 case DIST_TO_GROUND:
282 return getEntity().getDistanceToGroundText();
283 case DIST_TO_WATER:
284 return getEntity().getDistanceToWaterSurfaceText();
285 }
286 return null;
287 }
288
289 }
290