0001: /*******************************************************************************
0002: * Copyright (c) 2000, 2007 IBM Corporation and others.
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * IBM Corporation - initial API and implementation
0010: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences;
0011:
0012: import java.io.File;
0013: import java.io.IOException;
0014: import java.io.InputStream;
0015: import java.net.HttpURLConnection;
0016: import java.net.MalformedURLException;
0017: import java.net.URL;
0018: import java.net.URLConnection;
0019: import java.util.ArrayList;
0020: import java.util.List;
0021: import java.util.zip.ZipFile;
0022:
0023: import org.eclipse.core.runtime.IPath;
0024: import org.eclipse.core.runtime.IStatus;
0025: import org.eclipse.core.runtime.Path;
0026:
0027: import org.eclipse.core.resources.IFile;
0028: import org.eclipse.core.resources.IResource;
0029: import org.eclipse.core.resources.IWorkspaceRoot;
0030: import org.eclipse.core.resources.ResourcesPlugin;
0031:
0032: import org.eclipse.swt.SWT;
0033: import org.eclipse.swt.custom.BusyIndicator;
0034: import org.eclipse.swt.graphics.Image;
0035: import org.eclipse.swt.layout.GridData;
0036: import org.eclipse.swt.layout.GridLayout;
0037: import org.eclipse.swt.widgets.Composite;
0038: import org.eclipse.swt.widgets.Control;
0039: import org.eclipse.swt.widgets.DirectoryDialog;
0040: import org.eclipse.swt.widgets.FileDialog;
0041: import org.eclipse.swt.widgets.Shell;
0042:
0043: import org.eclipse.jface.dialogs.MessageDialog;
0044: import org.eclipse.jface.viewers.ILabelProvider;
0045: import org.eclipse.jface.viewers.ITreeContentProvider;
0046: import org.eclipse.jface.viewers.LabelProvider;
0047: import org.eclipse.jface.viewers.Viewer;
0048: import org.eclipse.jface.viewers.ViewerComparator;
0049: import org.eclipse.jface.window.Window;
0050:
0051: import org.eclipse.ui.ISharedImages;
0052: import org.eclipse.ui.PlatformUI;
0053: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
0054: import org.eclipse.ui.dialogs.ISelectionStatusValidator;
0055: import org.eclipse.ui.model.WorkbenchContentProvider;
0056: import org.eclipse.ui.model.WorkbenchLabelProvider;
0057: import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
0058:
0059: import org.eclipse.ui.views.navigator.ResourceComparator;
0060:
0061: import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations;
0062:
0063: import org.eclipse.jdt.ui.JavaUI;
0064:
0065: import org.eclipse.jdt.internal.ui.JavaPlugin;
0066: import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
0067: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
0068: import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
0069: import org.eclipse.jdt.internal.ui.util.PixelConverter;
0070: import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
0071: import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
0072: import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter;
0073: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
0074: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
0075: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
0076: import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
0077: import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
0078:
0079: public class JavadocConfigurationBlock {
0080: private static final String FILE_IMPORT_MASK = "*.jar;*.zip"; //$NON-NLS-1$
0081: private static final String ERROR_DIALOG_TITLE = "Error Dialog"; //$NON-NLS-1$
0082:
0083: private StringDialogField fURLField;
0084: private StringDialogField fArchiveField;
0085: private StringDialogField fArchivePathField;
0086: private URL fInitialURL;
0087: private SelectionButtonDialogField fValidateURLButton;
0088: private SelectionButtonDialogField fValidateArchiveButton;
0089: private SelectionButtonDialogField fBrowseFolder;
0090: private SelectionButtonDialogField fURLRadioButton;
0091: private SelectionButtonDialogField fArchiveRadioButton;
0092: private SelectionButtonDialogField fBrowseArchive;
0093: private SelectionButtonDialogField fExternalRadio, fWorkspaceRadio;
0094: private SelectionButtonDialogField fBrowseArchivePath;
0095: private Shell fShell;
0096: private IStatusChangeListener fContext;
0097:
0098: private IStatus fURLStatus;
0099: private IStatus fArchiveStatus;
0100: private IStatus fArchivePathStatus;
0101:
0102: private URL fURLResult;
0103: private URL fArchiveURLResult;
0104:
0105: boolean fIsForSource;
0106:
0107: public JavadocConfigurationBlock(Shell shell,
0108: IStatusChangeListener context, URL initURL,
0109: boolean forSource) {
0110: fShell = shell;
0111: fContext = context;
0112: fInitialURL = initURL;
0113: fIsForSource = forSource;
0114:
0115: JDocConfigurationAdapter adapter = new JDocConfigurationAdapter();
0116:
0117: if (!forSource) {
0118: fURLRadioButton = new SelectionButtonDialogField(SWT.RADIO);
0119: fURLRadioButton.setDialogFieldListener(adapter);
0120: fURLRadioButton
0121: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_location_type_path_label);
0122: }
0123:
0124: fURLField = new StringDialogField();
0125: fURLField.setDialogFieldListener(adapter);
0126: fURLField
0127: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_location_path_label);
0128:
0129: fBrowseFolder = new SelectionButtonDialogField(SWT.PUSH);
0130: fBrowseFolder.setDialogFieldListener(adapter);
0131: fBrowseFolder
0132: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_browse_folder_button);
0133:
0134: fValidateURLButton = new SelectionButtonDialogField(SWT.PUSH);
0135: fValidateURLButton.setDialogFieldListener(adapter);
0136: fValidateURLButton
0137: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_validate_button);
0138:
0139: if (!forSource) {
0140: fArchiveRadioButton = new SelectionButtonDialogField(
0141: SWT.RADIO);
0142: fArchiveRadioButton.setDialogFieldListener(adapter);
0143: fArchiveRadioButton
0144: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_location_type_jar_label);
0145:
0146: fExternalRadio = new SelectionButtonDialogField(SWT.RADIO);
0147: fExternalRadio.setDialogFieldListener(adapter);
0148: fExternalRadio
0149: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_external_radio);
0150:
0151: fWorkspaceRadio = new SelectionButtonDialogField(SWT.RADIO);
0152: fWorkspaceRadio.setDialogFieldListener(adapter);
0153: fWorkspaceRadio
0154: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_workspace_radio);
0155:
0156: fArchiveField = new StringDialogField();
0157: fArchiveField.setDialogFieldListener(adapter);
0158: fArchiveField
0159: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_location_jar_label);
0160:
0161: fBrowseArchive = new SelectionButtonDialogField(SWT.PUSH);
0162: fBrowseArchive.setDialogFieldListener(adapter);
0163: fBrowseArchive
0164: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_browse_archive_button);
0165:
0166: fArchivePathField = new StringDialogField();
0167: fArchivePathField.setDialogFieldListener(adapter);
0168: fArchivePathField
0169: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_jar_path_label);
0170:
0171: fBrowseArchivePath = new SelectionButtonDialogField(
0172: SWT.PUSH);
0173: fBrowseArchivePath.setDialogFieldListener(adapter);
0174: fBrowseArchivePath
0175: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_browse_archive_path_button);
0176:
0177: fValidateArchiveButton = new SelectionButtonDialogField(
0178: SWT.PUSH);
0179: fValidateArchiveButton.setDialogFieldListener(adapter);
0180: fValidateArchiveButton
0181: .setLabelText(PreferencesMessages.JavadocConfigurationBlock_validate_button);
0182: }
0183:
0184: fURLStatus = new StatusInfo();
0185: fArchiveStatus = new StatusInfo();
0186: fArchivePathStatus = new StatusInfo();
0187:
0188: initializeSelections();
0189: }
0190:
0191: public Control createContents(Composite parent) {
0192: fShell = parent.getShell();
0193:
0194: PixelConverter converter = new PixelConverter(parent);
0195: Composite topComp = new Composite(parent, SWT.NONE);
0196: GridLayout topLayout = new GridLayout();
0197: topLayout.numColumns = 3;
0198: topLayout.marginWidth = 0;
0199: topLayout.marginHeight = 0;
0200: topComp.setLayout(topLayout);
0201:
0202: // Add the first radio button for the path
0203: if (!fIsForSource) {
0204: fURLRadioButton.doFillIntoGrid(topComp, 3);
0205: }
0206:
0207: fURLField.doFillIntoGrid(topComp, 2);
0208: LayoutUtil.setWidthHint(fURLField.getTextControl(null),
0209: converter.convertWidthInCharsToPixels(43));
0210: LayoutUtil
0211: .setHorizontalGrabbing(fURLField.getTextControl(null));
0212:
0213: fBrowseFolder.doFillIntoGrid(topComp, 1);
0214:
0215: DialogField.createEmptySpace(topComp, 2);
0216: fValidateURLButton.doFillIntoGrid(topComp, 1);
0217:
0218: //DialogField.createEmptySpace(topComp, 3);
0219:
0220: if (!fIsForSource) {
0221: // Add the second radio button for the jar/zip
0222: fArchiveRadioButton.doFillIntoGrid(topComp, 3);
0223:
0224: // external - workspace selection
0225: DialogField.createEmptySpace(topComp, 1);
0226: Composite radioComposite = new Composite(topComp, SWT.NONE);
0227: radioComposite.setLayoutData(new GridData(GridData.FILL,
0228: GridData.FILL, false, false));
0229: GridLayout layout = new GridLayout(2, true);
0230: layout.marginHeight = 0;
0231: layout.marginWidth = 0;
0232: radioComposite.setLayout(layout);
0233: fExternalRadio.doFillIntoGrid(radioComposite, 1);
0234: fWorkspaceRadio.doFillIntoGrid(radioComposite, 1);
0235: DialogField.createEmptySpace(topComp, 1);
0236:
0237: // Add the jar/zip field
0238: fArchiveField.doFillIntoGrid(topComp, 2);
0239: LayoutUtil.setWidthHint(fArchiveField.getTextControl(null),
0240: converter.convertWidthInCharsToPixels(43));
0241: LayoutUtil.setHorizontalGrabbing(fArchiveField
0242: .getTextControl(null));
0243:
0244: fBrowseArchive.doFillIntoGrid(topComp, 1);
0245:
0246: // Add the path chooser for the jar/zip
0247: fArchivePathField.doFillIntoGrid(topComp, 2);
0248: LayoutUtil.setWidthHint(fArchivePathField
0249: .getTextControl(null), converter
0250: .convertWidthInCharsToPixels(43));
0251: LayoutUtil.setHorizontalGrabbing(fArchivePathField
0252: .getTextControl(null));
0253:
0254: fBrowseArchivePath.doFillIntoGrid(topComp, 1);
0255:
0256: DialogField.createEmptySpace(topComp, 2);
0257: fValidateArchiveButton.doFillIntoGrid(topComp, 1);
0258:
0259: int indent = converter.convertWidthInCharsToPixels(2);
0260: LayoutUtil.setHorizontalIndent(fArchiveField
0261: .getLabelControl(null), indent);
0262: LayoutUtil.setHorizontalIndent(fArchivePathField
0263: .getLabelControl(null), indent);
0264: LayoutUtil.setHorizontalIndent(fURLField
0265: .getLabelControl(null), indent);
0266:
0267: fURLRadioButton.attachDialogFields(new DialogField[] {
0268: fURLField, fBrowseFolder, fValidateURLButton });
0269: fArchiveRadioButton.attachDialogFields(new DialogField[] {
0270: fArchiveField, fBrowseArchive, fExternalRadio,
0271: fWorkspaceRadio, fArchivePathField,
0272: fBrowseArchivePath, fValidateArchiveButton });
0273: }
0274:
0275: return topComp;
0276: }
0277:
0278: private void initializeSelections() {
0279: String initialValue = fInitialURL != null ? fInitialURL
0280: .toExternalForm() : ""; //$NON-NLS-1$
0281:
0282: if (fIsForSource) {
0283: fURLField.setText(initialValue);
0284: return;
0285: }
0286: String prefix = JavaDocLocations.ARCHIVE_PREFIX;
0287: boolean isArchive = initialValue.startsWith(prefix);
0288:
0289: boolean isWorkspaceArchive = false;
0290:
0291: fURLRadioButton.setSelection(!isArchive);
0292: fArchiveRadioButton.setSelection(isArchive);
0293:
0294: if (isArchive) {
0295: String jarPathStr;
0296: String insidePath = ""; //$NON-NLS-1$
0297: int excIndex = initialValue.indexOf("!/"); //$NON-NLS-1$
0298: if (excIndex == -1) {
0299: jarPathStr = initialValue.substring(prefix.length());
0300: } else {
0301: jarPathStr = initialValue.substring(prefix.length(),
0302: excIndex);
0303: insidePath = initialValue.substring(excIndex + 2);
0304: }
0305:
0306: final String fileProtocol = "file:/"; //$NON-NLS-1$
0307: final String resourceProtocol = "platform:/resource/"; //$NON-NLS-1$
0308:
0309: if (jarPathStr.startsWith(fileProtocol)) {
0310: jarPathStr = jarPathStr
0311: .substring(fileProtocol.length());
0312: } else if (jarPathStr.startsWith(resourceProtocol)) {
0313: jarPathStr = jarPathStr.substring(resourceProtocol
0314: .length());
0315: isWorkspaceArchive = true;
0316: } else {
0317: fURLField.setText(initialValue);
0318: return;
0319: }
0320: IPath jarPath = new Path(decodeExclamationMarks(jarPathStr));
0321: fArchivePathField
0322: .setText(decodeExclamationMarks(insidePath));
0323: if (isWorkspaceArchive) {
0324: fArchiveField
0325: .setText(jarPath.makeRelative().toString());
0326: } else {
0327: fArchiveField.setText(jarPath.makeAbsolute()
0328: .toOSString());
0329: }
0330: } else {
0331: fURLField.setText(initialValue);
0332: }
0333: fExternalRadio.setSelection(!isWorkspaceArchive);
0334: fWorkspaceRadio.setSelection(isWorkspaceArchive);
0335:
0336: }
0337:
0338: public void setFocus() {
0339: fURLField.postSetFocusOnDialogField(fShell.getDisplay());
0340: }
0341:
0342: public void performDefaults() {
0343: initializeSelections();
0344: }
0345:
0346: public URL getJavadocLocation() {
0347: if (fIsForSource || fURLRadioButton.isSelected()) {
0348: return fURLResult;
0349: }
0350: return fArchiveURLResult;
0351: }
0352:
0353: private class EntryValidator implements Runnable {
0354:
0355: private String fInvalidMessage = PreferencesMessages.JavadocConfigurationBlock_InvalidLocation_message;
0356: private String fValidMessage = PreferencesMessages.JavadocConfigurationBlock_ValidLocation_message;
0357: private String fTitle = PreferencesMessages.JavadocConfigurationBlock_MessageDialog_title;
0358: private String fUnable = PreferencesMessages.JavadocConfigurationBlock_UnableToValidateLocation_message;
0359:
0360: public void run() {
0361:
0362: URL location = getJavadocLocation();
0363: if (location == null) {
0364: MessageDialog.openWarning(fShell, fTitle,
0365: fInvalidMessage);
0366: return;
0367: }
0368:
0369: try {
0370: String protocol = location.getProtocol();
0371: if (protocol.startsWith("http") || protocol.equals("jar")) { //$NON-NLS-1$ //$NON-NLS-2$
0372: validateURL(location);
0373: } else if (protocol.equals("file")) { //$NON-NLS-1$
0374: validateFile(location);
0375: } else {
0376: MessageDialog.openWarning(fShell, fTitle, fUnable);
0377: }
0378: } catch (MalformedURLException e) {
0379: MessageDialog.openWarning(fShell, fTitle, fUnable);
0380: }
0381:
0382: }
0383:
0384: public void spawnInBrowser(URL url) {
0385: OpenBrowserUtil.open(url, fShell.getDisplay(), fTitle);
0386: }
0387:
0388: private void validateFile(URL location)
0389: throws MalformedURLException {
0390: File folder = new File(location.getFile());
0391: if (folder.isDirectory()) {
0392: File indexFile = new File(folder, "index.html"); //$NON-NLS-1$
0393: if (indexFile.isFile()) {
0394: File packageList = new File(folder, "package-list"); //$NON-NLS-1$
0395: if (packageList.exists()) {
0396: if (MessageDialog.openConfirm(fShell, fTitle,
0397: fValidMessage)) {
0398: spawnInBrowser(indexFile.toURL());
0399: }
0400: return;
0401: }
0402: }
0403: }
0404: MessageDialog.openWarning(fShell, fTitle, fInvalidMessage);
0405: }
0406:
0407: private void validateURL(URL location)
0408: throws MalformedURLException {
0409: IPath path = new Path(location.toExternalForm());
0410: IPath index = path.append("index.html"); //$NON-NLS-1$
0411: IPath packagelist = path.append("package-list"); //$NON-NLS-1$
0412: URL indexURL = new URL(index.toString());
0413: URL packagelistURL = new URL(packagelist.toString());
0414:
0415: boolean suc = checkURLConnection(indexURL)
0416: && checkURLConnection(packagelistURL);
0417: if (suc) {
0418: if (MessageDialog.openConfirm(fShell, fTitle,
0419: fValidMessage))
0420: spawnInBrowser(indexURL);
0421: } else {
0422: MessageDialog.openWarning(fShell, fTitle,
0423: fInvalidMessage);
0424: }
0425: }
0426: }
0427:
0428: private boolean checkURLConnection(URL url) {
0429: int res = 0;
0430: URLConnection connection = null;
0431: try {
0432: connection = url.openConnection();
0433: if (connection instanceof HttpURLConnection) {
0434: connection.connect();
0435: res = ((HttpURLConnection) connection)
0436: .getResponseCode();
0437: }
0438: InputStream is = null;
0439: try {
0440: is = connection.getInputStream();
0441: byte[] buffer = new byte[256];
0442: while (is.read(buffer) != -1) {
0443: }
0444: } finally {
0445: if (is != null)
0446: is.close();
0447: }
0448: } catch (IllegalArgumentException e) {
0449: return false; // bug 91072
0450: } catch (IOException e) {
0451: return false;
0452: }
0453: return res < 400;
0454: }
0455:
0456: private class JDocConfigurationAdapter implements
0457: IDialogFieldListener {
0458:
0459: // ---------- IDialogFieldListener --------
0460: public void dialogFieldChanged(DialogField field) {
0461: jdocDialogFieldChanged(field);
0462: }
0463: }
0464:
0465: private void jdocDialogFieldChanged(DialogField field) {
0466: if (field == fURLField) {
0467: fURLStatus = updateURLStatus();
0468: statusChanged();
0469: } else if (field == fArchiveField) {
0470: fArchiveStatus = updateArchiveStatus();
0471: statusChanged();
0472: } else if (field == fArchivePathField) {
0473: fArchivePathStatus = updateArchivePathStatus();
0474: statusChanged();
0475: } else if (field == fValidateURLButton
0476: || field == fValidateArchiveButton) {
0477: EntryValidator validator = new EntryValidator();
0478: BusyIndicator.showWhile(fShell.getDisplay(), validator);
0479: } else if (field == fBrowseFolder) {
0480: String url = chooseJavaDocFolder();
0481: if (url != null) {
0482: fURLField.setText(url);
0483: }
0484: } else if (field == fBrowseArchive) {
0485: String jarPath = chooseArchive();
0486: if (jarPath != null) {
0487: fArchiveField.setText(jarPath);
0488: }
0489: } else if (field == fExternalRadio || field == fWorkspaceRadio) {
0490: fArchiveStatus = updateArchiveStatus();
0491: statusChanged();
0492: } else if (field == fBrowseArchivePath) {
0493: String archivePath = chooseArchivePath();
0494: if (archivePath != null) {
0495: fArchivePathField.setText(archivePath);
0496: }
0497: } else if (field == fURLRadioButton
0498: || field == fArchiveRadioButton) {
0499: statusChanged();
0500: }
0501: }
0502:
0503: private void statusChanged() {
0504: IStatus status;
0505: boolean isURL = fIsForSource || fURLRadioButton.isSelected();
0506: if (isURL) {
0507: status = fURLStatus;
0508: } else {
0509: status = StatusUtil.getMoreSevere(fArchiveStatus,
0510: fArchivePathStatus);
0511: }
0512: if (!fIsForSource) {
0513: boolean canBrowseArchivePath = !isURL
0514: && fArchiveStatus.isOK()
0515: && fArchiveField.getText().length() > 0;
0516: if (canBrowseArchivePath && fWorkspaceRadio.isSelected()) {
0517: IResource resource = ResourcesPlugin.getWorkspace()
0518: .getRoot().findMember(
0519: new Path(fArchiveField.getText()));
0520: canBrowseArchivePath = resource != null
0521: && resource.getLocation() != null;
0522: }
0523: fBrowseArchivePath.setEnabled(canBrowseArchivePath);
0524: }
0525: fContext.statusChanged(status);
0526: }
0527:
0528: private String chooseArchivePath() {
0529: final String[] res = new String[] { null };
0530: BusyIndicator.showWhile(fShell.getDisplay(), new Runnable() {
0531: public void run() {
0532: res[0] = internalChooseArchivePath();
0533: }
0534: });
0535: return res[0];
0536: }
0537:
0538: private String encodeExclamationMarks(String str) {
0539: StringBuffer buf = new StringBuffer(str.length());
0540: for (int i = 0; i < str.length(); i++) {
0541: char ch = str.charAt(i);
0542: if (ch == '!') {
0543: buf.append("%21"); //$NON-NLS-1$
0544: } else {
0545: buf.append(ch);
0546: }
0547: }
0548: return buf.toString();
0549: }
0550:
0551: private String decodeExclamationMarks(String str) {
0552: StringBuffer buf = new StringBuffer(str.length());
0553: int length = str.length();
0554: for (int i = 0; i < length; i++) {
0555: char ch = str.charAt(i);
0556: if (ch == '%' && (i < length - 2)
0557: && str.charAt(i + 1) == '2'
0558: && str.charAt(i + 2) == '1') {
0559: buf.append('!');
0560: i += 2;
0561: } else {
0562: buf.append(ch);
0563: }
0564: }
0565: return buf.toString();
0566: }
0567:
0568: private String internalChooseArchivePath() {
0569: ZipFile zipFile = null;
0570: try {
0571: if (fWorkspaceRadio.isSelected()) {
0572: IResource resource = ResourcesPlugin.getWorkspace()
0573: .getRoot().findMember(
0574: new Path(fArchiveField.getText()));
0575: if (resource != null) {
0576: IPath location = resource.getLocation();
0577: if (location != null) {
0578: zipFile = new ZipFile(location.toOSString());
0579: }
0580: }
0581: } else {
0582: zipFile = new ZipFile(fArchiveField.getText());
0583: }
0584: if (zipFile == null) {
0585: return null;
0586: }
0587:
0588: ZipFileStructureProvider provider = new ZipFileStructureProvider(
0589: zipFile);
0590:
0591: ILabelProvider lp = new ZipDialogLabelProvider(provider);
0592: ZipDialogContentProvider cp = new ZipDialogContentProvider(
0593: provider);
0594:
0595: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
0596: fShell, lp, cp);
0597: dialog.setAllowMultiple(false);
0598: dialog.setValidator(new ZipDialogValidator());
0599: dialog
0600: .setTitle(PreferencesMessages.JavadocConfigurationBlock_browse_jarorzip_path_title);
0601: dialog
0602: .setMessage(PreferencesMessages.JavadocConfigurationBlock_location_in_jarorzip_message);
0603: dialog.setComparator(new ViewerComparator());
0604:
0605: String init = fArchivePathField.getText();
0606: if (init.length() == 0) {
0607: init = "docs/api"; //$NON-NLS-1$
0608: }
0609: dialog.setInitialSelection(cp.findElement(new Path(init)));
0610:
0611: dialog.setInput(this );
0612: if (dialog.open() == Window.OK) {
0613: String name = provider.getFullPath(dialog
0614: .getFirstResult());
0615: return new Path(name).removeTrailingSeparator()
0616: .toString();
0617: }
0618: } catch (IOException e) {
0619: JavaPlugin.log(e);
0620: } finally {
0621: if (zipFile != null) {
0622: try {
0623: zipFile.close();
0624: } catch (IOException e1) {
0625: // ignore
0626: }
0627: }
0628: }
0629: return null;
0630: }
0631:
0632: private String chooseArchive() {
0633: if (fWorkspaceRadio.isSelected()) {
0634: return chooseWorkspaceArchive();
0635: }
0636:
0637: IPath currPath = new Path(fArchiveField.getText());
0638: if (ArchiveFileFilter.isArchivePath(currPath)) {
0639: currPath = currPath.removeLastSegments(1);
0640: }
0641:
0642: FileDialog dialog = new FileDialog(fShell, SWT.OPEN);
0643: dialog.setFilterExtensions(new String[] { FILE_IMPORT_MASK });
0644: dialog
0645: .setText(PreferencesMessages.JavadocConfigurationBlock_zipImportSource_title);
0646: dialog.setFilterPath(currPath.toOSString());
0647:
0648: return dialog.open();
0649: }
0650:
0651: private String chooseWorkspaceArchive() {
0652: String initSelection = fArchiveField.getText();
0653:
0654: ILabelProvider lp = new WorkbenchLabelProvider();
0655: ITreeContentProvider cp = new WorkbenchContentProvider();
0656: Class[] acceptedClasses = new Class[] { IFile.class };
0657: TypedElementSelectionValidator validator = new TypedElementSelectionValidator(
0658: acceptedClasses, true);
0659:
0660: IResource initSel = null;
0661: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
0662: if (initSelection.length() > 0) {
0663: initSel = root.findMember(new Path(initSelection));
0664: }
0665:
0666: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
0667: fShell, lp, cp);
0668: dialog.addFilter(new ArchiveFileFilter((List) null, true));
0669: dialog.setAllowMultiple(false);
0670: dialog.setValidator(validator);
0671: dialog.setComparator(new ResourceComparator(
0672: ResourceComparator.NAME));
0673: dialog
0674: .setTitle(PreferencesMessages.JavadocConfigurationBlock_workspace_archive_selection_dialog_title);
0675: dialog
0676: .setMessage(PreferencesMessages.JavadocConfigurationBlock_workspace_archive_selection_dialog_description);
0677: dialog.setInput(root);
0678: dialog.setInitialSelection(initSel);
0679: dialog.setHelpAvailable(false);
0680: if (dialog.open() == Window.OK) {
0681: IResource res = (IResource) dialog.getFirstResult();
0682: return res.getFullPath().makeRelative().toString();
0683: }
0684: return null;
0685: }
0686:
0687: /**
0688: * Display an error dialog with the specified message.
0689: *
0690: * @param message the error message
0691: */
0692: protected void displayErrorDialog(String message) {
0693: MessageDialog.openError(fShell, ERROR_DIALOG_TITLE, message);
0694: }
0695:
0696: private String chooseJavaDocFolder() {
0697: String initPath = ""; //$NON-NLS-1$
0698: if (fURLResult != null
0699: && "file".equals(fURLResult.getProtocol())) { //$NON-NLS-1$
0700: initPath = (new File(fURLResult.getFile())).getPath();
0701: }
0702: DirectoryDialog dialog = new DirectoryDialog(fShell);
0703: dialog
0704: .setText(PreferencesMessages.JavadocConfigurationBlock_javadocFolderDialog_label);
0705: dialog
0706: .setMessage(PreferencesMessages.JavadocConfigurationBlock_javadocFolderDialog_message);
0707: dialog.setFilterPath(initPath);
0708: String result = dialog.open();
0709: if (result != null) {
0710: try {
0711: URL url = new File(result).toURL();
0712: return url.toExternalForm();
0713: } catch (MalformedURLException e) {
0714: JavaPlugin.log(e);
0715: }
0716: }
0717: return null;
0718: }
0719:
0720: private IStatus updateURLStatus() {
0721: StatusInfo status = new StatusInfo();
0722: fURLResult = null;
0723: try {
0724: String jdocLocation = fURLField.getText();
0725: if (jdocLocation.length() == 0) {
0726: return status;
0727: }
0728: URL url = new URL(jdocLocation);
0729: if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
0730: if (url.getFile() == null) {
0731: status
0732: .setError(PreferencesMessages.JavadocConfigurationBlock_error_notafolder);
0733: return status;
0734: }
0735: }
0736: fURLResult = url;
0737: } catch (MalformedURLException e) {
0738: status
0739: .setError(PreferencesMessages.JavadocConfigurationBlock_MalformedURL_error);
0740: return status;
0741: }
0742:
0743: return status;
0744: }
0745:
0746: private IStatus updateArchiveStatus() {
0747: try {
0748: fArchiveURLResult = null;
0749:
0750: StatusInfo status = new StatusInfo();
0751: String jdocLocation = fArchiveField.getText();
0752: if (jdocLocation.length() > 0) {
0753: if (!Path.ROOT.isValidPath(jdocLocation)) {
0754: status
0755: .setError(PreferencesMessages.JavadocConfigurationBlock_error_invalidarchivepath);
0756: return status;
0757: }
0758: if (fWorkspaceRadio.isSelected()) {
0759: IWorkspaceRoot root = ResourcesPlugin
0760: .getWorkspace().getRoot();
0761: IResource res = root.findMember(new Path(
0762: jdocLocation));
0763: if (res != null) {
0764: if (!(res instanceof IFile)) {
0765: status
0766: .setError(PreferencesMessages.JavadocConfigurationBlock_error_archive_not_found_in_workspace);
0767: return status;
0768: }
0769: } else {
0770: status
0771: .setError(PreferencesMessages.JavadocConfigurationBlock_error_archive_not_found_in_workspace);
0772: return status;
0773: }
0774: } else {
0775: IPath path = Path.fromOSString(jdocLocation);
0776: if (!path.isAbsolute()) {
0777: status
0778: .setError(PreferencesMessages.JavadocConfigurationBlock_error_archivepathnotabsolute);
0779: return status;
0780: }
0781: File jarFile = new File(jdocLocation);
0782: if (jarFile.isDirectory()) {
0783: status
0784: .setError(PreferencesMessages.JavadocConfigurationBlock_error_notafile);
0785: return status;
0786: }
0787: if (!jarFile.exists()) {
0788: status
0789: .setWarning(PreferencesMessages.JavadocConfigurationBlock_error_notafile);
0790: }
0791: }
0792: fArchiveURLResult = getArchiveURL();
0793: }
0794: return status;
0795: } catch (MalformedURLException e) {
0796: StatusInfo status = new StatusInfo();
0797: status.setError(e.getMessage());
0798: return status;
0799: }
0800: }
0801:
0802: private IStatus updateArchivePathStatus() {
0803: // no validation yet
0804: try {
0805: fArchiveURLResult = getArchiveURL();
0806: } catch (MalformedURLException e) {
0807: fArchiveURLResult = null;
0808: StatusInfo status = new StatusInfo();
0809: status.setError(e.getMessage());
0810: //status.setError(PreferencesMessages.getString("JavadocConfigurationBlock.MalformedURL.error")); //$NON-NLS-1$
0811: return status;
0812: }
0813: return new StatusInfo();
0814:
0815: }
0816:
0817: private URL getArchiveURL() throws MalformedURLException {
0818: String jarLoc = fArchiveField.getText();
0819: String innerPath = fArchivePathField.getText().trim();
0820:
0821: StringBuffer buf = new StringBuffer();
0822: buf.append("jar:"); //$NON-NLS-1$
0823:
0824: if (fWorkspaceRadio.isSelected()) {
0825: IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
0826: .getRoot();
0827: IResource res = root.findMember(new Path(jarLoc));
0828: if (res != null) {
0829: buf
0830: .append("platform:/resource").append(encodeExclamationMarks(res.getFullPath().toString())); //$NON-NLS-1$
0831: }
0832: } else {
0833: buf.append(encodeExclamationMarks(new File(jarLoc).toURL()
0834: .toExternalForm()));
0835: }
0836: buf.append('!');
0837: if (innerPath.length() > 0) {
0838: if (innerPath.charAt(0) != '/') {
0839: buf.append('/');
0840: }
0841: buf.append(innerPath);
0842: } else {
0843: buf.append('/');
0844: }
0845: return new URL(buf.toString());
0846: }
0847:
0848: /**
0849: * An adapter for presenting a zip file in a tree viewer.
0850: */
0851: private static class ZipDialogContentProvider implements
0852: ITreeContentProvider {
0853:
0854: private ZipFileStructureProvider fProvider;
0855:
0856: public ZipDialogContentProvider(
0857: ZipFileStructureProvider provider) {
0858: fProvider = provider;
0859: }
0860:
0861: public Object findElement(IPath path) {
0862: String[] segments = path.segments();
0863:
0864: Object elem = fProvider.getRoot();
0865: for (int i = 0; i < segments.length && elem != null; i++) {
0866: List list = fProvider.getChildren(elem);
0867: String name = segments[i];
0868: elem = null;
0869: for (int k = 0; k < list.size(); k++) {
0870: Object curr = list.get(k);
0871: if (fProvider.isFolder(curr)
0872: && name.equals(fProvider.getLabel(curr))) {
0873: elem = curr;
0874: break;
0875: }
0876: }
0877: }
0878: return elem;
0879: }
0880:
0881: private Object recursiveFind(Object element, String name) {
0882: if (name.equals(fProvider.getLabel(element))) {
0883: return element;
0884: }
0885: List list = fProvider.getChildren(element);
0886: if (list != null) {
0887: for (int k = 0; k < list.size(); k++) {
0888: Object res = recursiveFind(list.get(k), name);
0889: if (res != null) {
0890: return res;
0891: }
0892: }
0893: }
0894: return null;
0895: }
0896:
0897: public Object findFileByName(String name) {
0898: return recursiveFind(fProvider.getRoot(), name);
0899: }
0900:
0901: /* non java-doc
0902: * @see ITreeContentProvider#inputChanged
0903: */
0904: public void inputChanged(Viewer viewer, Object oldInput,
0905: Object newInput) {
0906: }
0907:
0908: /* non java-doc
0909: * @see ITreeContentProvider#getParent
0910: */
0911: public Object getParent(Object element) {
0912: if (element.equals(fProvider.getRoot())) {
0913: return null;
0914: }
0915: IPath path = new Path(fProvider.getFullPath(element));
0916: if (path.segmentCount() > 0) {
0917: return findElement(path.removeLastSegments(1));
0918: }
0919: return fProvider.getRoot();
0920: }
0921:
0922: /* non java-doc
0923: * @see ITreeContentProvider#hasChildren
0924: */
0925: public boolean hasChildren(Object element) {
0926: List list = fProvider.getChildren(element);
0927: if (list != null) {
0928: for (int i = 0; i < list.size(); i++) {
0929: if (fProvider.isFolder(list.get(i))) {
0930: return true;
0931: }
0932: }
0933: }
0934: return false;
0935: }
0936:
0937: /* non java-doc
0938: * @see ITreeContentProvider#getChildren
0939: */
0940: public Object[] getChildren(Object element) {
0941: List list = fProvider.getChildren(element);
0942: ArrayList res = new ArrayList();
0943: if (list != null) {
0944: for (int i = 0; i < list.size(); i++) {
0945: Object curr = list.get(i);
0946: if (fProvider.isFolder(curr)) {
0947: res.add(curr);
0948: }
0949: }
0950: }
0951: return res.toArray();
0952: }
0953:
0954: /* non java-doc
0955: * @see ITreeContentProvider#getElements
0956: */
0957: public Object[] getElements(Object element) {
0958: return new Object[] { fProvider.getRoot() };
0959: }
0960:
0961: /* non java-doc
0962: * @see IContentProvider#dispose
0963: */
0964: public void dispose() {
0965: }
0966: }
0967:
0968: private static class ZipDialogLabelProvider extends LabelProvider {
0969:
0970: private final Image IMG_JAR = JavaUI
0971: .getSharedImages()
0972: .getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_JAR);
0973: private final Image IMG_FOLDER = PlatformUI.getWorkbench()
0974: .getSharedImages().getImage(
0975: ISharedImages.IMG_OBJ_FOLDER);
0976:
0977: private ZipFileStructureProvider fProvider;
0978:
0979: public ZipDialogLabelProvider(ZipFileStructureProvider provider) {
0980: fProvider = provider;
0981: }
0982:
0983: public Image getImage(Object element) {
0984: if (element == fProvider.getRoot()) {
0985: return IMG_JAR;
0986: } else {
0987: return IMG_FOLDER;
0988: }
0989: }
0990:
0991: public String getText(Object element) {
0992: if (element == fProvider.getRoot()) {
0993: return fProvider.getZipFile().getName();
0994: }
0995: return fProvider.getLabel(element);
0996: }
0997: }
0998:
0999: private static class ZipDialogValidator implements
1000: ISelectionStatusValidator {
1001: public ZipDialogValidator() {
1002: super ();
1003: }
1004:
1005: /*
1006: * @see ISelectionValidator#validate(Object[])
1007: */
1008: public IStatus validate(Object[] selection) {
1009: String message = ""; //$NON-NLS-1$
1010: return new StatusInfo(IStatus.INFO, message);
1011: }
1012: }
1013:
1014: }
|