KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > JavadocConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.preferences;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.HttpURLConnection JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.net.URLConnection JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.zip.ZipFile JavaDoc;
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 JavaDoc FILE_IMPORT_MASK= "*.jar;*.zip"; //$NON-NLS-1$
82
private static final String JavaDoc ERROR_DIALOG_TITLE= "Error Dialog"; //$NON-NLS-1$
83

84     private StringDialogField fURLField;
85     private StringDialogField fArchiveField;
86     private StringDialogField fArchivePathField;
87     private URL JavaDoc 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 JavaDoc fURLResult;
104     private URL JavaDoc fArchiveURLResult;
105     
106     boolean fIsForSource;
107     
108     
109     public JavadocConfigurationBlock(Shell shell, IStatusChangeListener context, URL JavaDoc 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         // Add the first radio button for the path
188
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         //DialogField.createEmptySpace(topComp, 3);
202

203         if (!fIsForSource) {
204             // Add the second radio button for the jar/zip
205
fArchiveRadioButton.doFillIntoGrid(topComp, 3);
206     
207             
208             // external - workspace selection
209
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             // Add the jar/zip field
221
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             // Add the path chooser for the jar/zip
228
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 JavaDoc initialValue = fInitialURL != null ? fInitialURL.toExternalForm() : ""; //$NON-NLS-1$
252

253         if (fIsForSource) {
254             fURLField.setText(initialValue);
255             return;
256         }
257         String JavaDoc 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 JavaDoc jarPathStr;
267             String JavaDoc insidePath= ""; //$NON-NLS-1$
268
int excIndex= initialValue.indexOf("!/"); //$NON-NLS-1$
269
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 JavaDoc fileProtocol= "file:/"; //$NON-NLS-1$
277
final String JavaDoc resourceProtocol= "platform:/resource/"; //$NON-NLS-1$
278

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 JavaDoc getJavadocLocation() {
312         if (fIsForSource || fURLRadioButton.isSelected()) {
313             return fURLResult;
314         }
315         return fArchiveURLResult;
316     }
317         
318     private class EntryValidator implements Runnable JavaDoc {
319
320         private String JavaDoc fInvalidMessage= PreferencesMessages.JavadocConfigurationBlock_InvalidLocation_message;
321         private String JavaDoc fValidMessage= PreferencesMessages.JavadocConfigurationBlock_ValidLocation_message;
322         private String JavaDoc fTitle= PreferencesMessages.JavadocConfigurationBlock_MessageDialog_title;
323         private String JavaDoc fUnable= PreferencesMessages.JavadocConfigurationBlock_UnableToValidateLocation_message;
324         public void run() {
325
326             URL JavaDoc location= getJavadocLocation();
327             if (location == null) {
328                 MessageDialog.openWarning(fShell, fTitle, fInvalidMessage);
329                 return;
330             }
331
332             try {
333                 String JavaDoc protocol = location.getProtocol();
334                 if (protocol.startsWith("http") || protocol.equals("jar")) { //$NON-NLS-1$ //$NON-NLS-2$
335
validateURL(location);
336                 } else if (protocol.equals("file")) { //$NON-NLS-1$
337
validateFile(location);
338                 } else {
339                     MessageDialog.openWarning(fShell, fTitle, fUnable);
340                 }
341             } catch (MalformedURLException JavaDoc e) {
342                 MessageDialog.openWarning(fShell, fTitle, fUnable);
343             }
344
345         }
346         
347         public void spawnInBrowser(URL JavaDoc url) {
348             OpenBrowserUtil.open(url, fShell.getDisplay(), fTitle);
349         }
350
351         private void validateFile(URL JavaDoc location) throws MalformedURLException JavaDoc {
352             File JavaDoc folder = new File JavaDoc(location.getFile());
353             if (folder.isDirectory()) {
354                 File JavaDoc indexFile= new File JavaDoc(folder, "index.html"); //$NON-NLS-1$
355
if (indexFile.isFile()) {
356                     File JavaDoc packageList= new File JavaDoc(folder, "package-list"); //$NON-NLS-1$
357
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 JavaDoc location) throws MalformedURLException JavaDoc {
369             IPath path= new Path(location.toExternalForm());
370             IPath index = path.append("index.html"); //$NON-NLS-1$
371
IPath packagelist = path.append("package-list"); //$NON-NLS-1$
372
URL JavaDoc indexURL = new URL JavaDoc(index.toString());
373             URL JavaDoc packagelistURL = new URL JavaDoc(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 JavaDoc url) {
386         int res= 0;
387         URLConnection JavaDoc connection= null;
388         try {
389             connection= url.openConnection();
390             if (connection instanceof HttpURLConnection JavaDoc) {
391                 connection.connect();
392                 res= ((HttpURLConnection JavaDoc) connection).getResponseCode();
393             }
394             InputStream JavaDoc 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 JavaDoc e) {
405             return false; // bug 91072
406
} catch (IOException JavaDoc e) {
407             return false;
408         }
409         return res < 400;
410     }
411     
412     
413     private class JDocConfigurationAdapter implements IDialogFieldListener {
414
415         // ---------- IDialogFieldListener --------
416
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 JavaDoc url= chooseJavaDocFolder();
437             if (url != null) {
438                 fURLField.setText(url);
439             }
440         } else if (field == fBrowseArchive) {
441             String JavaDoc 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 JavaDoc 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 JavaDoc chooseArchivePath() {
479         final String JavaDoc[] res= new String JavaDoc[] { null };
480         BusyIndicator.showWhile(fShell.getDisplay(), new Runnable JavaDoc() {
481             public void run() {
482                 res[0]= internalChooseArchivePath();
483             }
484         });
485         return res[0];
486     }
487     
488     private String JavaDoc encodeExclamationMarks(String JavaDoc str) {
489         StringBuffer JavaDoc buf= new StringBuffer JavaDoc(str.length());
490         for (int i= 0; i < str.length(); i++) {
491             char ch= str.charAt(i);
492             if (ch == '!') {
493                 buf.append("%21"); //$NON-NLS-1$
494
} else {
495                 buf.append(ch);
496             }
497         }
498         return buf.toString();
499     }
500     
501     private String JavaDoc decodeExclamationMarks(String JavaDoc str) {
502         StringBuffer JavaDoc buf= new StringBuffer JavaDoc(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 JavaDoc internalChooseArchivePath() {
519         ZipFile JavaDoc 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 JavaDoc(location.toOSString());
527                     }
528                 }
529             } else {
530                 zipFile= new ZipFile JavaDoc(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 JavaDoc init= fArchivePathField.getText();
549             if (init.length() == 0) {
550                 init= "docs/api"; //$NON-NLS-1$
551
}
552             dialog.setInitialSelection(cp.findElement(new Path(init)));
553             
554             dialog.setInput(this);
555             if (dialog.open() == Window.OK) {
556                 String JavaDoc name= provider.getFullPath(dialog.getFirstResult());
557                 return new Path(name).removeTrailingSeparator().toString();
558             }
559         } catch (IOException JavaDoc e) {
560             JavaPlugin.log(e);
561         } finally {
562             if (zipFile != null) {
563                 try {
564                     zipFile.close();
565                 } catch (IOException JavaDoc e1) {
566                     // ignore
567
}
568             }
569         }
570         return null;
571     }
572
573     private String JavaDoc 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 JavaDoc[] { 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 JavaDoc chooseWorkspaceArchive() {
592         String JavaDoc initSelection= fArchiveField.getText();
593         
594         ILabelProvider lp= new WorkbenchLabelProvider();
595         ITreeContentProvider cp= new WorkbenchContentProvider();
596         Class JavaDoc[] acceptedClasses= new Class JavaDoc[] { 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 JavaDoc) 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     /**
623      * Display an error dialog with the specified message.
624      *
625      * @param message the error message
626      */

627     protected void displayErrorDialog(String JavaDoc message) {
628         MessageDialog.openError(fShell, ERROR_DIALOG_TITLE, message);
629     }
630         
631     private String JavaDoc chooseJavaDocFolder() {
632         String JavaDoc initPath= ""; //$NON-NLS-1$
633
if (fURLResult != null && "file".equals(fURLResult.getProtocol())) { //$NON-NLS-1$
634
initPath= (new File JavaDoc(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 JavaDoc result= dialog.open();
641         if (result != null) {
642             try {
643                 URL JavaDoc url= new File JavaDoc(result).toURL();
644                 return url.toExternalForm();
645             } catch (MalformedURLException JavaDoc 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 JavaDoc jdocLocation= fURLField.getText();
657             if (jdocLocation.length() == 0) {
658                 return status;
659             }
660             URL JavaDoc url= new URL JavaDoc(jdocLocation);
661             if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
662
if (url.getFile() == null) {
663                     status.setError(PreferencesMessages.JavadocConfigurationBlock_error_notafolder);
664                     return status;
665                 }
666             }
667             fURLResult= url;
668         } catch (MalformedURLException JavaDoc 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 JavaDoc 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 JavaDoc jarFile= new File JavaDoc(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 JavaDoc e) {
718             StatusInfo status= new StatusInfo();
719             status.setError(e.getMessage());
720             return status;
721         }
722     }
723     
724     private IStatus updateArchivePathStatus() {
725         // no validation yet
726
try {
727             fArchiveURLResult= getArchiveURL();
728         } catch (MalformedURLException JavaDoc e) {
729             fArchiveURLResult= null;
730             StatusInfo status= new StatusInfo();
731             status.setError(e.getMessage());
732             //status.setError(PreferencesMessages.getString("JavadocConfigurationBlock.MalformedURL.error")); //$NON-NLS-1$
733
return status;
734         }
735         return new StatusInfo();
736     
737     }
738     
739     
740     private URL JavaDoc getArchiveURL() throws MalformedURLException JavaDoc {
741         String JavaDoc jarLoc= fArchiveField.getText();
742         String JavaDoc innerPath= fArchivePathField.getText().trim();
743         
744         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
745         buf.append("jar:"); //$NON-NLS-1$
746

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())); //$NON-NLS-1$
752
}
753         } else {
754             buf.append(encodeExclamationMarks(new File JavaDoc(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 JavaDoc(buf.toString());
766     }
767     
768
769     /**
770      * An adapter for presenting a zip file in a tree viewer.
771      */

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 JavaDoc findElement(IPath path) {
781             String JavaDoc[] segments= path.segments();
782             
783             Object JavaDoc elem= fProvider.getRoot();
784             for (int i= 0; i < segments.length && elem != null; i++) {
785                 List JavaDoc list= fProvider.getChildren(elem);
786                 String JavaDoc name= segments[i];
787                 elem= null;
788                 for (int k= 0; k < list.size(); k++) {
789                     Object JavaDoc 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 JavaDoc recursiveFind(Object JavaDoc element, String JavaDoc name) {
800             if (name.equals(fProvider.getLabel(element))) {
801                 return element;
802             }
803             List JavaDoc list= fProvider.getChildren(element);
804             if (list != null) {
805                 for (int k= 0; k < list.size(); k++) {
806                     Object JavaDoc res= recursiveFind(list.get(k), name);
807                     if (res != null) {
808                         return res;
809                     }
810                 }
811             }
812             return null;
813         }
814         
815         public Object JavaDoc findFileByName(String JavaDoc name) {
816             return recursiveFind(fProvider.getRoot(), name);
817         }
818
819         /* non java-doc
820          * @see ITreeContentProvider#inputChanged
821          */

822         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
823         }
824     
825         /* non java-doc
826           * @see ITreeContentProvider#getParent
827           */

828         public Object JavaDoc getParent(Object JavaDoc 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         /* non java-doc
840          * @see ITreeContentProvider#hasChildren
841          */

842         public boolean hasChildren(Object JavaDoc element) {
843             List JavaDoc 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         /* non java-doc
855          * @see ITreeContentProvider#getChildren
856          */

857         public Object JavaDoc[] getChildren(Object JavaDoc element) {
858             List JavaDoc list= fProvider.getChildren(element);
859             ArrayList JavaDoc res= new ArrayList JavaDoc();
860             if (list != null) {
861                 for (int i= 0; i < list.size(); i++) {
862                     Object JavaDoc curr= list.get(i);
863                     if (fProvider.isFolder(curr)) {
864                         res.add(curr);
865                     }
866                 }
867             }
868             return res.toArray();
869         }
870     
871         /* non java-doc
872          * @see ITreeContentProvider#getElements
873          */

874         public Object JavaDoc[] getElements(Object JavaDoc element) {
875             return new Object JavaDoc[] {fProvider.getRoot() };
876         }
877     
878         /* non java-doc
879          * @see IContentProvider#dispose
880          */

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 JavaDoc element) {
899             if (element == fProvider.getRoot()) {
900                 return IMG_JAR;
901             } else {
902                 return IMG_FOLDER;
903             }
904         }
905     
906         public String JavaDoc getText(Object JavaDoc 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         /*
920          * @see ISelectionValidator#validate(Object[])
921          */

922         public IStatus validate(Object JavaDoc[] selection) {
923             String JavaDoc message= ""; //$NON-NLS-1$
924
return new StatusInfo(IStatus.INFO, message);
925         }
926     }
927
928 }
929
Popular Tags