KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.io.BufferedInputStream JavaDoc;
14 import java.io.BufferedOutputStream JavaDoc;
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.eclipse.core.runtime.Assert;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IProgressMonitor;
32
33 import org.eclipse.ltk.core.refactoring.RefactoringCore;
34 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
35 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
36 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
37
38 import org.eclipse.ltk.internal.core.refactoring.IRefactoringSerializationConstants;
39 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryManager;
40 import org.eclipse.ltk.internal.ui.refactoring.Messages;
41 import org.eclipse.ltk.internal.ui.refactoring.RefactoringPluginImages;
42 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
43 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
44
45 import org.eclipse.swt.SWTError;
46 import org.eclipse.swt.dnd.Clipboard;
47 import org.eclipse.swt.dnd.TextTransfer;
48 import org.eclipse.swt.dnd.Transfer;
49
50 import org.eclipse.jface.dialogs.IDialogConstants;
51 import org.eclipse.jface.dialogs.IDialogSettings;
52 import org.eclipse.jface.dialogs.MessageDialog;
53 import org.eclipse.jface.operation.IRunnableWithProgress;
54 import org.eclipse.jface.wizard.IWizardContainer;
55 import org.eclipse.jface.wizard.Wizard;
56
57 /**
58  * Wizard to create a refactoring script.
59  *
60  * @since 3.2
61  */

62 public final class CreateRefactoringScriptWizard extends Wizard {
63
64     /** The dialog settings key */
65     private static String JavaDoc DIALOG_SETTINGS_KEY= "CreateRefactoringScriptWizard"; //$NON-NLS-1$
66

67     /** Has the wizard new dialog settings? */
68     private boolean fNewSettings;
69
70     /** The selected refactoring descriptors, or the empty array */
71     private RefactoringDescriptorProxy[] fRefactoringDescriptors= {};
72
73     /** The refactoring history */
74     private RefactoringHistory fRefactoringHistory;
75
76     /** The refactoring script location, or <code>null</code> */
77     private URI JavaDoc fScriptLocation= null;
78
79     /** Should the wizard put the refactoring script to the clipboard? */
80     private boolean fUseClipboard= false;
81
82     /** The create refactoring script wizard page */
83     private final CreateRefactoringScriptWizardPage fWizardPage;
84
85     /**
86      * Creates a new create refactoring script wizard.
87      */

88     public CreateRefactoringScriptWizard() {
89         setNeedsProgressMonitor(false);
90         setWindowTitle(ScriptingMessages.CreateRefactoringScriptWizard_caption);
91         setDefaultPageImageDescriptor(RefactoringPluginImages.DESC_WIZBAN_CREATE_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         fWizardPage= new CreateRefactoringScriptWizardPage(this);
101     }
102
103     /**
104      * {@inheritDoc}
105      */

106     public void addPages() {
107         super.addPages();
108         addPage(fWizardPage);
109     }
110
111     /**
112      * {@inheritDoc}
113      */

114     public boolean canFinish() {
115         return (fUseClipboard || fScriptLocation != null) && fRefactoringDescriptors.length > 0;
116     }
117
118     /**
119      * Returns the refactoring history to create a script from.
120      *
121      * @return the refactoring history.
122      */

123     public RefactoringHistory getRefactoringHistory() {
124         return fRefactoringHistory;
125     }
126
127     /**
128      * Performs the actual refactoring script export.
129      *
130      * @return <code>true</code> if the wizard can be finished,
131      * <code>false</code> otherwise
132      */

133     private boolean performExport() {
134         RefactoringDescriptorProxy[] writable= fRefactoringDescriptors;
135         if (fScriptLocation != null) {
136             final File JavaDoc file= new File JavaDoc(fScriptLocation);
137             if (file.exists()) {
138                 final MessageDialog message= new MessageDialog(getShell(), getShell().getText(), null, Messages.format(ScriptingMessages.CreateRefactoringScriptWizard_overwrite_query, new String JavaDoc[] { ScriptingMessages.CreateRefactoringScriptWizard_merge_button, ScriptingMessages.CreateRefactoringScriptWizard_overwrite_button}), MessageDialog.QUESTION, new String JavaDoc[] { ScriptingMessages.CreateRefactoringScriptWizard_merge_button, ScriptingMessages.CreateRefactoringScriptWizard_overwrite_button, IDialogConstants.CANCEL_LABEL}, 0);
139                 final int result= message.open();
140                 if (result == 0) {
141                     InputStream JavaDoc stream= null;
142                     try {
143                         stream= new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
144                         final RefactoringDescriptorProxy[] existing= RefactoringCore.getHistoryService().readRefactoringHistory(stream, RefactoringDescriptor.NONE).getDescriptors();
145                         final Set JavaDoc set= new HashSet JavaDoc();
146                         for (int index= 0; index < existing.length; index++)
147                             set.add(existing[index]);
148                         for (int index= 0; index < fRefactoringDescriptors.length; index++)
149                             set.add(fRefactoringDescriptors[index]);
150                         writable= new RefactoringDescriptorProxy[set.size()];
151                         set.toArray(writable);
152                     } catch (FileNotFoundException JavaDoc exception) {
153                         MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, exception.getLocalizedMessage());
154                         return true;
155                     } catch (CoreException exception) {
156                         final Throwable JavaDoc throwable= exception.getStatus().getException();
157                         if (throwable instanceof IOException JavaDoc) {
158                             MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, throwable.getLocalizedMessage());
159                             return true;
160                         } else {
161                             RefactoringUIPlugin.log(exception);
162                             return false;
163                         }
164                     } finally {
165                         if (stream != null) {
166                             try {
167                                 stream.close();
168                             } catch (IOException JavaDoc exception) {
169                                 // Do nothing
170
}
171                         }
172                     }
173                 } else if (result == 2)
174                     return false;
175             }
176             OutputStream JavaDoc stream= null;
177             try {
178                 stream= new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
179                 writeRefactoringDescriptorProxies(writable, stream);
180                 return true;
181             } catch (CoreException exception) {
182                 final Throwable JavaDoc throwable= exception.getStatus().getException();
183                 if (throwable instanceof IOException JavaDoc) {
184                     MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, throwable.getLocalizedMessage());
185                     return true;
186                 } else {
187                     RefactoringUIPlugin.log(exception);
188                     return false;
189                 }
190             } catch (FileNotFoundException JavaDoc exception) {
191                 MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, exception.getLocalizedMessage());
192                 return true;
193             } finally {
194                 if (stream != null) {
195                     try {
196                         stream.close();
197                     } catch (IOException JavaDoc exception) {
198                         // Do nothing
199
}
200                 }
201             }
202         } else if (fUseClipboard) {
203             try {
204                 final ByteArrayOutputStream JavaDoc stream= new ByteArrayOutputStream JavaDoc(2048);
205                 writeRefactoringDescriptorProxies(writable, stream);
206                 try {
207                     final String JavaDoc string= stream.toString(IRefactoringSerializationConstants.OUTPUT_ENCODING);
208                     Clipboard clipboard= null;
209                     try {
210                         clipboard= new Clipboard(getShell().getDisplay());
211                         try {
212                             clipboard.setContents(new Object JavaDoc[] { string}, new Transfer[] { TextTransfer.getInstance()});
213                             return true;
214                         } catch (SWTError error) {
215                             MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, error.getLocalizedMessage());
216                             return false;
217                         }
218                     } finally {
219                         if (clipboard != null)
220                             clipboard.dispose();
221                     }
222                 } catch (UnsupportedEncodingException JavaDoc exception) {
223                     // Does not happen
224
return false;
225                 }
226             } catch (CoreException exception) {
227                 final Throwable JavaDoc throwable= exception.getStatus().getException();
228                 if (throwable instanceof IOException JavaDoc) {
229                     MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, throwable.getLocalizedMessage());
230                     return true;
231                 } else {
232                     RefactoringUIPlugin.log(exception);
233                     return false;
234                 }
235             }
236         }
237         return false;
238     }
239
240     /**
241      * {@inheritDoc}
242      */

243     public boolean performFinish() {
244         if (fNewSettings) {
245             final IDialogSettings settings= RefactoringUIPlugin.getDefault().getDialogSettings();
246             IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY);
247             section= settings.addNewSection(DIALOG_SETTINGS_KEY);
248             setDialogSettings(section);
249         }
250         fWizardPage.performFinish();
251         return performExport();
252     }
253
254     /**
255      * Sets the selected refactoring descriptors.
256      *
257      * @param proxies
258      * the selected refactoring descriptors
259      */

260     public void setRefactoringDescriptors(final RefactoringDescriptorProxy[] proxies) {
261         Assert.isNotNull(proxies);
262         fRefactoringDescriptors= proxies;
263         final IWizardContainer wizard= getContainer();
264         if (wizard.getCurrentPage() != null)
265             wizard.updateButtons();
266     }
267
268     /**
269      * Sets the refactoring history to use.
270      *
271      * @param history
272      * the refactoring history to use
273      */

274     public void setRefactoringHistory(final RefactoringHistory history) {
275         Assert.isNotNull(history);
276         fRefactoringHistory= history;
277     }
278
279     /**
280      * Sets the refactoring script location.
281      *
282      * @param location
283      * the refactoring script location, or <code>null</code>
284      */

285     public void setRefactoringScript(final URI JavaDoc location) {
286         fScriptLocation= location;
287         final IWizardContainer wizard= getContainer();
288         if (wizard.getCurrentPage() != null)
289             wizard.updateButtons();
290     }
291
292     /**
293      * Determines whether the wizard should save the refactoring script to the
294      * clipboard.
295      *
296      * @param clipboard
297      * <code>true</code> to save the script to clipboard,
298      * <code>false</code> otherwise
299      */

300     public void setUseClipboard(final boolean clipboard) {
301         fUseClipboard= clipboard;
302         final IWizardContainer wizard= getContainer();
303         if (wizard.getCurrentPage() != null)
304             wizard.updateButtons();
305     }
306
307     /**
308      * Writes the refactoring descriptor proxies to the specified output stream.
309      *
310      * @param writable
311      * the refactoring descriptor proxies
312      * @param stream
313      * the output stream to write to
314      * @throws CoreException
315      * if an error occurs
316      */

317     private void writeRefactoringDescriptorProxies(final RefactoringDescriptorProxy[] writable, final OutputStream JavaDoc stream) throws CoreException {
318         RefactoringHistoryManager.sortRefactoringDescriptorsAscending(writable);
319         try {
320             getContainer().run(false, false, new IRunnableWithProgress() {
321
322                 public void run(final IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
323                     try {
324                         RefactoringCore.getHistoryService().writeRefactoringDescriptors(writable, stream, RefactoringDescriptor.NONE, false, monitor);
325                     } catch (CoreException exception) {
326                         throw new InvocationTargetException JavaDoc(exception);
327                     }
328                 }
329             });
330         } catch (InvocationTargetException JavaDoc exception) {
331             final Throwable JavaDoc throwable= exception.getTargetException();
332             if (throwable instanceof CoreException) {
333                 final CoreException extended= (CoreException) throwable;
334                 throw extended;
335             }
336         } catch (InterruptedException JavaDoc exception) {
337             // Do nothing
338
}
339     }
340 }
Popular Tags