1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.pcmbench.navigator; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collection; |
8 | import java.util.Collections; |
9 | import java.util.Iterator; |
10 | |
11 | import org.eclipse.emf.common.command.UnexecutableCommand; |
12 | import org.eclipse.emf.ecore.EObject; |
13 | import org.eclipse.emf.edit.command.CommandActionDelegate; |
14 | import org.eclipse.emf.edit.ui.action.CreateChildAction; |
15 | import org.eclipse.emf.edit.ui.action.CreateSiblingAction; |
16 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
17 | import org.eclipse.jface.action.IAction; |
18 | import org.eclipse.jface.action.IContributionManager; |
19 | import org.eclipse.jface.action.IMenuManager; |
20 | import org.eclipse.jface.action.MenuManager; |
21 | import org.eclipse.jface.resource.ImageDescriptor; |
22 | import org.eclipse.jface.viewers.ISelection; |
23 | import org.eclipse.jface.viewers.IStructuredSelection; |
24 | import org.eclipse.ui.IActionBars; |
25 | import org.eclipse.ui.IWorkbenchWindow; |
26 | import org.eclipse.ui.navigator.CommonActionProvider; |
27 | import org.eclipse.ui.navigator.ICommonActionConstants; |
28 | import org.eclipse.ui.navigator.ICommonActionExtensionSite; |
29 | import org.eclipse.ui.navigator.ICommonMenuConstants; |
30 | import org.eclipse.ui.navigator.ICommonViewerSite; |
31 | import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite; |
32 | |
33 | import de.uka.ipd.sdq.pcm.seff.ResourceDemandingSEFF; |
34 | import de.uka.ipd.sdq.pcmbench.ui.provider.categoryaware.GenericCategoryItemProvider; |
35 | |
36 | /** |
37 | * @author Snowball |
38 | * This class is OBSOLETE and only left for demonstration purposes. |
39 | */ |
40 | public class ChildAddMenuProvider extends CommonActionProvider { |
41 | |
42 | private Collection createChildActions; |
43 | private Collection createSiblingActions; |
44 | private IWorkbenchWindow workbenchWindow; |
45 | |
46 | /** |
47 | * |
48 | */ |
49 | public ChildAddMenuProvider() { |
50 | // TODO Auto-generated constructor stub |
51 | } |
52 | |
53 | /* (non-Javadoc) |
54 | * @see org.eclipse.ui.navigator.CommonActionProvider#init(org.eclipse.ui.navigator.ICommonActionExtensionSite) |
55 | */ |
56 | @Override |
57 | public void init(ICommonActionExtensionSite aSite) { |
58 | // TODO Auto-generated method stub |
59 | super.init(aSite); |
60 | ICommonViewerSite viewSite = aSite.getViewSite(); |
61 | if (viewSite instanceof ICommonViewerWorkbenchSite) |
62 | { |
63 | ICommonViewerWorkbenchSite workbench = (ICommonViewerWorkbenchSite)viewSite; |
64 | workbenchWindow = workbench.getWorkbenchWindow(); |
65 | // seffAction = new OpenSEFFDiagramAction( |
66 | // workbenchWindow.getActivePage(), workbench.getSelectionProvider()); |
67 | // seffAction.init(workbenchWindow); |
68 | } |
69 | } |
70 | |
71 | /* (non-Javadoc) |
72 | * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager) |
73 | */ |
74 | @SuppressWarnings("unchecked") |
75 | @Override |
76 | public void fillContextMenu(IMenuManager menu) { |
77 | super.fillContextMenu(menu); |
78 | |
79 | // Query the new selection for appropriate new child/sibling descriptors |
80 | // |
81 | final TransactionalEditingDomain domain = null; |
82 | // TransactionalEditingDomain.Registry.INSTANCE.getEditingDomain(EditingDomainFactory.EDITING_DOMAIN_ID); |
83 | |
84 | Collection newChildDescriptors = null; |
85 | Collection newSiblingDescriptors = null; |
86 | |
87 | ISelection selection = this.getContext().getSelection(); |
88 | if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) { |
89 | Object object = ((IStructuredSelection)selection).getFirstElement(); |
90 | |
91 | newChildDescriptors = domain.getNewChildDescriptors(object, null); |
92 | newSiblingDescriptors = domain.getNewChildDescriptors(null, object); |
93 | |
94 | if (object instanceof ResourceDemandingSEFF) |
95 | { |
96 | // menu.add(seffAction); |
97 | } |
98 | } |
99 | |
100 | // Generate actions for selection; populate and redraw the menus. |
101 | // |
102 | // createChildActions = generateCreateChildActions(newChildDescriptors, selection); |
103 | createChildActions = new ArrayList(); |
104 | if (newChildDescriptors != null) { |
105 | for (Iterator i = newChildDescriptors.iterator(); i.hasNext(); ) { |
106 | createChildActions.add(new CreateChildAction(workbenchWindow.getActivePage().getActivePart(), selection, i.next()) |
107 | { |
108 | |
109 | /* (non-Javadoc) |
110 | * @see org.eclipse.emf.edit.ui.action.StaticSelectionCommandAction#configureAction(org.eclipse.jface.viewers.ISelection) |
111 | */ |
112 | @Override |
113 | public void configureAction(ISelection selection) { |
114 | editingDomain = domain; |
115 | // convert the selection to a collection of the selected objects |
116 | IStructuredSelection sselection = (IStructuredSelection) selection; |
117 | Collection collection = new ArrayList(sselection.size()); |
118 | for (Iterator i = sselection.iterator(); i.hasNext(); ) |
119 | { |
120 | Object selectedObject = i.next(); |
121 | if (selectedObject instanceof GenericCategoryItemProvider) |
122 | selectedObject = ((GenericCategoryItemProvider)selectedObject).getParent(selectedObject); |
123 | collection.add(selectedObject); |
124 | } |
125 | |
126 | // if we found an editing domain, create command |
127 | if (editingDomain != null) |
128 | { |
129 | command = createActionCommand(editingDomain, collection); |
130 | setEnabled(command.canExecute()); |
131 | } |
132 | |
133 | // give up if we couldn't create the command; otherwise, use a |
134 | // CommandActionDelegate to set the action's text, tooltip, icon, |
135 | // etc. or just use the default icon |
136 | if (command == null || command == UnexecutableCommand.INSTANCE) |
137 | { |
138 | disable(); |
139 | } |
140 | else if (!(command instanceof CommandActionDelegate)) |
141 | { |
142 | if (getDefaultImageDescriptor() != null) |
143 | { |
144 | setImageDescriptor(getDefaultImageDescriptor()); |
145 | } |
146 | } |
147 | else |
148 | { |
149 | CommandActionDelegate commandActionDelegate = |
150 | (CommandActionDelegate) command; |
151 | |
152 | ImageDescriptor imageDescriptor = |
153 | objectToImageDescriptor(commandActionDelegate.getImage()); |
154 | if (imageDescriptor != null) |
155 | { |
156 | setImageDescriptor(imageDescriptor); |
157 | } |
158 | else if (getDefaultImageDescriptor() != null) |
159 | { |
160 | setImageDescriptor(getDefaultImageDescriptor()); |
161 | } |
162 | |
163 | if (commandActionDelegate.getText() != null) |
164 | { |
165 | setText(commandActionDelegate.getText()); |
166 | } |
167 | |
168 | if (commandActionDelegate.getDescription() != null) |
169 | { |
170 | setDescription(commandActionDelegate.getDescription()); |
171 | } |
172 | |
173 | if (commandActionDelegate.getToolTipText() != null) |
174 | { |
175 | setToolTipText(commandActionDelegate.getToolTipText()); |
176 | } |
177 | } |
178 | } |
179 | }); |
180 | } |
181 | } |
182 | |
183 | // Generate actions for selection; populate and redraw the menus. |
184 | // |
185 | // createChildActions = generateCreateChildActions(newChildDescriptors, selection); |
186 | if (newSiblingDescriptors != null && ((IStructuredSelection)selection).getFirstElement() instanceof EObject) |
187 | { |
188 | createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection); |
189 | } |
190 | else |
191 | createSiblingActions = Collections.EMPTY_SET; |
192 | |
193 | MenuManager childSubmenu = new MenuManager("Child"); |
194 | if (childSubmenu != null) { |
195 | populateManager(childSubmenu, createChildActions, null); |
196 | childSubmenu.update(true); |
197 | } |
198 | MenuManager siblingSubmenu = new MenuManager("Sibling"); |
199 | if (siblingSubmenu != null) { |
200 | populateManager(siblingSubmenu, createSiblingActions, null); |
201 | siblingSubmenu.update(true); |
202 | } |
203 | |
204 | // OpenRepositoryAction ora = new OpenRepositoryAction(); |
205 | // ora.init(workbenchWindow); |
206 | // |
207 | // menu.add(ora); |
208 | // menu.insertBefore(ICommonMenuConstants.GROUP_EDIT, |
209 | // childSubmenu); |
210 | // menu.insertBefore(ICommonMenuConstants.GROUP_EDIT, |
211 | // siblingSubmenu); |
212 | } |
213 | |
214 | /** |
215 | * This generates a {@link org.eclipse.emf.edit.ui.action.CreateChildAction} for each object in <code>descriptors</code>, |
216 | * and returns the collection of these actions. |
217 | * <!-- begin-user-doc --> |
218 | * <!-- end-user-doc --> |
219 | * @generated |
220 | */ |
221 | protected Collection generateCreateChildActions(Collection descriptors, ISelection selection) { |
222 | Collection actions = new ArrayList(); |
223 | if (descriptors != null) { |
224 | for (Iterator i = descriptors.iterator(); i.hasNext(); ) { |
225 | actions.add(new CreateChildAction(workbenchWindow.getActivePage().getActivePart(), selection, i.next())); |
226 | } |
227 | } |
228 | return actions; |
229 | } |
230 | |
231 | /** |
232 | * This generates a {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} for each object in <code>descriptors</code>, |
233 | * and returns the collection of these actions. |
234 | * <!-- begin-user-doc --> |
235 | * <!-- end-user-doc --> |
236 | * @generated |
237 | */ |
238 | protected Collection generateCreateSiblingActions(Collection descriptors, ISelection selection) { |
239 | Collection actions = new ArrayList(); |
240 | if (descriptors != null) { |
241 | for (Iterator i = descriptors.iterator(); i.hasNext(); ) { |
242 | actions.add(new CreateSiblingAction(workbenchWindow.getActivePage().getActivePart(), selection, i.next())); |
243 | } |
244 | } |
245 | return actions; |
246 | } |
247 | |
248 | /** |
249 | * This populates the specified <code>manager</code> with {@link org.eclipse.jface.action.ActionContributionItem}s |
250 | * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection, |
251 | * by inserting them before the specified contribution item <code>contributionID</code>. |
252 | * If <code>ID</code> is <code>null</code>, they are simply added. |
253 | * <!-- begin-user-doc --> |
254 | * <!-- end-user-doc --> |
255 | * @generated |
256 | */ |
257 | protected void populateManager(IContributionManager manager, Collection actions, String contributionID) { |
258 | if (actions != null) { |
259 | for (Iterator i = actions.iterator(); i.hasNext(); ) { |
260 | IAction action = (IAction)i.next(); |
261 | if (contributionID != null) { |
262 | manager.insertBefore(contributionID, action); |
263 | } |
264 | else { |
265 | manager.add(action); |
266 | } |
267 | } |
268 | } |
269 | } |
270 | |
271 | /* (non-Javadoc) |
272 | * @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars) |
273 | */ |
274 | @Override |
275 | public void fillActionBars(IActionBars actionBars) { |
276 | // // TODO Auto-generated method stub |
277 | // super.fillActionBars(actionBars); |
278 | // if (this.seffAction.isEnabled()) |
279 | // actionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, seffAction); |
280 | } |
281 | |
282 | } |