KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.BufferedInputStream JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.net.URI JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.CoreException;
24
25 import org.eclipse.ltk.core.refactoring.RefactoringCore;
26 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
27
28 import org.eclipse.ltk.internal.core.refactoring.IRefactoringSerializationConstants;
29 import org.eclipse.ltk.internal.ui.refactoring.IRefactoringHelpContextIds;
30 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
31
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.dnd.Clipboard;
34 import org.eclipse.swt.dnd.TextTransfer;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Group;
39
40 import org.eclipse.jface.dialogs.Dialog;
41 import org.eclipse.jface.wizard.WizardPage;
42
43 import org.eclipse.ui.PlatformUI;
44
45 /**
46  * First page of the apply refactoring script wizard.
47  *
48  * @since 3.2
49  */

50 public final class ApplyRefactoringScriptWizardPage extends WizardPage {
51
52     /** The apply refactoring script wizard page name */
53     private static final String JavaDoc PAGE_NAME= "ApplyRefactoringScriptWizardPage"; //$NON-NLS-1$
54

55     /** Is the wizard page displayed for the first time? */
56     private boolean fFirstTime= true;
57
58     /** The refactoring script location control */
59     private RefactoringScriptLocationControl fLocationControl= null;
60
61     /** The apply script wizard */
62     private final ApplyRefactoringScriptWizard fWizard;
63
64     /**
65      * Creates a new apply refactoring script wizard page.
66      *
67      * @param wizard
68      * the associated wizard
69      */

70     public ApplyRefactoringScriptWizardPage(final ApplyRefactoringScriptWizard wizard) {
71         super(PAGE_NAME);
72         Assert.isNotNull(wizard);
73         fWizard= wizard;
74         setTitle(ScriptingMessages.ApplyRefactoringScriptWizard_title);
75         setDescription(ScriptingMessages.ApplyRefactoringScriptWizard_description);
76     }
77
78     /**
79      * {@inheritDoc}
80      */

81     public boolean canFlipToNextPage() {
82         return super.canFlipToNextPage();
83     }
84
85     /**
86      * {@inheritDoc}
87      */

88     public void createControl(final Composite parent) {
89         initializeDialogUnits(parent);
90         final Composite composite= new Composite(parent, SWT.NONE);
91         composite.setLayout(new GridLayout());
92         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
93
94         final Group group= new Group(composite, SWT.NONE);
95         group.setText(ScriptingMessages.ApplyRefactoringScriptWizardPage_location_caption);
96         final GridLayout layout= new GridLayout();
97         layout.marginWidth= 0;
98         group.setLayout(layout);
99         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
100
101         fLocationControl= new RefactoringScriptLocationControl(fWizard, group) {
102
103             protected final void handleClipboardScriptChanged() {
104                 super.handleClipboardScriptChanged();
105                 ApplyRefactoringScriptWizardPage.this.fWizard.setRefactoringHistory(null);
106                 setErrorMessage(null);
107                 setPageComplete(true);
108                 handleClipboardChanged();
109             }
110
111             protected final void handleExternalLocationChanged() {
112                 super.handleExternalLocationChanged();
113                 ApplyRefactoringScriptWizardPage.this.fWizard.setRefactoringHistory(null);
114                 setErrorMessage(null);
115                 setPageComplete(true);
116                 handleLocationChanged();
117             }
118         };
119         setPageComplete(false);
120         fLocationControl.loadHistory();
121         fFirstTime= false;
122         setControl(composite);
123         Dialog.applyDialogFont(composite);
124         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IRefactoringHelpContextIds.REFACTORING_APPLY_SCRIPT_PAGE);
125     }
126
127     /**
128      * Handles the clipboard changed event.
129      */

130     private void handleClipboardChanged() {
131         Clipboard clipboard= null;
132         try {
133             clipboard= new Clipboard(fLocationControl.getDisplay());
134             final Object JavaDoc contents= clipboard.getContents(TextTransfer.getInstance());
135             if (contents == null) {
136                 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_empty_clipboard);
137                 setPageComplete(false);
138                 return;
139             }
140             if (contents instanceof String JavaDoc) {
141                 final String JavaDoc script= (String JavaDoc) contents;
142                 try {
143                     final ByteArrayInputStream JavaDoc stream= new ByteArrayInputStream JavaDoc(script.getBytes(IRefactoringSerializationConstants.OUTPUT_ENCODING));
144                     fWizard.setRefactoringHistory(RefactoringCore.getHistoryService().readRefactoringHistory(stream, RefactoringDescriptor.NONE));
145                 } catch (UnsupportedEncodingException JavaDoc exception) {
146                     // Does not happen
147
} catch (CoreException exception) {
148                     setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_no_script_clipboard);
149                     setPageComplete(false);
150                     return;
151                 }
152             } else {
153                 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_no_text_clipboard);
154                 setPageComplete(false);
155             }
156         } finally {
157             if (clipboard != null)
158                 clipboard.dispose();
159         }
160     }
161
162     /**
163      * Handles the location changed event.
164      */

165     private void handleLocationChanged() {
166         final URI JavaDoc uri= fLocationControl.getRefactoringScript();
167         if (uri == null) {
168             setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_invalid_location);
169             setPageComplete(false);
170             return;
171         }
172         final File JavaDoc file= new File JavaDoc(uri);
173         if (!file.exists()) {
174             setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_invalid_script_file);
175             setPageComplete(false);
176             return;
177         }
178         InputStream JavaDoc stream= null;
179         try {
180             stream= new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
181             fWizard.setRefactoringHistory(RefactoringCore.getHistoryService().readRefactoringHistory(stream, RefactoringDescriptor.NONE));
182         } catch (IOException JavaDoc exception) {
183             setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_error_cannot_read);
184             setPageComplete(false);
185             return;
186         } catch (CoreException exception) {
187             setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_invalid_format);
188             setPageComplete(false);
189             return;
190         } finally {
191             if (stream != null) {
192                 try {
193                     stream.close();
194                 } catch (IOException JavaDoc exception) {
195                     // Do nothing
196
}
197             }
198         }
199     }
200
201     /**
202      * Gets called if the wizard is finished.
203      */

204     public void performFinish() {
205         fLocationControl.saveHistory();
206     }
207
208     /**
209      * {@inheritDoc}
210      */

211     public void setErrorMessage(final String JavaDoc message) {
212         if (!fFirstTime)
213             super.setErrorMessage(message);
214         else
215             setMessage(message, NONE);
216     }
217
218     /**
219      * {@inheritDoc}
220      */

221     public void setVisible(final boolean visible) {
222         if (visible) {
223             final URI JavaDoc uri= fWizard.getRefactoringScript();
224             if (uri != null) {
225                 fWizard.setRefactoringScript(null);
226                 try {
227                     fLocationControl.setRefactoringScript(uri);
228                     handleLocationChanged();
229                 } catch (IllegalArgumentException JavaDoc exception) {
230                     RefactoringUIPlugin.log(exception);
231                 }
232             }
233         }
234         super.setVisible(visible);
235     }
236 }
237
Popular Tags