Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 91b1ca0854f55cca44d423ae2390a1000255abaf (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*****************************************************************************
 * 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:
 *   Nicolas Guyomar (Mia-Software) - initial API and implementation
 *   Nicolas Guyomar (Mia-Software) - Bug 334546 - [celleditors] no border on Text field
 *   Nicolas Bros (Mia-Software) - Bug 334539 - [celleditors] change listener
 *   Nicolas Guyomar (Mia-Software) - Bug 349574 - EMF Facet Ecore String composite textField cannot be disabled
 *****************************************************************************/
package org.eclipse.papyrus.emf.facet.widgets.celleditors.ecore.composite;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.papyrus.emf.facet.widgets.celleditors.AbstractCellEditorComposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public abstract class StringComposite<T> extends AbstractCellEditorComposite<T> {

	/** the control that is used to edit the contents of the cell */
	private Text textField = null;

	private final Composite parentControl;

	/** the original value of the cell **/
	private IConverter<T> converter;

	protected static final Color RED = new Color(Display.getCurrent(), 255, 192, 192);
	protected static final Color WHITE = Display.getCurrent().getSystemColor(
			SWT.COLOR_LIST_BACKGROUND);

	public StringComposite(final Composite parent, final int style) {
		super(parent);
		setLayout(new FillLayout());
		this.parentControl = parent;
		this.textField = new Text(this, style);

		this.textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(final KeyEvent event) {
				// Enter key pressed
				if ((event.keyCode == SWT.CR && event.stateMask == 0)
						|| (event.keyCode == SWT.KEYPAD_CR && event.stateMask == 0)) {
					if (StringComposite.this.getTextField().getEditable()) {
						StringComposite.this.commit();
					}
				} else if (event.keyCode == SWT.ESC && event.stateMask == 0) {
					// Escape key pressed
					close();
				}
			}

			@Override
			public void keyReleased(final KeyEvent e) {
				if (!StringComposite.this.validate(StringComposite.this.getTextField().getText())
						.isOK()) {
					StringComposite.this.getTextField().setBackground(StringComposite.RED);
				} else {

					StringComposite.this.getTextField().setBackground(StringComposite.WHITE);
				}
				super.keyReleased(e);
			}

		});
		final Listener clickListener = new Listener() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt. widgets.Event)
			 */
			public void handleEvent(final Event event) {
				if (event.widget instanceof Control) {
					Control control = (Control) event.widget;
					if (control.getShell() == StringComposite.this.getParentControl().getShell()) {
						if (event.widget != StringComposite.this.getTextField()) {
							StringComposite.this.commit();
						}
					}
				}
			}
		};
		Display.getDefault().addFilter(SWT.MouseDown, clickListener);
		// this listener is only here to remove
		// the other listener from the Display
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(final DisposeEvent e) {
				Display.getDefault().removeFilter(SWT.MouseDown, clickListener);
			}
		});
		
		this.textField.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				fireChanged();
			}
		});

		this.textField.forceFocus();
	}

	public StringComposite(final Composite parent) {
		this(parent, SWT.NONE);
	}

	protected void commit() {
		if (getValidator().validate(getValue()).isOK()) {
			fireCommit();
		}
	}

	@Override
	public void setEnabled(final boolean enabled) {
		this.textField.setEnabled(enabled);
		super.setEnabled(enabled);
	}
	
	public boolean getEditable() {
		return this.textField.getEditable();
	}

	public void setValue(final T value) {
		if (value == null) {
			this.textField.setText(""); //$NON-NLS-1$
		} else {
			String strValue = getConverter().toString(value);
			this.textField.setText(strValue);
			this.textField.setSelection(0, strValue.length());
		}
	}

	public String getText() {
		return this.textField.getText();
	}

	protected Text getTextField() {
		return this.textField;
	}

	protected Composite getParentControl() {
		return this.parentControl;
	}

	public T getValue() {
		return this.getConverter().toObject(this.getText());
	}

	private IConverter<T> getConverter() {
		if (this.converter == null) {
			this.converter = this.initConverter();
		}
		return this.converter;
	}

	public abstract IConverter<T> initConverter();

	public interface IConverter<T> {
		public T toObject(String string);

		public String toString(T object);
	}

	public abstract IStatus validate(String string);
}

Back to the top