001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.actions;
011:
012: import org.eclipse.jface.dialogs.IDialogSettings;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.jface.viewers.IStructuredSelection;
015: import org.eclipse.jface.viewers.StructuredSelection;
016: import org.eclipse.jface.wizard.WizardDialog;
017: import org.eclipse.swt.widgets.Shell;
018: import org.eclipse.ui.ISelectionListener;
019: import org.eclipse.ui.IWorkbench;
020: import org.eclipse.ui.IWorkbenchPart;
021: import org.eclipse.ui.IWorkbenchWindow;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
024: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
025: import org.eclipse.ui.internal.PerspectiveTracker;
026: import org.eclipse.ui.internal.WorkbenchImages;
027: import org.eclipse.ui.internal.WorkbenchMessages;
028: import org.eclipse.ui.internal.WorkbenchPlugin;
029: import org.eclipse.ui.internal.dialogs.ImportExportWizard;
030:
031: /**
032: * Action representing a generic export operation.
033: * <p>
034: * This class may be instantiated. It is not intended to be subclassed.
035: * </p>
036: * <p>
037: * This method automatically registers listeners so that it can keep its
038: * enablement state up to date. Ordinarily, the window's references to these
039: * listeners will be dropped automatically when the window closes. However,
040: * if the client needs to get rid of an action while the window is still open,
041: * the client must call IWorkbenchAction#dispose to give the
042: * action an opportunity to deregister its listeners and to perform any other
043: * cleanup.
044: * </p>
045: * <p>
046: * Note: Despite the name, an export operation can deal with things other than
047: * resources; the current name was retained for historical reasons.
048: * </p>
049: *
050: * @since 2.0
051: */
052: public class ExportResourcesAction extends BaseSelectionListenerAction
053: implements ActionFactory.IWorkbenchAction {
054:
055: private static final int SIZING_WIZARD_WIDTH = 470;
056:
057: private static final int SIZING_WIZARD_HEIGHT = 550;
058:
059: /**
060: * The workbench window; or <code>null</code> if this
061: * action has been <code>dispose</code>d.
062: */
063: private IWorkbenchWindow workbenchWindow;
064:
065: /**
066: * Tracks perspective activation, to update this action's
067: * enabled state.
068: */
069: private PerspectiveTracker tracker;
070:
071: /**
072: * Listen for the selection changing and update the
073: * actions that are interested
074: */
075: private final ISelectionListener selectionListener = new ISelectionListener() {
076: public void selectionChanged(IWorkbenchPart part,
077: ISelection selection) {
078: if (selection instanceof IStructuredSelection) {
079: IStructuredSelection structured = (IStructuredSelection) selection;
080: ExportResourcesAction.this .selectionChanged(structured);
081: }
082: }
083: };
084:
085: /**
086: * Create a new instance of this class.
087: *
088: * @param window the window
089: */
090: public ExportResourcesAction(IWorkbenchWindow window) {
091: this (window, WorkbenchMessages.ExportResourcesAction_text);
092: }
093:
094: /**
095: * Create a new instance of this class.
096: *
097: * @param window the window
098: * @param label the label
099: */
100: public ExportResourcesAction(IWorkbenchWindow window, String label) {
101: super (label);
102: if (window == null) {
103: throw new IllegalArgumentException();
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 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(
150: ImportExportWizard.EXPORT);
151: IStructuredSelection selectionToPass;
152: // get the current workbench selection
153: ISelection workbenchSelection = workbenchWindow
154: .getSelectionService().getSelection();
155: if (workbenchSelection instanceof IStructuredSelection) {
156: selectionToPass = (IStructuredSelection) workbenchSelection;
157: } else {
158: selectionToPass = StructuredSelection.EMPTY;
159: }
160:
161: wizard.init(workbenchWindow.getWorkbench(), selectionToPass);
162: IDialogSettings workbenchSettings = WorkbenchPlugin
163: .getDefault().getDialogSettings();
164: IDialogSettings wizardSettings = workbenchSettings
165: .getSection("ImportExportAction"); //$NON-NLS-1$
166: if (wizardSettings == null) {
167: wizardSettings = workbenchSettings
168: .addNewSection("ImportExportAction"); //$NON-NLS-1$
169: }
170: wizard.setDialogSettings(wizardSettings);
171: wizard.setForcePreviousAndNextButtons(true);
172:
173: Shell parent = workbenchWindow.getShell();
174: WizardDialog dialog = new WizardDialog(parent, wizard);
175: dialog.create();
176: dialog.getShell().setSize(
177: Math.max(SIZING_WIZARD_WIDTH, dialog.getShell()
178: .getSize().x), SIZING_WIZARD_HEIGHT);
179: PlatformUI.getWorkbench().getHelpSystem().setHelp(
180: dialog.getShell(),
181: IWorkbenchHelpContextIds.EXPORT_WIZARD);
182: dialog.open();
183: }
184:
185: /**
186: * Sets the current selection.
187: * In for backwards compatability. Use selectionChanged() instead.
188: * @param selection the new selection
189: * @deprecated
190: */
191: public void setSelection(IStructuredSelection selection) {
192: selectionChanged(selection);
193: }
194:
195: /* (non-Javadoc)
196: * Method declared on ActionFactory.IWorkbenchAction.
197: * @since 3.0
198: */
199: public void dispose() {
200: if (workbenchWindow == null) {
201: // action has already been disposed
202: return;
203: }
204: tracker.dispose();
205: workbenchWindow.getSelectionService().removeSelectionListener(
206: selectionListener);
207: workbenchWindow = null;
208: }
209: }
|