Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1be61eb48cc46c61f87744aba29f09a86773b470 (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
/**
 * 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 org.eclipse.emf.facet.util.core.Logger;
import org.eclipse.emf.facet.util.ui.internal.Activator;
import org.eclipse.emf.facet.util.ui.internal.exported.util.tree.item.AbstractTreeItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * Abstract class for the tree menu item. Each Menu Item had an handler which
 * provide the methods: <li>{@link #isEnabled()} : return if the menu item has
 * to be displayed.</li> <li>{@link #onMenuItemSelection()} : this method will
 * be execute when the item is selected.</li> <li>
 * {@link #getAssociatedTreeItemClass()} : links this class with an
 * {@link AbstractTreeItem}. This item will be instantiate when the user select
 * this element on the tree menu.</li>
 * 
 * @see AbstractTreeItem
 * @see AbstractTreeMenu
 * @since 0.3
 */
public abstract class AbstractTreeMenuItem<T extends Object> extends
		AbstractTreeMenu<T> {

	private final List<AbstractTreeItem<T>> items;

	/**
	 * Constructor.
	 */
	public AbstractTreeMenuItem() {
		super();
		this.items = new LinkedList<AbstractTreeItem<T>>();
	}

	/**
	 * This method creates the associated item and set the extra properties to
	 * it.
	 */
	@Override
	public T onMenuItemSelection() {
		AbstractTreeItem<T> item = null;
		if (getCurrentItemSelected() == null) {
			item = createItem(getTree());
		} else {
			item = createItem(getCurrentItemSelected());
		}
		this.getItems().add(item);
		item.setExtraProperties(getProperties());
		fireChanged();
		return item.onItemCreation();
	}

	/**
	 * Create a new item {@link TreeItem} into the parent.
	 * 
	 * @param parent
	 *            the parent of the item to create.
	 */
	public AbstractTreeItem<T> createItem(final Tree parent) {
		return createTreeItem(parent);
	}

	/**
	 * Create a new item {@link TreeItem} into the parent.
	 * 
	 * @param parent
	 *            the parent of the item to create.
	 */
	public AbstractTreeItem<T> createItem(final TreeItem parent) {
		return createTreeItem(parent);
	}

	/**
	 * Create a new item {@link TreeItem} into the parent.
	 * 
	 * @param parent
	 *            the parent of the item to create.
	 */
	protected AbstractTreeItem<T> createTreeItem(final Object parent) {
		AbstractTreeItem<T> newTreeItem = null;
		try {
			newTreeItem = getAssociatedTreeItemClass().newInstance();
			if (parent instanceof Tree) {
				newTreeItem.createItem(this, (Tree) parent);
			} else if (parent instanceof TreeItem) {
				newTreeItem.createItem(this, (TreeItem) parent);
			}
		} catch (final SecurityException e) {
			Logger.logError(e, Activator.getDefault());
		} catch (final IllegalArgumentException e) {
			Logger.logError(e, Activator.getDefault());
		} catch (final InstantiationException e) {
			Logger.logError(e, Activator.getDefault());
		} catch (final IllegalAccessException e) {
			Logger.logError(e, Activator.getDefault());
		}

		return newTreeItem;
	}

	/**
	 * Find the treeItem searching into the all tree.
	 * 
	 * @param treeItem
	 *            the treeItem to find.
	 * @return the corresponding {@link AbstractTreeItem}. Null if not.
	 */
	public AbstractTreeItem<T> findTreeItemExtended(final TreeItem treeItem) {
		return this.getTreeMenu().getTreeItemExtended(treeItem);
	}

	/**
	 * This method only find the item into the corresponding element of this
	 * menu item. To find an element searching it into the all tree, use the
	 * method {@link #findTreeItemExtended(TreeItem)}.
	 */
	@Override
	public AbstractTreeItem<T> getTreeItemExtended(final TreeItem item) {
		AbstractTreeItem<T> result = null;
		for (final AbstractTreeItem<T> itemExtended : this.getItems()) {
			if (itemExtended.getTreeItem().equals(item)) {
				result = itemExtended;
				break;
			}
		}
		return result;
	}

	@Override
	public boolean removeItem(final TreeItem item) {
		boolean removed = false;
		final AbstractTreeItem<T> itemExtended = findTreeItemExtended(item);
		if (itemExtended != null) {
			this.getItems().remove(itemExtended);
			itemExtended.removeItem();
			removed = true;
			fireChanged();
		}
		return removed;
	}

	/**
	 * @return get the current selection of the tree.
	 */
	public TreeItem getCurrentItemSelected() {
		return getTreeMenu().getCurrentItemSelected();
	}

	/**
	 * Remove the last item created.
	 */
	protected void removeLastItem() {
		// The list created is a LinkedList<AbstractTreeItem>
		((LinkedList<AbstractTreeItem<T>>) this.getItems()).getLast()
				.removeItem();
		fireChanged();
	}

	/**
	 * Set a new text to the last item created.
	 * 
	 * @param newText
	 *            the new text to set.
	 */
	protected void setLastItemText(final String newText) {
		// The list created is a LinkedList<AbstractTreeItem>
		((LinkedList<AbstractTreeItem<T>>) this.getItems()).getLast()
				.setTreeItemText(newText);
		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() {
		final ExtendedTreeMenu<T> treeMenu = getTreeMenu();
		if (treeMenu != null) {
			treeMenu.fireChanged();
		}
	}

	/**
	 * Return the class associated with this menu item. When this menu item will
	 * be selected, a new instance of this associated {@link AbstractTreeItem}
	 * will be created.
	 * 
	 * @return the associated class.
	 */
	public abstract Class<? extends AbstractTreeItem<T>> getAssociatedTreeItemClass();


	/**
	 * @return the items created by this menu item.
	 */
	public List<AbstractTreeItem<T>> getItems() {
		return this.items;
	}
}

Back to the top