Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 80f676ba1cc8c1e520a43adc5b892358805df197 (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
/*****************************************************************************
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.properties.tabbed.customization.dialog.actions;

import java.util.ArrayList;

import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.papyrus.properties.runtime.controller.descriptor.IPropertyEditorControllerDescriptor;
import org.eclipse.papyrus.properties.runtime.view.FragmentDescriptorState;
import org.eclipse.papyrus.properties.runtime.view.content.ContainerDescriptor;
import org.eclipse.papyrus.properties.runtime.view.content.ContainerDescriptorState;
import org.eclipse.papyrus.properties.runtime.view.content.ExpandableContainerDescriptor;
import org.eclipse.papyrus.properties.runtime.view.content.GridLayoutDescriptor;
import org.eclipse.papyrus.properties.runtime.view.content.GroupContainerDescriptor;
import org.eclipse.papyrus.properties.tabbed.core.view.SectionDescriptorState;
import org.eclipse.papyrus.properties.tabbed.core.view.SectionSetDescriptorState;
import org.eclipse.papyrus.properties.tabbed.customization.Activator;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Stereotype;


/**
 * Menu creator for {@link FragmentDescriptorState}
 */
public class FragmentMenuCreator extends AbstractMenuCreator {

	/** element on which the menu should be created */
	private final FragmentDescriptorState fragmentDescriptorState;

	/** menu manager used to create elements */
	private MenuManager manager;

	/**
	 * Creates a new FragmentMenuCreator.
	 * 
	 * @param fragmentDescriptorState
	 *        the state on which this menu is created
	 */
	public FragmentMenuCreator(FragmentDescriptorState fragmentDescriptorState, SectionSetDescriptorState sectionSetDescriptorState, EClassifier currentMetaclass, Stereotype currentStereotype) {
		super(sectionSetDescriptorState, currentMetaclass, currentStereotype);
		this.fragmentDescriptorState = fragmentDescriptorState;
	}

	/**
	 * {@inheritDoc}
	 */
	public Menu getMenu(final Control parent) {
		if(manager == null) {
			manager = new MenuManager();
		}
		Menu menu = manager.getMenu();
		if(menu != null) {
			menu.dispose();
			menu = null;
		}
		manager.removeAll();
		menu = manager.createContextMenu(parent);
		IAction removeAction = new Action("Remove Fragment", Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/delete.gif")) {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void run() {
				// remove this section descriptor state from its parent
				if(parent instanceof Tree) {
					TreeItem[] selectedItems = ((Tree)parent).getSelection();
					if(selectedItems.length < 1) {
						Activator.log.warn("Impossible to find the current selection in the tree");
						return;
					}
					TreeItem selectedItem = selectedItems[0];
					TreeItem parentItem = selectedItem.getParentItem();
					// parent item should be the section descriptor set 
					if(parentItem == null) {
						Activator.log.warn("Impossible to find the parent for current selection in the tree ");
						return;
					}

					Object parent = parentItem.getData();
					// test the parent is a SectionDescriptorState
					if((parent instanceof SectionDescriptorState)) {
						((SectionDescriptorState)parent).removeFragmentDescriptorState(fragmentDescriptorState);
					}
				}
			}

		};
		manager.add(removeAction);
		manager.add(new Separator(ADD_GROUP));

		IAction addSimpleContainerAction = new Action("Add Simple Container", Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/NewSimpleContainer.gif")) {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void run() {
				// adds a simple container to the current element
				ContainerDescriptor containerDescriptor = new ContainerDescriptor(new GridLayoutDescriptor(), new ArrayList<IPropertyEditorControllerDescriptor>());
				ContainerDescriptorState containerDescriptorState = containerDescriptor.createState(false);
				fragmentDescriptorState.addContainerDescriptorState(containerDescriptorState);
				// try to retrieve the selection
				if(parent instanceof Tree) {
					TreeItem[] parentItems = ((Tree)parent).getSelection();
					if(parentItems.length < 1) {
						return;
					}
					parentItems[0].setExpanded(true);
					for(TreeItem child : parentItems[0].getItems()) {
						if(containerDescriptorState.equals(child.getData())) {
							((Tree)parent).select(child);
							return;
						}
					}
				}
			}

		};
		manager.appendToGroup(ADD_GROUP, addSimpleContainerAction);

		IAction addExpandableContainerAction = new Action("Add Expandable Container", Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/NewExpandableContainer.gif")) {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void run() {
				// adds an expandable container to the current element
				ExpandableContainerDescriptor containerDescriptor = new ExpandableContainerDescriptor(new GridLayoutDescriptor(), "Label", new ArrayList<IPropertyEditorControllerDescriptor>());
				ContainerDescriptorState containerDescriptorState = containerDescriptor.createState(false);
				fragmentDescriptorState.addContainerDescriptorState(containerDescriptorState);
				// try to retrieve the selection
				if(parent instanceof Tree) {
					TreeItem[] parentItems = ((Tree)parent).getSelection();
					if(parentItems.length < 1) {
						return;
					}

					parentItems[0].setExpanded(true);
					for(TreeItem child : parentItems[0].getItems()) {
						if(containerDescriptorState.equals(child.getData())) {
							((Tree)parent).select(child);
							return;
						}
					}
				}
			}

		};
		manager.appendToGroup(ADD_GROUP, addExpandableContainerAction);

		IAction addGroupContainerAction = new Action("Add Group Container", Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/NewGroupContainer.gif")) {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void run() {
				// adds an expandable container to the current element
				GroupContainerDescriptor containerDescriptor = new GroupContainerDescriptor(new GridLayoutDescriptor(), "Label", new ArrayList<IPropertyEditorControllerDescriptor>());
				ContainerDescriptorState containerDescriptorState = containerDescriptor.createState(false);
				fragmentDescriptorState.addContainerDescriptorState(containerDescriptorState);

				// try to retrieve the selection
				if(parent instanceof Tree) {
					TreeItem[] parentItems = ((Tree)parent).getSelection();
					if(parentItems.length < 1) {
						return;
					}
					parentItems[0].setExpanded(true);
					for(TreeItem child : parentItems[0].getItems()) {
						if(containerDescriptorState.equals(child.getData())) {
							((Tree)parent).select(child);
							return;
						}
					}
				}
			}
		};
		manager.appendToGroup(ADD_GROUP, addGroupContainerAction);


		return menu;
	}

	/**
	 * {@inheritDoc}
	 */
	public void dispose() {
		if(manager != null) {
			Menu menu = manager.getMenu();
			if(menu != null) {
				menu.dispose();
				menu = null;
			}
		}
	}
}

Back to the top