1 11 package org.eclipse.jdt.internal.ui.jarpackager; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 20 import java.util.jar.Attributes ; 21 import java.util.jar.Manifest ; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.Path; 27 28 import org.eclipse.core.resources.IFile; 29 import org.eclipse.core.resources.IResource; 30 import org.eclipse.core.resources.IWorkspace; 31 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.events.KeyAdapter; 34 import org.eclipse.swt.events.KeyEvent; 35 import org.eclipse.swt.events.SelectionAdapter; 36 import org.eclipse.swt.events.SelectionEvent; 37 import org.eclipse.swt.layout.GridData; 38 import org.eclipse.swt.layout.GridLayout; 39 import org.eclipse.swt.widgets.Button; 40 import org.eclipse.swt.widgets.Composite; 41 import org.eclipse.swt.widgets.Control; 42 import org.eclipse.swt.widgets.Event; 43 import org.eclipse.swt.widgets.Label; 44 import org.eclipse.swt.widgets.Listener; 45 import org.eclipse.swt.widgets.Text; 46 47 import org.eclipse.jface.dialogs.Dialog; 48 import org.eclipse.jface.dialogs.IDialogSettings; 49 import org.eclipse.jface.dialogs.IMessageProvider; 50 import org.eclipse.jface.resource.JFaceResources; 51 import org.eclipse.jface.viewers.DecoratingLabelProvider; 52 import org.eclipse.jface.window.Window; 53 import org.eclipse.jface.wizard.IWizardPage; 54 import org.eclipse.jface.wizard.WizardPage; 55 56 import org.eclipse.ui.PlatformUI; 57 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 58 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 59 import org.eclipse.ui.dialogs.SaveAsDialog; 60 import org.eclipse.ui.dialogs.SelectionDialog; 61 62 import org.eclipse.jdt.core.ICompilationUnit; 63 import org.eclipse.jdt.core.IPackageFragment; 64 import org.eclipse.jdt.core.IPackageFragmentRoot; 65 import org.eclipse.jdt.core.IType; 66 import org.eclipse.jdt.core.JavaCore; 67 import org.eclipse.jdt.core.JavaModelException; 68 import org.eclipse.jdt.core.search.IJavaSearchScope; 69 70 import org.eclipse.jdt.internal.corext.util.Messages; 71 72 import org.eclipse.jdt.ui.JavaElementComparator; 73 import org.eclipse.jdt.ui.JavaElementLabelProvider; 74 import org.eclipse.jdt.ui.JavaUI; 75 import org.eclipse.jdt.ui.ProblemsLabelDecorator; 76 import org.eclipse.jdt.ui.StandardJavaElementContentProvider; 77 import org.eclipse.jdt.ui.jarpackager.JarPackageData; 78 79 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 80 import org.eclipse.jdt.internal.ui.JavaPlugin; 81 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 82 import org.eclipse.jdt.internal.ui.filters.EmptyInnerPackageFilter; 83 import org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory; 84 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 85 import org.eclipse.jdt.internal.ui.util.MainMethodSearchEngine; 86 import org.eclipse.jdt.internal.ui.util.SWTUtil; 87 import org.eclipse.jdt.internal.ui.viewsupport.LibraryFilter; 88 89 92 class JarManifestWizardPage extends WizardPage implements IJarPackageWizardPage { 93 94 private class UntypedListener implements Listener { 96 99 public void handleEvent(Event e) { 100 if (getControl() == null) 101 return; 102 update(); 103 } 104 } 105 private UntypedListener fUntypedListener= new UntypedListener(); 106 107 private JarPackageData fJarPackage; 109 110 private IType[] fMainTypes; 112 113 private Composite fManifestGroup; 115 private Button fGenerateManifestRadioButton; 116 private Button fSaveManifestCheckbox; 117 private Button fReuseManifestCheckbox; 118 private Text fNewManifestFileText; 119 private Label fNewManifestFileLabel; 120 private Button fNewManifestFileBrowseButton; 121 private Button fUseManifestRadioButton; 122 private Text fManifestFileText; 123 private Label fManifestFileLabel; 124 private Button fManifestFileBrowseButton; 125 126 private Label fSealingHeaderLabel; 127 private Button fSealJarRadioButton; 128 private Label fSealJarLabel; 129 private Button fSealedPackagesDetailsButton; 130 private Button fSealPackagesRadioButton; 131 private Label fSealPackagesLabel; 132 private Button fUnSealedPackagesDetailsButton; 133 134 private Label fMainClassHeaderLabel; 135 private Label fMainClassLabel; 136 private Text fMainClassText; 137 private Button fMainClassBrowseButton; 138 139 private final static String PAGE_NAME= "JarManifestWizardPage"; 142 private final static String STORE_GENERATE_MANIFEST= PAGE_NAME + ".GENERATE_MANIFEST"; private final static String STORE_SAVE_MANIFEST= PAGE_NAME + ".SAVE_MANIFEST"; private final static String STORE_REUSE_MANIFEST= PAGE_NAME + ".REUSE_MANIFEST"; private final static String STORE_MANIFEST_LOCATION= PAGE_NAME + ".MANIFEST_LOCATION"; 148 private final static String STORE_SEAL_JAR= PAGE_NAME + ".SEAL_JAR"; 151 154 public JarManifestWizardPage(JarPackageData jarPackage) { 155 super(PAGE_NAME); 156 setTitle(JarPackagerMessages.JarManifestWizardPage_title); 157 setDescription(JarPackagerMessages.JarManifestWizardPage_description); 158 fJarPackage= jarPackage; 159 } 160 161 163 166 public void createControl(Composite parent) { 167 168 initializeDialogUnits(parent); 169 170 Composite composite= new Composite(parent, SWT.NULL); 171 composite.setLayout(new GridLayout()); 172 composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL)); 173 174 createLabel(composite, JarPackagerMessages.JarManifestWizardPage_manifestSource_label, false); 175 createManifestGroup(composite); 176 177 createSpacer(composite); 178 179 fSealingHeaderLabel= createLabel(composite, JarPackagerMessages.JarManifestWizardPage_sealingHeader_label, false); 180 createSealingGroup(composite); 181 182 createSpacer(composite); 183 184 fMainClassHeaderLabel= createLabel(composite, JarPackagerMessages.JarManifestWizardPage_mainClassHeader_label, false); 185 createMainClassGroup(composite); 186 187 setEqualButtonSizes(); 188 189 restoreWidgetValues(); 190 191 setControl(composite); 192 update(); 193 194 Dialog.applyDialogFont(composite); 195 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.JARMANIFEST_WIZARD_PAGE); 196 197 } 198 203 protected void createManifestGroup(Composite parent) { 204 fManifestGroup= new Composite(parent, SWT.NONE); 205 GridLayout layout= new GridLayout(); 206 fManifestGroup.setLayout(layout); 207 fManifestGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 208 209 fGenerateManifestRadioButton= new Button(fManifestGroup, SWT.RADIO | SWT.LEFT); 210 fGenerateManifestRadioButton.setText(JarPackagerMessages.JarManifestWizardPage_genetateManifest_text); 211 fGenerateManifestRadioButton.addListener(SWT.Selection, fUntypedListener); 212 213 Composite saveOptions= new Composite(fManifestGroup, SWT.NONE); 214 GridLayout saveOptionsLayout= new GridLayout(); 215 saveOptionsLayout.marginWidth= 0; 216 saveOptions.setLayout(saveOptionsLayout); 217 218 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 219 data.horizontalIndent=20; 220 saveOptions.setLayoutData(data); 221 222 fSaveManifestCheckbox= new Button(saveOptions, SWT.CHECK | SWT.LEFT); 223 fSaveManifestCheckbox.setText(JarPackagerMessages.JarManifestWizardPage_saveManifest_text); 224 fSaveManifestCheckbox.addListener(SWT.MouseUp, fUntypedListener); 225 226 fReuseManifestCheckbox= new Button(saveOptions, SWT.CHECK | SWT.LEFT); 227 fReuseManifestCheckbox.setText(JarPackagerMessages.JarManifestWizardPage_reuseManifest_text); 228 fReuseManifestCheckbox.addListener(SWT.MouseUp, fUntypedListener); 229 230 createNewManifestFileGroup(saveOptions); 231 232 fUseManifestRadioButton= new Button(fManifestGroup, SWT.RADIO | SWT.LEFT); 233 fUseManifestRadioButton.setText(JarPackagerMessages.JarManifestWizardPage_useManifest_text); 234 235 fUseManifestRadioButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 236 Composite existingManifestGroup= new Composite(fManifestGroup, SWT.NONE); 237 GridLayout existingManifestLayout= new GridLayout(); 238 existingManifestLayout.marginWidth= 0; 239 existingManifestGroup.setLayout(existingManifestLayout); 240 data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 241 data.horizontalIndent=20; 242 existingManifestGroup.setLayoutData(data); 243 createManifestFileGroup(existingManifestGroup); 244 } 245 246 protected void createNewManifestFileGroup(Composite parent) { 247 Composite manifestFileGroup= new Composite(parent, SWT.NONE); 249 GridLayout layout= new GridLayout(); 250 layout.marginWidth= 0; 251 layout.numColumns= 3; 252 manifestFileGroup.setLayout(layout); 253 manifestFileGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 254 255 fNewManifestFileLabel= new Label(manifestFileGroup, SWT.NONE); 256 fNewManifestFileLabel.setText(JarPackagerMessages.JarManifestWizardPage_newManifestFile_text); 257 258 fNewManifestFileText= new Text(manifestFileGroup, SWT.SINGLE | SWT.BORDER); 260 fNewManifestFileText.addListener(SWT.Modify, fUntypedListener); 261 262 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 263 data.widthHint= convertWidthInCharsToPixels(40); 264 fNewManifestFileText.setLayoutData(data); 265 266 fNewManifestFileBrowseButton= new Button(manifestFileGroup, SWT.PUSH); 268 fNewManifestFileBrowseButton.setText(JarPackagerMessages.JarManifestWizardPage_newManifestFileBrowseButton_text); 269 fNewManifestFileBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 270 fNewManifestFileBrowseButton.addSelectionListener(new SelectionAdapter() { 271 public void widgetSelected(SelectionEvent e) { 272 handleNewManifestFileBrowseButtonPressed(); 273 } 274 }); 275 } 276 277 protected void createManifestFileGroup(Composite parent) { 278 Composite manifestFileGroup= new Composite(parent, SWT.NONE); 280 GridLayout layout= new GridLayout(); 281 layout.numColumns= 3; 282 layout.marginWidth= 0; 283 284 manifestFileGroup.setLayout(layout); 285 manifestFileGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 286 287 fManifestFileLabel= new Label(manifestFileGroup, SWT.NONE); 288 fManifestFileLabel.setText(JarPackagerMessages.JarManifestWizardPage_manifestFile_text); 289 290 fManifestFileText= new Text(manifestFileGroup, SWT.SINGLE | SWT.BORDER); 292 fManifestFileText.addListener(SWT.Modify, fUntypedListener); 293 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 294 data.widthHint= convertWidthInCharsToPixels(40); 295 fManifestFileText.setLayoutData(data); 296 297 fManifestFileBrowseButton= new Button(manifestFileGroup, SWT.PUSH); 299 fManifestFileBrowseButton.setText(JarPackagerMessages.JarManifestWizardPage_manifestFileBrowse_text); 300 fManifestFileBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 301 fManifestFileBrowseButton.addSelectionListener(new SelectionAdapter() { 302 public void widgetSelected(SelectionEvent e) { 303 handleManifestFileBrowseButtonPressed(); 304 } 305 }); 306 } 307 312 protected void createSealingGroup(Composite parent) { 313 Composite sealingGroup= new Composite(parent, SWT.NONE); 315 GridLayout layout= new GridLayout(); 316 layout.numColumns= 3; 317 layout.horizontalSpacing += 3; 318 sealingGroup.setLayout(layout); 319 sealingGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); 320 createSealJarGroup(sealingGroup); 321 createSealPackagesGroup(sealingGroup); 322 323 } 324 329 protected void createSealJarGroup(Composite sealGroup) { 330 fSealJarRadioButton= new Button(sealGroup, SWT.RADIO); 331 fSealJarRadioButton.setText(JarPackagerMessages.JarManifestWizardPage_sealJar_text); 332 fSealJarRadioButton.addListener(SWT.Selection, fUntypedListener); 333 334 fSealJarLabel= new Label(sealGroup, SWT.RIGHT); 335 fSealJarLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 336 fSealJarLabel.setText(""); 338 fUnSealedPackagesDetailsButton= new Button(sealGroup, SWT.PUSH); 339 fUnSealedPackagesDetailsButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 340 fUnSealedPackagesDetailsButton.setText(JarPackagerMessages.JarManifestWizardPage_unsealPackagesButton_text); 341 fUnSealedPackagesDetailsButton.addSelectionListener(new SelectionAdapter() { 342 public void widgetSelected(SelectionEvent e) { 343 handleUnSealPackagesDetailsButtonPressed(); 344 } 345 }); 346 347 } 348 353 protected void createSealPackagesGroup(Composite sealGroup) { 354 fSealPackagesRadioButton= new Button(sealGroup, SWT.RADIO); 355 fSealPackagesRadioButton.setText(JarPackagerMessages.JarManifestWizardPage_sealPackagesButton_text); 356 357 fSealPackagesLabel= new Label(sealGroup, SWT.RIGHT); 358 fSealPackagesLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 359 fSealPackagesLabel.setText(""); 361 fSealedPackagesDetailsButton= new Button(sealGroup, SWT.PUSH); 362 fSealedPackagesDetailsButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 363 fSealedPackagesDetailsButton.setText(JarPackagerMessages.JarManifestWizardPage_sealedPackagesDetailsButton_text); 364 fSealedPackagesDetailsButton.addSelectionListener(new SelectionAdapter() { 365 public void widgetSelected(SelectionEvent e) { 366 handleSealPackagesDetailsButtonPressed(); 367 } 368 }); 369 } 370 371 protected void createMainClassGroup(Composite parent) { 372 Composite mainClassGroup= new Composite(parent, SWT.NONE); 374 GridLayout layout= new GridLayout(); 375 layout.numColumns= 3; 376 mainClassGroup.setLayout(layout); 377 mainClassGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); 378 379 fMainClassLabel= new Label(mainClassGroup, SWT.NONE); 380 fMainClassLabel.setText(JarPackagerMessages.JarManifestWizardPage_mainClass_label); 381 382 fMainClassText= new Text(mainClassGroup, SWT.SINGLE | SWT.BORDER); 384 fMainClassText.addListener(SWT.Modify, fUntypedListener); 385 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 386 data.widthHint= convertWidthInCharsToPixels(40); 387 fMainClassText.setLayoutData(data); 388 fMainClassText.addKeyListener(new KeyAdapter() { 389 392 public void keyReleased(KeyEvent e) { 393 fJarPackage.setManifestMainClass(findMainMethodByName(fMainClassText.getText())); 394 update(); 395 } 396 }); 397 398 fMainClassBrowseButton= new Button(mainClassGroup, SWT.PUSH); 400 fMainClassBrowseButton.setText(JarPackagerMessages.JarManifestWizardPage_mainClassBrowseButton_text); 401 fMainClassBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 402 fMainClassBrowseButton.addSelectionListener(new SelectionAdapter() { 403 public void widgetSelected(SelectionEvent e) { 404 handleMainClassBrowseButtonPressed(); 405 } 406 }); 407 } 408 409 411 private void update() { 412 updateModel(); 413 updateEnableState(); 414 updatePageCompletion(); 415 } 416 417 421 protected void handleNewManifestFileBrowseButtonPressed() { 422 SaveAsDialog dialog= new SaveAsDialog(getContainer().getShell()); 424 dialog.create(); 425 dialog.getShell().setText(JarPackagerMessages.JarManifestWizardPage_saveAsDialog_title); 426 dialog.setMessage(JarPackagerMessages.JarManifestWizardPage_saveAsDialog_message); 427 dialog.setOriginalFile(createFileHandle(fJarPackage.getManifestLocation())); 428 if (dialog.open() == Window.OK) { 429 fJarPackage.setManifestLocation(dialog.getResult()); 430 fNewManifestFileText.setText(dialog.getResult().toString()); 431 } 432 } 433 434 protected void handleManifestFileBrowseButtonPressed() { 435 ElementTreeSelectionDialog dialog= createWorkspaceFileSelectionDialog(JarPackagerMessages.JarManifestWizardPage_manifestSelectionDialog_title, JarPackagerMessages.JarManifestWizardPage_manifestSelectionDialog_message); 436 if (fJarPackage.isManifestAccessible()) 437 dialog.setInitialSelections(new IResource[] {fJarPackage.getManifestFile()}); 438 if (dialog.open() == Window.OK) { 439 Object [] resources= dialog.getResult(); 440 if (resources.length != 1) 441 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_onlyOneManifestMustBeSelected); 442 else { 443 setErrorMessage(""); fJarPackage.setManifestLocation(((IResource)resources[0]).getFullPath()); 445 fManifestFileText.setText(fJarPackage.getManifestLocation().toString()); 446 } 447 } 448 } 449 450 private IType findMainMethodByName(String name) { 451 if (fMainTypes == null) { 452 List resources= JarPackagerUtil.asResources(fJarPackage.getElements()); 453 if (resources == null) { 454 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_noResourceSelected); 455 return null; 456 } 457 IJavaSearchScope searchScope= JavaSearchScopeFactory.getInstance().createJavaSearchScope((IResource[])resources.toArray(new IResource[resources.size()]), true); 458 MainMethodSearchEngine engine= new MainMethodSearchEngine(); 459 try { 460 fMainTypes= engine.searchMainMethods(getContainer(), searchScope, 0); 461 } catch (InvocationTargetException ex) { 462 } catch (InterruptedException e) { 464 } 466 } 467 for (int i= 0; i < fMainTypes.length; i++) { 468 if (fMainTypes[i].getFullyQualifiedName().equals(name)) 469 return fMainTypes[i]; 470 } 471 return null; 472 } 473 474 protected void handleMainClassBrowseButtonPressed() { 475 List resources= JarPackagerUtil.asResources(fJarPackage.getElements()); 476 if (resources == null) { 477 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_noResourceSelected); 478 return; 479 } 480 IJavaSearchScope searchScope= JavaSearchScopeFactory.getInstance().createJavaSearchScope((IResource[])resources.toArray(new IResource[resources.size()]), true); 481 SelectionDialog dialog= JavaUI.createMainTypeDialog(getContainer().getShell(), getContainer(), searchScope, 0, false, ""); dialog.setTitle(JarPackagerMessages.JarManifestWizardPage_mainTypeSelectionDialog_title); 483 dialog.setMessage(JarPackagerMessages.JarManifestWizardPage_mainTypeSelectionDialog_message); 484 if (fJarPackage.getManifestMainClass() != null) 485 dialog.setInitialSelections(new Object [] {fJarPackage.getManifestMainClass()}); 486 487 if (dialog.open() == Window.OK) { 488 fJarPackage.setManifestMainClass((IType)dialog.getResult()[0]); 489 fMainClassText.setText(JarPackagerUtil.getMainClassName(fJarPackage)); 490 } else if (!fJarPackage.isMainClassValid(getContainer())) { 491 fJarPackage.setManifestMainClass(null); 493 fMainClassText.setText(JarPackagerUtil.getMainClassName(fJarPackage)); 494 } 495 } 496 497 protected void handleSealPackagesDetailsButtonPressed() { 498 SelectionDialog dialog= createPackageDialog(getPackagesForSelectedResources(fJarPackage)); 499 dialog.setTitle(JarPackagerMessages.JarManifestWizardPage_sealedPackagesSelectionDialog_title); 500 dialog.setMessage(JarPackagerMessages.JarManifestWizardPage_sealedPackagesSelectionDialog_message); 501 dialog.setInitialSelections(fJarPackage.getPackagesToSeal()); 502 if (dialog.open() == Window.OK) 503 fJarPackage.setPackagesToSeal(getPackagesFromDialog(dialog)); 504 updateSealingInfo(); 505 } 506 507 protected void handleUnSealPackagesDetailsButtonPressed() { 508 SelectionDialog dialog= createPackageDialog(getPackagesForSelectedResources(fJarPackage)); 509 dialog.setTitle(JarPackagerMessages.JarManifestWizardPage_unsealedPackagesSelectionDialog_title); 510 dialog.setMessage(JarPackagerMessages.JarManifestWizardPage_unsealedPackagesSelectionDialog_message); 511 dialog.setInitialSelections(fJarPackage.getPackagesToUnseal()); 512 if (dialog.open() == Window.OK) 513 fJarPackage.setPackagesToUnseal(getPackagesFromDialog(dialog)); 514 updateSealingInfo(); 515 } 516 519 protected void updateEnableState() { 520 boolean generate= fGenerateManifestRadioButton.getSelection(); 521 522 boolean save= generate && fSaveManifestCheckbox.getSelection(); 523 fSaveManifestCheckbox.setEnabled(generate); 524 fReuseManifestCheckbox.setEnabled(fJarPackage.isDescriptionSaved() && save); 525 fNewManifestFileText.setEnabled(save); 526 fNewManifestFileLabel.setEnabled(save); 527 fNewManifestFileBrowseButton.setEnabled(save); 528 529 fManifestFileText.setEnabled(!generate); 530 fManifestFileLabel.setEnabled(!generate); 531 fManifestFileBrowseButton.setEnabled(!generate); 532 533 fSealingHeaderLabel.setEnabled(generate); 534 boolean sealState= fSealJarRadioButton.getSelection(); 535 fSealJarRadioButton.setEnabled(generate); 536 fSealJarLabel.setEnabled(generate); 537 fUnSealedPackagesDetailsButton.setEnabled(sealState && generate); 538 fSealPackagesRadioButton.setEnabled(generate); 539 fSealPackagesLabel.setEnabled(generate); 540 fSealedPackagesDetailsButton.setEnabled(!sealState && generate); 541 542 fMainClassHeaderLabel.setEnabled(generate); 543 fMainClassLabel.setEnabled(generate); 544 fMainClassText.setEnabled(generate); 545 fMainClassBrowseButton.setEnabled(generate); 546 547 updateSealingInfo(); 548 } 549 550 protected void updateSealingInfo() { 551 if (fJarPackage.isJarSealed()) { 552 fSealPackagesLabel.setText(""); int i= fJarPackage.getPackagesToUnseal().length; 554 if (i == 0) 555 fSealJarLabel.setText(JarPackagerMessages.JarManifestWizardPage_jarSealed); 556 else if (i == 1) 557 fSealJarLabel.setText(JarPackagerMessages.JarManifestWizardPage_jarSealedExceptOne); 558 else 559 fSealJarLabel.setText(Messages.format(JarPackagerMessages.JarManifestWizardPage_jarSealedExceptSome, new Integer (i))); 560 561 } 562 else { 563 fSealJarLabel.setText(""); int i= fJarPackage.getPackagesToSeal().length; 565 if (i == 0) 566 fSealPackagesLabel.setText(JarPackagerMessages.JarManifestWizardPage_nothingSealed); 567 else if (i == 1) 568 fSealPackagesLabel.setText(JarPackagerMessages.JarManifestWizardPage_onePackageSealed); 569 else 570 fSealPackagesLabel.setText(Messages.format(JarPackagerMessages.JarManifestWizardPage_somePackagesSealed, new Integer (i))); 571 } 572 } 573 576 public boolean isPageComplete() { 577 boolean isPageComplete= true; 578 setMessage(null); 579 580 if (!fJarPackage.areGeneratedFilesExported()) 581 return true; 582 583 if (fJarPackage.isManifestGenerated() && fJarPackage.isManifestSaved()) { 584 if (fJarPackage.getManifestLocation().toString().length() == 0) 585 isPageComplete= false; 586 else { 587 IPath location= fJarPackage.getManifestLocation(); 588 if (!location.toString().startsWith("/")) { setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_manifestPathMustBeAbsolute); 590 return false; 591 } 592 IResource resource= findResource(location); 593 if (resource != null && resource.getType() != IResource.FILE) { 594 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_manifestMustNotBeExistingContainer); 595 return false; 596 } 597 resource= findResource(location.removeLastSegments(1)); 598 if (resource == null || resource.getType() == IResource.FILE) { 599 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_manifestContainerDoesNotExist); 600 return false; 601 } 602 } 603 } 604 if (!fJarPackage.isManifestGenerated()) { 605 if (fJarPackage.isManifestAccessible()) { 606 Manifest manifest= null; 607 try { 608 manifest= fJarPackage.getManifestProvider().create(fJarPackage); 609 } catch (CoreException ex) { 610 } 612 if (manifest != null && manifest.getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION) == null) 613 setMessage(JarPackagerMessages.JarManifestWizardPage_warning_noManifestVersion, IMessageProvider.WARNING); 614 } else { 615 if (fJarPackage.getManifestLocation().toString().length() == 0) 616 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_noManifestFile); 617 else 618 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_invalidManifestFile); 619 return false; 620 } 621 } 622 Set selectedPackages= getPackagesForSelectedResources(fJarPackage); 623 if (fJarPackage.isJarSealed() 624 && !selectedPackages.containsAll(Arrays.asList(fJarPackage.getPackagesToUnseal()))) { 625 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_unsealedPackagesNotInSelection); 626 return false; 627 } 628 if (!fJarPackage.isJarSealed() 629 && !selectedPackages.containsAll(Arrays.asList(fJarPackage.getPackagesToSeal()))) { 630 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_sealedPackagesNotInSelection); 631 return false; 632 } 633 if (!fJarPackage.isMainClassValid(getContainer()) || (fJarPackage.getManifestMainClass() == null && fMainClassText.getText().length() > 0)) { 634 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_invalidMainClass); 635 return false; 636 } 637 638 setErrorMessage(null); 639 return isPageComplete; 640 } 641 642 645 public void setPreviousPage(IWizardPage page) { 646 super.setPreviousPage(page); 647 fMainTypes= null; 648 updateEnableState(); 649 if (getContainer() != null) 650 updatePageCompletion(); 651 } 652 653 656 public void finish() { 657 saveWidgetValues(); 658 } 659 660 662 668 public final void saveWidgetValues() { 669 IDialogSettings settings= getDialogSettings(); 670 if (settings != null) { 671 settings.put(STORE_GENERATE_MANIFEST, fJarPackage.isManifestGenerated()); 673 settings.put(STORE_SAVE_MANIFEST, fJarPackage.isManifestSaved()); 674 settings.put(STORE_REUSE_MANIFEST, fJarPackage.isManifestReused()); 675 settings.put(STORE_MANIFEST_LOCATION, fJarPackage.getManifestLocation().toString()); 676 677 settings.put(STORE_SEAL_JAR, fJarPackage.isJarSealed()); 679 } 680 681 internalSaveWidgetValues(); 683 } 684 687 protected void internalSaveWidgetValues() { 688 } 689 693 protected void restoreWidgetValues() { 694 if (!((JarPackageWizard)getWizard()).isInitializingFromJarPackage()) 695 initializeJarPackage(); 696 697 if (fJarPackage.isManifestGenerated()) 699 fGenerateManifestRadioButton.setSelection(true); 700 else 701 fUseManifestRadioButton.setSelection(true); 702 fSaveManifestCheckbox.setSelection(fJarPackage.isManifestSaved()); 703 fReuseManifestCheckbox.setSelection(fJarPackage.isManifestReused()); 704 fManifestFileText.setText(fJarPackage.getManifestLocation().toString()); 705 fNewManifestFileText.setText(fJarPackage.getManifestLocation().toString()); 706 707 if (fJarPackage.isJarSealed()) 709 fSealJarRadioButton.setSelection(true); 710 else 711 fSealPackagesRadioButton.setSelection(true); 712 713 fMainClassText.setText(JarPackagerUtil.getMainClassName(fJarPackage)); 715 } 716 719 protected void initializeJarPackage() { 720 IDialogSettings settings= getDialogSettings(); 721 if (settings != null) { 722 fJarPackage.setGenerateManifest(settings.getBoolean(STORE_GENERATE_MANIFEST)); 724 fJarPackage.setSaveManifest(settings.getBoolean(STORE_SAVE_MANIFEST)); 725 fJarPackage.setReuseManifest(settings.getBoolean(STORE_REUSE_MANIFEST)); 726 String pathStr= settings.get(STORE_MANIFEST_LOCATION); 727 if (pathStr == null) 728 pathStr= ""; fJarPackage.setManifestLocation(new Path(pathStr)); 730 731 fJarPackage.setSealJar(settings.getBoolean(STORE_SEAL_JAR)); 733 } 734 } 735 738 protected void updateModel() { 739 if (getControl() == null) 740 return; 741 742 fJarPackage.setGenerateManifest(fGenerateManifestRadioButton.getSelection()); 744 fJarPackage.setSaveManifest(fSaveManifestCheckbox.getSelection()); 745 fJarPackage.setReuseManifest(fReuseManifestCheckbox.getSelection()); 746 String path; 747 if (fJarPackage.isManifestGenerated()) 748 path= fNewManifestFileText.getText(); 749 else 750 path= fManifestFileText.getText(); 751 if (path == null) 752 path= ""; fJarPackage.setManifestLocation(new Path(path)); 754 755 fJarPackage.setSealJar(fSealJarRadioButton.getSelection()); 757 } 758 761 protected void updatePageCompletion() { 762 boolean pageComplete= isPageComplete(); 763 setPageComplete(pageComplete); 764 if (pageComplete) { 765 setErrorMessage(null); 766 } 767 } 768 769 771 779 protected IFile createFileHandle(IPath filePath) { 780 if (filePath.isValidPath(filePath.toString()) && filePath.segmentCount() >= 2) 781 return JavaPlugin.getWorkspace().getRoot().getFile(filePath); 782 else 783 return null; 784 } 785 792 protected Label createLabel(Composite parent, String text, boolean bold) { 793 Label label= new Label(parent, SWT.NONE); 794 if (bold) 795 label.setFont(JFaceResources.getBannerFont()); 796 label.setText(text); 797 GridData data= new GridData(); 798 data.verticalAlignment= GridData.FILL; 799 data.horizontalAlignment= GridData.FILL; 800 label.setLayoutData(data); 801 return label; 802 } 803 810 protected void setSize(Control control, int width, int height) { 811 GridData gd= new GridData(GridData.END); 812 gd.widthHint= width ; 813 gd.heightHint= height; 814 control.setLayoutData(gd); 815 } 816 819 protected void setEqualButtonSizes() { 820 int width= SWTUtil.getButtonWidthHint(fManifestFileBrowseButton); 821 int width2= SWTUtil.getButtonWidthHint(fNewManifestFileBrowseButton); 822 width= Math.max(width, width2); 823 824 width2= SWTUtil.getButtonWidthHint(fSealedPackagesDetailsButton); 825 width= Math.max(width, width2); 826 827 width2= SWTUtil.getButtonWidthHint(fUnSealedPackagesDetailsButton); 828 width= Math.max(width, width2); 829 830 width2= SWTUtil.getButtonWidthHint(fMainClassBrowseButton); 831 width= Math.max(width, width2); 832 833 setSize(fManifestFileBrowseButton, width, SWT.DEFAULT); 834 setSize(fNewManifestFileBrowseButton, width, SWT.DEFAULT); 835 setSize(fSealedPackagesDetailsButton, width, SWT.DEFAULT); 836 setSize(fUnSealedPackagesDetailsButton, width, SWT.DEFAULT); 837 setSize(fMainClassBrowseButton, width, SWT.DEFAULT); 838 } 839 840 845 protected void createSpacer(Composite parent) { 846 Label spacer= new Label(parent, SWT.NONE); 847 GridData data= new GridData(); 848 data.horizontalAlignment= GridData.FILL; 849 data.verticalAlignment= GridData.BEGINNING; 850 spacer.setLayoutData(data); 851 } 852 858 protected IResource findResource(IPath path) { 859 IWorkspace workspace= JavaPlugin.getWorkspace(); 860 IStatus result= workspace.validatePath( 861 path.toString(), 862 IResource.ROOT | IResource.PROJECT | IResource.FOLDER | IResource.FILE); 863 if (result.isOK() && workspace.getRoot().exists(path)) 864 return workspace.getRoot().findMember(path); 865 return null; 866 } 867 868 protected IPath getPathFromString(String text) { 869 return new Path(text).makeAbsolute(); 870 } 871 881 protected SelectionDialog createPackageDialog(Set packageFragments) { 882 List packages= new ArrayList (packageFragments.size()); 883 for (Iterator iter= packageFragments.iterator(); iter.hasNext();) { 884 IPackageFragment fragment= (IPackageFragment)iter.next(); 885 boolean containsJavaElements= false; 886 int kind; 887 try { 888 kind= fragment.getKind(); 889 containsJavaElements= fragment.getChildren().length > 0; 890 } catch (JavaModelException ex) { 891 ExceptionHandler.handle(ex, getContainer().getShell(), JarPackagerMessages.JarManifestWizardPage_error_jarPackageWizardError_title, Messages.format(JarPackagerMessages.JarManifestWizardPage_error_jarPackageWizardError_message, fragment.getElementName())); 892 continue; 893 } 894 if (kind != IPackageFragmentRoot.K_BINARY && containsJavaElements) 895 packages.add(fragment); 896 } 897 StandardJavaElementContentProvider cp= new StandardJavaElementContentProvider() { 898 public boolean hasChildren(Object element) { 899 return !(element instanceof IPackageFragment) && super.hasChildren(element); 901 } 902 }; 903 final DecoratingLabelProvider provider= new DecoratingLabelProvider(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT), new ProblemsLabelDecorator(null)); 904 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getContainer().getShell(), provider, cp); 905 dialog.setDoubleClickSelects(false); 906 dialog.setComparator(new JavaElementComparator()); 907 dialog.setInput(JavaCore.create(JavaPlugin.getWorkspace().getRoot())); 908 dialog.addFilter(new EmptyInnerPackageFilter()); 909 dialog.addFilter(new LibraryFilter()); 910 dialog.addFilter(new SealPackagesFilter(packages)); 911 dialog.setValidator(new ISelectionStatusValidator() { 912 public IStatus validate(Object [] selection) { 913 StatusInfo res= new StatusInfo(); 914 for (int i= 0; i < selection.length; i++) { 915 if (!(selection[i] instanceof IPackageFragment)) { 916 res.setError(JarPackagerMessages.JarManifestWizardPage_error_mustContainPackages); 917 return res; 918 } 919 } 920 res.setOK(); 921 return res; 922 } 923 }); 924 return dialog; 925 } 926 931 protected IPackageFragment[] getPackagesFromDialog(SelectionDialog dialog) { 932 if (dialog.getReturnCode() == Window.OK && dialog.getResult().length > 0) 933 return (IPackageFragment[])Arrays.asList(dialog.getResult()).toArray(new IPackageFragment[dialog.getResult().length]); 934 else 935 return new IPackageFragment[0]; 936 } 937 940 protected ElementTreeSelectionDialog createWorkspaceFileSelectionDialog(String title, String message) { 941 int labelFlags= JavaElementLabelProvider.SHOW_BASICS 942 | JavaElementLabelProvider.SHOW_OVERLAY_ICONS 943 | JavaElementLabelProvider.SHOW_SMALL_ICONS; 944 final DecoratingLabelProvider provider= new DecoratingLabelProvider(new JavaElementLabelProvider(labelFlags), new ProblemsLabelDecorator(null)); 945 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), provider, new StandardJavaElementContentProvider()); 946 dialog.setComparator(new JavaElementComparator()); 947 dialog.setAllowMultiple(false); 948 dialog.setValidator(new ISelectionStatusValidator() { 949 public IStatus validate(Object [] selection) { 950 StatusInfo res= new StatusInfo(); 951 if (selection.length == 1 && (selection[0] instanceof IFile)) 953 res.setOK(); 954 else 955 res.setError(""); return res; 957 } 958 }); 959 dialog.addFilter(new EmptyInnerPackageFilter()); 960 dialog.addFilter(new LibraryFilter()); 961 dialog.setTitle(title); 962 dialog.setMessage(message); 963 dialog.setStatusLineAboveButtons(true); 964 dialog.setInput(JavaCore.create(JavaPlugin.getWorkspace().getRoot())); 965 return dialog; 966 } 967 968 972 private Set getPackagesForSelectedResources(JarPackageData jarPackage) { 973 Set packages= new HashSet (); 974 int n= fJarPackage.getElements().length; 975 for (int i= 0; i < n; i++) { 976 Object element= fJarPackage.getElements()[i]; 977 if (element instanceof ICompilationUnit) { 978 packages.add(((ICompilationUnit) element).getParent()); 979 } 980 } 981 return packages; 982 } 983 } 984 | Popular Tags |