KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > scripting > ApplyRefactoringScriptWizard


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ltk.internal.ui.refactoring.scripting;
12
13 import java.net.URI JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16
17 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
18 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
19
20 import org.eclipse.ltk.internal.ui.refactoring.RefactoringPluginImages;
21 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
22
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.wizard.IWizardContainer;
26
27 import org.eclipse.ui.IWorkbench;
28 import org.eclipse.ui.IWorkbenchWizard;
29
30 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration;
31 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryWizard;
32
33 /**
34  * Wizard to apply a refactoring script.
35  *
36  * @since 3.2
37  */

38 public final class ApplyRefactoringScriptWizard extends RefactoringHistoryWizard implements IWorkbenchWizard {
39
40     /** Proxy which encapsulates a refactoring history */
41     private final class RefactoringHistoryProxy extends RefactoringHistory {
42
43         /**
44          * {@inheritDoc}
45          */

46         public RefactoringDescriptorProxy[] getDescriptors() {
47             if (fRefactoringHistory != null)
48                 return fRefactoringHistory.getDescriptors();
49             return new RefactoringDescriptorProxy[0];
50         }
51
52         /**
53          * {@inheritDoc}
54          */

55         public boolean isEmpty() {
56             final RefactoringDescriptorProxy[] proxies= getDescriptors();
57             if (proxies != null)
58                 return proxies.length == 0;
59             return true;
60         }
61
62         /**
63          * {@inheritDoc}
64          */

65         public RefactoringHistory removeAll(final RefactoringHistory history) {
66             throw new UnsupportedOperationException JavaDoc();
67         }
68     }
69
70     /** The dialog settings key */
71     private static String JavaDoc DIALOG_SETTINGS_KEY= "ApplyRefactoringScriptWizard"; //$NON-NLS-1$
72

73     /** Has the wizard new dialog settings? */
74     private boolean fNewSettings;
75
76     /** The refactoring history, or <code>null</code> */
77     private RefactoringHistory fRefactoringHistory= null;
78
79     /** The location of the refactoring script file, or <code>null</code> */
80     private URI JavaDoc fScriptLocation= null;
81
82     /** The apply script wizard page */
83     private ApplyRefactoringScriptWizardPage fWizardPage;
84
85     /**
86      * Creates a new apply refactoring script wizard.
87      */

88     public ApplyRefactoringScriptWizard() {
89         super(ScriptingMessages.ApplyRefactoringScriptWizard_caption, ScriptingMessages.ApplyRefactoringScriptWizard_title, ScriptingMessages.ApplyRefactoringScriptWizard_description);
90         setInput(new RefactoringHistoryProxy());
91         setDefaultPageImageDescriptor(RefactoringPluginImages.DESC_WIZBAN_APPLY_SCRIPT);
92         final IDialogSettings settings= RefactoringUIPlugin.getDefault().getDialogSettings();
93         final IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY);
94         if (section == null)
95             fNewSettings= true;
96         else {
97             fNewSettings= false;
98             setDialogSettings(section);
99         }
100         setConfiguration(new RefactoringHistoryControlConfiguration(null, false, false) {
101
102             public String JavaDoc getProjectPattern() {
103                 return ScriptingMessages.ApplyRefactoringScriptWizard_project_pattern;
104             }
105
106             public String JavaDoc getWorkspaceCaption() {
107                 return ScriptingMessages.ApplyRefactoringScriptWizard_workspace_caption;
108             }
109         });
110     }
111
112     /**
113      * {@inheritDoc}
114      */

115     protected void addUserDefinedPages() {
116         fWizardPage= new ApplyRefactoringScriptWizardPage(this);
117         addPage(fWizardPage);
118     }
119
120     /**
121      * {@inheritDoc}
122      */

123     public boolean canFinish() {
124         return super.canFinish() && fRefactoringHistory != null;
125     }
126
127     /**
128      * Returns the refactoring history to apply.
129      *
130      * @return the refactoring history to apply, or <code>null</code>
131      */

132     public RefactoringHistory getRefactoringHistory() {
133         return fRefactoringHistory;
134     }
135
136     /**
137      * Returns the location of the refactoring script.
138      *
139      * @return the location of the script, or <code>null</code>
140      */

141     public URI JavaDoc getRefactoringScript() {
142         return fScriptLocation;
143     }
144
145     /**
146      * {@inheritDoc}
147      */

148     public void init(final IWorkbench workbench, final IStructuredSelection selection) {
149         if (selection != null && selection.size() == 1) {
150             final Object JavaDoc element= selection.getFirstElement();
151             if (element instanceof IFile) {
152                 final IFile file= (IFile) element;
153                 if (file.getFileExtension().equals(ScriptingMessages.CreateRefactoringScriptWizardPage_script_extension))
154                     fScriptLocation= file.getRawLocationURI();
155             }
156         }
157     }
158
159     /**
160      * {@inheritDoc}
161      */

162     public boolean performFinish() {
163         if (fNewSettings) {
164             final IDialogSettings settings= RefactoringUIPlugin.getDefault().getDialogSettings();
165             IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY);
166             section= settings.addNewSection(DIALOG_SETTINGS_KEY);
167             setDialogSettings(section);
168         }
169         fWizardPage.performFinish();
170         return super.performFinish();
171     }
172
173     /**
174      * Sets the refactoring history to apply.
175      *
176      * @param history
177      * the refactoring history to apply, or <code>null</code>
178      */

179     public void setRefactoringHistory(final RefactoringHistory history) {
180         fRefactoringHistory= history;
181         final IWizardContainer wizard= getContainer();
182         if (wizard.getCurrentPage() != null)
183             wizard.updateButtons();
184     }
185
186     /**
187      * Sets the location of the refactoring script.
188      *
189      * @param uri
190      * the location of the script, or <code>null</code>
191      */

192     public void setRefactoringScript(final URI JavaDoc uri) {
193         fScriptLocation= uri;
194         final IWizardContainer wizard= getContainer();
195         if (wizard.getCurrentPage() != null)
196             wizard.updateButtons();
197     }
198 }
199
Popular Tags