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