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