KickJava   Java API By Example, From Geeks To Geeks.

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


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 import 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  *
46  * </p>
47  * <p>
48  * Note: Despite the name, an import operation can deal with things other than
49  * resources; the current name was retained for historical reasons.
50  * </p>
51  *
52  * @since 2.0
53  */

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

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

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

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

91     public ImportResourcesAction(IWorkbenchWindow window) {
92         super(WorkbenchMessages.ImportResourcesAction_text);
93         if (window == null) {
94             throw new IllegalArgumentException JavaDoc();
95         }
96         this.workbenchWindow = window;
97         tracker = new PerspectiveTracker(window, this);
98         setToolTipText(WorkbenchMessages.ImportResourcesAction_toolTip);
99         setId("import"); //$NON-NLS-1$
100
window.getWorkbench().getHelpSystem().setHelp(this,
101                 IWorkbenchHelpContextIds.IMPORT_ACTION);
102         // self-register selection listener (new for 3.0)
103
workbenchWindow.getSelectionService().addSelectionListener(
104                 selectionListener);
105
106         setImageDescriptor(WorkbenchImages
107                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_IMPORT_WIZ));
108     }
109
110     /**
111      * Create a new instance of this class
112      *
113      * @param workbench the workbench
114      * @deprecated use the constructor <code>ImportResourcesAction(IWorkbenchWindow)</code>
115      */

116     public ImportResourcesAction(IWorkbench workbench) {
117         this(workbench.getActiveWorkbenchWindow());
118     }
119
120     /**
121      * Invoke the Import wizards selection Wizard.
122      */

123     public void run() {
124         if (workbenchWindow == null) {
125             // action has been disposed
126
return;
127         }
128         ImportExportWizard wizard = new ImportExportWizard(ImportExportWizard.IMPORT);
129         IStructuredSelection selectionToPass;
130         // get the current workbench selection
131
ISelection workbenchSelection = workbenchWindow.getSelectionService()
132                 .getSelection();
133         if (workbenchSelection instanceof IStructuredSelection) {
134             selectionToPass = (IStructuredSelection) workbenchSelection;
135         } else {
136             selectionToPass = StructuredSelection.EMPTY;
137         }
138
139         wizard.init(workbenchWindow.getWorkbench(), selectionToPass);
140         IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault()
141                 .getDialogSettings();
142         IDialogSettings wizardSettings = workbenchSettings
143                 .getSection("ImportExportAction"); //$NON-NLS-1$
144
if (wizardSettings == null) {
145             wizardSettings = workbenchSettings
146                     .addNewSection("ImportExportAction"); //$NON-NLS-1$
147
}
148         wizard.setDialogSettings(wizardSettings);
149         wizard.setForcePreviousAndNextButtons(true);
150
151         Shell parent = workbenchWindow.getShell();
152         WizardDialog dialog = new WizardDialog(parent, wizard);
153         dialog.create();
154         dialog.getShell().setSize(
155                 Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x),
156                 SIZING_WIZARD_HEIGHT);
157         PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
158                 IWorkbenchHelpContextIds.IMPORT_WIZARD);
159         dialog.open();
160     }
161
162     /**
163      * Sets the current selection.
164      * In for backwards compatability. Use selectionChanged() instead.
165      * @param selection the new selection
166      * @deprecated
167      */

168     public void setSelection(IStructuredSelection selection) {
169         selectionChanged(selection);
170     }
171
172     /* (non-Javadoc)
173      * Method declared on ActionFactory.IWorkbenchAction.
174      * @since 3.0
175      */

176     public void dispose() {
177         if (workbenchWindow == null) {
178             // action has already been disposed
179
return;
180         }
181         tracker.dispose();
182         workbenchWindow.getSelectionService().removeSelectionListener(
183                 selectionListener);
184         workbenchWindow = null;
185     }
186 }
187
Popular Tags