Project

General

Profile

Download (6.28 KB) Statistics
| Branch: | Tag: | Revision:
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.apache.commons.lang.StringUtils;
16
import org.eclipse.jface.util.PropertyChangeEvent;
17

    
18
import eu.etaxonomy.cdm.model.common.TermType;
19
import eu.etaxonomy.cdm.model.location.Point;
20
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
21
import eu.etaxonomy.taxeditor.preference.Resources;
22
import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
23

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

    
36
	private Point point;
37

    
38
	private final TextWithLabelElement text_latitude;
39
	private final TextWithLabelElement text_longitude;
40
	private final NumberWithLabelElement number_errorRadius;
41
	private final TermComboElement<ReferenceSystem> combo_referenceSystem;
42

    
43
	private final TextWithLabelElement text_latitudeParsed;
44

    
45
	private final TextWithLabelElement text_longitudeParsed;
46

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

    
67
		formFactory.addPropertyChangeListener(this);
68

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

    
84
		setPoint(point);
85
	}
86

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

    
101
		boolean propagate = false;
102

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

    
150
		if (propagate) {
151
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
152
		}
153
	}
154

    
155
	/*
156
	 * (non-Javadoc)
157
	 *
158
	 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
159
	 */
160
	/** {@inheritDoc} */
161
	@Override
162
	public Point getEntity() {
163
		return getPoint();
164
	}
165

    
166
	/**
167
	 * <p>
168
	 * setEntity
169
	 * </p>
170
	 *
171
	 * @param point
172
	 *            a {@link eu.etaxonomy.cdm.model.location.Point} object.
173
	 */
174
	public void setEntity(Point point) {
175
		setPoint(point);
176
	}
177

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

    
202
	/**
203
	 * <p>
204
	 * Getter for the field <code>point</code>.
205
	 * </p>
206
	 *
207
	 * @return the point
208
	 */
209
	public Point getPoint() {
210
		if (point == null) {
211
			point = Point.NewInstance();
212
		}
213

    
214
		return point;
215
	}
216

    
217
	@Override
218
    public void setSelected(boolean selected) {
219
		setBackground(selected ? SELECTED : getPersistentBackground());
220
	}
221

    
222
}
(35-35/44)