KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.zip.ZipEntry JavaDoc;
20 import java.util.zip.ZipFile JavaDoc;
21
22 import org.eclipse.core.filesystem.URIUtil;
23
24 import org.eclipse.core.runtime.Assert;
25 import org.eclipse.core.runtime.CoreException;
26
27 import org.eclipse.core.resources.ResourcesPlugin;
28
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.FileDialog;
39 import org.eclipse.swt.widgets.Label;
40
41 import org.eclipse.jface.dialogs.Dialog;
42 import org.eclipse.jface.dialogs.IDialogConstants;
43 import org.eclipse.jface.viewers.DecoratingLabelProvider;
44 import org.eclipse.jface.viewers.ISelectionChangedListener;
45 import org.eclipse.jface.viewers.IStructuredSelection;
46 import org.eclipse.jface.viewers.SelectionChangedEvent;
47 import org.eclipse.jface.viewers.StructuredSelection;
48 import org.eclipse.jface.viewers.TreeViewer;
49 import org.eclipse.jface.wizard.WizardPage;
50
51 import org.eclipse.ui.PlatformUI;
52
53 import org.eclipse.ltk.core.refactoring.RefactoringCore;
54
55 import org.eclipse.jdt.core.IJavaElement;
56 import org.eclipse.jdt.core.IJavaModel;
57 import org.eclipse.jdt.core.IJavaProject;
58 import org.eclipse.jdt.core.IPackageFragment;
59 import org.eclipse.jdt.core.IPackageFragmentRoot;
60 import org.eclipse.jdt.core.JavaCore;
61 import org.eclipse.jdt.core.JavaModelException;
62 import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
63
64 import org.eclipse.jdt.ui.JavaElementComparator;
65 import org.eclipse.jdt.ui.JavaElementLabelProvider;
66 import org.eclipse.jdt.ui.ProblemsLabelDecorator;
67 import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
68
69 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
70 import org.eclipse.jdt.internal.ui.JavaPlugin;
71 import org.eclipse.jdt.internal.ui.filters.EmptyPackageFilter;
72 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil;
73 import org.eclipse.jdt.internal.ui.refactoring.binary.BinaryRefactoringHistoryWizard;
74 import org.eclipse.jdt.internal.ui.util.SWTUtil;
75
76 /**
77  * Jar import wizard page.
78  *
79  * @since 3.2
80  */

81 public final class JarImportWizardPage extends WizardPage {
82
83     /** The jar import wizard page name */
84     private static final String JavaDoc PAGE_NAME= "JarImportWizardPage"; //$NON-NLS-1$
85

86     /** The history dialog setting */
87     protected static final String JavaDoc SETTING_HISTORY= "org.eclipse.jdt.ui.refactoring.jarHistory"; //$NON-NLS-1$
88

89     /** Is the wizard page displayed for the first time? */
90     private boolean fFirstTime= true;
91
92     /** Is the wizard part of an import wizard? */
93     private final boolean fImportWizard;
94
95     /** The location control */
96     private RefactoringLocationControl fLocationControl= null;
97
98     /** The java model viewer */
99     private TreeViewer fTreeViewer= null;
100
101     /** The import wizard */
102     private final JarImportWizard fWizard;
103
104     /**
105      * Creates a new jar import wizard page.
106      *
107      * @param wizard
108      * the jar import wizard
109      * @param importWizard
110      * <code>true</code> if the wizard is part of an import wizard,
111      * <code>false</code> otherwise
112      */

113     public JarImportWizardPage(final JarImportWizard wizard, final boolean importWizard) {
114         super(PAGE_NAME);
115         Assert.isNotNull(wizard);
116         fWizard= wizard;
117         fImportWizard= importWizard;
118         if (fImportWizard) {
119             setTitle(JarImportMessages.JarImportWizardPage_page_title);
120             setDescription(JarImportMessages.JarImportWizardPage_page_description);
121         } else {
122             setTitle(JarImportMessages.JarImportWizardPage_page_replace_title);
123             setDescription(JarImportMessages.JarImportWizardPage_page_replace_description);
124         }
125     }
126
127     /**
128      * {@inheritDoc}
129      */

130     public void createControl(final Composite parent) {
131         initializeDialogUnits(parent);
132         final Composite composite= new Composite(parent, SWT.NONE);
133         composite.setLayout(new GridLayout());
134         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
135         createLocationGroup(composite);
136         if (fImportWizard)
137             createInputGroup(composite);
138         createRenameGroup(composite);
139         setPageComplete(false);
140         if (fImportWizard && !fTreeViewer.getControl().isEnabled())
141             setMessage(JarImportMessages.JarImportWizardPage_no_jar_files, INFORMATION);
142         setControl(composite);
143         Dialog.applyDialogFont(composite);
144         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.JARIMPORT_WIZARD_PAGE);
145     }
146
147     /**
148      * Creates a new grid data.
149      *
150      * @param flag
151      * the flags to use
152      * @param hspan
153      * the horizontal span
154      * @param indent
155      * the indent
156      * @return the grid data
157      */

158     protected GridData createGridData(final int flag, final int hspan, final int indent) {
159         final GridData data= new GridData(flag);
160         data.horizontalIndent= indent;
161         data.horizontalSpan= hspan;
162         return data;
163     }
164
165     /**
166      * Creates the input group.
167      *
168      * @param parent
169      * the parent control
170      */

171     protected void createInputGroup(final Composite parent) {
172         Assert.isNotNull(parent);
173         new Label(parent, SWT.NONE);
174         final Label label= new Label(parent, SWT.NONE);
175         label.setText(JarImportMessages.JarImportWizardPage_import_message);
176         final StandardJavaElementContentProvider contentProvider= new StandardJavaElementContentProvider() {
177
178             public Object JavaDoc[] getChildren(Object JavaDoc element) {
179                 if ((element instanceof IJavaProject) || (element instanceof IJavaModel))
180                     return super.getChildren(element);
181                 return new Object JavaDoc[0];
182             }
183
184             protected Object JavaDoc[] getJavaProjects(final IJavaModel model) throws JavaModelException {
185                 final Set JavaDoc set= new HashSet JavaDoc();
186                 final IJavaProject[] projects= model.getJavaProjects();
187                 for (int index= 0; index < projects.length; index++) {
188                     if (JarImportWizard.isValidJavaProject(projects[index])) {
189                         final Object JavaDoc[] roots= getPackageFragmentRoots(projects[index]);
190                         if (roots.length > 0)
191                             set.add(projects[index]);
192                     }
193                 }
194                 return set.toArray();
195             }
196
197             protected Object JavaDoc[] getPackageFragmentRoots(final IJavaProject project) throws JavaModelException {
198                 final Set JavaDoc set= new HashSet JavaDoc();
199                 final IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
200                 for (int offset= 0; offset < roots.length; offset++) {
201                     if (JarImportWizard.isValidClassPathEntry(roots[offset].getRawClasspathEntry()))
202                         set.add(roots[offset]);
203                 }
204                 return set.toArray();
205             }
206
207             public boolean hasChildren(final Object JavaDoc element) {
208                 return (element instanceof IJavaProject) || (element instanceof IJavaModel);
209             }
210         };
211
212         final DecoratingLabelProvider labelProvider= new DecoratingLabelProvider(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_BASICS | JavaElementLabelProvider.SHOW_OVERLAY_ICONS | JavaElementLabelProvider.SHOW_SMALL_ICONS), new ProblemsLabelDecorator(null));
213         fTreeViewer= new TreeViewer(parent, SWT.SINGLE | SWT.BORDER);
214         fTreeViewer.getTree().setLayoutData(createGridData(GridData.FILL_BOTH, 6, 0));
215         fTreeViewer.setLabelProvider(labelProvider);
216         fTreeViewer.setContentProvider(contentProvider);
217         fTreeViewer.addFilter(new EmptyPackageFilter());
218         fTreeViewer.setComparator(new JavaElementComparator());
219         fTreeViewer.setAutoExpandLevel(2);
220         fTreeViewer.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()));
221         final IPackageFragmentRoot root= fWizard.getPackageFragmentRoot();
222         if (root != null) {
223             fTreeViewer.setSelection(new StructuredSelection(new Object JavaDoc[] { root}), true);
224             fTreeViewer.expandToLevel(root, 1);
225         }
226         fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
227
228             public void selectionChanged(final SelectionChangedEvent event) {
229                 handleInputChanged();
230             }
231         });
232         if (contentProvider.getChildren(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot())).length == 0) {
233             fTreeViewer.getControl().setEnabled(false);
234             label.setEnabled(false);
235         }
236     }
237
238     /**
239      * Creates the location group.
240      *
241      * @param parent
242      * the parent control
243      */

244     protected void createLocationGroup(final Composite parent) {
245         Assert.isNotNull(parent);
246         new Label(parent, SWT.NONE).setText(JarImportMessages.JarImportWizardPage_import_label);
247         final Composite composite= new Composite(parent, SWT.NONE);
248         composite.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 6, 0));
249         composite.setLayout(new GridLayout(3, false));
250         final Label label= new Label(composite, SWT.NONE);
251         label.setText(JarImportMessages.JarImportWizardPage_location_label);
252         label.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_BEGINNING, 1, 0));
253         fLocationControl= new RefactoringLocationControl(fWizard, composite, SETTING_HISTORY);
254         fLocationControl.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 1, 0));
255         fLocationControl.loadHistory();
256         fLocationControl.getControl().addModifyListener(new ModifyListener() {
257
258             public final void modifyText(final ModifyEvent event) {
259                 handleInputChanged();
260             }
261         });
262         fLocationControl.getControl().addSelectionListener(new SelectionAdapter() {
263
264             public final void widgetSelected(final SelectionEvent event) {
265                 handleInputChanged();
266             }
267         });
268         fLocationControl.setFocus();
269         final Button button= new Button(composite, SWT.PUSH);
270         button.setText(JarImportMessages.JarImportWizardPage_browse_button_label);
271         button.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 1, 0));
272         SWTUtil.setButtonDimensionHint(button);
273         button.addSelectionListener(new SelectionAdapter() {
274
275             public final void widgetSelected(final SelectionEvent event) {
276                 handleBrowseButtonSelected();
277             }
278         });
279     }
280
281     /**
282      * Creates the rename group.
283      *
284      * @param parent
285      * the parent control
286      */

287     protected void createRenameGroup(final Composite parent) {
288         Assert.isNotNull(parent);
289         final JarImportData data= fWizard.getImportData();
290         final Button button= new Button(parent, SWT.CHECK);
291         button.setText(JarImportMessages.JarImportWizardPage_replace_jar_file);
292         button.setSelection(!data.isRenameJarFile());
293         button.addSelectionListener(new SelectionAdapter() {
294
295             public void widgetSelected(final SelectionEvent event) {
296                 data.setRenameJarFile(!button.getSelection());
297             }
298         });
299         if (fImportWizard && !fTreeViewer.getControl().isEnabled())
300             button.setEnabled(false);
301         if (!fImportWizard) {
302             final GridData gd= new GridData();
303             gd.horizontalIndent= IDialogConstants.HORIZONTAL_MARGIN;
304             button.setLayoutData(gd);
305         }
306     }
307
308     /**
309      * Handles the browse button selected event.
310      */

311     protected void handleBrowseButtonSelected() {
312         final FileDialog file= new FileDialog(getShell(), SWT.OPEN);
313         file.setText(JarImportMessages.JarImportWizardPage_browse_caption);
314         file.setFilterNames(new String JavaDoc[] { "*.jar", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
315
file.setFilterExtensions(new String JavaDoc[] { "*.jar", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
316
final String JavaDoc path= file.open();
317         if (path != null) {
318             fLocationControl.setText(path);
319             handleInputChanged();
320         }
321     }
322
323     /**
324      * Handles the input changed event.
325      */

326     protected void handleInputChanged() {
327         final JarImportData data= fWizard.getImportData();
328         data.setRefactoringHistory(null);
329         data.setRefactoringFileLocation(null);
330         setErrorMessage(null);
331         setMessage(null, NONE);
332         setPageComplete(true);
333         handleJarFileChanged();
334         if (isPageComplete())
335             handlePackageFragmentRootChanged();
336         if (fImportWizard && !fTreeViewer.getControl().isEnabled())
337             setErrorMessage(JarImportMessages.JarImportWizardPage_no_jar_files);
338         fFirstTime= false;
339         getContainer().updateButtons();
340     }
341
342     /**
343      * Handles the jar file changed event.
344      */

345     protected void handleJarFileChanged() {
346         if (fLocationControl != null) {
347             final String JavaDoc path= fLocationControl.getText();
348             if ("".equals(path)) { //$NON-NLS-1$
349
setErrorMessage(JarImportMessages.JarImportWizardPage_empty_location);
350                 setPageComplete(false);
351                 return;
352             } else {
353                 final File JavaDoc file= new File JavaDoc(path);
354                 if (!file.exists()) {
355                     setErrorMessage(JarImportMessages.JarImportWizardPage_invalid_location);
356                     setPageComplete(false);
357                     return;
358                 }
359                 ZipFile JavaDoc zip= null;
360                 try {
361                     try {
362                         zip= new ZipFile JavaDoc(file, ZipFile.OPEN_READ);
363                     } catch (IOException JavaDoc exception) {
364                         setErrorMessage(JarImportMessages.JarImportWizardPage_invalid_location);
365                         setPageComplete(false);
366                         return;
367                     }
368                     final JarImportData data= fWizard.getImportData();
369                     data.setRefactoringFileLocation(URIUtil.toURI(path));
370                     ZipEntry JavaDoc entry= zip.getEntry(JarPackagerUtil.getRefactoringsEntry());
371                     if (entry == null) {
372                         setMessage(JarImportMessages.JarImportWizardPage_no_refactorings, INFORMATION);
373                         setPageComplete(true);
374                         return;
375                     }
376                     handleTimeStampChanged();
377                     if (data.getExistingTimeStamp() > entry.getTime()) {
378                         setMessage(JarImportMessages.JarImportWizardPage_version_warning, WARNING);
379                         setPageComplete(true);
380                         return;
381                     }
382                     InputStream JavaDoc stream= null;
383                     try {
384                         stream= zip.getInputStream(entry);
385                         data.setRefactoringHistory(RefactoringCore.getHistoryService().readRefactoringHistory(stream, JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING));
386                     } catch (IOException JavaDoc exception) {
387                         setErrorMessage(JarImportMessages.JarImportWizardPage_no_refactorings);
388                         setPageComplete(false);
389                         return;
390                     } catch (CoreException exception) {
391                         JavaPlugin.log(exception);
392                         setErrorMessage(JarImportMessages.JarImportWizardPage_no_refactorings);
393                         setPageComplete(false);
394                         return;
395                     } finally {
396                         if (stream != null) {
397                             try {
398                                 stream.close();
399                             } catch (IOException JavaDoc exception) {
400                                 // Do nothing
401
}
402                         }
403                     }
404                 } finally {
405                     if (zip != null) {
406                         try {
407                             zip.close();
408                         } catch (IOException JavaDoc e) {
409                         }
410                     }
411                 }
412             }
413         }
414     }
415
416     /**
417      * Handles the package fragment root changed event.
418      */

419     protected void handlePackageFragmentRootChanged() {
420         if (fTreeViewer != null) {
421             final IStructuredSelection selection= (IStructuredSelection) fTreeViewer.getSelection();
422             final Object JavaDoc[] elements= selection.toArray();
423             if (elements.length != 1) {
424                 setErrorMessage(JarImportMessages.JarImportWizardPage_select_single_jar);
425                 setPageComplete(false);
426                 return;
427             } else {
428                 final JarImportData data= fWizard.getImportData();
429                 final Object JavaDoc element= elements[0];
430                 if (element instanceof IPackageFragmentRoot)
431                     data.setPackageFragmentRoot((IPackageFragmentRoot) element);
432                 else if (element instanceof IPackageFragment) {
433                     data.setPackageFragmentRoot((IPackageFragmentRoot) ((IJavaElement) element).getParent());
434                 } else {
435                     setErrorMessage(JarImportMessages.JarImportWizardPage_select_single_jar);
436                     setPageComplete(false);
437                 }
438             }
439         }
440     }
441
442     /**
443      * Handles the time stamp changed event.
444      */

445     protected void handleTimeStampChanged() {
446         final IPackageFragmentRoot root= fWizard.getPackageFragmentRoot();
447         if (root != null) {
448             try {
449                 final URI JavaDoc uri= BinaryRefactoringHistoryWizard.getLocationURI(root.getRawClasspathEntry());
450                 if (uri != null) {
451                     final File JavaDoc file= new File JavaDoc(uri);
452                     if (file.exists()) {
453                         ZipFile JavaDoc zip= null;
454                         try {
455                             zip= new ZipFile JavaDoc(file, ZipFile.OPEN_READ);
456                             ZipEntry JavaDoc entry= zip.getEntry(JarPackagerUtil.getRefactoringsEntry());
457                             if (entry != null) {
458                                 fWizard.getImportData().setExistingTimeStamp(entry.getTime());
459                             }
460                         } catch (IOException JavaDoc exception) {
461                             // Just leave it
462
} finally {
463                             if (zip != null) {
464                                 try {
465                                     zip.close();
466                                 } catch (IOException JavaDoc e) {
467                                 }
468                             }
469                         }
470                     }
471                 }
472             } catch (CoreException exception) {
473                 JavaPlugin.log(exception);
474             }
475         }
476     }
477
478     /**
479      * Gets called if the wizard is finished.
480      */

481     public void performFinish() {
482         fLocationControl.saveHistory();
483     }
484
485     /**
486      * {@inheritDoc}
487      */

488     public void setErrorMessage(final String JavaDoc message) {
489         if (!fFirstTime)
490             super.setErrorMessage(message);
491         else
492             setMessage(message, NONE);
493     }
494
495     /**
496      * {@inheritDoc}
497      */

498     public void setVisible(final boolean visible) {
499         super.setVisible(visible);
500         if (visible)
501             handleInputChanged();
502     }
503 }
504
Popular Tags