]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/jarimport/JarImportWizardPage.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / jarimport / JarImportWizardPage.java
1 /*******************************************************************************
2  * Copyright (c) 2005, 2011 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;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.URI;
17 import java.util.HashSet;
18 import java.util.Set;
19 import java.util.zip.ZipEntry;
20 import java.util.zip.ZipFile;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.FileDialog;
32 import org.eclipse.swt.widgets.Label;
33
34 import org.eclipse.core.filesystem.URIUtil;
35
36 import org.eclipse.core.runtime.Assert;
37 import org.eclipse.core.runtime.CoreException;
38
39 import org.eclipse.core.resources.ResourcesPlugin;
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.IClasspathEntry;
56 import org.eclipse.jdt.core.IJavaElement;
57 import org.eclipse.jdt.core.IJavaModel;
58 import org.eclipse.jdt.core.IJavaProject;
59 import org.eclipse.jdt.core.IPackageFragment;
60 import org.eclipse.jdt.core.IPackageFragmentRoot;
61 import org.eclipse.jdt.core.JavaCore;
62 import org.eclipse.jdt.core.JavaModelException;
63 import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
64
65 import org.eclipse.jdt.ui.JavaElementComparator;
66 import org.eclipse.jdt.ui.JavaElementLabelProvider;
67 import org.eclipse.jdt.ui.ProblemsLabelDecorator;
68 import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
69
70 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
71 import org.eclipse.jdt.internal.ui.JavaPlugin;
72 import org.eclipse.jdt.internal.ui.filters.EmptyPackageFilter;
73 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil;
74 import org.eclipse.jdt.internal.ui.refactoring.binary.BinaryRefactoringHistoryWizard;
75 import org.eclipse.jdt.internal.ui.util.SWTUtil;
76 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter;
77
78 /**
79  * Jar import wizard page.
80  *
81  * @since 3.2
82  */
83 public final class JarImportWizardPage extends WizardPage {
84
85         /** The jar import wizard page name */
86         private static final String PAGE_NAME= "JarImportWizardPage"; //$NON-NLS-1$
87
88         /** The history dialog setting */
89         protected static final String SETTING_HISTORY= "org.eclipse.jdt.ui.refactoring.jarHistory"; //$NON-NLS-1$
90
91         /** Is the wizard page displayed for the first time? */
92         private boolean fFirstTime= true;
93
94         /** Is the wizard part of an import wizard? */
95         private final boolean fImportWizard;
96
97         /** The location control */
98         private RefactoringLocationControl fLocationControl= null;
99
100         /** The java model viewer */
101         private TreeViewer fTreeViewer= null;
102
103         /** The import wizard */
104         private final JarImportWizard fWizard;
105
106         /**
107          * Creates a new jar import wizard page.
108          *
109          * @param wizard
110          *            the jar import wizard
111          * @param importWizard
112          *            <code>true</code> if the wizard is part of an import wizard,
113          *            <code>false</code> otherwise
114          */
115         public JarImportWizardPage(final JarImportWizard wizard, final boolean importWizard) {
116                 super(PAGE_NAME);
117                 Assert.isNotNull(wizard);
118                 fWizard= wizard;
119                 fImportWizard= importWizard;
120                 if (fImportWizard) {
121                         setTitle(JarImportMessages.JarImportWizardPage_page_title);
122                         setDescription(JarImportMessages.JarImportWizardPage_page_description);
123                 } else {
124                         setTitle(JarImportMessages.JarImportWizardPage_page_replace_title);
125                         setDescription(JarImportMessages.JarImportWizardPage_page_replace_description);
126                 }
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         public void createControl(final Composite parent) {
133                 initializeDialogUnits(parent);
134                 final Composite composite= new Composite(parent, SWT.NONE);
135                 composite.setLayout(new GridLayout());
136                 composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
137                 createLocationGroup(composite);
138                 if (fImportWizard)
139                         createInputGroup(composite);
140                 createRenameGroup(composite);
141                 setPageComplete(false);
142                 if (fImportWizard && !fTreeViewer.getControl().isEnabled())
143                         setMessage(JarImportMessages.JarImportWizardPage_no_jar_files, INFORMATION);
144                 setControl(composite);
145                 Dialog.applyDialogFont(composite);
146                 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.JARIMPORT_WIZARD_PAGE);
147         }
148
149         /**
150          * Creates a new grid data.
151          *
152          * @param flag
153          *            the flags to use
154          * @param hspan
155          *            the horizontal span
156          * @param indent
157          *            the indent
158          * @return the grid data
159          */
160         protected GridData createGridData(final int flag, final int hspan, final int indent) {
161                 final GridData data= new GridData(flag);
162                 data.horizontalIndent= indent;
163                 data.horizontalSpan= hspan;
164                 return data;
165         }
166
167         /**
168          * Creates the input group.
169          *
170          * @param parent
171          *            the parent control
172          */
173         protected void createInputGroup(final Composite parent) {
174                 Assert.isNotNull(parent);
175                 new Label(parent, SWT.NONE);
176                 final Label label= new Label(parent, SWT.NONE);
177                 label.setText(JarImportMessages.JarImportWizardPage_import_message);
178                 final StandardJavaElementContentProvider contentProvider= new StandardJavaElementContentProvider() {
179
180                         @Override
181                         public Object[] getChildren(Object element) {
182                                 if ((element instanceof IJavaProject) || (element instanceof IJavaModel))
183                                         return super.getChildren(element);
184                                 return new Object[0];
185                         }
186
187                         @Override
188                         protected Object[] getJavaProjects(final IJavaModel model) throws JavaModelException {
189                                 final Set<IJavaProject> set= new HashSet<IJavaProject>();
190                                 final IJavaProject[] projects= model.getJavaProjects();
191                                 for (int index= 0; index < projects.length; index++) {
192                                         if (JarImportWizard.isValidJavaProject(projects[index])) {
193                                                 final Object[] roots= getPackageFragmentRoots(projects[index]);
194                                                 if (roots.length > 0)
195                                                         set.add(projects[index]);
196                                         }
197                                 }
198                                 return set.toArray();
199                         }
200
201                         @Override
202                         protected Object[] getPackageFragmentRoots(final IJavaProject project) throws JavaModelException {
203                                 final Set<IPackageFragmentRoot> set= new HashSet<IPackageFragmentRoot>();
204                                 final IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
205                                 for (int offset= 0; offset < roots.length; offset++) {
206                                         IPackageFragmentRoot root= roots[offset];
207                                         IClasspathEntry entry= root.getRawClasspathEntry();
208                                         if (JarImportWizard.isValidClassPathEntry(entry)
209                                                         && root.getResolvedClasspathEntry().getReferencingEntry() == null)
210                                                 set.add(root);
211                                 }
212                                 return set.toArray();
213                         }
214
215                         @Override
216                         public boolean hasChildren(final Object element) {
217                                 return (element instanceof IJavaProject) || (element instanceof IJavaModel);
218                         }
219                 };
220
221                 final DecoratingLabelProvider labelProvider= new DecoratingLabelProvider(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_BASICS | JavaElementLabelProvider.SHOW_OVERLAY_ICONS | JavaElementLabelProvider.SHOW_SMALL_ICONS), new ProblemsLabelDecorator(null));
222                 fTreeViewer= new TreeViewer(parent, SWT.SINGLE | SWT.BORDER);
223                 fTreeViewer.getTree().setLayoutData(createGridData(GridData.FILL_BOTH, 6, 0));
224                 fTreeViewer.setLabelProvider(labelProvider);
225                 fTreeViewer.setContentProvider(contentProvider);
226                 fTreeViewer.addFilter(new EmptyPackageFilter());
227                 fTreeViewer.setComparator(new JavaElementComparator());
228                 fTreeViewer.setAutoExpandLevel(2);
229                 fTreeViewer.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()));
230                 final IPackageFragmentRoot root= fWizard.getPackageFragmentRoot();
231                 if (root != null) {
232                         fTreeViewer.setSelection(new StructuredSelection(new Object[] { root}), true);
233                         fTreeViewer.expandToLevel(root, 1);
234                 }
235                 fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
236
237                         public void selectionChanged(final SelectionChangedEvent event) {
238                                 handleInputChanged();
239                         }
240                 });
241                 if (contentProvider.getChildren(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot())).length == 0) {
242                         fTreeViewer.getControl().setEnabled(false);
243                         label.setEnabled(false);
244                 }
245         }
246
247         /**
248          * Creates the location group.
249          *
250          * @param parent
251          *            the parent control
252          */
253         protected void createLocationGroup(final Composite parent) {
254                 Assert.isNotNull(parent);
255                 new Label(parent, SWT.NONE).setText(JarImportMessages.JarImportWizardPage_import_label);
256                 final Composite composite= new Composite(parent, SWT.NONE);
257                 composite.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 6, 0));
258                 composite.setLayout(new GridLayout(3, false));
259                 final Label label= new Label(composite, SWT.NONE);
260                 label.setText(JarImportMessages.JarImportWizardPage_location_label);
261                 label.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_BEGINNING, 1, 0));
262                 fLocationControl= new RefactoringLocationControl(fWizard, composite, SETTING_HISTORY);
263                 fLocationControl.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 1, 0));
264                 fLocationControl.loadHistory();
265                 fLocationControl.getControl().addModifyListener(new ModifyListener() {
266
267                         public final void modifyText(final ModifyEvent event) {
268                                 handleInputChanged();
269                         }
270                 });
271                 fLocationControl.getControl().addSelectionListener(new SelectionAdapter() {
272
273                         @Override
274                         public final void widgetSelected(final SelectionEvent event) {
275                                 handleInputChanged();
276                         }
277                 });
278                 fLocationControl.setFocus();
279                 final Button button= new Button(composite, SWT.PUSH);
280                 button.setText(JarImportMessages.JarImportWizardPage_browse_button_label);
281                 button.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 1, 0));
282                 SWTUtil.setButtonDimensionHint(button);
283                 button.addSelectionListener(new SelectionAdapter() {
284
285                         @Override
286                         public final void widgetSelected(final SelectionEvent event) {
287                                 handleBrowseButtonSelected();
288                         }
289                 });
290         }
291
292         /**
293          * Creates the rename group.
294          *
295          * @param parent
296          *            the parent control
297          */
298         protected void createRenameGroup(final Composite parent) {
299                 Assert.isNotNull(parent);
300                 final JarImportData data= fWizard.getImportData();
301                 final Button button= new Button(parent, SWT.CHECK);
302                 button.setText(JarImportMessages.JarImportWizardPage_replace_jar_file);
303                 button.setSelection(!data.isRenameJarFile());
304                 button.addSelectionListener(new SelectionAdapter() {
305
306                         @Override
307                         public void widgetSelected(final SelectionEvent event) {
308                                 data.setRenameJarFile(!button.getSelection());
309                         }
310                 });
311                 if (fImportWizard && !fTreeViewer.getControl().isEnabled())
312                         button.setEnabled(false);
313                 if (!fImportWizard) {
314                         final GridData gd= new GridData();
315                         gd.horizontalIndent= IDialogConstants.HORIZONTAL_MARGIN;
316                         button.setLayoutData(gd);
317                 }
318         }
319
320         /**
321          * Handles the browse button selected event.
322          */
323         protected void handleBrowseButtonSelected() {
324                 final FileDialog file= new FileDialog(getShell(), SWT.OPEN);
325                 file.setText(JarImportMessages.JarImportWizardPage_browse_caption);
326                 file.setFilterNames(ArchiveFileFilter.ALL_ARCHIVES_FILTER_EXTENSIONS);
327                 file.setFilterExtensions(ArchiveFileFilter.ALL_ARCHIVES_FILTER_EXTENSIONS);
328                 final String path= file.open();
329                 if (path != null) {
330                         fLocationControl.setText(path);
331                         handleInputChanged();
332                 }
333         }
334
335         /**
336          * Handles the input changed event.
337          */
338         protected void handleInputChanged() {
339                 final JarImportData data= fWizard.getImportData();
340                 data.setRefactoringHistory(null);
341                 data.setRefactoringFileLocation(null);
342                 setErrorMessage(null);
343                 setMessage(null, NONE);
344                 setPageComplete(true);
345                 handleJarFileChanged();
346                 if (isPageComplete())
347                         handlePackageFragmentRootChanged();
348                 if (fImportWizard && !fTreeViewer.getControl().isEnabled())
349                         setErrorMessage(JarImportMessages.JarImportWizardPage_no_jar_files);
350                 fFirstTime= false;
351                 getContainer().updateButtons();
352         }
353
354         /**
355          * Handles the jar file changed event.
356          */
357         protected void handleJarFileChanged() {
358                 if (fLocationControl != null) {
359                         final String path= fLocationControl.getText();
360                         if ("".equals(path)) { //$NON-NLS-1$
361                                 setErrorMessage(JarImportMessages.JarImportWizardPage_empty_location);
362                                 setPageComplete(false);
363                                 return;
364                         } else {
365                                 final File file= new File(path);
366                                 if (!file.exists()) {
367                                         setErrorMessage(JarImportMessages.JarImportWizardPage_invalid_location);
368                                         setPageComplete(false);
369                                         return;
370                                 }
371                                 ZipFile zip= null;
372                                 try {
373                                         try {
374                                                 zip= new ZipFile(file, ZipFile.OPEN_READ);
375                                         } catch (IOException exception) {
376                                                 setErrorMessage(JarImportMessages.JarImportWizardPage_invalid_location);
377                                                 setPageComplete(false);
378                                                 return;
379                                         }
380                                         final JarImportData data= fWizard.getImportData();
381                                         data.setRefactoringFileLocation(URIUtil.toURI(path));
382                                         ZipEntry entry= zip.getEntry(JarPackagerUtil.getRefactoringsEntry());
383                                         if (entry == null) {
384                                                 setMessage(JarImportMessages.JarImportWizardPage_no_refactorings, INFORMATION);
385                                                 setPageComplete(true);
386                                                 return;
387                                         }
388                                         handleTimeStampChanged();
389                                         if (data.getExistingTimeStamp() > entry.getTime()) {
390                                                 setMessage(JarImportMessages.JarImportWizardPage_version_warning, WARNING);
391                                                 setPageComplete(true);
392                                                 return;
393                                         }
394                                         InputStream stream= null;
395                                         try {
396                                                 stream= zip.getInputStream(entry);
397                                                 data.setRefactoringHistory(RefactoringCore.getHistoryService().readRefactoringHistory(stream, JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING));
398                                         } catch (IOException exception) {
399                                                 setErrorMessage(JarImportMessages.JarImportWizardPage_no_refactorings);
400                                                 setPageComplete(false);
401                                                 return;
402                                         } catch (CoreException exception) {
403                                                 JavaPlugin.log(exception);
404                                                 setErrorMessage(JarImportMessages.JarImportWizardPage_no_refactorings);
405                                                 setPageComplete(false);
406                                                 return;
407                                         } finally {
408                                                 if (stream != null) {
409                                                         try {
410                                                                 stream.close();
411                                                         } catch (IOException exception) {
412                                                                 // Do nothing
413                                                         }
414                                                 }
415                                         }
416                                 } finally {
417                                         if (zip != null) {
418                                                 try {
419                                                         zip.close();
420                                                 } catch (IOException e) {
421                                                 }
422                                         }
423                                 }
424                         }
425                 }
426         }
427
428         /**
429          * Handles the package fragment root changed event.
430          */
431         protected void handlePackageFragmentRootChanged() {
432                 if (fTreeViewer != null) {
433                         final IStructuredSelection selection= (IStructuredSelection) fTreeViewer.getSelection();
434                         final Object[] elements= selection.toArray();
435                         if (elements.length != 1) {
436                                 setErrorMessage(JarImportMessages.JarImportWizardPage_select_single_jar);
437                                 setPageComplete(false);
438                                 return;
439                         } else {
440                                 final JarImportData data= fWizard.getImportData();
441                                 final Object element= elements[0];
442                                 if (element instanceof IPackageFragmentRoot)
443                                         data.setPackageFragmentRoot((IPackageFragmentRoot) element);
444                                 else if (element instanceof IPackageFragment) {
445                                         data.setPackageFragmentRoot((IPackageFragmentRoot) ((IJavaElement) element).getParent());
446                                 } else {
447                                         setErrorMessage(JarImportMessages.JarImportWizardPage_select_single_jar);
448                                         setPageComplete(false);
449                                 }
450                         }
451                 }
452         }
453
454         /**
455          * Handles the time stamp changed event.
456          */
457         protected void handleTimeStampChanged() {
458                 final IPackageFragmentRoot root= fWizard.getPackageFragmentRoot();
459                 if (root != null) {
460                         try {
461                                 final URI uri= BinaryRefactoringHistoryWizard.getLocationURI(root.getRawClasspathEntry());
462                                 if (uri != null) {
463                                         final File file= new File(uri);
464                                         if (file.exists()) {
465                                                 ZipFile zip= null;
466                                                 try {
467                                                         zip= new ZipFile(file, ZipFile.OPEN_READ);
468                                                         ZipEntry entry= zip.getEntry(JarPackagerUtil.getRefactoringsEntry());
469                                                         if (entry != null) {
470                                                                 fWizard.getImportData().setExistingTimeStamp(entry.getTime());
471                                                         }
472                                                 } catch (IOException exception) {
473                                                         // Just leave it
474                                                 } finally {
475                                                         if (zip != null) {
476                                                                 try {
477                                                                         zip.close();
478                                                                 } catch (IOException e) {
479                                                                 }
480                                                         }
481                                                 }
482                                         }
483                                 }
484                         } catch (CoreException exception) {
485                                 JavaPlugin.log(exception);
486                         }
487                 }
488         }
489
490         /**
491          * Gets called if the wizard is finished.
492          */
493         public void performFinish() {
494                 fLocationControl.saveHistory();
495         }
496
497         /**
498          * {@inheritDoc}
499          */
500         @Override
501         public void setErrorMessage(final String message) {
502                 if (!fFirstTime)
503                         super.setErrorMessage(message);
504                 else
505                         setMessage(message, NONE);
506         }
507
508         /**
509          * {@inheritDoc}
510          */
511         @Override
512         public void setVisible(final boolean visible) {
513                 super.setVisible(visible);
514                 if (visible)
515                         handleInputChanged();
516         }
517 }