1 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.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 81 public final class JarImportWizardPage extends WizardPage { 82 83 84 private static final String PAGE_NAME= "JarImportWizardPage"; 86 87 protected static final String SETTING_HISTORY= "org.eclipse.jdt.ui.refactoring.jarHistory"; 89 90 private boolean fFirstTime= true; 91 92 93 private final boolean fImportWizard; 94 95 96 private RefactoringLocationControl fLocationControl= null; 97 98 99 private TreeViewer fTreeViewer= null; 100 101 102 private final JarImportWizard fWizard; 103 104 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 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 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 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 [] getChildren(Object element) { 179 if ((element instanceof IJavaProject) || (element instanceof IJavaModel)) 180 return super.getChildren(element); 181 return new Object [0]; 182 } 183 184 protected Object [] getJavaProjects(final IJavaModel model) throws JavaModelException { 185 final Set set= new HashSet (); 186 final IJavaProject[] projects= model.getJavaProjects(); 187 for (int index= 0; index < projects.length; index++) { 188 if (JarImportWizard.isValidJavaProject(projects[index])) { 189 final Object [] 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 [] getPackageFragmentRoots(final IJavaProject project) throws JavaModelException { 198 final Set set= new HashSet (); 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 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 [] { 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 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 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 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 [] { "*.jar", "*.*"}); file.setFilterExtensions(new String [] { "*.jar", "*.*"}); final String path= file.open(); 317 if (path != null) { 318 fLocationControl.setText(path); 319 handleInputChanged(); 320 } 321 } 322 323 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 345 protected void handleJarFileChanged() { 346 if (fLocationControl != null) { 347 final String path= fLocationControl.getText(); 348 if ("".equals(path)) { setErrorMessage(JarImportMessages.JarImportWizardPage_empty_location); 350 setPageComplete(false); 351 return; 352 } else { 353 final File file= new File (path); 354 if (!file.exists()) { 355 setErrorMessage(JarImportMessages.JarImportWizardPage_invalid_location); 356 setPageComplete(false); 357 return; 358 } 359 ZipFile zip= null; 360 try { 361 try { 362 zip= new ZipFile (file, ZipFile.OPEN_READ); 363 } catch (IOException 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 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 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 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 exception) { 400 } 402 } 403 } 404 } finally { 405 if (zip != null) { 406 try { 407 zip.close(); 408 } catch (IOException e) { 409 } 410 } 411 } 412 } 413 } 414 } 415 416 419 protected void handlePackageFragmentRootChanged() { 420 if (fTreeViewer != null) { 421 final IStructuredSelection selection= (IStructuredSelection) fTreeViewer.getSelection(); 422 final Object [] 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 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 445 protected void handleTimeStampChanged() { 446 final IPackageFragmentRoot root= fWizard.getPackageFragmentRoot(); 447 if (root != null) { 448 try { 449 final URI uri= BinaryRefactoringHistoryWizard.getLocationURI(root.getRawClasspathEntry()); 450 if (uri != null) { 451 final File file= new File (uri); 452 if (file.exists()) { 453 ZipFile zip= null; 454 try { 455 zip= new ZipFile (file, ZipFile.OPEN_READ); 456 ZipEntry entry= zip.getEntry(JarPackagerUtil.getRefactoringsEntry()); 457 if (entry != null) { 458 fWizard.getImportData().setExistingTimeStamp(entry.getTime()); 459 } 460 } catch (IOException exception) { 461 } finally { 463 if (zip != null) { 464 try { 465 zip.close(); 466 } catch (IOException e) { 467 } 468 } 469 } 470 } 471 } 472 } catch (CoreException exception) { 473 JavaPlugin.log(exception); 474 } 475 } 476 } 477 478 481 public void performFinish() { 482 fLocationControl.saveHistory(); 483 } 484 485 488 public void setErrorMessage(final String message) { 489 if (!fFirstTime) 490 super.setErrorMessage(message); 491 else 492 setMessage(message, NONE); 493 } 494 495 498 public void setVisible(final boolean visible) { 499 super.setVisible(visible); 500 if (visible) 501 handleInputChanged(); 502 } 503 } 504 | Popular Tags |