Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / PointElement.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 java.text.ParseException;
13
14 import org.apache.commons.lang.StringUtils;
15 import org.eclipse.jface.util.PropertyChangeEvent;
16
17 import eu.etaxonomy.cdm.model.common.TermType;
18 import eu.etaxonomy.cdm.model.location.Point;
19 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
20 import eu.etaxonomy.taxeditor.preference.Resources;
21 import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
22
23 /**
24 * <p>
25 * PointElement class.
26 * </p>
27 *
28 * @author n.hoffmann
29 * @created Oct 15, 2010
30 */
31 public class PointElement extends AbstractCdmFormElement implements
32 IEntityElement<Point>, ISelectable {
33
34 private Point point;
35
36 private final TextWithLabelElement text_latitude;
37 private final TextWithLabelElement text_longitude;
38 private final NumberWithLabelElement number_errorRadius;
39 private final TermComboElement<ReferenceSystem> combo_referenceSystem;
40
41 private final TextWithLabelElement text_latitudeParsed;
42
43 private final TextWithLabelElement text_longitudeParsed;
44
45 /**
46 * <p>
47 * Constructor for PointElement.
48 * </p>
49 *
50 * @param formFactory
51 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
52 * object.
53 * @param formElement
54 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
55 * object.
56 * @param point
57 * a {@link eu.etaxonomy.cdm.model.location.Point} object.
58 * @param style
59 * a int.
60 */
61 public PointElement(CdmFormFactory formFactory,
62 ICdmFormElement formElement, Point point, final int style) {
63 super(formFactory, formElement);
64
65 formFactory.addPropertyChangeListener(this);
66
67 text_latitude = formFactory.createTextWithLabelElement(formElement,
68 "Latitude", null, style);
69 text_latitudeParsed = formFactory.createTextWithLabelElement(
70 formElement, "", null, style);
71 text_latitudeParsed.setEnabled(false);
72 text_longitude = formFactory.createTextWithLabelElement(formElement,
73 "Longitude", null, style);
74 text_longitudeParsed = formFactory.createTextWithLabelElement(
75 formElement, "", null, style);
76 text_longitudeParsed.setEnabled(false);
77 number_errorRadius = formFactory.createNumberTextWithLabelElement(
78 formElement, "Error Radius (m)", null, style);
79 combo_referenceSystem = formFactory.createDefinedTermComboElement(TermType.ReferenceSystem,
80 formElement, "Reference System", null, style);
81
82 setPoint(point);
83 }
84
85 /*
86 * (non-Javadoc)
87 *
88 * @see
89 * eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org
90 * .eclipse.jface.util.PropertyChangeEvent)
91 */
92 /** {@inheritDoc} */
93 @Override
94 public void propertyChange(PropertyChangeEvent event) {
95 if (event == null) {
96 return;
97 }
98
99 boolean propagate = false;
100
101 Object eventSource = event.getSource();
102 if (eventSource == text_latitude) {
103 try {
104 if (StringUtils.isBlank(text_latitude.getText())){
105 getPoint().setLatitudeByParsing(null);
106 text_latitudeParsed.setText(null);
107 text_latitude
108 .setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
109 }else{
110 getPoint().setLatitudeByParsing(text_latitude.getText());
111 text_latitudeParsed.setText(point.getLatitudeSexagesimal()
112 .toString(false));
113 text_latitude
114 .setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
115 }
116 propagate = true;
117 } catch (ParseException e) {
118 text_latitude
119 .setBackground(getColor(Resources.COLOR_PARSE_ERROR));
120 }
121 } else if (eventSource == text_longitude) {
122 try {
123 if (StringUtils.isBlank(text_longitude.getText())){
124 getPoint().setLongitudeByParsing(null);
125 text_longitudeParsed.setText(null);
126 text_longitude
127 .setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
128 }else{
129 getPoint().setLongitudeByParsing(text_longitude.getText());
130 text_longitudeParsed.setText(point.getLongitudeSexagesimal()
131 .toString(false));
132 text_longitude
133 .setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
134 }
135 propagate = true;
136 } catch (ParseException e) {
137 text_longitude
138 .setBackground(getColor(Resources.COLOR_PARSE_ERROR));
139 }
140 } else if (eventSource == number_errorRadius) {
141 getPoint().setErrorRadius(number_errorRadius.getInteger());
142 propagate = true;
143 } else if (eventSource == combo_referenceSystem) {
144 getPoint().setReferenceSystem(combo_referenceSystem.getSelection());
145 propagate = true;
146 }
147
148 if (propagate) {
149 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
150 }
151 }
152
153 /** {@inheritDoc} */
154 @Override
155 public Point getEntity() {
156 return getPoint();
157 }
158
159 /**
160 * <p>
161 * setEntity
162 * </p>
163 *
164 * @param point
165 * a {@link eu.etaxonomy.cdm.model.location.Point} object.
166 */
167 public void setEntity(Point point) {
168 setPoint(point);
169 }
170
171 /**
172 * <p>
173 * Setter for the field <code>point</code>.
174 * </p>
175 *
176 * @param point
177 * the point to set
178 */
179 public void setPoint(Point point) {
180 this.point = point;
181 if (point != null) {
182 String latitude = point.getLatitudeSexagesimal() == null ? ""
183 : point.getLatitudeSexagesimal().toString(false);
184 String longitude = point.getLongitudeSexagesimal() == null ? ""
185 : point.getLongitudeSexagesimal().toString(false);
186 text_latitude.setText(latitude);
187 text_latitudeParsed.setText(latitude);
188 text_longitude.setText(longitude);
189 text_longitudeParsed.setText(longitude);
190 number_errorRadius.setNumber(point.getErrorRadius());
191 combo_referenceSystem.setSelection(point.getReferenceSystem());
192 }
193 }
194
195 /**
196 * <p>
197 * Getter for the field <code>point</code>.
198 * </p>
199 *
200 * @return the point
201 */
202 public Point getPoint() {
203 if (point == null) {
204 point = Point.NewInstance();
205 }
206
207 return point;
208 }
209
210 @Override
211 public void setSelected(boolean selected) {
212 setBackground(selected ? SELECTED : getPersistentBackground());
213 }
214
215 }