Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 533fe51c7ffa8182c8b293ad5788f528a4441632 (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
121
122
123
124
125
package org.eclipse.papyrus.infra.widgets.editors;

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

public class UnlimitedNaturalEditor extends StringEditor {
	/**
	 * The IConverter for converting data from the widget to the model
	 */
	private IConverter targetToModelConverter;

	/**
	 * Constructs an editor for Integer values. The widget is a Text field.
	 *
	 * @param parent
	 *            The Composite in which this editor is created
	 * @param style
	 *            The Text's style
	 */
	public UnlimitedNaturalEditor(Composite parent, int style) {
		this(parent, style, null);
	}

	/**
	 * Constructs an editor for Integer values. The widget is a Text field.
	 *
	 * @param parent
	 *            The Composite in which this editor is created
	 * @param style
	 *            The Text's style
	 * @param label
	 *            The editor's label
	 */
	public UnlimitedNaturalEditor(Composite parent, int style, String label) {
		super(parent, style, label);

		targetValidator = new UnlimitedNaturalValidator();

		targetToModelConverter = new IConverter() {

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

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

			@Override
			public Integer convert(Object fromObject) {
				if (fromObject instanceof String) {
					String newString = ((String) fromObject).replaceAll(" ", ""); //$NON-NLS-1$ //$NON-NLS-2$
					if (newString.equals("*"))
					{
						return -1;
					}
					return (Integer) StringToNumberConverter.toInteger(false).convert(newString);
				}
				return 0;
			}
		};

		IConverter integerToString = new IConverter() {

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

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

			@Override
			public Object convert(Object fromObject) {
				if (fromObject instanceof Integer) {
					if (((Integer) fromObject).intValue() == -1) {
						return "*"; //$NON-NLS-1$
					}
					return Integer.toString((Integer) fromObject);
				}
				return ""; //$NON-NLS-1$
			}
		};
		setValidateOnDelay(true);

		setConverters(targetToModelConverter, integerToString);
		setTargetAfterGetValidator(targetValidator);

	}



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

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Integer getValue() {
		try {

			return (Integer) targetToModelConverter.convert(super.getValue());
		} catch (Exception ex) {
			Activator.log.error(ex);
			return null;
		}
	}




}

Back to the top