Project

General

Profile

Download (6.16 KB) Statistics
| Branch: | Tag: | Revision:
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 java.text.ParseException;
12

    
13
import org.apache.commons.lang3.StringUtils;
14
import org.eclipse.jface.util.PropertyChangeEvent;
15

    
16
import eu.etaxonomy.cdm.model.location.Point;
17
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
18
import eu.etaxonomy.cdm.model.term.TermType;
19
import eu.etaxonomy.taxeditor.preference.Resources;
20
import eu.etaxonomy.taxeditor.ui.combo.term.TermComboElement;
21

    
22
/**
23
 * <p>
24
 * PointElement class.
25
 * </p>
26
 *
27
 * @author n.hoffmann
28
 * @created Oct 15, 2010
29
 */
30
public class PointElement extends AbstractCdmFormElement implements
31
		IEntityElement<Point>, ISelectable {
32

    
33
	private Point point;
34

    
35
	private final TextWithLabelElement text_latitude;
36
	private final TextWithLabelElement text_longitude;
37
	private final NumberWithLabelElement number_errorRadius;
38
	private final TermComboElement<ReferenceSystem> combo_referenceSystem;
39

    
40
	private final TextWithLabelElement text_latitudeParsed;
41

    
42
	private final TextWithLabelElement text_longitudeParsed;
43

    
44
	/**
45
	 * <p>
46
	 * Constructor for PointElement.
47
	 * </p>
48
	 *
49
	 * @param formFactory
50
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
51
	 *            object.
52
	 * @param formElement
53
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
54
	 *            object.
55
	 * @param point
56
	 *            a {@link eu.etaxonomy.cdm.model.location.Point} object.
57
	 * @param style
58
	 *            a int.
59
	 */
60
	public PointElement(CdmFormFactory formFactory,
61
			ICdmFormElement formElement, Point point, final int style) {
62
		super(formFactory, formElement);
63

    
64
		formFactory.addPropertyChangeListener(this);
65

    
66
		text_latitude = formFactory.createTextWithLabelElement(formElement,
67
				"Latitude", null, style);
68
		text_latitudeParsed = formFactory.createTextWithLabelElement(
69
				formElement, "", null, style);
70
		text_latitudeParsed.setEnabled(false);
71
		text_longitude = formFactory.createTextWithLabelElement(formElement,
72
				"Longitude", null, style);
73
		text_longitudeParsed = formFactory.createTextWithLabelElement(
74
				formElement, "", null, style);
75
		text_longitudeParsed.setEnabled(false);
76
		number_errorRadius = formFactory.createFloatTextWithLabelElement(
77
				formElement, "Error Radius (m)", null, style);
78
		combo_referenceSystem = formFactory.createDefinedTermComboElement(TermType.ReferenceSystem,
79
						formElement, "Reference System", null, style);
80

    
81
		setPoint(point);
82
	}
83

    
84
	/*
85
	 * (non-Javadoc)
86
	 *
87
	 * @see
88
	 * eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org
89
	 * .eclipse.jface.util.PropertyChangeEvent)
90
	 */
91
	/** {@inheritDoc} */
92
	@Override
93
	public void propertyChange(PropertyChangeEvent event) {
94
		if (event == null) {
95
			return;
96
		}
97

    
98
		boolean propagate = false;
99

    
100
		Object eventSource = event.getSource();
101
		if (eventSource == text_latitude) {
102
			try {
103
			    if (StringUtils.isBlank(text_latitude.getText())){
104
			        getPoint().setLatitudeByParsing(null);
105
			        text_latitudeParsed.setText(null);
106
			        text_latitude
107
                    .setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
108
			    }else{
109
    				getPoint().setLatitudeByParsing(text_latitude.getText());
110
    				text_latitudeParsed.setText(point.getLatitudeSexagesimal()
111
    						.toString(false));
112
    				text_latitude
113
    						.setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
114
			    }
115
				propagate = true;
116
			} catch (ParseException e) {
117
				text_latitude
118
						.setBackground(getColor(Resources.COLOR_PARSE_ERROR));
119
			}
120
		} else if (eventSource == text_longitude) {
121
			try {
122
			    if (StringUtils.isBlank(text_longitude.getText())){
123
                    getPoint().setLongitudeByParsing(null);
124
                    text_longitudeParsed.setText(null);
125
                    text_longitude
126
                    .setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
127
                }else{
128
    				getPoint().setLongitudeByParsing(text_longitude.getText());
129
    				text_longitudeParsed.setText(point.getLongitudeSexagesimal()
130
    						.toString(false));
131
    				text_longitude
132
    						.setBackground(getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
133
                }
134
				propagate = true;
135
			} catch (ParseException e) {
136
				text_longitude
137
						.setBackground(getColor(Resources.COLOR_PARSE_ERROR));
138
			}
139
		} else if (eventSource == number_errorRadius) {
140
			getPoint().setErrorRadius(number_errorRadius.getInteger());
141
			propagate = true;
142
		} else if (eventSource == combo_referenceSystem) {
143
			getPoint().setReferenceSystem(combo_referenceSystem.getSelection());
144
			propagate = true;
145
		}
146

    
147
		if (propagate) {
148
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
149
		}
150
	}
151

    
152
	/** {@inheritDoc} */
153
	@Override
154
	public Point getEntity() {
155
		return getPoint();
156
	}
157

    
158
	/**
159
	 * <p>
160
	 * setEntity
161
	 * </p>
162
	 *
163
	 * @param point
164
	 *            a {@link eu.etaxonomy.cdm.model.location.Point} object.
165
	 */
166
	public void setEntity(Point point) {
167
		setPoint(point);
168
	}
169

    
170
	/**
171
	 * <p>
172
	 * Setter for the field <code>point</code>.
173
	 * </p>
174
	 *
175
	 * @param point
176
	 *            the point to set
177
	 */
178
	public void setPoint(Point point) {
179
		this.point = point;
180
		if (point != null) {
181
			String latitude = point.getLatitudeSexagesimal() == null ? ""
182
					: point.getLatitudeSexagesimal().toString(false);
183
			String longitude = point.getLongitudeSexagesimal() == null ? ""
184
					: point.getLongitudeSexagesimal().toString(false);
185
			text_latitude.setText(latitude);
186
			text_latitudeParsed.setText(latitude);
187
			text_longitude.setText(longitude);
188
			text_longitudeParsed.setText(longitude);
189
			number_errorRadius.setNumber(point.getErrorRadius());
190
			combo_referenceSystem.setSelection(point.getReferenceSystem());
191
		}
192
	}
193

    
194
	/**
195
	 * <p>
196
	 * Getter for the field <code>point</code>.
197
	 * </p>
198
	 *
199
	 * @return the point
200
	 */
201
	public Point getPoint() {
202
		if (point == null) {
203
			point = Point.NewInstance();
204
		}
205

    
206
		return point;
207
	}
208

    
209
	@Override
210
    public void setSelected(boolean selected) {
211
		setBackground(selected ? SELECTED : getPersistentBackground());
212
	}
213

    
214
}
(41-41/53)