1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.HttpURLConnection ; 17 import java.net.MalformedURLException ; 18 import java.net.URL ; 19 import java.net.URLConnection ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.zip.ZipFile ; 23 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.IWorkspaceRoot; 31 import org.eclipse.core.resources.ResourcesPlugin; 32 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.custom.BusyIndicator; 35 import org.eclipse.swt.graphics.Image; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.DirectoryDialog; 41 import org.eclipse.swt.widgets.FileDialog; 42 import org.eclipse.swt.widgets.Shell; 43 44 import org.eclipse.jface.dialogs.MessageDialog; 45 import org.eclipse.jface.viewers.ILabelProvider; 46 import org.eclipse.jface.viewers.ITreeContentProvider; 47 import org.eclipse.jface.viewers.LabelProvider; 48 import org.eclipse.jface.viewers.Viewer; 49 import org.eclipse.jface.viewers.ViewerComparator; 50 import org.eclipse.jface.window.Window; 51 52 import org.eclipse.ui.ISharedImages; 53 import org.eclipse.ui.PlatformUI; 54 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 55 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 56 import org.eclipse.ui.model.WorkbenchContentProvider; 57 import org.eclipse.ui.model.WorkbenchLabelProvider; 58 import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider; 59 60 import org.eclipse.ui.views.navigator.ResourceComparator; 61 62 import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations; 63 64 import org.eclipse.jdt.ui.JavaUI; 65 66 import org.eclipse.jdt.internal.ui.JavaPlugin; 67 import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil; 68 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 69 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil; 70 import org.eclipse.jdt.internal.ui.util.PixelConverter; 71 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener; 72 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; 73 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter; 74 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 75 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 76 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 77 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 78 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 79 80 public class JavadocConfigurationBlock { 81 private static final String FILE_IMPORT_MASK= "*.jar;*.zip"; private static final String ERROR_DIALOG_TITLE= "Error Dialog"; 84 private StringDialogField fURLField; 85 private StringDialogField fArchiveField; 86 private StringDialogField fArchivePathField; 87 private URL fInitialURL; 88 private SelectionButtonDialogField fValidateURLButton; 89 private SelectionButtonDialogField fValidateArchiveButton; 90 private SelectionButtonDialogField fBrowseFolder; 91 private SelectionButtonDialogField fURLRadioButton; 92 private SelectionButtonDialogField fArchiveRadioButton; 93 private SelectionButtonDialogField fBrowseArchive; 94 private SelectionButtonDialogField fExternalRadio, fWorkspaceRadio; 95 private SelectionButtonDialogField fBrowseArchivePath; 96 private Shell fShell; 97 private IStatusChangeListener fContext; 98 99 private IStatus fURLStatus; 100 private IStatus fArchiveStatus; 101 private IStatus fArchivePathStatus; 102 103 private URL fURLResult; 104 private URL fArchiveURLResult; 105 106 boolean fIsForSource; 107 108 109 public JavadocConfigurationBlock(Shell shell, IStatusChangeListener context, URL initURL, boolean forSource) { 110 fShell= shell; 111 fContext= context; 112 fInitialURL= initURL; 113 fIsForSource= forSource; 114 115 JDocConfigurationAdapter adapter= new JDocConfigurationAdapter(); 116 117 if (!forSource) { 118 fURLRadioButton= new SelectionButtonDialogField(SWT.RADIO); 119 fURLRadioButton.setDialogFieldListener(adapter); 120 fURLRadioButton.setLabelText(PreferencesMessages.JavadocConfigurationBlock_location_type_path_label); 121 } 122 123 fURLField= new StringDialogField(); 124 fURLField.setDialogFieldListener(adapter); 125 fURLField.setLabelText(PreferencesMessages.JavadocConfigurationBlock_location_path_label); 126 127 fBrowseFolder= new SelectionButtonDialogField(SWT.PUSH); 128 fBrowseFolder.setDialogFieldListener(adapter); 129 fBrowseFolder.setLabelText(PreferencesMessages.JavadocConfigurationBlock_browse_folder_button); 130 131 fValidateURLButton= new SelectionButtonDialogField(SWT.PUSH); 132 fValidateURLButton.setDialogFieldListener(adapter); 133 fValidateURLButton.setLabelText(PreferencesMessages.JavadocConfigurationBlock_validate_button); 134 135 if (!forSource) { 136 fArchiveRadioButton= new SelectionButtonDialogField(SWT.RADIO); 137 fArchiveRadioButton.setDialogFieldListener(adapter); 138 fArchiveRadioButton.setLabelText(PreferencesMessages.JavadocConfigurationBlock_location_type_jar_label); 139 140 fExternalRadio= new SelectionButtonDialogField(SWT.RADIO); 141 fExternalRadio.setDialogFieldListener(adapter); 142 fExternalRadio.setLabelText(PreferencesMessages.JavadocConfigurationBlock_external_radio); 143 144 fWorkspaceRadio= new SelectionButtonDialogField(SWT.RADIO); 145 fWorkspaceRadio.setDialogFieldListener(adapter); 146 fWorkspaceRadio.setLabelText(PreferencesMessages.JavadocConfigurationBlock_workspace_radio); 147 148 fArchiveField= new StringDialogField(); 149 fArchiveField.setDialogFieldListener(adapter); 150 fArchiveField.setLabelText(PreferencesMessages.JavadocConfigurationBlock_location_jar_label); 151 152 fBrowseArchive= new SelectionButtonDialogField(SWT.PUSH); 153 fBrowseArchive.setDialogFieldListener(adapter); 154 fBrowseArchive.setLabelText(PreferencesMessages.JavadocConfigurationBlock_browse_archive_button); 155 156 fArchivePathField= new StringDialogField(); 157 fArchivePathField.setDialogFieldListener(adapter); 158 fArchivePathField.setLabelText(PreferencesMessages.JavadocConfigurationBlock_jar_path_label); 159 160 fBrowseArchivePath= new SelectionButtonDialogField(SWT.PUSH); 161 fBrowseArchivePath.setDialogFieldListener(adapter); 162 fBrowseArchivePath.setLabelText(PreferencesMessages.JavadocConfigurationBlock_browse_archive_path_button); 163 164 fValidateArchiveButton= new SelectionButtonDialogField(SWT.PUSH); 165 fValidateArchiveButton.setDialogFieldListener(adapter); 166 fValidateArchiveButton.setLabelText(PreferencesMessages.JavadocConfigurationBlock_validate_button); 167 } 168 169 fURLStatus= new StatusInfo(); 170 fArchiveStatus= new StatusInfo(); 171 fArchivePathStatus= new StatusInfo(); 172 173 initializeSelections(); 174 } 175 176 public Control createContents(Composite parent) { 177 fShell= parent.getShell(); 178 179 PixelConverter converter= new PixelConverter(parent); 180 Composite topComp= new Composite(parent, SWT.NONE); 181 GridLayout topLayout= new GridLayout(); 182 topLayout.numColumns= 3; 183 topLayout.marginWidth= 0; 184 topLayout.marginHeight= 0; 185 topComp.setLayout(topLayout); 186 187 if (!fIsForSource) { 189 fURLRadioButton.doFillIntoGrid(topComp, 3); 190 } 191 192 fURLField.doFillIntoGrid(topComp, 2); 193 LayoutUtil.setWidthHint(fURLField.getTextControl(null), converter.convertWidthInCharsToPixels(43)); 194 LayoutUtil.setHorizontalGrabbing(fURLField.getTextControl(null)); 195 196 fBrowseFolder.doFillIntoGrid(topComp, 1); 197 198 DialogField.createEmptySpace(topComp, 2); 199 fValidateURLButton.doFillIntoGrid(topComp, 1); 200 201 203 if (!fIsForSource) { 204 fArchiveRadioButton.doFillIntoGrid(topComp, 3); 206 207 208 DialogField.createEmptySpace(topComp, 1); 210 Composite radioComposite= new Composite(topComp, SWT.NONE); 211 radioComposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false)); 212 GridLayout layout= new GridLayout(2, true); 213 layout.marginHeight= 0; 214 layout.marginWidth= 0; 215 radioComposite.setLayout(layout); 216 fExternalRadio.doFillIntoGrid(radioComposite, 1); 217 fWorkspaceRadio.doFillIntoGrid(radioComposite, 1); 218 DialogField.createEmptySpace(topComp, 1); 219 220 fArchiveField.doFillIntoGrid(topComp, 2); 222 LayoutUtil.setWidthHint(fArchiveField.getTextControl(null), converter.convertWidthInCharsToPixels(43)); 223 LayoutUtil.setHorizontalGrabbing(fArchiveField.getTextControl(null)); 224 225 fBrowseArchive.doFillIntoGrid(topComp, 1); 226 227 fArchivePathField.doFillIntoGrid(topComp, 2); 229 LayoutUtil.setWidthHint(fArchivePathField.getTextControl(null), converter.convertWidthInCharsToPixels(43)); 230 LayoutUtil.setHorizontalGrabbing(fArchivePathField.getTextControl(null)); 231 232 fBrowseArchivePath.doFillIntoGrid(topComp, 1); 233 234 DialogField.createEmptySpace(topComp, 2); 235 fValidateArchiveButton.doFillIntoGrid(topComp, 1); 236 237 int indent= converter.convertWidthInCharsToPixels(2); 238 LayoutUtil.setHorizontalIndent(fArchiveField.getLabelControl(null), indent); 239 LayoutUtil.setHorizontalIndent(fArchivePathField.getLabelControl(null), indent); 240 LayoutUtil.setHorizontalIndent(fURLField.getLabelControl(null), indent); 241 242 fURLRadioButton.attachDialogFields(new DialogField[] {fURLField, fBrowseFolder, fValidateURLButton }); 243 fArchiveRadioButton.attachDialogFields(new DialogField[] {fArchiveField, fBrowseArchive, fExternalRadio, fWorkspaceRadio, fArchivePathField, fBrowseArchivePath, fValidateArchiveButton }); 244 } 245 246 247 return topComp; 248 } 249 250 private void initializeSelections() { 251 String initialValue = fInitialURL != null ? fInitialURL.toExternalForm() : ""; 253 if (fIsForSource) { 254 fURLField.setText(initialValue); 255 return; 256 } 257 String prefix= JavaDocLocations.ARCHIVE_PREFIX; 258 boolean isArchive= initialValue.startsWith(prefix); 259 260 boolean isWorkspaceArchive= false; 261 262 fURLRadioButton.setSelection(!isArchive); 263 fArchiveRadioButton.setSelection(isArchive); 264 265 if (isArchive) { 266 String jarPathStr; 267 String insidePath= ""; int excIndex= initialValue.indexOf("!/"); if (excIndex == -1) { 270 jarPathStr= initialValue.substring(prefix.length()); 271 } else { 272 jarPathStr= initialValue.substring(prefix.length(), excIndex); 273 insidePath= initialValue.substring(excIndex + 2); 274 } 275 276 final String fileProtocol= "file:/"; final String resourceProtocol= "platform:/resource/"; 279 if (jarPathStr.startsWith(fileProtocol)) { 280 jarPathStr= jarPathStr.substring(fileProtocol.length()); 281 } else if (jarPathStr.startsWith(resourceProtocol)) { 282 jarPathStr= jarPathStr.substring(resourceProtocol.length()); 283 isWorkspaceArchive= true; 284 } else { 285 fURLField.setText(initialValue); 286 return; 287 } 288 IPath jarPath= new Path(decodeExclamationMarks(jarPathStr)); 289 fArchivePathField.setText(decodeExclamationMarks(insidePath)); 290 if (isWorkspaceArchive) { 291 fArchiveField.setText(jarPath.makeRelative().toString()); 292 } else { 293 fArchiveField.setText(jarPath.makeAbsolute().toOSString()); 294 } 295 } else { 296 fURLField.setText(initialValue); 297 } 298 fExternalRadio.setSelection(!isWorkspaceArchive); 299 fWorkspaceRadio.setSelection(isWorkspaceArchive); 300 301 } 302 303 public void setFocus() { 304 fURLField.postSetFocusOnDialogField(fShell.getDisplay()); 305 } 306 307 public void performDefaults() { 308 initializeSelections(); 309 } 310 311 public URL getJavadocLocation() { 312 if (fIsForSource || fURLRadioButton.isSelected()) { 313 return fURLResult; 314 } 315 return fArchiveURLResult; 316 } 317 318 private class EntryValidator implements Runnable { 319 320 private String fInvalidMessage= PreferencesMessages.JavadocConfigurationBlock_InvalidLocation_message; 321 private String fValidMessage= PreferencesMessages.JavadocConfigurationBlock_ValidLocation_message; 322 private String fTitle= PreferencesMessages.JavadocConfigurationBlock_MessageDialog_title; 323 private String fUnable= PreferencesMessages.JavadocConfigurationBlock_UnableToValidateLocation_message; 324 public void run() { 325 326 URL location= getJavadocLocation(); 327 if (location == null) { 328 MessageDialog.openWarning(fShell, fTitle, fInvalidMessage); 329 return; 330 } 331 332 try { 333 String protocol = location.getProtocol(); 334 if (protocol.startsWith("http") || protocol.equals("jar")) { validateURL(location); 336 } else if (protocol.equals("file")) { validateFile(location); 338 } else { 339 MessageDialog.openWarning(fShell, fTitle, fUnable); 340 } 341 } catch (MalformedURLException e) { 342 MessageDialog.openWarning(fShell, fTitle, fUnable); 343 } 344 345 } 346 347 public void spawnInBrowser(URL url) { 348 OpenBrowserUtil.open(url, fShell.getDisplay(), fTitle); 349 } 350 351 private void validateFile(URL location) throws MalformedURLException { 352 File folder = new File (location.getFile()); 353 if (folder.isDirectory()) { 354 File indexFile= new File (folder, "index.html"); if (indexFile.isFile()) { 356 File packageList= new File (folder, "package-list"); if (packageList.exists()) { 358 if (MessageDialog.openConfirm(fShell, fTitle, fValidMessage)) { 359 spawnInBrowser(indexFile.toURL()); 360 } 361 return; 362 } 363 } 364 } 365 MessageDialog.openWarning(fShell, fTitle, fInvalidMessage); 366 } 367 368 private void validateURL(URL location) throws MalformedURLException { 369 IPath path= new Path(location.toExternalForm()); 370 IPath index = path.append("index.html"); IPath packagelist = path.append("package-list"); URL indexURL = new URL (index.toString()); 373 URL packagelistURL = new URL (packagelist.toString()); 374 375 boolean suc= checkURLConnection(indexURL) && checkURLConnection(packagelistURL); 376 if (suc) { 377 if (MessageDialog.openConfirm(fShell, fTitle, fValidMessage)) 378 spawnInBrowser(indexURL); 379 } else { 380 MessageDialog.openWarning(fShell, fTitle, fInvalidMessage); 381 } 382 } 383 } 384 385 private boolean checkURLConnection(URL url) { 386 int res= 0; 387 URLConnection connection= null; 388 try { 389 connection= url.openConnection(); 390 if (connection instanceof HttpURLConnection ) { 391 connection.connect(); 392 res= ((HttpURLConnection ) connection).getResponseCode(); 393 } 394 InputStream is= null; 395 try { 396 is= connection.getInputStream(); 397 byte[] buffer= new byte[256]; 398 while (is.read(buffer) != -1) { 399 } 400 } finally { 401 if (is != null) 402 is.close(); 403 } 404 } catch (IllegalArgumentException e) { 405 return false; } catch (IOException e) { 407 return false; 408 } 409 return res < 400; 410 } 411 412 413 private class JDocConfigurationAdapter implements IDialogFieldListener { 414 415 public void dialogFieldChanged(DialogField field) { 417 jdocDialogFieldChanged(field); 418 } 419 } 420 421 422 private void jdocDialogFieldChanged(DialogField field) { 423 if (field == fURLField) { 424 fURLStatus= updateURLStatus(); 425 statusChanged(); 426 } else if (field == fArchiveField) { 427 fArchiveStatus= updateArchiveStatus(); 428 statusChanged(); 429 } else if (field == fArchivePathField) { 430 fArchivePathStatus= updateArchivePathStatus(); 431 statusChanged(); 432 } else if (field == fValidateURLButton || field == fValidateArchiveButton) { 433 EntryValidator validator= new EntryValidator(); 434 BusyIndicator.showWhile(fShell.getDisplay(), validator); 435 } else if (field == fBrowseFolder) { 436 String url= chooseJavaDocFolder(); 437 if (url != null) { 438 fURLField.setText(url); 439 } 440 } else if (field == fBrowseArchive) { 441 String jarPath= chooseArchive(); 442 if (jarPath != null) { 443 fArchiveField.setText(jarPath); 444 } 445 } else if (field == fExternalRadio || field == fWorkspaceRadio) { 446 fArchiveStatus= updateArchiveStatus(); 447 statusChanged(); 448 } else if (field == fBrowseArchivePath) { 449 String archivePath= chooseArchivePath(); 450 if (archivePath != null) { 451 fArchivePathField.setText(archivePath); 452 } 453 } else if (field == fURLRadioButton || field == fArchiveRadioButton) { 454 statusChanged(); 455 } 456 } 457 458 private void statusChanged() { 459 IStatus status; 460 boolean isURL= fIsForSource || fURLRadioButton.isSelected(); 461 if (isURL) { 462 status= fURLStatus; 463 } else { 464 status= StatusUtil.getMoreSevere(fArchiveStatus, fArchivePathStatus); 465 } 466 if (!fIsForSource) { 467 boolean canBrowseArchivePath= !isURL && fArchiveStatus.isOK() && fArchiveField.getText().length() > 0; 468 if (canBrowseArchivePath && fWorkspaceRadio.isSelected()) { 469 IResource resource= ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(fArchiveField.getText())); 470 canBrowseArchivePath= resource != null && resource.getLocation() != null; 471 } 472 fBrowseArchivePath.setEnabled(canBrowseArchivePath); 473 } 474 fContext.statusChanged(status); 475 } 476 477 478 private String chooseArchivePath() { 479 final String [] res= new String [] { null }; 480 BusyIndicator.showWhile(fShell.getDisplay(), new Runnable () { 481 public void run() { 482 res[0]= internalChooseArchivePath(); 483 } 484 }); 485 return res[0]; 486 } 487 488 private String encodeExclamationMarks(String str) { 489 StringBuffer buf= new StringBuffer (str.length()); 490 for (int i= 0; i < str.length(); i++) { 491 char ch= str.charAt(i); 492 if (ch == '!') { 493 buf.append("%21"); } else { 495 buf.append(ch); 496 } 497 } 498 return buf.toString(); 499 } 500 501 private String decodeExclamationMarks(String str) { 502 StringBuffer buf= new StringBuffer (str.length()); 503 int length= str.length(); 504 for (int i= 0; i < length; i++) { 505 char ch= str.charAt(i); 506 if (ch == '%' && (i < length - 2) && str.charAt(i + 1) == '2' && str.charAt(i + 2) == '1') { 507 buf.append('!'); 508 i+= 2; 509 } else { 510 buf.append(ch); 511 } 512 } 513 return buf.toString(); 514 } 515 516 517 518 private String internalChooseArchivePath() { 519 ZipFile zipFile= null; 520 try { 521 if (fWorkspaceRadio.isSelected()) { 522 IResource resource= ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(fArchiveField.getText())); 523 if (resource != null) { 524 IPath location= resource.getLocation(); 525 if (location != null) { 526 zipFile= new ZipFile (location.toOSString()); 527 } 528 } 529 } else { 530 zipFile= new ZipFile (fArchiveField.getText()); 531 } 532 if (zipFile == null) { 533 return null; 534 } 535 536 ZipFileStructureProvider provider= new ZipFileStructureProvider(zipFile); 537 538 ILabelProvider lp= new ZipDialogLabelProvider(provider); 539 ZipDialogContentProvider cp= new ZipDialogContentProvider(provider); 540 541 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(fShell, lp, cp); 542 dialog.setAllowMultiple(false); 543 dialog.setValidator(new ZipDialogValidator()); 544 dialog.setTitle(PreferencesMessages.JavadocConfigurationBlock_browse_jarorzip_path_title); 545 dialog.setMessage(PreferencesMessages.JavadocConfigurationBlock_location_in_jarorzip_message); 546 dialog.setComparator(new ViewerComparator()); 547 548 String init= fArchivePathField.getText(); 549 if (init.length() == 0) { 550 init= "docs/api"; } 552 dialog.setInitialSelection(cp.findElement(new Path(init))); 553 554 dialog.setInput(this); 555 if (dialog.open() == Window.OK) { 556 String name= provider.getFullPath(dialog.getFirstResult()); 557 return new Path(name).removeTrailingSeparator().toString(); 558 } 559 } catch (IOException e) { 560 JavaPlugin.log(e); 561 } finally { 562 if (zipFile != null) { 563 try { 564 zipFile.close(); 565 } catch (IOException e1) { 566 } 568 } 569 } 570 return null; 571 } 572 573 private String chooseArchive() { 574 if (fWorkspaceRadio.isSelected()) { 575 return chooseWorkspaceArchive(); 576 } 577 578 IPath currPath= new Path(fArchiveField.getText()); 579 if (ArchiveFileFilter.isArchivePath(currPath)) { 580 currPath= currPath.removeLastSegments(1); 581 } 582 583 FileDialog dialog= new FileDialog(fShell, SWT.OPEN); 584 dialog.setFilterExtensions(new String [] { FILE_IMPORT_MASK }); 585 dialog.setText(PreferencesMessages.JavadocConfigurationBlock_zipImportSource_title); 586 dialog.setFilterPath(currPath.toOSString()); 587 588 return dialog.open(); 589 } 590 591 private String chooseWorkspaceArchive() { 592 String initSelection= fArchiveField.getText(); 593 594 ILabelProvider lp= new WorkbenchLabelProvider(); 595 ITreeContentProvider cp= new WorkbenchContentProvider(); 596 Class [] acceptedClasses= new Class [] { IFile.class }; 597 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, true); 598 599 IResource initSel= null; 600 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 601 if (initSelection.length() > 0) { 602 initSel= root.findMember(new Path(initSelection)); 603 } 604 605 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(fShell, lp, cp); 606 dialog.addFilter(new ArchiveFileFilter((List ) null, true)); 607 dialog.setAllowMultiple(false); 608 dialog.setValidator(validator); 609 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME)); 610 dialog.setTitle(PreferencesMessages.JavadocConfigurationBlock_workspace_archive_selection_dialog_title); 611 dialog.setMessage(PreferencesMessages.JavadocConfigurationBlock_workspace_archive_selection_dialog_description); 612 dialog.setInput(root); 613 dialog.setInitialSelection(initSel); 614 dialog.setHelpAvailable(false); 615 if (dialog.open() == Window.OK) { 616 IResource res= (IResource) dialog.getFirstResult(); 617 return res.getFullPath().makeRelative().toString(); 618 } 619 return null; 620 } 621 622 627 protected void displayErrorDialog(String message) { 628 MessageDialog.openError(fShell, ERROR_DIALOG_TITLE, message); 629 } 630 631 private String chooseJavaDocFolder() { 632 String initPath= ""; if (fURLResult != null && "file".equals(fURLResult.getProtocol())) { initPath= (new File (fURLResult.getFile())).getPath(); 635 } 636 DirectoryDialog dialog= new DirectoryDialog(fShell); 637 dialog.setText(PreferencesMessages.JavadocConfigurationBlock_javadocFolderDialog_label); 638 dialog.setMessage(PreferencesMessages.JavadocConfigurationBlock_javadocFolderDialog_message); 639 dialog.setFilterPath(initPath); 640 String result= dialog.open(); 641 if (result != null) { 642 try { 643 URL url= new File (result).toURL(); 644 return url.toExternalForm(); 645 } catch (MalformedURLException e) { 646 JavaPlugin.log(e); 647 } 648 } 649 return null; 650 } 651 652 private IStatus updateURLStatus() { 653 StatusInfo status= new StatusInfo(); 654 fURLResult= null; 655 try { 656 String jdocLocation= fURLField.getText(); 657 if (jdocLocation.length() == 0) { 658 return status; 659 } 660 URL url= new URL (jdocLocation); 661 if ("file".equals(url.getProtocol())) { if (url.getFile() == null) { 663 status.setError(PreferencesMessages.JavadocConfigurationBlock_error_notafolder); 664 return status; 665 } 666 } 667 fURLResult= url; 668 } catch (MalformedURLException e) { 669 status.setError(PreferencesMessages.JavadocConfigurationBlock_MalformedURL_error); 670 return status; 671 } 672 673 return status; 674 } 675 676 private IStatus updateArchiveStatus() { 677 try { 678 fArchiveURLResult= null; 679 680 StatusInfo status= new StatusInfo(); 681 String jdocLocation= fArchiveField.getText(); 682 if (jdocLocation.length() > 0) { 683 if (!Path.ROOT.isValidPath(jdocLocation)) { 684 status.setError(PreferencesMessages.JavadocConfigurationBlock_error_invalidarchivepath); 685 return status; 686 } 687 if (fWorkspaceRadio.isSelected()) { 688 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 689 IResource res= root.findMember(new Path(jdocLocation)); 690 if (res != null) { 691 if (!(res instanceof IFile)) { 692 status.setError(PreferencesMessages.JavadocConfigurationBlock_error_archive_not_found_in_workspace); 693 return status; 694 } 695 } else { 696 status.setError(PreferencesMessages.JavadocConfigurationBlock_error_archive_not_found_in_workspace); 697 return status; 698 } 699 } else { 700 IPath path= Path.fromOSString(jdocLocation); 701 if (!path.isAbsolute()) { 702 status.setError(PreferencesMessages.JavadocConfigurationBlock_error_archivepathnotabsolute); 703 return status; 704 } 705 File jarFile= new File (jdocLocation); 706 if (jarFile.isDirectory()) { 707 status.setError(PreferencesMessages.JavadocConfigurationBlock_error_notafile); 708 return status; 709 } 710 if (!jarFile.exists()) { 711 status.setWarning(PreferencesMessages.JavadocConfigurationBlock_error_notafile); 712 } 713 } 714 fArchiveURLResult= getArchiveURL(); 715 } 716 return status; 717 } catch (MalformedURLException e) { 718 StatusInfo status= new StatusInfo(); 719 status.setError(e.getMessage()); 720 return status; 721 } 722 } 723 724 private IStatus updateArchivePathStatus() { 725 try { 727 fArchiveURLResult= getArchiveURL(); 728 } catch (MalformedURLException e) { 729 fArchiveURLResult= null; 730 StatusInfo status= new StatusInfo(); 731 status.setError(e.getMessage()); 732 return status; 734 } 735 return new StatusInfo(); 736 737 } 738 739 740 private URL getArchiveURL() throws MalformedURLException { 741 String jarLoc= fArchiveField.getText(); 742 String innerPath= fArchivePathField.getText().trim(); 743 744 StringBuffer buf= new StringBuffer (); 745 buf.append("jar:"); 747 if (fWorkspaceRadio.isSelected()) { 748 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 749 IResource res= root.findMember(new Path(jarLoc)); 750 if (res != null) { 751 buf.append("platform:/resource").append(encodeExclamationMarks(res.getFullPath().toString())); } 753 } else { 754 buf.append(encodeExclamationMarks(new File (jarLoc).toURL().toExternalForm())); 755 } 756 buf.append('!'); 757 if (innerPath.length() > 0) { 758 if (innerPath.charAt(0) != '/') { 759 buf.append('/'); 760 } 761 buf.append(innerPath); 762 } else { 763 buf.append('/'); 764 } 765 return new URL (buf.toString()); 766 } 767 768 769 772 private static class ZipDialogContentProvider implements ITreeContentProvider { 773 774 private ZipFileStructureProvider fProvider; 775 776 public ZipDialogContentProvider(ZipFileStructureProvider provider) { 777 fProvider= provider; 778 } 779 780 public Object findElement(IPath path) { 781 String [] segments= path.segments(); 782 783 Object elem= fProvider.getRoot(); 784 for (int i= 0; i < segments.length && elem != null; i++) { 785 List list= fProvider.getChildren(elem); 786 String name= segments[i]; 787 elem= null; 788 for (int k= 0; k < list.size(); k++) { 789 Object curr= list.get(k); 790 if (fProvider.isFolder(curr) && name.equals(fProvider.getLabel(curr))) { 791 elem= curr; 792 break; 793 } 794 } 795 } 796 return elem; 797 } 798 799 private Object recursiveFind(Object element, String name) { 800 if (name.equals(fProvider.getLabel(element))) { 801 return element; 802 } 803 List list= fProvider.getChildren(element); 804 if (list != null) { 805 for (int k= 0; k < list.size(); k++) { 806 Object res= recursiveFind(list.get(k), name); 807 if (res != null) { 808 return res; 809 } 810 } 811 } 812 return null; 813 } 814 815 public Object findFileByName(String name) { 816 return recursiveFind(fProvider.getRoot(), name); 817 } 818 819 822 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 823 } 824 825 828 public Object getParent(Object element) { 829 if (element.equals(fProvider.getRoot())) { 830 return null; 831 } 832 IPath path= new Path(fProvider.getFullPath(element)); 833 if (path.segmentCount() > 0) { 834 return findElement(path.removeLastSegments(1)); 835 } 836 return fProvider.getRoot(); 837 } 838 839 842 public boolean hasChildren(Object element) { 843 List list= fProvider.getChildren(element); 844 if (list != null) { 845 for (int i= 0; i < list.size(); i++) { 846 if (fProvider.isFolder(list.get(i))) { 847 return true; 848 } 849 } 850 } 851 return false; 852 } 853 854 857 public Object [] getChildren(Object element) { 858 List list= fProvider.getChildren(element); 859 ArrayList res= new ArrayList (); 860 if (list != null) { 861 for (int i= 0; i < list.size(); i++) { 862 Object curr= list.get(i); 863 if (fProvider.isFolder(curr)) { 864 res.add(curr); 865 } 866 } 867 } 868 return res.toArray(); 869 } 870 871 874 public Object [] getElements(Object element) { 875 return new Object [] {fProvider.getRoot() }; 876 } 877 878 881 public void dispose() { 882 } 883 } 884 885 private static class ZipDialogLabelProvider extends LabelProvider { 886 887 private final Image IMG_JAR= 888 JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_JAR); 889 private final Image IMG_FOLDER= 890 PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER); 891 892 private ZipFileStructureProvider fProvider; 893 894 public ZipDialogLabelProvider(ZipFileStructureProvider provider) { 895 fProvider= provider; 896 } 897 898 public Image getImage(Object element) { 899 if (element == fProvider.getRoot()) { 900 return IMG_JAR; 901 } else { 902 return IMG_FOLDER; 903 } 904 } 905 906 public String getText(Object element) { 907 if (element == fProvider.getRoot()) { 908 return fProvider.getZipFile().getName(); 909 } 910 return fProvider.getLabel(element); 911 } 912 } 913 914 private static class ZipDialogValidator implements ISelectionStatusValidator { 915 public ZipDialogValidator() { 916 super(); 917 } 918 919 922 public IStatus validate(Object [] selection) { 923 String message= ""; return new StatusInfo(IStatus.INFO, message); 925 } 926 } 927 928 } 929 | Popular Tags |