Merge branch 'release/4.0.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / PointElement.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 java.text.ParseException;
14
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 getPoint().setLatitudeByParsing(text_latitude.getText());
106 text_latitudeParsed.setText(point.getLatitudeSexagesimal()
107 .toString(false));
108 text_latitude
109 .setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
110 propagate = true;
111 } catch (ParseException e) {
112 text_latitude
113 .setBackground(getColor(Resources.COLOR_PARSE_ERROR));
114 }
115 } else if (eventSource == text_longitude) {
116 try {
117 getPoint().setLongitudeByParsing(text_longitude.getText());
118 text_longitudeParsed.setText(point.getLongitudeSexagesimal()
119 .toString(false));
120 text_longitude
121 .setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
122 propagate = true;
123 } catch (ParseException e) {
124 text_longitude
125 .setBackground(getColor(Resources.COLOR_PARSE_ERROR));
126 }
127 } else if (eventSource == number_errorRadius) {
128 getPoint().setErrorRadius(number_errorRadius.getInteger());
129 propagate = true;
130 } else if (eventSource == combo_referenceSystem) {
131 getPoint().setReferenceSystem(combo_referenceSystem.getSelection());
132 propagate = true;
133 }
134
135 if (propagate) {
136 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
137 }
138 }
139
140 /*
141 * (non-Javadoc)
142 *
143 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
144 */
145 /** {@inheritDoc} */
146 @Override
147 public Point getEntity() {
148 return getPoint();
149 }
150
151 /**
152 * <p>
153 * setEntity
154 * </p>
155 *
156 * @param point
157 * a {@link eu.etaxonomy.cdm.model.location.Point} object.
158 */
159 public void setEntity(Point point) {
160 setPoint(point);
161 }
162
163 /**
164 * <p>
165 * Setter for the field <code>point</code>.
166 * </p>
167 *
168 * @param point
169 * the point to set
170 */
171 public void setPoint(Point point) {
172 this.point = point;
173 if (point != null) {
174 String latitude = point.getLatitudeSexagesimal() == null ? ""
175 : point.getLatitudeSexagesimal().toString(false);
176 String longitude = point.getLongitudeSexagesimal() == null ? ""
177 : point.getLongitudeSexagesimal().toString(false);
178 text_latitude.setText(latitude);
179 text_latitudeParsed.setText(latitude);
180 text_longitude.setText(longitude);
181 text_longitudeParsed.setText(longitude);
182 number_errorRadius.setNumber(point.getErrorRadius());
183 combo_referenceSystem.setSelection(point.getReferenceSystem());
184 }
185 }
186
187 /**
188 * <p>
189 * Getter for the field <code>point</code>.
190 * </p>
191 *
192 * @return the point
193 */
194 public Point getPoint() {
195 if (point == null) {
196 point = Point.NewInstance();
197 }
198
199 return point;
200 }
201
202 @Override
203 public void setSelected(boolean selected) {
204 setBackground(selected ? SELECTED : getPersistentBackground());
205 }
206
207 }