KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarimport > JarImportWizard


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.jdt.internal.ui.jarimport;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URI JavaDoc;
17 import java.util.zip.ZipEntry JavaDoc;
18 import java.util.zip.ZipFile JavaDoc;
19
20 import org.eclipse.core.filesystem.EFS;
21 import org.eclipse.core.filesystem.IFileStore;
22 import org.eclipse.core.filesystem.URIUtil;
23
24 import org.eclipse.core.runtime.Assert;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.SubProgressMonitor;
31
32 import org.eclipse.core.resources.IResource;
33 import org.eclipse.core.resources.ResourcesPlugin;
34
35 import org.eclipse.jface.dialogs.IDialogSettings;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.wizard.IWizardPage;
38
39 import org.eclipse.ui.IImportWizard;
40 import org.eclipse.ui.IWorkbench;
41
42 import org.eclipse.ltk.core.refactoring.RefactoringCore;
43 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
44 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
45 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration;
46
47 import org.eclipse.jdt.core.IClasspathEntry;
48 import org.eclipse.jdt.core.IJavaProject;
49 import org.eclipse.jdt.core.IPackageFragmentRoot;
50 import org.eclipse.jdt.core.JavaCore;
51 import org.eclipse.jdt.core.JavaModelException;
52 import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
53
54
55 import org.eclipse.jdt.internal.ui.JavaPlugin;
56 import org.eclipse.jdt.internal.ui.JavaPluginImages;
57 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil;
58 import org.eclipse.jdt.internal.ui.refactoring.binary.BinaryRefactoringHistoryWizard;
59
60 /**
61  * Import wizard to import a refactoring-aware Java Archive (JAR) file.
62  * <p>
63  * This class may be instantiated and used without further configuration; this
64  * class is not intended to be subclassed.
65  * </p>
66  * <p>
67  * Example:
68  *
69  * <pre>
70  * IWizard wizard= new JarImportWizard();
71  * wizard.init(workbench, selection);
72  * WizardDialog dialog= new WizardDialog(shell, wizard);
73  * dialog.open();
74  * </pre>
75  *
76  * During the call to <code>open</code>, the wizard dialog is presented to
77  * the user. When the user hits Finish, the user-selected JAR file is inspected
78  * for associated refactorings, the wizard executes eventual refactorings,
79  * copies the JAR file over its old version, the dialog closes, and the call to
80  * <code>open</code> returns.
81  * </p>
82  *
83  * @since 3.2
84  */

85 public final class JarImportWizard extends BinaryRefactoringHistoryWizard implements IImportWizard {
86
87     /** Proxy which requests the refactoring history from the import data */
88     private final class RefactoringHistoryProxy extends RefactoringHistory {
89
90         /** The cached refactoring history delta */
91         private RefactoringDescriptorProxy[] fHistoryDelta= null;
92
93         /**
94          * {@inheritDoc}
95          */

96         public RefactoringDescriptorProxy[] getDescriptors() {
97             if (fHistoryDelta != null)
98                 return fHistoryDelta;
99             final RefactoringHistory incoming= fImportData.getRefactoringHistory();
100             if (incoming != null) {
101                 fHistoryDelta= incoming.getDescriptors();
102                 final IPackageFragmentRoot root= fImportData.getPackageFragmentRoot();
103                 if (root != null) {
104                     try {
105                         final URI JavaDoc uri= getLocationURI(root.getRawClasspathEntry());
106                         if (uri != null) {
107                             final File JavaDoc file= new File JavaDoc(uri);
108                             if (file.exists()) {
109                                 ZipFile JavaDoc zip= null;
110                                 try {
111                                     zip= new ZipFile JavaDoc(file, ZipFile.OPEN_READ);
112                                     ZipEntry JavaDoc entry= zip.getEntry(JarPackagerUtil.getRefactoringsEntry());
113                                     if (entry != null) {
114                                         InputStream JavaDoc stream= null;
115                                         try {
116                                             stream= zip.getInputStream(entry);
117                                             final RefactoringHistory existing= RefactoringCore.getHistoryService().readRefactoringHistory(stream, JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING);
118                                             if (existing != null)
119                                                 fHistoryDelta= incoming.removeAll(existing).getDescriptors();
120                                         } finally {
121                                             if (stream != null) {
122                                                 try {
123                                                     stream.close();
124                                                 } catch (IOException JavaDoc exception) {
125                                                     // Do nothing
126
}
127                                             }
128                                         }
129                                     }
130                                 } catch (IOException JavaDoc exception) {
131                                     // Just leave it
132
}
133                             }
134                         }
135                     } catch (CoreException exception) {
136                         JavaPlugin.log(exception);
137                     }
138                 }
139                 return fHistoryDelta;
140             }
141             return new RefactoringDescriptorProxy[0];
142         }
143
144         /**
145          * {@inheritDoc}
146          */

147         public boolean isEmpty() {
148             final RefactoringDescriptorProxy[] proxies= getDescriptors();
149             if (proxies != null)
150                 return proxies.length == 0;
151             return true;
152         }
153
154         /**
155          * {@inheritDoc}
156          */

157         public RefactoringHistory removeAll(final RefactoringHistory history) {
158             throw new UnsupportedOperationException JavaDoc();
159         }
160     }
161
162     /** The dialog settings key */
163     private static String JavaDoc DIALOG_SETTINGS_KEY= "JarImportWizard"; //$NON-NLS-1$
164

165     /**
166      * Is the specified class path entry pointing to a valid location for
167      * import?
168      *
169      * @param entry
170      * the class path entry
171      * @return <code>true</code> if it is a valid package fragment root,
172      * <code>false</code> otherwise
173      */

174     public static boolean isValidClassPathEntry(final IClasspathEntry entry) {
175         Assert.isNotNull(entry);
176         final int kind= entry.getEntryKind();
177         if (kind == IClasspathEntry.CPE_LIBRARY)
178             return entry.getContentKind() == IPackageFragmentRoot.K_BINARY;
179         else if (kind == IClasspathEntry.CPE_VARIABLE)
180             return true; // be optimistic
181
return false;
182     }
183
184     /**
185      * Is the specified java project a valid project for import?
186      *
187      * @param project
188      * the java project
189      * @throws JavaModelException
190      * if an error occurs
191      */

192     public static boolean isValidJavaProject(final IJavaProject project) throws JavaModelException {
193         Assert.isNotNull(project);
194         return project.getProject().isAccessible();
195     }
196
197     /** The refactoring history proxy */
198     private final RefactoringHistoryProxy fHistoryProxy;
199
200     /** The jar import data */
201     private final JarImportData fImportData= new JarImportData();
202
203     /** The jar import page, or <code>null</code> */
204     private JarImportWizardPage fImportPage= null;
205
206     /** Is the wizard part of an import wizard? */
207     private boolean fImportWizard= true;
208
209     /** Has the wizard new dialog settings? */
210     private boolean fNewSettings;
211
212     /**
213      * Creates a new jar import wizard.
214      */

215     public JarImportWizard() {
216         super(JarImportMessages.JarImportWizard_window_title, JarImportMessages.RefactoringImportPreviewPage_title, JarImportMessages.RefactoringImportPreviewPage_description);
217         fImportData.setRefactoringAware(true);
218         fImportData.setIncludeDirectoryEntries(true);
219         fHistoryProxy= new RefactoringHistoryProxy();
220         setInput(fHistoryProxy);
221         final IDialogSettings section= JavaPlugin.getDefault().getDialogSettings().getSection(DIALOG_SETTINGS_KEY);
222         if (section == null)
223             fNewSettings= true;
224         else {
225             fNewSettings= false;
226             setDialogSettings(section);
227         }
228         setConfiguration(new RefactoringHistoryControlConfiguration(null, false, false) {
229
230             public String JavaDoc getProjectPattern() {
231                 return JarImportMessages.JarImportWizard_project_pattern;
232             }
233
234             public String JavaDoc getWorkspaceCaption() {
235                 return JarImportMessages.JarImportWizard_workspace_caption;
236             }
237         });
238         setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_REPLACE_JAR);
239     }
240
241     /**
242      * Creates a new jar import wizard.
243      *
244      * @param wizard
245      * <code>true</code> if the wizard is part of an import wizard,
246      * <code>false</code> otherwise
247      */

248     public JarImportWizard(final boolean wizard) {
249         this();
250         fImportWizard= wizard;
251         setWindowTitle(JarImportMessages.JarImportWizard_replace_title);
252     }
253
254     /**
255      * {@inheritDoc}
256      */

257     protected void addUserDefinedPages() {
258         fImportPage= new JarImportWizardPage(this, fImportWizard);
259         addPage(fImportPage);
260     }
261
262     /**
263      * {@inheritDoc}
264      */

265     public boolean canFinish() {
266         return super.canFinish() && fImportData.getPackageFragmentRoot() != null && fImportData.getRefactoringFileLocation() != null;
267     }
268
269     /**
270      * {@inheritDoc}
271      */

272     protected boolean deconfigureClasspath(final IClasspathEntry[] entries, final IProgressMonitor monitor) throws CoreException {
273         final boolean rename= fImportData.isRenameJarFile();
274         if (rename && !fCancelled) {
275             final IPackageFragmentRoot root= getPackageFragmentRoot();
276             if (root != null) {
277                 final IClasspathEntry entry= root.getRawClasspathEntry();
278                 for (int index= 0; index < entries.length; index++) {
279                     if (entries[index].equals(entry)) {
280                         final IPath path= getTargetPath(entries[index]);
281                         if (path != null)
282                             entries[index]= JavaCore.newLibraryEntry(path, entries[index].getSourceAttachmentPath(), entries[index].getSourceAttachmentRootPath(), entries[index].getAccessRules(), entries[index].getExtraAttributes(), entries[index].isExported());
283                     }
284                 }
285             }
286         }
287         if (!fCancelled)
288             replaceJarFile(new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
289         return rename;
290     }
291
292     /**
293      * Returns the jar import data.
294      *
295      * @return the jar import data
296      */

297     public JarImportData getImportData() {
298         return fImportData;
299     }
300
301     /**
302      * {@inheritDoc}
303      */

304     public IWizardPage getNextPage(final IWizardPage page) {
305         if (page == fImportPage && fImportData.getRefactoringHistory() == null)
306             return null;
307         return super.getNextPage(page);
308     }
309
310     /**
311      * {@inheritDoc}
312      */

313     protected IPackageFragmentRoot getPackageFragmentRoot() {
314         return fImportData.getPackageFragmentRoot();
315     }
316
317     /**
318      * {@inheritDoc}
319      */

320     protected RefactoringHistory getRefactoringHistory() {
321         return fHistoryProxy;
322     }
323
324     /**
325      * Returns the target path to be used for the updated classpath entry.
326      *
327      * @param entry
328      * the classpath entry
329      * @return the target path, or <code>null</code>
330      * @throws CoreException
331      * if an error occurs
332      */

333     private IPath getTargetPath(final IClasspathEntry entry) throws CoreException {
334         final URI JavaDoc location= getLocationURI(entry);
335         if (location != null) {
336             final URI JavaDoc target= getTargetURI(location);
337             if (target != null) {
338                 IPath path= URIUtil.toPath(target);
339                 if (path != null) {
340                     final IPath workspace= ResourcesPlugin.getWorkspace().getRoot().getLocation();
341                     if (workspace.isPrefixOf(path)) {
342                         path= path.removeFirstSegments(workspace.segmentCount());
343                         path= path.setDevice(null);
344                         path= path.makeAbsolute();
345                     }
346                 }
347                 return path;
348             }
349         }
350         return null;
351     }
352
353     /**
354      * Returns the target uri taking any renaming of the jar file into account.
355      *
356      * @param uri
357      * the location uri
358      * @return the target uri
359      * @throws CoreException
360      * if an error occurs
361      */

362     private URI JavaDoc getTargetURI(final URI JavaDoc uri) throws CoreException {
363         final IFileStore parent= EFS.getStore(uri).getParent();
364         if (parent != null) {
365             final URI JavaDoc location= fImportData.getRefactoringFileLocation();
366             if (location != null)
367                 return parent.getChild(EFS.getStore(location).getName()).toURI();
368         }
369         return uri;
370     }
371
372     /**
373      * {@inheritDoc}
374      */

375     public void init(final IWorkbench workbench, final IStructuredSelection selection) {
376         if (selection != null && selection.size() == 1) {
377             final Object JavaDoc element= selection.getFirstElement();
378             if (element instanceof IPackageFragmentRoot) {
379                 final IPackageFragmentRoot root= (IPackageFragmentRoot) element;
380                 try {
381                     final IClasspathEntry entry= root.getRawClasspathEntry();
382                     if (isValidClassPathEntry(entry))
383                         fImportData.setPackageFragmentRoot(root);
384                 } catch (JavaModelException exception) {
385                     JavaPlugin.log(exception);
386                 }
387             }
388         }
389     }
390
391     /**
392      * {@inheritDoc}
393      */

394     public boolean performFinish() {
395         if (fNewSettings) {
396             final IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
397             IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY);
398             section= settings.addNewSection(DIALOG_SETTINGS_KEY);
399             setDialogSettings(section);
400         }
401         fImportPage.performFinish();
402         return super.performFinish();
403     }
404
405     /**
406      * Replaces the old jar file with the new one.
407      *
408      * @param monitor
409      * the progress monitor to use
410      * @throws CoreException
411      * if an error occurs
412      */

413     private void replaceJarFile(final IProgressMonitor monitor) throws CoreException {
414         try {
415             monitor.beginTask(JarImportMessages.JarImportWizard_cleanup_import, 250);
416             final URI JavaDoc location= fImportData.getRefactoringFileLocation();
417             if (location != null) {
418                 final IPackageFragmentRoot root= fImportData.getPackageFragmentRoot();
419                 if (root != null) {
420                     final URI JavaDoc uri= getLocationURI(root.getRawClasspathEntry());
421                     if (uri != null) {
422                         final IFileStore store= EFS.getStore(location);
423                         if (fImportData.isRenameJarFile()) {
424                             final URI JavaDoc target= getTargetURI(uri);
425                             store.copy(EFS.getStore(target), EFS.OVERWRITE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
426                             if (!uri.equals(target))
427                                 EFS.getStore(uri).delete(EFS.NONE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
428                         } else
429                             store.copy(EFS.getStore(uri), EFS.OVERWRITE, new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
430                         if (fJavaProject != null)
431                             fJavaProject.getResource().refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
432                         return;
433                     }
434                 }
435             }
436             throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, JarImportMessages.JarImportWizard_error_copying_jar, null));
437         } finally {
438             monitor.done();
439         }
440     }
441 }
442
Popular Tags