Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d76d447681b764b5d65007e4baa583e4bba84f6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Thibault Le Ouay t.leouay@sherpa-eng.com - Add binding implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.editors;


import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.papyrus.infra.widgets.Activator;
import org.eclipse.papyrus.infra.widgets.validator.RealValidator;
import org.eclipse.swt.widgets.Composite;


/**
 * An editor representing a float value as a text box
 *
 * @author Camille Letavernier
 */
public class DoubleEditor extends StringEditor {

	private IConverter targetToModelConverter;

	/**
	 *
	 * Constructs an Editor for a Double value. The widget is a Text field.
	 *
	 * @param parent
	 *            The Composite in which the editor is created
	 * @param style
	 *            The Text's style
	 */
	public DoubleEditor(Composite parent, int style) {
		super(parent, style);

		targetValidator = new RealValidator();
		targetToModelConverter = new IConverter() {

			@Override
			public Object getFromType() {
				return String.class;
			}

			@Override
			public Object getToType() {
				return Double.class;
			}

			@Override
			public Double convert(Object fromObject) {
				if (fromObject instanceof String) {
					String newString = ((String) fromObject)
							.replaceAll(" ", ""); //$NON-NLS-1$ //$NON-NLS-2$
					try {
						return Double.parseDouble(newString);
					} catch (NumberFormatException ex) {
						Activator.log.error(ex);
						return null;
					}
				}
				return null;
			}

		};

		IConverter doubleToString = new IConverter() {

			@Override
			public Object getFromType() {
				return Double.class;
			}

			@Override
			public Object getToType() {
				return String.class;
			}

			@Override
			public Object convert(Object fromObject) {
				if (fromObject instanceof Double) {
					return Double.toString((Double) fromObject);
				}
				return ""; //$NON-NLS-1$
			}
		};
		setValidateOnDelay(true);
		setConverters(targetToModelConverter, doubleToString);
		setTargetAfterGetValidator(targetValidator);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getEditableType() {
		return Double.class;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Double getValue() {
		try {
			return (Double) targetToModelConverter.convert(super.getValue());
		} catch (Exception ex) {
			Activator.log.error(ex);
			return null;
		}
	}


}

Back to the top