Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db1d6132146bb514d0b7c0e40b0a851a60c9f580 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*****************************************************************************
 * Copyright (c) 2013 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
 *  Shuai Li (CEA LIST) shuai.li@cea.fr - SelectionMenu#refresh method
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.editors;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

/**
 * A basic menu which proposes a list of choices.
 *
 * Implementation is based on a JFace TableViewer
 *
 * Typical usage: JDT-like Ctrl + Click (Navigation)
 *
 * @author Camille Letavernier
 *
 */
public class SelectionMenu {

	private ILabelProvider labelProvider;

	private IStructuredContentProvider contentProvider;

	private Shell parentShell;

	private Point location;

	private Object input;

	private Shell shell;

	private TableViewer tableViewer;

	private ISelectionChangedListener selectionChangedListener;

	private List<ISelectionChangedListener> selectionChangedListeners;

	private KeyListener keyListener;

	private List<KeyListener> keyListeners;

	private MouseTrackListener mouseTrackListener;

	private List<MouseTrackListener> mouseTrackListeners;

	public SelectionMenu(Shell parentShell) {
		this(parentShell, parentShell.getDisplay().getCursorLocation());
	}

	/**
	 * @since 2.0
	 */
	public SelectionMenu(Shell parentShell, Object source) {
		if (source instanceof TableViewer && parentShell != null) {
			TableViewer tableViewer = (TableViewer) source;

			// Get the cell's y position (we can't use the getCell(Point location) method)
			int selectionIndex = tableViewer.getTable().getSelectionIndex();
			int cellHeight = tableViewer.getTable().getItem(selectionIndex).getBounds().height;
			int y = tableViewer.getTable().getShell().getLocation().y;
			y += selectionIndex * cellHeight;

			// Get the cell's x position and append by the table's width
			// int width= tableViewer.getTable().getSize().x;
			int width = tableViewer.getTable().getShell().getBounds().width;
			int x = tableViewer.getTable().getShell().getLocation().x + width;

			Point location = new Point(x, y);
			init(parentShell, location, -1, 0);
			return;
		}

		init(parentShell, parentShell.getDisplay().getCursorLocation(), 1, 1);
	}

	/**
	 * @since 2.0
	 */
	public SelectionMenu(Shell parentShell, Object source, Point cursorPosition) {
		if (source instanceof Table && parentShell != null && cursorPosition != null) {
			Table table = (Table) source;

			TableItem item = table.getItem(cursorPosition);

			if (item != null) {
				int selectionIndex = 0;
				for (Object tableItem : table.getItems()) {
					if (tableItem.equals(item)) {
						break;
					}
					selectionIndex++;
				}

				int cellHeight = item.getBounds().height;
				int y = table.getShell().getLocation().y;
				y += selectionIndex * cellHeight;

				// Get the cell's x position and append by the table's width
				// int width= tableViewer.getTable().getSize().x;
				int width = table.getShell().getBounds().width;
				int x = table.getShell().getLocation().x + width;

				Point location = new Point(x, y);
				init(parentShell, location, -1, 0);
				return;
			}
		}

		init(parentShell, parentShell.getDisplay().getCursorLocation(), 1, 1);
	}

	public SelectionMenu(Shell parentShell, Point location) {
		init(parentShell, location, 1, 1);
	}

	/**
	 * @since 2.0
	 */
	protected void init(Shell parentShell, Point location, int xOffset, int yOffset) {
		// Move the shell so that it doesn't open under the mouse
		// The hovered element can still be selected
		location.x += xOffset;
		location.y += yOffset;

		this.parentShell = parentShell;
		this.location = location;
		labelProvider = new LabelProvider();

		selectionChangedListeners = new LinkedList<ISelectionChangedListener>();
		selectionChangedListener = new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				for (ISelectionChangedListener listener : selectionChangedListeners) {
					listener.selectionChanged(event);
				}
			}
		};

		keyListeners = new LinkedList<KeyListener>();
		keyListener = new KeyListener() {
			@Override
			public void keyPressed(KeyEvent event) {
				for (KeyListener listener : keyListeners) {
					listener.keyPressed(event);
				}
			}

			@Override
			public void keyReleased(KeyEvent event) {
				for (KeyListener listener : keyListeners) {
					listener.keyReleased(event);
				}
			}
		};

		mouseTrackListeners = new LinkedList<MouseTrackListener>();
		mouseTrackListener = new MouseTrackListener() {
			@Override
			public void mouseEnter(MouseEvent event) {
				for (MouseTrackListener mouseTrackListener : mouseTrackListeners) {
					mouseTrackListener.mouseEnter(event);
				}
			}

			@Override
			public void mouseExit(MouseEvent event) {
				for (MouseTrackListener mouseTrackListener : mouseTrackListeners) {
					mouseTrackListener.mouseExit(event);
				}
			}

			@Override
			public void mouseHover(MouseEvent event) {
				for (MouseTrackListener mouseTrackListener : mouseTrackListeners) {
					mouseTrackListener.mouseHover(event);
				}
			}
		};
	}

	public void open() {
		// Shell background and background
		shell = new Shell(parentShell, SWT.NONE);
		shell.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		/*
		 * GridLayout gridLayout = new GridLayout(1, false);
		 * gridLayout.marginWidth = 5;
		 * gridLayout.marginHeight = 5;
		 * shell.setLayout(gridLayout);
		 */
		shell.setLayout(new GridLayout(1, false));

		// TableViewer for menu items
		tableViewer = new TableViewer(shell, SWT.NO_SCROLL);
		tableViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		tableViewer.setContentProvider(contentProvider);
		tableViewer.setLabelProvider(labelProvider);
		ColumnViewerToolTipSupport.enableFor(tableViewer);
		tableViewer.setInput(input);

		// Listeners
		tableViewer.addSelectionChangedListener(selectionChangedListener);
		tableViewer.getTable().addKeyListener(keyListener);
		tableViewer.getTable().addMouseTrackListener(mouseTrackListener);

		// Open
		shell.setLocation(location);
		shell.pack();
		shell.open();


	}

	/**
	 * @since 2.0
	 */
	public void refresh() {
		tableViewer.refresh();
		shell.pack();
	}

	public void dispose() {
		if (tableViewer != null) {
			tableViewer.removeSelectionChangedListener(selectionChangedListener);
		}

		if (tableViewer.getTable() != null) {
			tableViewer.getTable().removeKeyListener(keyListener);
		}

		if (shell != null) {
			shell.dispose();
		}
	}

	public void setContentProvider(IStructuredContentProvider provider) {
		this.contentProvider = provider;
	}

	public void setInput(Object input) {
		this.input = input;
	}

	public void setLabelProvider(ILabelProvider labelProvider) {
		this.labelProvider = labelProvider;
	}

	/**
	 * @since 1.2
	 */
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		this.selectionChangedListeners.add(listener);
	}

	/**
	 * @since 2.0
	 */
	public void addKeyListener(KeyListener listener) {
		this.keyListeners.add(listener);
	}

	/**
	 * @since 2.0
	 */
	public void addMouseTrackListener(MouseTrackListener listener) {
		this.mouseTrackListeners.add(listener);
	}

	/**
	 * @since 2.0
	 */
	public Shell getShell() {
		return shell;
	}

	/**
	 * @since 2.0
	 */
	public void setShell(Shell shell) {
		this.shell = shell;
	}

	/**
	 * @since 2.0
	 */
	public Shell getParentShell() {
		return parentShell;
	}

	/**
	 * @since 2.0
	 */
	public TableViewer getTableViewer() {
		return tableViewer;
	}
}

Back to the top