Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 75f7a643a08bfd54d62ff07d5ef9075cbf83f866 (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
/**
 * 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.papyrus.emf.facet.util.ui.internal.exported.util.tree.menu;

import java.util.Map;

import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.tree.item.AbstractTreeItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * Abstract class providing the creation of any item into the menu.
 *
 * @see AbstractTreeMenuItem
 * @see AbstractTreeSubMenu
 * @since 0.3
 */
public abstract class AbstractTreeMenu<T extends Object> {

	private ExtendedTreeMenu<T> treeMenu;
	private MenuItem menuItem;
	private boolean menuItemCreated;
	private Map<String, Object> properties;

	/**
	 * Refresh the the menu according to the actual context.
	 */
	public void refresh(final ExtendedTreeMenu<T> menu) {
		this.treeMenu = menu;
		refreshMenu(this.treeMenu.getMenu());
	}

	/**
	 * Refresh the the menu according to the actual context.
	 */
	public void refresh(final ExtendedTreeMenu<T> menuTree, final Menu menu) {
		this.treeMenu = menuTree;
		refreshMenu(menu);
	}

	/**
	 * Refresh the the menu according to the actual context.
	 */
	private void refreshMenu(final Menu menu) {
		if (isEnabled()) {
			if (this.isMenuItemCreated()) {
				deleteMenuItem();
			}
			this.setMenuItemCreated(true);
			this.menuItem = createMenuItem(menu);
		} else {
			deleteMenuItem();
			this.setMenuItemCreated(false);
		}
	}

	/**
	 * Return if the menu item has to be enabled or not (by checking if the
	 * associated AbstractTreeItem is enabled or not).
	 *
	 * @return if the menu item has to be enabled or not.
	 */
	public boolean isEnabled(final ExtendedTreeMenu<T> menuTree) {
		this.treeMenu = menuTree;
		return isEnabled();
	}

	/**
	 * Return if the menu item has to be enabled or not (by checking if the
	 * associated AbstractTreeItem is enabled or not).
	 *
	 * @return if the menu item has to be enabled or not.
	 */
	public abstract boolean isEnabled();

	/**
	 * Create the concrete item. Has to be overrided by the classes extending
	 * this abstract class.
	 *
	 * @param parent
	 *            the parent of the item.
	 * @return the item created.
	 */
	protected MenuItem createMenuItem(final Menu parent) {
		final MenuItem item = new MenuItem(parent, SWT.NONE);
		item.setText(getMenuName());
		item.addSelectionListener(new SelectionListener() {

			public void widgetSelected(final SelectionEvent selectionEvent) {
				onMenuItemSelection();
			}

			public void widgetDefaultSelected(
					final SelectionEvent selectionEvent) {
				// Nothing.
			}
		});
		return item;
	}

	/**
	 * @return the menu item name.
	 */
	public abstract String getMenuName();

	/**
	 * This method is called when the menu item is selected.
	 *
	 * @return can return an instance of T.
	 */
	public abstract T onMenuItemSelection();

	/**
	 * Delete the item of the menu.
	 */
	public void deleteMenuItem() {
		if (this.isMenuItemCreated() && (this.menuItem != null)
				&& !this.menuItem.isDisposed()) {
			if (this.menuItem.getMenu() != null) {
				this.menuItem.getMenu().dispose();
			}
			this.menuItem.dispose();
		}
	}

	/**
	 * @return the treeMenu of the tree.
	 */
	public ExtendedTreeMenu<T> getTreeMenu() {
		return this.treeMenu;
	}

	/**
	 * Set the properties to pass to the new items.
	 *
	 * @param extraProperties
	 *            the properties to pass.
	 *
	 */
	public void setExtraProperties(final Map<String, Object> extraProperties) {
		this.properties = extraProperties;
	}

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

	/**
	 * @return the properties.
	 */
	public Map<String, Object> getProperties() {
		return this.properties;
	}

	/**
	 * @return true if the menu for this concrete item is created or not.
	 */
	public boolean isMenuItemCreated() {
		return this.menuItemCreated;
	}

	/**
	 * @param menuItemCreated
	 *            the menuItemCreated to set.
	 */
	public void setMenuItemCreated(final boolean menuItemCreated) {
		this.menuItemCreated = menuItemCreated;
	}

	/**
	 * Return the {@link AbstractTreeItem} according to the {@link TreeItem}.
	 *
	 * @param treeItem
	 *            the treeItem related.
	 * @return the {@link AbstractTreeItem} related to the <code>treeItem</code> .
	 */
	public abstract AbstractTreeItem<T> getTreeItemExtended(
			final TreeItem treeItem);

	/**
	 * Remove the item in parameter of the tree.
	 *
	 * @param item
	 *            the item to remove.
	 */
	public abstract boolean removeItem(final TreeItem item);
}

Back to the top