1 11 package org.eclipse.jdt.internal.ui.jarpackager; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.resources.IWorkspace; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Path; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.SelectionAdapter; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.layout.GridLayout; 26 import org.eclipse.swt.widgets.Button; 27 import org.eclipse.swt.widgets.Composite; 28 import org.eclipse.swt.widgets.Event; 29 import org.eclipse.swt.widgets.Label; 30 import org.eclipse.swt.widgets.Listener; 31 import org.eclipse.swt.widgets.Text; 32 33 import org.eclipse.jface.dialogs.Dialog; 34 import org.eclipse.jface.dialogs.IDialogSettings; 35 import org.eclipse.jface.resource.JFaceResources; 36 import org.eclipse.jface.window.Window; 37 import org.eclipse.jface.wizard.IWizardPage; 38 import org.eclipse.jface.wizard.WizardPage; 39 40 import org.eclipse.ui.dialogs.SaveAsDialog; 41 import org.eclipse.ui.PlatformUI; 42 43 import org.eclipse.jdt.internal.corext.util.Messages; 44 45 import org.eclipse.jdt.ui.jarpackager.JarPackageData; 46 47 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 48 import org.eclipse.jdt.internal.ui.JavaPlugin; 49 50 import org.eclipse.jdt.internal.ui.util.SWTUtil; 51 52 55 class JarOptionsPage extends WizardPage implements IJarPackageWizardPage { 56 57 private class UntypedListener implements Listener { 59 62 public void handleEvent(Event e) { 63 if (getControl() == null) 64 return; 65 update(); 66 } 67 } 68 69 private JarPackageData fJarPackage; 70 71 private Button fExportErrorsCheckbox; 73 private Button fExportWarningsCheckbox; 74 private Button fUseSourceFoldersCheckbox; 75 private Composite fDescriptionFileGroup; 76 private Button fSaveDescriptionCheckbox; 77 private Label fDescriptionFileLabel; 78 private Text fDescriptionFileText; 79 private Button fDescriptionFileBrowseButton; 80 private Button fBuildIfNeededCheckbox; 81 82 private final static String PAGE_NAME= "jarOptionsWizardPage"; 85 private final static String STORE_EXPORT_WARNINGS= PAGE_NAME + ".EXPORT_WARNINGS"; private final static String STORE_EXPORT_ERRORS= PAGE_NAME + ".EXPORT_ERRORS"; private final static String STORE_SAVE_DESCRIPTION= PAGE_NAME + ".SAVE_DESCRIPTION"; private final static String STORE_DESCRIPTION_LOCATION= PAGE_NAME + ".DESCRIPTION_LOCATION"; private final static String STORE_USE_SRC_FOLDERS= PAGE_NAME + ".STORE_USE_SRC_FOLDERS"; private final static String STORE_BUILD_IF_NEEDED= PAGE_NAME + ".BUILD_IF_NEEDED"; 92 95 public JarOptionsPage(JarPackageData jarPackage) { 96 super(PAGE_NAME); 97 setTitle(JarPackagerMessages.JarOptionsPage_title); 98 setDescription(JarPackagerMessages.JarOptionsPage_description); 99 fJarPackage= jarPackage; 100 } 101 102 105 public void createControl(Composite parent) { 106 Composite composite= new Composite(parent, SWT.NULL); 107 composite.setLayout(new GridLayout()); 108 composite.setLayoutData( 109 new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL)); 110 111 createOptionsGroup(composite); 112 113 restoreWidgetValues(); 114 setControl(composite); 115 update(); 116 117 Dialog.applyDialogFont(composite); 118 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.JAROPTIONS_WIZARD_PAGE); 119 } 120 121 126 protected void createOptionsGroup(Composite parent) { 127 128 initializeDialogUnits(parent); 129 130 Composite optionsGroup= new Composite(parent, SWT.NONE); 131 GridLayout layout= new GridLayout(); 132 layout.marginHeight= 0; 133 optionsGroup.setLayout(layout); 134 135 createLabel(optionsGroup, JarPackagerMessages.JarOptionsPage_howTreatProblems_label, false); 136 137 UntypedListener selectionListener= new UntypedListener(); 138 139 fExportErrorsCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 140 fExportErrorsCheckbox.setText(JarPackagerMessages.JarOptionsPage_exportErrors_text); 141 fExportErrorsCheckbox.addListener(SWT.Selection, selectionListener); 142 143 fExportWarningsCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 144 fExportWarningsCheckbox.setText(JarPackagerMessages.JarOptionsPage_exportWarnings_text); 145 fExportWarningsCheckbox.addListener(SWT.Selection, selectionListener); 146 147 createSpacer(optionsGroup); 148 149 fUseSourceFoldersCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 150 fUseSourceFoldersCheckbox.setText(JarPackagerMessages.JarOptionsPage_useSourceFoldersHierarchy); 151 fUseSourceFoldersCheckbox.addListener(SWT.Selection, selectionListener); 152 fUseSourceFoldersCheckbox.setEnabled(fJarPackage.areJavaFilesExported() && !fJarPackage.areGeneratedFilesExported()); 153 154 createSpacer(optionsGroup); 155 156 fBuildIfNeededCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 157 fBuildIfNeededCheckbox.setText(JarPackagerMessages.JarOptionsPage_buildIfNeeded); 158 fBuildIfNeededCheckbox.addListener(SWT.Selection, selectionListener); 159 160 createSpacer(optionsGroup); 161 162 fSaveDescriptionCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 163 fSaveDescriptionCheckbox.setText(JarPackagerMessages.JarOptionsPage_saveDescription_text); 164 fSaveDescriptionCheckbox.addListener(SWT.Selection, selectionListener); 165 createDescriptionFileGroup(parent); 166 } 167 168 174 public final void saveWidgetValues() { 175 IDialogSettings settings= getDialogSettings(); 177 if (settings != null) { 178 settings.put(STORE_EXPORT_WARNINGS, fJarPackage.exportWarnings()); 179 settings.put(STORE_EXPORT_ERRORS, fJarPackage.areErrorsExported()); 180 settings.put(STORE_USE_SRC_FOLDERS, fJarPackage.useSourceFolderHierarchy()); 181 settings.put(STORE_BUILD_IF_NEEDED, fJarPackage.isBuildingIfNeeded()); 182 settings.put(STORE_SAVE_DESCRIPTION, fJarPackage.isDescriptionSaved()); 183 settings.put(STORE_DESCRIPTION_LOCATION, fJarPackage.getDescriptionLocation().toString()); 184 } 185 internalSaveWidgetValues(); 187 } 188 189 192 protected void internalSaveWidgetValues() { 193 } 194 195 199 protected void restoreWidgetValues() { 200 if (!((JarPackageWizard)getWizard()).isInitializingFromJarPackage()) 201 initializeJarPackage(); 202 203 fExportWarningsCheckbox.setSelection(fJarPackage.exportWarnings()); 204 fExportErrorsCheckbox.setSelection(fJarPackage.areErrorsExported()); 205 fBuildIfNeededCheckbox.setSelection(fJarPackage.isBuildingIfNeeded()); 206 fUseSourceFoldersCheckbox.setSelection(fJarPackage.useSourceFolderHierarchy()); 207 fSaveDescriptionCheckbox.setSelection(fJarPackage.isDescriptionSaved()); 208 fDescriptionFileText.setText(fJarPackage.getDescriptionLocation().toString()); 209 } 210 211 214 protected void initializeJarPackage() { 215 IDialogSettings settings= getDialogSettings(); 216 if (settings != null) { 217 fJarPackage.setExportWarnings(settings.getBoolean(STORE_EXPORT_WARNINGS)); 218 fJarPackage.setExportErrors(settings.getBoolean(STORE_EXPORT_ERRORS)); 219 fJarPackage.setUseSourceFolderHierarchy(settings.getBoolean(STORE_USE_SRC_FOLDERS)); 220 fJarPackage.setSaveDescription(false); String pathStr= settings.get(STORE_DESCRIPTION_LOCATION); 222 if (pathStr == null) 223 pathStr= ""; fJarPackage.setDescriptionLocation(new Path(pathStr)); 225 if (settings.get(STORE_BUILD_IF_NEEDED) != null) 226 fJarPackage.setBuildIfNeeded(settings.getBoolean(STORE_BUILD_IF_NEEDED)); 227 } 228 } 229 230 private void update() { 231 updateModel(); 232 updateWidgetEnablements(); 233 updatePageCompletion(); 234 } 235 236 239 protected void updateModel() { 240 if (getControl() == null) 241 return; 242 fJarPackage.setExportWarnings(fExportWarningsCheckbox.getSelection()); 243 fJarPackage.setExportErrors(fExportErrorsCheckbox.getSelection()); 244 fJarPackage.setBuildIfNeeded(fBuildIfNeededCheckbox.getSelection()); 245 fJarPackage.setSaveDescription(fSaveDescriptionCheckbox.getSelection()); 246 fJarPackage.setDescriptionLocation(new Path(fDescriptionFileText.getText())); 247 fJarPackage.setUseSourceFolderHierarchy(fUseSourceFoldersCheckbox.getSelection()); 248 } 249 250 254 protected void handleDescriptionFileBrowseButtonPressed() { 255 SaveAsDialog dialog= new SaveAsDialog(getContainer().getShell()); 256 dialog.create(); 257 dialog.getShell().setText(JarPackagerMessages.JarOptionsPage_saveAsDialog_title); 258 dialog.setMessage(JarPackagerMessages.JarOptionsPage_saveAsDialog_message); 259 dialog.setOriginalFile(createFileHandle(fJarPackage.getDescriptionLocation())); 260 if (dialog.open() == Window.OK) { 261 IPath path= dialog.getResult(); 262 path= path.removeFileExtension().addFileExtension(JarPackagerUtil.DESCRIPTION_EXTENSION); 263 fDescriptionFileText.setText(path.toString()); 264 } 265 } 266 267 270 protected void updateWidgetEnablements() { 271 boolean saveDescription= fSaveDescriptionCheckbox.getSelection(); 272 fDescriptionFileGroup.setEnabled(saveDescription); 273 fDescriptionFileBrowseButton.setEnabled(saveDescription); 274 fDescriptionFileText.setEnabled(saveDescription); 275 fDescriptionFileLabel.setEnabled(saveDescription); 276 277 boolean exportClassFiles= fJarPackage.areClassFilesExported() && !fJarPackage.areOutputFoldersExported(); 278 fExportWarningsCheckbox.setEnabled(exportClassFiles); 279 fExportErrorsCheckbox.setEnabled(exportClassFiles); 280 281 boolean isAutobuilding= ResourcesPlugin.getWorkspace().isAutoBuilding(); 282 fBuildIfNeededCheckbox.setEnabled(exportClassFiles && !isAutobuilding); 283 284 fUseSourceFoldersCheckbox.setEnabled(fJarPackage.areJavaFilesExported() && !fJarPackage.areGeneratedFilesExported()); 285 } 286 287 290 public boolean isPageComplete() { 291 if (fJarPackage.isDescriptionSaved()){ 292 if (fJarPackage.getDescriptionLocation().toString().length() == 0) { 293 setErrorMessage(null); 294 return false; 295 } 296 IPath location= fJarPackage.getDescriptionLocation(); 297 if (!location.toString().startsWith("/")) { setErrorMessage(JarPackagerMessages.JarOptionsPage_error_descriptionMustBeAbsolute); 299 return false; 300 } 301 IResource resource= findResource(location); 302 if (resource != null && resource.getType() != IResource.FILE) { 303 setErrorMessage(JarPackagerMessages.JarOptionsPage_error_descriptionMustNotBeExistingContainer); 304 return false; 305 } 306 resource= findResource(location.removeLastSegments(1)); 307 if (resource == null || resource.getType() == IResource.FILE) { 308 setErrorMessage(JarPackagerMessages.JarOptionsPage_error_descriptionContainerDoesNotExist); 309 return false; 310 } 311 String fileExtension= fJarPackage.getDescriptionLocation().getFileExtension(); 312 if (fileExtension == null || !fileExtension.equals(JarPackagerUtil.DESCRIPTION_EXTENSION)) { 313 setErrorMessage(Messages.format(JarPackagerMessages.JarOptionsPage_error_invalidDescriptionExtension, JarPackagerUtil.DESCRIPTION_EXTENSION)); 314 return false; 315 } 316 } 317 setErrorMessage(null); 318 return true; 319 } 320 321 public boolean canFlipToNextPage() { 322 return fJarPackage.areGeneratedFilesExported() && super.canFlipToNextPage(); 323 } 324 325 328 protected void createDescriptionFileGroup(Composite parent) { 329 fDescriptionFileGroup= new Composite(parent, SWT.NONE); 331 GridLayout layout= new GridLayout(); 332 layout.numColumns= 3; 333 fDescriptionFileGroup.setLayout(layout); 334 fDescriptionFileGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 335 336 fDescriptionFileLabel= new Label(fDescriptionFileGroup, SWT.NONE); 337 fDescriptionFileLabel.setText(JarPackagerMessages.JarOptionsPage_descriptionFile_label); 338 339 fDescriptionFileText= new Text(fDescriptionFileGroup, SWT.SINGLE | SWT.BORDER); 341 fDescriptionFileText.addListener(SWT.Modify, new UntypedListener()); 342 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 343 data.widthHint= convertWidthInCharsToPixels(40); 344 fDescriptionFileText.setLayoutData(data); 345 346 fDescriptionFileBrowseButton= new Button(fDescriptionFileGroup, SWT.PUSH); 348 fDescriptionFileBrowseButton.setText(JarPackagerMessages.JarOptionsPage_browseButton_text); 349 fDescriptionFileBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 350 SWTUtil.setButtonDimensionHint(fDescriptionFileBrowseButton); 351 fDescriptionFileBrowseButton.addSelectionListener(new SelectionAdapter() { 352 public void widgetSelected(SelectionEvent e) { 353 handleDescriptionFileBrowseButtonPressed(); 354 } 355 }); 356 } 357 358 366 protected IFile createFileHandle(IPath filePath) { 367 if (filePath.isValidPath(filePath.toString()) && filePath.segmentCount() >= 2) 368 return JavaPlugin.getWorkspace().getRoot().getFile(filePath); 369 else 370 return null; 371 } 372 375 public void setPreviousPage(IWizardPage page) { 376 super.setPreviousPage(page); 377 updateWidgetEnablements(); 378 if (getControl() != null) 379 updatePageCompletion(); 380 } 381 382 385 public void finish() { 386 saveWidgetValues(); 387 } 388 389 396 protected Label createLabel(Composite parent, String text, boolean bold) { 397 Label label= new Label(parent, SWT.NONE); 398 if (bold) 399 label.setFont(JFaceResources.getBannerFont()); 400 label.setText(text); 401 GridData data= new GridData(); 402 data.verticalAlignment= GridData.FILL; 403 data.horizontalAlignment= GridData.FILL; 404 label.setLayoutData(data); 405 return label; 406 } 407 408 413 protected void createSpacer(Composite parent) { 414 Label spacer= new Label(parent, SWT.NONE); 415 GridData data= new GridData(); 416 data.horizontalAlignment= GridData.FILL; 417 data.verticalAlignment= GridData.BEGINNING; 418 spacer.setLayoutData(data); 419 } 420 421 424 protected void updatePageCompletion() { 425 boolean pageComplete= isPageComplete(); 426 setPageComplete(pageComplete); 427 if (pageComplete) { 428 setErrorMessage(null); 429 } 430 } 431 432 438 protected IResource findResource(IPath path) { 439 IWorkspace workspace= JavaPlugin.getWorkspace(); 440 IStatus result= workspace.validatePath( 441 path.toString(), 442 IResource.ROOT | IResource.PROJECT | IResource.FOLDER | IResource.FILE); 443 if (result.isOK() && workspace.getRoot().exists(path)) 444 return workspace.getRoot().findMember(path); 445 return null; 446 } 447 } 448 | Popular Tags |