KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Sebastian Davids <sdavids@gmx.de> - Collapse all action
11  * Sebastian Davids <sdavids@gmx.de> - Images for menu items
12  *******************************************************************************/

13 package org.eclipse.ui.internal.navigator.resources.actions;
14
15 import java.net.URL JavaDoc;
16 import java.util.Collections JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.FileLocator;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.action.Separator;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IWorkbenchWindow;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.actions.ActionFactory;
31 import org.eclipse.ui.actions.ExportResourcesAction;
32 import org.eclipse.ui.actions.ImportResourcesAction;
33 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages;
34 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorPlugin;
35 import org.eclipse.ui.navigator.CommonActionProvider;
36 import org.eclipse.ui.navigator.ICommonActionExtensionSite;
37 import org.eclipse.ui.navigator.ICommonMenuConstants;
38 import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite;
39 import org.eclipse.ui.navigator.WizardActionGroup;
40
41 /**
42  * Adds actions for Import/Export wizards. The group is smart, in that it will
43  * either add actions for Import and Export, or if there are context sensitive
44  * options available (as defined by <b>org.eclipse.ui.navigator.commonWizard</b>),
45  * then it will compound these options into a submenu with the appropriate lead
46  * text ("Import" or "Export").
47  *
48  * <p>
49  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
50  * part of a work in progress. There is a guarantee neither that this API will
51  * work nor that it will remain the same. Please do not use this API without
52  * consulting with the Platform/UI team.
53  * </p>
54  *
55  * @since 3.2
56  */

57 public class PortingActionProvider extends CommonActionProvider {
58
59     private static final String JavaDoc COMMON_NAVIGATOR_IMPORT_MENU = "common.import.menu"; //$NON-NLS-1$
60

61     private static final String JavaDoc COMMON_NAVIGATOR_EXPORT_MENU = "common.export.menu"; //$NON-NLS-1$
62

63     private ImportResourcesAction importAction;
64
65     private ExportResourcesAction exportAction;
66
67     private WizardActionGroup importWizardActionGroup;
68
69     private WizardActionGroup exportWizardActionGroup;
70
71     private boolean disposed = false;
72
73     private boolean contribute= false;
74
75     public void init(ICommonActionExtensionSite anExtensionSite) {
76
77         Assert.isTrue(!disposed);
78
79         if (anExtensionSite.getViewSite() instanceof ICommonViewerWorkbenchSite) {
80
81             IWorkbenchWindow window = ((ICommonViewerWorkbenchSite) anExtensionSite
82                     .getViewSite()).getWorkbenchWindow();
83             importAction = new ImportResourcesAction(window);
84             exportAction = new ExportResourcesAction(window);
85             importWizardActionGroup = new WizardActionGroup(window, PlatformUI
86                     .getWorkbench().getImportWizardRegistry(),
87                     WizardActionGroup.TYPE_IMPORT, anExtensionSite.getContentService());
88             exportWizardActionGroup = new WizardActionGroup(window, PlatformUI
89                     .getWorkbench().getExportWizardRegistry(),
90                     WizardActionGroup.TYPE_EXPORT, anExtensionSite.getContentService());
91             contribute = true;
92         }
93     }
94
95     /**
96      * Extends the superclass implementation to dispose the subgroups.
97      */

98     public void dispose() {
99         if(!contribute) {
100             return;
101         }
102         importWizardActionGroup.dispose();
103         exportWizardActionGroup.dispose();
104         importAction = null;
105         exportAction = null;
106         disposed = true;
107     }
108
109     public void fillActionBars(IActionBars theActionBars) {
110         if(!contribute) {
111             return;
112         }
113
114         Assert.isTrue(!disposed);
115
116         theActionBars.setGlobalActionHandler(ActionFactory.IMPORT.getId(),
117                 importAction);
118         theActionBars.setGlobalActionHandler(ActionFactory.EXPORT.getId(),
119                 exportAction);
120
121     }
122
123     public void fillContextMenu(IMenuManager aMenu) {
124         if(!contribute) {
125             return;
126         }
127
128         Assert.isTrue(!disposed);
129
130         ISelection selection = getContext().getSelection();
131         if (!(selection instanceof IStructuredSelection) || ((IStructuredSelection) selection).size() > 1) {
132             addSimplePortingMenus(aMenu);
133         } else {
134             addImportMenu(aMenu);
135             addExportMenu(aMenu);
136         }
137     }
138
139     /**
140      * Returns the image descriptor with the given relative path.
141      */

142     protected ImageDescriptor getImageDescriptor(String JavaDoc relativePath) {
143         String JavaDoc iconPath = "icons/full/"; //$NON-NLS-1$
144
URL JavaDoc url = FileLocator.find(WorkbenchNavigatorPlugin.getDefault().getBundle(), new Path(iconPath + relativePath), Collections.EMPTY_MAP);
145         if (url == null) {
146             return ImageDescriptor.getMissingImageDescriptor();
147         }
148         return ImageDescriptor.createFromURL(url);
149     }
150
151     private void addSimplePortingMenus(IMenuManager aMenu) {
152         aMenu.appendToGroup(ICommonMenuConstants.GROUP_PORT, importAction);
153         aMenu.appendToGroup(ICommonMenuConstants.GROUP_PORT, exportAction);
154     }
155
156     private void addImportMenu(IMenuManager aMenu) {
157
158         importWizardActionGroup.setContext(getContext());
159         if (importWizardActionGroup.getWizardActionIds().length == 0) {
160             aMenu.appendToGroup(ICommonMenuConstants.GROUP_PORT, importAction);
161             return;
162         }
163
164         IMenuManager submenu = new MenuManager(
165                 WorkbenchNavigatorMessages.PortingActionProvider_ImportResourcesMenu_label,
166                 COMMON_NAVIGATOR_IMPORT_MENU);
167         importWizardActionGroup.fillContextMenu(submenu);
168
169         submenu.add(new Separator(ICommonMenuConstants.GROUP_ADDITIONS));
170         submenu.add(new Separator());
171         submenu.add(importAction);
172         aMenu.appendToGroup(ICommonMenuConstants.GROUP_PORT, submenu);
173     }
174
175     private void addExportMenu(IMenuManager aMenu) {
176
177         exportWizardActionGroup.setContext(getContext());
178         if (exportWizardActionGroup.getWizardActionIds().length == 0) {
179             aMenu.appendToGroup(ICommonMenuConstants.GROUP_PORT, exportAction);
180             return;
181         }
182         IMenuManager submenu = new MenuManager(
183                 WorkbenchNavigatorMessages.PortingActionProvider_ExportResourcesMenu_label,
184                 COMMON_NAVIGATOR_EXPORT_MENU);
185         exportWizardActionGroup.fillContextMenu(submenu);
186
187         submenu.add(new Separator(ICommonMenuConstants.GROUP_ADDITIONS));
188         submenu.add(new Separator());
189         submenu.add(exportAction);
190         aMenu.appendToGroup(ICommonMenuConstants.GROUP_PORT, submenu);
191     }
192
193 }
194
Popular Tags