c6fbd056f10c04187faa9b57b33ed3117047f402
[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.location.Point;
18 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
19 import eu.etaxonomy.taxeditor.preference.Resources;
20 import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
21
22 /**
23 * <p>
24 * PointElement class.
25 * </p>
26 *
27 * @author n.hoffmann
28 * @created Oct 15, 2010
29 * @version 1.0
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 (hexagesimal)", null, style);
69 text_latitudeParsed = formFactory.createTextWithLabelElement(
70 formElement, "", null, style);
71 text_latitudeParsed.setEnabled(false);
72 text_longitude = formFactory.createTextWithLabelElement(formElement,
73 "Longitude (hexagesimal)", 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
80 .createTermComboElement(ReferenceSystem.class,
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 public void setSelected(boolean selected) {
203 setBackground(selected ? SELECTED : getPersistentBackground());
204 }
205
206 }