KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > OpenActionProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.ui.internal.navigator.resources.actions;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.mapping.ResourceMapping;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.action.GroupMarker;
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.ui.IActionBars;
22 import org.eclipse.ui.actions.OpenFileAction;
23 import org.eclipse.ui.actions.OpenWithMenu;
24 import org.eclipse.ui.internal.navigator.AdaptabilityUtility;
25 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages;
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.ICommonViewerWorkbenchSite;
31
32 /**
33  * Provides the open and open with menus for IResources.
34  *
35  * @since 3.2
36  *
37  */

38 public class OpenActionProvider extends CommonActionProvider {
39
40     private OpenFileAction openFileAction;
41
42     private ICommonViewerWorkbenchSite viewSite = null;
43
44     private boolean contribute = false;
45
46     public void init(ICommonActionExtensionSite aConfig) {
47         if (aConfig.getViewSite() instanceof ICommonViewerWorkbenchSite) {
48             viewSite = (ICommonViewerWorkbenchSite) aConfig.getViewSite();
49             openFileAction = new OpenFileAction(viewSite.getPage());
50             contribute = true;
51         }
52     }
53
54     public void fillContextMenu(IMenuManager aMenu) {
55         if (!contribute || getContext().getSelection().isEmpty()) {
56             return;
57         }
58
59         IStructuredSelection selection = (IStructuredSelection) getContext()
60                 .getSelection();
61
62         openFileAction.selectionChanged(selection);
63         if (openFileAction.isEnabled()) {
64             aMenu.insertAfter(ICommonMenuConstants.GROUP_OPEN, openFileAction);
65         }
66         addOpenWithMenu(aMenu);
67     }
68
69     public void fillActionBars(IActionBars theActionBars) {
70         if (!contribute) {
71             return;
72         }
73         IStructuredSelection selection = (IStructuredSelection) getContext()
74                 .getSelection();
75         if (selection.size() == 1
76                 && selection.getFirstElement() instanceof IFile) {
77             openFileAction.selectionChanged(selection);
78             theActionBars.setGlobalActionHandler(ICommonActionConstants.OPEN,
79                     openFileAction);
80         }
81
82     }
83
84     private void addOpenWithMenu(IMenuManager aMenu) {
85         IStructuredSelection ss = (IStructuredSelection) getContext()
86                 .getSelection();
87
88         if (ss == null || ss.size() != 1) {
89             return;
90         }
91
92         Object JavaDoc o = ss.getFirstElement();
93
94         // first try IResource
95
IAdaptable openable = (IAdaptable) AdaptabilityUtility.getAdapter(o,
96                 IResource.class);
97         // otherwise try ResourceMapping
98
if (openable == null) {
99             openable = (IAdaptable) AdaptabilityUtility.getAdapter(o,
100                     ResourceMapping.class);
101         } else if (((IResource) openable).getType() != IResource.FILE) {
102             openable = null;
103         }
104
105         if (openable != null) {
106             // Create a menu flyout.
107
IMenuManager submenu = new MenuManager(
108                     WorkbenchNavigatorMessages.OpenActionProvider_OpenWithMenu_label,
109                     ICommonMenuConstants.GROUP_OPEN_WITH);
110             submenu.add(new GroupMarker(ICommonMenuConstants.GROUP_TOP));
111             submenu.add(new OpenWithMenu(viewSite.getPage(), openable));
112             submenu.add(new GroupMarker(ICommonMenuConstants.GROUP_ADDITIONS));
113
114             // Add the submenu.
115
if (submenu.getItems().length > 2 && submenu.isEnabled()) {
116                 aMenu.appendToGroup(ICommonMenuConstants.GROUP_OPEN_WITH,
117                         submenu);
118             }
119         }
120     }
121
122 }
123
Popular Tags