]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/ui/actions/OpenViewActionGroup.java
60d59e51b54bf023c6ba00f4b77b1d0dc75f89a7
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / actions / OpenViewActionGroup.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
12
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.action.MenuManager;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionProvider;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19
20 import org.eclipse.ui.IActionBars;
21 import org.eclipse.ui.IViewPart;
22 import org.eclipse.ui.IWorkbenchCommandConstants;
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.actions.ActionFactory;
27 import org.eclipse.ui.actions.ActionGroup;
28 import org.eclipse.ui.actions.ContributionItemFactory;
29 import org.eclipse.ui.dialogs.PropertyDialogAction;
30 import org.eclipse.ui.keys.IBindingService;
31 import org.eclipse.ui.part.Page;
32
33 import org.eclipse.jdt.ui.IContextMenuConstants;
34
35 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
36 import org.eclipse.jdt.internal.ui.callhierarchy.OpenCallHierarchyAction;
37 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
38
39 /**
40  * Action group that adds actions to open a new JDT view part or an external
41  * viewer to a context menu and the global menu bar.
42  *
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  *
47  * @since 2.0
48  *
49  * @noextend This class is not intended to be subclassed by clients.
50  */
51 public class OpenViewActionGroup extends ActionGroup {
52
53     private boolean fEditorIsOwner;
54         private boolean fIsTypeHiararchyViewerOwner;
55     private boolean fIsCallHiararchyViewerOwner;
56
57         private ISelectionProvider fSelectionProvider;
58
59         private OpenSuperImplementationAction fOpenSuperImplementation;
60         private OpenImplementationAction fOpenImplementation;
61
62         private OpenAttachedJavadocAction fOpenAttachedJavadoc;
63         private OpenTypeHierarchyAction fOpenTypeHierarchy;
64     private OpenCallHierarchyAction fOpenCallHierarchy;
65         private PropertyDialogAction fOpenPropertiesDialog;
66
67         private boolean fShowOpenPropertiesAction= true;
68         private boolean fShowShowInMenu= true;
69
70         /**
71          * Creates a new <code>OpenActionGroup</code>. The group requires
72          * that the selection provided by the page's selection provider is
73          * of type {@link IStructuredSelection}.
74          *
75          * @param page the page that owns this action group
76          */
77         public OpenViewActionGroup(Page page) {
78                 createSiteActions(page.getSite(), null);
79         }
80
81         /**
82          * Creates a new <code>OpenActionGroup</code>. The group requires
83          * that the selection provided by the given selection provider is
84          * of type {@link IStructuredSelection}.
85          *
86          * @param page the page that owns this action group
87          * @param selectionProvider the selection provider used instead of the
88          *  page selection provider.
89          *
90          * @since 3.2
91          */
92         public OpenViewActionGroup(Page page, ISelectionProvider selectionProvider) {
93                 createSiteActions(page.getSite(), selectionProvider);
94         }
95
96         /**
97          * Creates a new <code>OpenActionGroup</code>. The group requires
98          * that the selection provided by the part's selection provider is
99          * of type {@link IStructuredSelection}.
100          *
101          * @param part the view part that owns this action group
102          */
103         public OpenViewActionGroup(IViewPart part) {
104                 this(part, null);
105         }
106
107         /**
108          * Creates a new <code>OpenActionGroup</code>. The group requires
109          * that the selection provided by the given selection provider is of type
110          * {@link IStructuredSelection}.
111          *
112          * @param part the view part that owns this action group
113          * @param selectionProvider the selection provider used instead of the
114          *  page selection provider.
115          *
116          * @since 3.2
117          */
118         public OpenViewActionGroup(IViewPart part, ISelectionProvider selectionProvider) {
119                 createSiteActions(part.getSite(), selectionProvider);
120                 // we do a name check here to avoid class loading.
121                 String partName= part.getClass().getName();
122                 fIsTypeHiararchyViewerOwner= "org.eclipse.jdt.internal.ui.typehierarchy.TypeHierarchyViewPart".equals(partName); //$NON-NLS-1$
123                 fIsCallHiararchyViewerOwner= "org.eclipse.jdt.internal.ui.callhierarchy.CallHierarchyViewPart".equals(partName); //$NON-NLS-1$
124         }
125
126         /**
127          * Creates a new <code>OpenActionGroup</code>. The group requires
128          * that the selection provided by the given selection provider is of type
129          * {@link IStructuredSelection}.
130          *
131          * @param site the site that will own the action group.
132          * @param selectionProvider the selection provider used instead of the
133          *  page selection provider.
134          *
135          * @since 3.2
136          */
137         public OpenViewActionGroup(IWorkbenchSite site, ISelectionProvider selectionProvider) {
138                 createSiteActions(site, selectionProvider);
139         }
140
141         /**
142          * Note: This constructor is for internal use only. Clients should not call this constructor.
143          * @param part the editor part
144          *
145          * @noreference This constructor is not intended to be referenced by clients.
146          */
147         public OpenViewActionGroup(JavaEditor part) {
148                 fEditorIsOwner= true;
149                 fShowShowInMenu= false;
150                 
151                 fOpenImplementation= new OpenImplementationAction(part);
152                 fOpenImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_IMPLEMENTATION);
153                 part.setAction("OpenImplementation", fOpenImplementation); //$NON-NLS-1$
154
155                 fOpenSuperImplementation= new OpenSuperImplementationAction(part);
156                 fOpenSuperImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_SUPER_IMPLEMENTATION);
157                 part.setAction("OpenSuperImplementation", fOpenSuperImplementation); //$NON-NLS-1$
158
159                 fOpenAttachedJavadoc= new OpenAttachedJavadocAction(part);
160                 fOpenAttachedJavadoc.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_ATTACHED_JAVADOC);
161                 part.setAction("OpenAttachedJavadoc", fOpenAttachedJavadoc); //$NON-NLS-1$
162
163                 fOpenTypeHierarchy= new OpenTypeHierarchyAction(part);
164                 fOpenTypeHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY);
165                 part.setAction("OpenTypeHierarchy", fOpenTypeHierarchy); //$NON-NLS-1$
166
167                 fOpenCallHierarchy= new OpenCallHierarchyAction(part);
168                 fOpenCallHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_CALL_HIERARCHY);
169                 part.setAction("OpenCallHierarchy", fOpenCallHierarchy); //$NON-NLS-1$
170
171                 initialize(part.getEditorSite().getSelectionProvider());
172         }
173
174         /**
175          * Specifies if this action group also contains the 'Properties' action ({@link PropertyDialogAction}).
176          * By default, the action is contained in the group.
177          *
178          * @param enable If set, the 'Properties' action is part of this action group
179          * @since 3.3
180          */
181         public void containsOpenPropertiesAction(boolean enable) {
182                 fShowOpenPropertiesAction= enable;
183         }
184
185         /**
186          * Specifies if this action group also contains the 'Show In' menu (See {@link ContributionItemFactory#VIEWS_SHOW_IN}).
187          * By default, the action is  contained in the group except for editors.
188          *
189          * @param enable If set, the 'Show In' menu is part of this action group
190          * @since 3.3
191          */
192         public void containsShowInMenu(boolean enable) {
193                 fShowShowInMenu= enable;
194         }
195
196         private void createSiteActions(IWorkbenchSite site, ISelectionProvider specialProvider) {
197                 fOpenImplementation= new OpenImplementationAction(site);
198                 fOpenImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_IMPLEMENTATION);
199                 fOpenImplementation.setSpecialSelectionProvider(specialProvider);
200                 
201                 fOpenSuperImplementation= new OpenSuperImplementationAction(site);
202                 fOpenSuperImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_SUPER_IMPLEMENTATION);
203                 fOpenSuperImplementation.setSpecialSelectionProvider(specialProvider);
204
205                 fOpenAttachedJavadoc= new OpenAttachedJavadocAction(site);
206                 fOpenAttachedJavadoc.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_ATTACHED_JAVADOC);
207                 fOpenAttachedJavadoc.setSpecialSelectionProvider(specialProvider);
208
209                 fOpenTypeHierarchy= new OpenTypeHierarchyAction(site);
210                 fOpenTypeHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY);
211                 fOpenTypeHierarchy.setSpecialSelectionProvider(specialProvider);
212
213                 fOpenCallHierarchy= new OpenCallHierarchyAction(site);
214                 fOpenCallHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_CALL_HIERARCHY);
215                 fOpenCallHierarchy.setSpecialSelectionProvider(specialProvider);
216
217                 ISelectionProvider provider= specialProvider != null ? specialProvider : site.getSelectionProvider();
218
219                 fOpenPropertiesDialog= new PropertyDialogAction(site, provider);
220                 fOpenPropertiesDialog.setActionDefinitionId(IWorkbenchCommandConstants.FILE_PROPERTIES);
221
222                 initialize(provider);
223         }
224
225         private void initialize(ISelectionProvider provider) {
226                 fSelectionProvider= provider;
227                 ISelection selection= provider.getSelection();
228                 fOpenImplementation.update(selection);
229                 fOpenSuperImplementation.update(selection);
230                 fOpenAttachedJavadoc.update(selection);
231                 fOpenTypeHierarchy.update(selection);
232                 fOpenCallHierarchy.update(selection);
233                 if (!fEditorIsOwner) {
234                         if (fShowOpenPropertiesAction) {
235                                 if (selection instanceof IStructuredSelection) {
236                                         fOpenPropertiesDialog.selectionChanged((IStructuredSelection) selection);
237                                 } else {
238                                         fOpenPropertiesDialog.selectionChanged(selection);
239                                 }
240                         }
241                         provider.addSelectionChangedListener(fOpenImplementation);
242                         provider.addSelectionChangedListener(fOpenSuperImplementation);
243                         provider.addSelectionChangedListener(fOpenAttachedJavadoc);
244                         provider.addSelectionChangedListener(fOpenTypeHierarchy);
245                         provider.addSelectionChangedListener(fOpenCallHierarchy);
246                         // no need to register the open properties dialog action since it registers itself
247                 }
248         }
249
250         /* (non-Javadoc)
251          * Method declared in ActionGroup
252          */
253         @Override
254         public void fillActionBars(IActionBars actionBar) {
255                 super.fillActionBars(actionBar);
256                 setGlobalActionHandlers(actionBar);
257         }
258
259         /* (non-Javadoc)
260          * Method declared in ActionGroup
261          */
262         @Override
263         public void fillContextMenu(IMenuManager menu) {
264                 super.fillContextMenu(menu);
265                 if (!fIsTypeHiararchyViewerOwner)
266                         appendToGroup(menu, fOpenTypeHierarchy);
267         if (!fIsCallHiararchyViewerOwner)
268             appendToGroup(menu, fOpenCallHierarchy);
269
270         if (fShowShowInMenu) {
271                         MenuManager showInSubMenu= new MenuManager(getShowInMenuLabel());
272                         IWorkbenchWindow workbenchWindow= fOpenSuperImplementation.getSite().getWorkbenchWindow();
273                         showInSubMenu.add(ContributionItemFactory.VIEWS_SHOW_IN.create(workbenchWindow));
274                         menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, showInSubMenu);
275         }
276
277                 IStructuredSelection selection= getStructuredSelection();
278                 if (fShowOpenPropertiesAction && selection != null && fOpenPropertiesDialog.isApplicableForSelection())
279                         menu.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, fOpenPropertiesDialog);
280         }
281
282         private String getShowInMenuLabel() {
283                 String keyBinding= null;
284
285                 IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
286                 if (bindingService != null)
287                         keyBinding= bindingService.getBestActiveBindingFormattedFor(IWorkbenchCommandConstants.NAVIGATE_SHOW_IN_QUICK_MENU);
288
289                 if (keyBinding == null)
290                         keyBinding= ""; //$NON-NLS-1$
291
292                 return ActionMessages.OpenViewActionGroup_showInAction_label + '\t' + keyBinding;
293         }
294
295         /*
296          * @see ActionGroup#dispose()
297          */
298         @Override
299         public void dispose() {
300                 fSelectionProvider.removeSelectionChangedListener(fOpenImplementation);
301                 fSelectionProvider.removeSelectionChangedListener(fOpenSuperImplementation);
302                 fSelectionProvider.removeSelectionChangedListener(fOpenAttachedJavadoc);
303                 fSelectionProvider.removeSelectionChangedListener(fOpenTypeHierarchy);
304                 fSelectionProvider.removeSelectionChangedListener(fOpenCallHierarchy);
305                 super.dispose();
306         }
307
308         private void setGlobalActionHandlers(IActionBars actionBars) {
309                 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_IMPLEMENTATION, fOpenImplementation);
310                 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_SUPER_IMPLEMENTATION, fOpenSuperImplementation);
311                 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_ATTACHED_JAVA_DOC, fOpenAttachedJavadoc);
312                 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_TYPE_HIERARCHY, fOpenTypeHierarchy);
313                 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_CALL_HIERARCHY, fOpenCallHierarchy);
314
315                 if (!fEditorIsOwner && fShowOpenPropertiesAction)
316                         actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), fOpenPropertiesDialog);
317         }
318
319         private void appendToGroup(IMenuManager menu, IAction action) {
320                 if (action.isEnabled())
321                         menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
322         }
323
324         private IStructuredSelection getStructuredSelection() {
325                 ISelection selection= getContext().getSelection();
326                 if (selection instanceof IStructuredSelection)
327                         return (IStructuredSelection)selection;
328                 return null;
329         }
330
331
332
333
334 }