Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fc17bbdc52c3bdfcfbb0d02e45c3381ba2068444 (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
/*****************************************************************************
 * 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
 *****************************************************************************/
package org.eclipse.papyrus.emf.facet.widgets.celleditors.ecore.composite;

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.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
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;

public class BooleanComposite extends AbstractCellEditorComposite<Boolean> {

	private Combo combo = null;
	private boolean enableSelectionListener = true;
	private Control parentControl = null;

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

	public BooleanComposite(final Composite parent, final int style) {
		super(parent, style);
		this.parentControl = parent;
		GridLayout gd = new GridLayout(1, false);
		gd.marginHeight = 0;
		gd.marginWidth = 0;
		gd.horizontalSpacing = 0;
		setLayout(gd);

		this.combo = new Combo(this, SWT.READ_ONLY);
		this.combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		this.combo.setItems(new String[] { "true", "false" }); //$NON-NLS-1$ //$NON-NLS-2$
		this.combo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(final KeyEvent event) {

				if ((event.keyCode == SWT.CR && event.stateMask == 0)) {
					// Enter key pressed
					commit();
				} else if (event.keyCode == SWT.ESC && event.stateMask == 0
						|| (event.keyCode == SWT.KEYPAD_CR && event.stateMask == 0)) {
					// Escape key pressed
					close();
				}
				BooleanComposite.this.setEnableSelectionListener(false);
			}
		});

		this.combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				if (BooleanComposite.this.getEnableSelectionListener()) {
					fireChanged();
					commit();
					super.widgetSelected(e);
				}
				BooleanComposite.this.setEnableSelectionListener(true);
			}
		});

		// commit the cell editor when the mouse is clicked
		// anywhere outside the text field
		final Listener clickListener = new Listener() {
			public void handleEvent(final Event event) {
				if (event.widget instanceof Control) {
					Control control = (Control) event.widget;
					if (control.getShell() == BooleanComposite.this.getParentControl().getShell()) {
						if (event.widget != BooleanComposite.this.getCombo()) {
							commit();
						}
					}
				}
			}
		};
		Display.getDefault().addFilter(SWT.MouseDown, clickListener);

		// this listener is only here to remove
		// the other listener from the Display
		getCombo().addDisposeListener(new DisposeListener() {
			public void widgetDisposed(final DisposeEvent e) {
				Display.getDefault().removeFilter(SWT.MouseDown, clickListener);
			}
		});

		this.combo.forceFocus();
	}

	protected void commit() {
		fireCommit();
	}

	public Boolean getValue() {
		if (this.combo.getSelectionIndex() == 0) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;

	}

	public void setValue(final Boolean value) {
		if (value != null) {
			if (value.booleanValue()) {
				this.combo.select(0);
			} else {
				this.combo.select(1);
			}
		}
	}

	protected Combo getCombo() {
		return this.combo;
	}

	protected boolean getEnableSelectionListener() {
		return this.enableSelectionListener;
	}

	protected void setEnableSelectionListener(final boolean value) {
		this.enableSelectionListener = value;
	}

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

}

Back to the top