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
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 updateTitle();
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 updateTitle();
171 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
172 }
173
174 private void updateTitle(){
175 String title = "";
176 if(text_minVal.getText()!=null){
177 title += text_minVal.getText();
178 }
179 if(text_maxVal.getText()!=null && !text_maxVal.getText().equals("")){
180 if(!title.equals("")){
181 title += " - "+text_maxVal.getText();
182 }
183 }
184 if(title.equals("") && text_freeText.getText()!=null){
185 title = text_freeText.getText();
186 }
187 this.setText(title);
188 layout();
189 }
190
191 /*
192 * (non-Javadoc)
193 *
194 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
195 */
196 /** {@inheritDoc} */
197 @Override
198 public void dispose() {
199 formFactory.removePropertyChangeListener(this);
200 super.dispose();
201 }
202
203 private void updateMinimum() {
204 switch(unitType) {
205 case ELEVATION:
206 getEntity().setAbsoluteElevation(text_minVal.getDouble()!=null?text_minVal.getDouble().intValue():null);
207 break;
208 case DIST_TO_GROUND:
209 getEntity().setDistanceToGround(text_minVal.getDouble());
210 break;
211 case DIST_TO_WATER:
212 getEntity().setDistanceToWaterSurface(text_minVal.getDouble());
213 break;
214 }
215 }
216
217 private Number getMinimum() {
218 if(getEntity() == null) {
219 return null;
220 }
221 switch(unitType) {
222 case ELEVATION:
223 return getEntity().getAbsoluteElevation();
224 case DIST_TO_GROUND:
225 return getEntity().getDistanceToGround();
226 case DIST_TO_WATER:
227 return getEntity().getDistanceToWaterSurface();
228 }
229 return null;
230 }
231
232 private void updateMaximum() {
233 switch(unitType) {
234 case ELEVATION:
235 getEntity().setAbsoluteElevationMax(text_maxVal.getDouble()!=null?text_maxVal.getDouble().intValue():null);
236 break;
237 case DIST_TO_GROUND:
238 getEntity().setDistanceToGroundMax(text_maxVal.getDouble());
239 break;
240 case DIST_TO_WATER:
241 getEntity().setDistanceToWaterSurfaceMax(text_maxVal.getDouble());
242 break;
243 }
244 }
245
246 private Number getMaximum() {
247 if(getEntity() == null) {
248 return null;
249 }
250 switch(unitType) {
251 case ELEVATION:
252 return getEntity().getAbsoluteElevationMaximum();
253 case DIST_TO_GROUND:
254 return getEntity().getDistanceToGroundMax();
255 case DIST_TO_WATER:
256 return getEntity().getDistanceToWaterSurfaceMax();
257 }
258 return null;
259 }
260
261 private void updateFreetext() {
262 switch(unitType) {
263 case ELEVATION:
264 getEntity().setAbsoluteElevationText(text_freeText.getText());
265 break;
266 case DIST_TO_GROUND:
267 getEntity().setDistanceToGroundText(text_freeText.getText());
268 break;
269 case DIST_TO_WATER:
270 getEntity().setDistanceToWaterSurfaceText(text_freeText.getText());
271 break;
272 }
273 }
274
275 private String getFreetext() {
276 if(getEntity() == null) {
277 return null;
278 }
279 switch(unitType) {
280 case ELEVATION:
281 return getEntity().getAbsoluteElevationText();
282 case DIST_TO_GROUND:
283 return getEntity().getDistanceToGroundText();
284 case DIST_TO_WATER:
285 return getEntity().getDistanceToWaterSurfaceText();
286 }
287 return null;
288 }
289
290 }
291