KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > OpenActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.views.navigator;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.MenuManager;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.actions.OpenFileAction;
22 import org.eclipse.ui.actions.OpenInNewWindowAction;
23 import org.eclipse.ui.actions.OpenWithMenu;
24 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
25
26 /**
27  * This is the action group for the open actions.
28  */

29 public class OpenActionGroup extends ResourceNavigatorActionGroup {
30
31     private OpenFileAction openFileAction;
32
33     /**
34      * The id for the Open With submenu.
35      */

36     public static final String JavaDoc OPEN_WITH_ID = PlatformUI.PLUGIN_ID
37             + ".OpenWithSubMenu"; //$NON-NLS-1$
38

39     public OpenActionGroup(IResourceNavigator navigator) {
40         super(navigator);
41     }
42
43     protected void makeActions() {
44         openFileAction = new OpenFileAction(navigator.getSite().getPage());
45     }
46
47     public void fillContextMenu(IMenuManager menu) {
48         IStructuredSelection selection = (IStructuredSelection) getContext()
49                 .getSelection();
50
51         boolean anyResourceSelected = !selection.isEmpty()
52                 && ResourceSelectionUtil.allResourcesAreOfType(selection,
53                         IResource.PROJECT | IResource.FOLDER | IResource.FILE);
54         boolean onlyFilesSelected = !selection.isEmpty()
55                 && ResourceSelectionUtil.allResourcesAreOfType(selection,
56                         IResource.FILE);
57
58         if (onlyFilesSelected) {
59             openFileAction.selectionChanged(selection);
60             menu.add(openFileAction);
61             fillOpenWithMenu(menu, selection);
62         }
63
64         if (anyResourceSelected) {
65             addNewWindowAction(menu, selection);
66         }
67     }
68
69     /**
70      * Adds the OpenWith submenu to the context menu.
71      *
72      * @param menu the context menu
73      * @param selection the current selection
74      */

75     private void fillOpenWithMenu(IMenuManager menu,
76             IStructuredSelection selection) {
77
78         // Only supported if exactly one file is selected.
79
if (selection.size() != 1) {
80             return;
81         }
82         Object JavaDoc element = selection.getFirstElement();
83         if (!(element instanceof IFile)) {
84             return;
85         }
86
87         MenuManager submenu = new MenuManager(ResourceNavigatorMessages.ResourceNavigator_openWith, OPEN_WITH_ID);
88         submenu.add(new OpenWithMenu(navigator.getSite().getPage(),
89                 (IFile) element));
90         menu.add(submenu);
91     }
92
93     /**
94      * Adds the Open in New Window action to the context menu.
95      *
96      * @param menu the context menu
97      * @param selection the current selection
98      */

99     private void addNewWindowAction(IMenuManager menu,
100             IStructuredSelection selection) {
101
102         // Only supported if exactly one container (i.e open project or folder) is selected.
103
if (selection.size() != 1) {
104             return;
105         }
106         Object JavaDoc element = selection.getFirstElement();
107         if (!(element instanceof IContainer)) {
108             return;
109         }
110         if (element instanceof IProject && !(((IProject) element).isOpen())) {
111             return;
112         }
113
114         menu.add(new OpenInNewWindowAction(navigator.getSite()
115                 .getWorkbenchWindow(), (IContainer) element));
116     }
117
118     /**
119      * Runs the default action (open file).
120      */

121     public void runDefaultAction(IStructuredSelection selection) {
122         Object JavaDoc element = selection.getFirstElement();
123         if (element instanceof IFile) {
124             openFileAction.selectionChanged(selection);
125             openFileAction.run();
126         }
127     }
128 }
129
Popular Tags