KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > ExportResourcesAction


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.actions;
12
13 import org.eclipse.jface.dialogs.IDialogSettings;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.viewers.StructuredSelection;
17 import org.eclipse.jface.wizard.WizardDialog;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.ISelectionListener;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.IWorkbenchWindow;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
25 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
26 import org.eclipse.ui.internal.PerspectiveTracker;
27 import org.eclipse.ui.internal.WorkbenchImages;
28 import org.eclipse.ui.internal.WorkbenchMessages;
29 import org.eclipse.ui.internal.WorkbenchPlugin;
30 import org.eclipse.ui.internal.dialogs.ImportExportWizard;
31
32 /**
33  * Action representing a generic export operation.
34  * <p>
35  * This class may be instantiated. It is not intended to be subclassed.
36  * </p>
37  * <p>
38  * This method automatically registers listeners so that it can keep its
39  * enablement state up to date. Ordinarily, the window's references to these
40  * listeners will be dropped automatically when the window closes. However,
41  * if the client needs to get rid of an action while the window is still open,
42  * the client must call IWorkbenchAction#dispose to give the
43  * action an opportunity to deregister its listeners and to perform any other
44  * cleanup.
45  * </p>
46  * <p>
47  * Note: Despite the name, an export operation can deal with things other than
48  * resources; the current name was retained for historical reasons.
49  * </p>
50  *
51  * @since 2.0
52  */

53 public class ExportResourcesAction extends BaseSelectionListenerAction
54         implements ActionFactory.IWorkbenchAction {
55
56     private static final int SIZING_WIZARD_WIDTH = 470;
57
58     private static final int SIZING_WIZARD_HEIGHT = 550;
59
60     /**
61      * The workbench window; or <code>null</code> if this
62      * action has been <code>dispose</code>d.
63      */

64     private IWorkbenchWindow workbenchWindow;
65
66     /**
67      * Tracks perspective activation, to update this action's
68      * enabled state.
69      */

70     private PerspectiveTracker tracker;
71
72     /**
73      * Listen for the selection changing and update the
74      * actions that are interested
75      */

76     private final ISelectionListener selectionListener = new ISelectionListener() {
77         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
78             if (selection instanceof IStructuredSelection) {
79                 IStructuredSelection structured = (IStructuredSelection) selection;
80                 ExportResourcesAction.this.selectionChanged(structured);
81             }
82         }
83     };
84
85     /**
86      * Create a new instance of this class.
87      *
88      * @param window the window
89      */

90     public ExportResourcesAction(IWorkbenchWindow window) {
91         this(window, WorkbenchMessages.ExportResourcesAction_text);
92     }
93
94     /**
95      * Create a new instance of this class.
96      *
97      * @param window the window
98      * @param label the label
99      */

100     public ExportResourcesAction(IWorkbenchWindow window, String JavaDoc label) {
101         super(label);
102         if (window == null) {
103             throw new IllegalArgumentException JavaDoc();
104         }
105         this.workbenchWindow = window;
106         tracker = new PerspectiveTracker(window, this);
107         setToolTipText(WorkbenchMessages.ExportResourcesAction_toolTip);
108         setId("export"); //$NON-NLS-1$
109
window.getWorkbench().getHelpSystem().setHelp(this,
110                 IWorkbenchHelpContextIds.EXPORT_ACTION);
111         // self-register selection listener (new for 3.0)
112
workbenchWindow.getSelectionService().addSelectionListener(
113                 selectionListener);
114
115         setText(WorkbenchMessages.ExportResourcesAction_fileMenuText);
116         setImageDescriptor(WorkbenchImages
117                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_EXPORT_WIZ));
118     }
119
120     /**
121      * Create a new instance of this class
122      *
123      * @param workbench the workbench
124      * @deprecated use the constructor <code>ExportResourcesAction(IWorkbenchWindow)</code>
125      */

126     public ExportResourcesAction(IWorkbench workbench) {
127         this(workbench.getActiveWorkbenchWindow());
128     }
129
130     /**
131      * Create a new instance of this class.
132      *
133      * @param workbench the workbench
134      * @param label the label
135      * @deprecated use the constructor <code>ExportResourcesAction(IWorkbenchWindow, String)</code>
136      */

137     public ExportResourcesAction(IWorkbench workbench, String JavaDoc label) {
138         this(workbench.getActiveWorkbenchWindow(), label);
139     }
140
141     /**
142      * Invoke the Export wizards selection Wizard.
143      */

144     public void run() {
145         if (workbenchWindow == null) {
146             // action has been disposed
147
return;
148         }
149         ImportExportWizard wizard = new ImportExportWizard(ImportExportWizard.EXPORT);
150         IStructuredSelection selectionToPass;
151         // get the current workbench selection
152
ISelection workbenchSelection = workbenchWindow.getSelectionService()
153                 .getSelection();
154         if (workbenchSelection instanceof IStructuredSelection) {
155             selectionToPass = (IStructuredSelection) workbenchSelection;
156         } else {
157             selectionToPass = StructuredSelection.EMPTY;
158         }
159
160         wizard.init(workbenchWindow.getWorkbench(), selectionToPass);
161         IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault()
162                 .getDialogSettings();
163         IDialogSettings wizardSettings = workbenchSettings
164                 .getSection("ImportExportAction"); //$NON-NLS-1$
165
if (wizardSettings == null) {
166             wizardSettings = workbenchSettings
167                     .addNewSection("ImportExportAction"); //$NON-NLS-1$
168
}
169         wizard.setDialogSettings(wizardSettings);
170         wizard.setForcePreviousAndNextButtons(true);
171
172         Shell parent = workbenchWindow.getShell();
173         WizardDialog dialog = new WizardDialog(parent, wizard);
174         dialog.create();
175         dialog.getShell().setSize(
176                 Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x),
177                 SIZING_WIZARD_HEIGHT);
178         PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
179                 IWorkbenchHelpContextIds.EXPORT_WIZARD);
180         dialog.open();
181     }
182
183     /**
184      * Sets the current selection.
185      * In for backwards compatability. Use selectionChanged() instead.
186      * @param selection the new selection
187      * @deprecated
188      */

189     public void setSelection(IStructuredSelection selection) {
190         selectionChanged(selection);
191     }
192
193     /* (non-Javadoc)
194      * Method declared on ActionFactory.IWorkbenchAction.
195      * @since 3.0
196      */

197     public void dispose() {
198         if (workbenchWindow == null) {
199             // action has already been disposed
200
return;
201         }
202         tracker.dispose();
203         workbenchWindow.getSelectionService().removeSelectionListener(
204                 selectionListener);
205         workbenchWindow = null;
206     }
207 }
208
Popular Tags