Skip to main content
summaryrefslogtreecommitdiffstats
blob: aebaed0ca520dfd9fc956f53f5c57bb6c5a8fbc4 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
 * Copyright (c) 2012 Mia-Software.
 *  
 * 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:
 *  	Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 */
package org.eclipse.emf.facet.util.ui.internal.exported.util.tree.menu;

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

import org.eclipse.emf.facet.util.ui.internal.exported.tree.menu.ITreeMenu;
import org.eclipse.emf.facet.util.ui.internal.exported.util.tree.ExtendedTree;
import org.eclipse.emf.facet.util.ui.internal.exported.util.tree.item.AbstractTreeItem;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * This class provide the creation of the menu {@link Menu} of the tree (
 * {@link #createMenu(Control, ExtendedTree)}. The menu add a list of
 * {@link AbstractTreeItem} and for each an {@link AbstractTreeItem} which will
 * be instantiate when the menu item is selected.
 * 
 * @since 0.3
 */
public class ExtendedTreeMenu<IDialog> implements ITreeMenu<IDialog> {

	private final Control parent;
	private final ExtendedTree treeExtended;
	private final List<AbstractTreeMenu<IDialog>> menuItems;
	private Menu menu;

	/**
	 * Constructor.
	 * 
	 * @param parent
	 *            the parent of the menu.
	 * @param treeExtended
	 *            the tree to which the menu will be linked.
	 * @param menuItems
	 */
	public ExtendedTreeMenu(final Control parent, final ExtendedTree treeExtended,
			final List<AbstractTreeMenu<IDialog>> menuItems) {
		this.parent = parent;
		this.treeExtended = treeExtended;
		this.menuItems = menuItems;
	}

	/**
	 * This method creates the tree menu, add put the listener into the menu.
	 * 
	 * @param parent
	 *            the parent of the menu.
	 * @param treeExtended
	 *            the associated {@link ExtendedTree} of the menu.
	 */
	public void createMenu() {
		this.menu = new Menu(this.parent);
		this.menu.addMenuListener(new MenuListener() {

			public void menuShown(final MenuEvent menuEvent) {
				onMenuShown();
			}

			public void menuHidden(final MenuEvent menuEvent) {
				// Nothing.
			}
		});
	}

	/**
	 * This method is called when the menu is displayed. It calls all the
	 * {@link AbstractTreeMenu#refresh(TreeMenu)} method of each element of the
	 * menu.
	 */
	public void onMenuShown() {
		for (final AbstractTreeMenu<IDialog> menuItem : this.getMenuItems()) {
			menuItem.refresh(this);
		}
	}

	/**
	 * Return the list of all the items of this menu.
	 * 
	 * @return the list of the {@link AbstractTreeMenu} of this menu.
	 */
	public List<AbstractTreeMenu<IDialog>> getTreeMenuItems() {
		return this.getMenuItems();
	}

	/**
	 * Return the {@link Menu} of this TreeMenu.
	 * 
	 * @return the menu.
	 */
	public Menu getMenu() {
		return this.menu;
	}

	/**
	 * Put this <code>properties</code> to each items created.
	 * 
	 * @param properties
	 *            the list of properties to pass.
	 */
	public void putExtraProperties(final Map<String, Object> properties) {
		for (final AbstractTreeMenu<IDialog> menuItem : this.getMenuItems()) {
			menuItem.setExtraProperties(properties);
		}
	}

	/**
	 * @return the current {@link TreeItem} selected on the tree.
	 */
	public TreeItem getCurrentItemSelected() {
		TreeItem result = null;
		final TreeItem[] items = this.treeExtended.getTree().getSelection();
		if (items.length > 0) {
			result = this.treeExtended.getTree().getSelection()[0];
		}
		return result;
	}

	/**
	 * @return the {@link AbstractTreeItem} selected on the tree. Can be null.
	 */
	public AbstractTreeItem<IDialog> getTreeItemExtended(final TreeItem treeItem) {
		AbstractTreeItem<IDialog> result = null;
		for (final AbstractTreeMenu<IDialog> menuItem : this.getMenuItems()) {
			final AbstractTreeItem<IDialog> itemExtended = menuItem
					.getTreeItemExtended(treeItem);
			if (itemExtended != null) {
				result = itemExtended;
				break;
			}
		}
		return result;
	}

	/**
	 * @return the {@link AbstractTreeMenu} selected on the tree. Can be null.
	 */
	public AbstractTreeMenu<IDialog> getTreeMenu(final TreeItem treeItem) {
		AbstractTreeMenu<IDialog> result = null;

		for (final AbstractTreeMenu<IDialog> menuItem : this.getMenuItems()) {
			final AbstractTreeItem<IDialog> itemExtended = menuItem
					.getTreeItemExtended(treeItem);
			if (itemExtended != null) {
				result = menuItem;
				break;
			}
		}
		return result;
	}

	/**
	 * Remove the current item selected of the tree.
	 */
	public void removeCurrentItemSelected() {
		final AbstractTreeMenu<IDialog> treeMenu = getTreeMenu(getCurrentItemSelected());
		if (treeMenu != null) {
			treeMenu.removeItem(getCurrentItemSelected());
		}
	}

	/**
	 * @return the {@link Tree}.
	 */
	public Tree getTree() {
		return this.treeExtended.getTree();
	}

	/**
	 * This method call the method
	 * {@link AbstractTreeItem#onItemMouseSelection()} of the current selection.
	 */
	public void onMouseSelection() {
		final AbstractTreeItem<IDialog> treeMenu = getTreeItemExtended(getCurrentItemSelected());
		if (treeMenu != null) {
			treeMenu.onItemMouseSelection();
			fireChanged();
		}
	}

	/**
	 * When a modification append, this method has to be called and the method
	 * {@link #notifyChanged()} is called for the listener of this Tree.
	 */
	public void fireChanged() {
		this.treeExtended.fireChanged();
	}

	/**
	 * @return the all the items of the menu.
	 */
	private List<AbstractTreeMenu<IDialog>> getMenuItems() {
		return this.menuItems;
	}

	/**
	 * Select the item into the menu (if the item exist).
	 * 
	 * @return can return a {@link IDialog}. Can be null.
	 */
	public IDialog selectMenuItem(
			final Class<? extends AbstractTreeMenuItem<IDialog>> menuItemType) {
		IDialog result = null;
		final AbstractTreeMenu<IDialog> treeMenu = findMenuItem(menuItemType);
		if (treeMenu != null) {
			result = treeMenu.onMenuItemSelection();
		}
		return result;
	}

	/**
	 * Return (if exist) the {@link AbstractTreeMenu} with the name
	 * <code>menuItemName</code> into the items of the tree menu.
	 * 
	 * @param menuItemType
	 *            the name of the element to find.
	 * @return the item menu with the name in parameter (can be null).
	 */
	private AbstractTreeMenuItem<IDialog> findMenuItem(
			final Class<? extends AbstractTreeMenuItem<IDialog>> menuItemType) {
		AbstractTreeMenuItem<IDialog> result = null;
		for (final AbstractTreeMenuItem<IDialog> item : getAllTreeMenuItems()) {
			if (item.getClass().isInstance(menuItemType)) {
				result = item;
			}
		}
		return result;
	}

	private List<AbstractTreeMenuItem<IDialog>> getAllTreeMenuItems() {
		final List<AbstractTreeMenuItem<IDialog>> result = new LinkedList<AbstractTreeMenuItem<IDialog>>();
		for (final AbstractTreeMenu<IDialog> menuItem : getMenuItems()) {
			if (menuItem instanceof AbstractTreeSubMenu) {
				result.addAll(getAllTreeMenuItems((AbstractTreeSubMenu<IDialog>) menuItem));
			} else {
				result.add((AbstractTreeMenuItem<IDialog>) menuItem);
			}
		}
		return result;
	}

	private List<AbstractTreeMenuItem<IDialog>> getAllTreeMenuItems(
			final AbstractTreeSubMenu<IDialog> subMenu) {
		final List<AbstractTreeMenuItem<IDialog>> result = new LinkedList<AbstractTreeMenuItem<IDialog>>();
		for (final AbstractTreeMenu<IDialog> menuItem : subMenu.getItems()) {
			if (menuItem instanceof AbstractTreeSubMenu) {
				result.addAll(getAllTreeMenuItems((AbstractTreeSubMenu<IDialog>) menuItem));
			} else {
				result.add((AbstractTreeMenuItem<IDialog>) menuItem);
			}
		}
		return result;
	}

	/**
	 * @return the treeMenuItem into the menu with is an instance of the type in
	 *         parameter.
	 */
	public AbstractTreeMenu<IDialog> getTreeMenuItem(final Class<?> treeMenuType) {
		AbstractTreeMenu<IDialog> result = null;
		final List<AbstractTreeMenu<IDialog>> treeMenuItems = getTreeMenuItems();
		for (final AbstractTreeMenu<IDialog> item : treeMenuItems) {
			result = getTreeMenuItem(item, treeMenuType);
			if (result != null) {
				break;
			}
		}
		return result;
	}

	private AbstractTreeMenu<IDialog> getTreeMenuItem(
			final AbstractTreeSubMenu<IDialog> item,
			final Class<?> treeMenuType) {
		AbstractTreeMenu<IDialog> result = null;
		for (final AbstractTreeMenu<IDialog> subItem : item.getItems()) {
			result = getTreeMenuItem(subItem, treeMenuType);
			if (result != null) {
				break;
			}
		}
		return result;
	}

	private AbstractTreeMenu<IDialog> getTreeMenuItem(
			final AbstractTreeMenu<IDialog> item,
			final Class<?> treeMenuType) {
		AbstractTreeMenu<IDialog> result = null;
		if (treeMenuType.isInstance(item)) {
			result = item;
		} else if (item instanceof AbstractTreeSubMenu) {
			result = getTreeMenuItem((AbstractTreeSubMenu<IDialog>) item,
					treeMenuType);
		}
		return result;
	}

	/**
	 * get all the items of the tree with the name <code>treeItemName</code>
	 * 
	 * @param treeItemName
	 *            the name of the item to select.
	 * @return the list of all the tree item with the name in parameter.
	 */
	public List<AbstractTreeItem<IDialog>> getTreeItems(
			final String treeItemName) {
		final List<AbstractTreeItem<IDialog>> result = new LinkedList<AbstractTreeItem<IDialog>>();
		for (final AbstractTreeMenuItem<IDialog> menuItem : getAllTreeMenuItems()) {
			for (final AbstractTreeItem<IDialog> treeItem : menuItem.getItems()) {
				if (treeItemName.equals(treeItem.getItemText())) {
					result.add(treeItem);
				}
			}
		}
		return result;
	}

	/**
	 * Select a item into the tree.
	 * 
	 * @param treeItem
	 *            the {@link AbstractTreeItem} to select
	 */
	public void selectTreeItem(final AbstractTreeItem<IDialog> treeItem) {
		this.treeExtended.getTree().setSelection(treeItem.getTreeItem());
	}
}

Back to the top