KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > infoviews > JavadocView


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.infoviews;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStreamReader JavaDoc;
16 import java.io.Reader JavaDoc;
17 import java.io.StringReader JavaDoc;
18 import java.net.URL JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.FileLocator;
22 import org.eclipse.core.runtime.ListenerList;
23 import org.eclipse.core.runtime.Platform;
24
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.SWTError;
27 import org.eclipse.swt.browser.Browser;
28 import org.eclipse.swt.custom.StyledText;
29 import org.eclipse.swt.events.ControlAdapter;
30 import org.eclipse.swt.events.ControlEvent;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Color;
34 import org.eclipse.swt.graphics.FontData;
35 import org.eclipse.swt.graphics.RGB;
36 import org.eclipse.swt.graphics.Rectangle;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Display;
40
41 import org.eclipse.jface.action.Action;
42 import org.eclipse.jface.action.IAction;
43 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
44 import org.eclipse.jface.internal.text.html.HTMLPrinter;
45 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
46 import org.eclipse.jface.preference.IPreferenceStore;
47 import org.eclipse.jface.resource.JFaceResources;
48 import org.eclipse.jface.util.IPropertyChangeListener;
49 import org.eclipse.jface.util.PropertyChangeEvent;
50 import org.eclipse.jface.viewers.ISelection;
51 import org.eclipse.jface.viewers.ISelectionChangedListener;
52 import org.eclipse.jface.viewers.ISelectionProvider;
53 import org.eclipse.jface.viewers.SelectionChangedEvent;
54 import org.eclipse.jface.viewers.StructuredSelection;
55 import org.eclipse.jface.window.Window;
56
57 import org.eclipse.jface.text.BadLocationException;
58 import org.eclipse.jface.text.BadPartitioningException;
59 import org.eclipse.jface.text.DefaultInformationControl;
60 import org.eclipse.jface.text.Document;
61 import org.eclipse.jface.text.IDocument;
62 import org.eclipse.jface.text.IDocumentExtension3;
63 import org.eclipse.jface.text.ITextSelection;
64 import org.eclipse.jface.text.ITypedRegion;
65 import org.eclipse.jface.text.TextPresentation;
66 import org.eclipse.jface.text.TextSelection;
67 import org.eclipse.jface.text.TextUtilities;
68
69 import org.eclipse.ui.IWorkbenchPart;
70 import org.eclipse.ui.PlatformUI;
71 import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds;
72 import org.eclipse.ui.texteditor.IDocumentProvider;
73 import org.eclipse.ui.texteditor.ITextEditor;
74
75 import org.eclipse.jdt.core.IClassFile;
76 import org.eclipse.jdt.core.ICompilationUnit;
77 import org.eclipse.jdt.core.IJavaElement;
78 import org.eclipse.jdt.core.IMember;
79 import org.eclipse.jdt.core.IOpenable;
80 import org.eclipse.jdt.core.IPackageFragmentRoot;
81 import org.eclipse.jdt.core.JavaModelException;
82
83 import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations;
84
85 import org.eclipse.jdt.ui.JavaElementLabels;
86 import org.eclipse.jdt.ui.JavadocContentAccess;
87 import org.eclipse.jdt.ui.PreferenceConstants;
88 import org.eclipse.jdt.ui.text.IJavaPartitions;
89
90 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
91 import org.eclipse.jdt.internal.ui.JavaPlugin;
92 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
93
94 import org.osgi.framework.Bundle;
95
96 /**
97  * View which shows Javadoc for a given Java element.
98  *
99  * FIXME: As of 3.0 selectAll() and getSelection() is not working
100  * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=63022
101  *
102  * @since 3.0
103  */

104 public class JavadocView extends AbstractInfoView {
105
106     /**
107      * Preference key for the preference whether to show a dialog
108      * when the SWT Browser widget is not available.
109      * @since 3.0
110      */

111     private static final String JavaDoc DO_NOT_WARN_PREFERENCE_KEY= "JavadocView.error.doNotWarn"; //$NON-NLS-1$
112

113     // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=73558
114
private static final boolean WARNING_DIALOG_ENABLED= false;
115
116     /** Flags used to render a label in the text widget. */
117     private static final long LABEL_FLAGS= JavaElementLabels.ALL_FULLY_QUALIFIED
118         | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_EXCEPTIONS
119         | JavaElementLabels.F_PRE_TYPE_SIGNATURE | JavaElementLabels.T_TYPE_PARAMETERS;
120
121
122     /** The HTML widget. */
123     private Browser fBrowser;
124     /** The text widget. */
125     private StyledText fText;
126     /** The information presenter. */
127     private DefaultInformationControl.IInformationPresenter fPresenter;
128     /** The text presentation. */
129     private TextPresentation fPresentation= new TextPresentation();
130     /** The select all action */
131     private SelectAllAction fSelectAllAction;
132     /** The style sheet (css) */
133     private static String JavaDoc fgStyleSheet;
134     /**
135      * <code>true</code> once the style sheet has been loaded.
136      * @since 3.3
137      */

138     private static boolean fgStyleSheetLoaded= false;
139
140     /** The Browser widget */
141     private boolean fIsUsingBrowserWidget;
142
143     private RGB fBackgroundColorRGB;
144     /**
145      * The font listener.
146      * @since 3.3
147      */

148     private IPropertyChangeListener fFontListener;
149
150     
151     /**
152      * The Javadoc view's select all action.
153      */

154     private class SelectAllAction extends Action {
155
156         /** The control. */
157         private Control fControl;
158         /** The selection provider. */
159         private SelectionProvider fSelectionProvider;
160
161         /**
162          * Creates the action.
163          *
164          * @param control the widget
165          * @param selectionProvider the selection provider
166          */

167         public SelectAllAction(Control control, SelectionProvider selectionProvider) {
168             super("selectAll"); //$NON-NLS-1$
169

170             Assert.isNotNull(control);
171             Assert.isNotNull(selectionProvider);
172             fControl= control;
173             fSelectionProvider= selectionProvider;
174
175             // FIXME: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=63022
176
setEnabled(!fIsUsingBrowserWidget);
177
178             setText(InfoViewMessages.SelectAllAction_label);
179             setToolTipText(InfoViewMessages.SelectAllAction_tooltip);
180             setDescription(InfoViewMessages.SelectAllAction_description);
181
182             PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IAbstractTextEditorHelpContextIds.SELECT_ALL_ACTION);
183         }
184
185         /**
186          * Selects all in the view.
187          */

188         public void run() {
189             if (fControl instanceof StyledText)
190                 ((StyledText)fControl).selectAll();
191             else {
192                 // FIXME: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=63022
193
// ((Browser)fControl).selectAll();
194
if (fSelectionProvider != null)
195                     fSelectionProvider.fireSelectionChanged();
196             }
197         }
198     }
199
200     /**
201      * The Javadoc view's selection provider.
202      */

203     private static class SelectionProvider implements ISelectionProvider {
204
205         /** The selection changed listeners. */
206         private ListenerList fListeners= new ListenerList(ListenerList.IDENTITY);
207         /** The widget. */
208         private Control fControl;
209
210         /**
211          * Creates a new selection provider.
212          *
213          * @param control the widget
214          */

215         public SelectionProvider(Control control) {
216             Assert.isNotNull(control);
217             fControl= control;
218             if (fControl instanceof StyledText) {
219                 ((StyledText)fControl).addSelectionListener(new SelectionAdapter() {
220                     public void widgetSelected(SelectionEvent e) {
221                         fireSelectionChanged();
222                     }
223                 });
224             } else {
225                 // FIXME: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=63022
226
// ((Browser)fControl).addSelectionListener(new SelectionAdapter() {
227
// public void widgetSelected(SelectionEvent e) {
228
// fireSelectionChanged();
229
// }
230
// });
231
}
232         }
233
234         /**
235          * Sends a selection changed event to all listeners.
236          */

237         public void fireSelectionChanged() {
238             ISelection selection= getSelection();
239             SelectionChangedEvent event= new SelectionChangedEvent(this, selection);
240             Object JavaDoc[] selectionChangedListeners= fListeners.getListeners();
241             for (int i= 0; i < selectionChangedListeners.length; i++)
242                 ((ISelectionChangedListener)selectionChangedListeners[i]).selectionChanged(event);
243         }
244
245         /*
246          * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
247          */

248         public void addSelectionChangedListener(ISelectionChangedListener listener) {
249             fListeners.add(listener);
250         }
251
252         /*
253          * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
254          */

255         public ISelection getSelection() {
256             if (fControl instanceof StyledText) {
257                 IDocument document= new Document(((StyledText)fControl).getSelectionText());
258                 return new TextSelection(document, 0, document.getLength());
259             } else {
260                 // FIXME: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=63022
261
return StructuredSelection.EMPTY;
262             }
263         }
264
265         /*
266          * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
267          */

268         public void removeSelectionChangedListener(ISelectionChangedListener listener) {
269             fListeners.remove(listener);
270         }
271
272         /*
273          * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
274          */

275         public void setSelection(ISelection selection) {
276             // not supported
277
}
278     }
279
280     /*
281      * @see AbstractInfoView#internalCreatePartControl(Composite)
282      */

283     protected void internalCreatePartControl(Composite parent) {
284         try {
285             fBrowser= new Browser(parent, SWT.NONE);
286             fIsUsingBrowserWidget= true;
287             
288         } catch (SWTError er) {
289
290             /* The Browser widget throws an SWTError if it fails to
291              * instantiate properly. Application code should catch
292              * this SWTError and disable any feature requiring the
293              * Browser widget.
294              * Platform requirements for the SWT Browser widget are available
295              * from the SWT FAQ web site.
296              */

297
298             IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
299             boolean doNotWarn= store.getBoolean(DO_NOT_WARN_PREFERENCE_KEY);
300             if (WARNING_DIALOG_ENABLED && !doNotWarn) {
301                 String JavaDoc title= InfoViewMessages.JavadocView_error_noBrowser_title;
302                 String JavaDoc message= InfoViewMessages.JavadocView_error_noBrowser_message;
303                 String JavaDoc toggleMessage= InfoViewMessages.JavadocView_error_noBrowser_doNotWarn;
304                 MessageDialogWithToggle dialog= MessageDialogWithToggle.openError(parent.getShell(), title, message, toggleMessage, false, null, null);
305                 if (dialog.getReturnCode() == Window.OK)
306                     store.setValue(DO_NOT_WARN_PREFERENCE_KEY, dialog.getToggleState());
307             }
308
309             fIsUsingBrowserWidget= false;
310         }
311
312         if (!fIsUsingBrowserWidget) {
313             fText= new StyledText(parent, SWT.V_SCROLL | SWT.H_SCROLL);
314             fText.setEditable(false);
315             fPresenter= new HTMLTextPresenter(false);
316
317             fText.addControlListener(new ControlAdapter() {
318                 /*
319                  * @see org.eclipse.swt.events.ControlAdapter#controlResized(org.eclipse.swt.events.ControlEvent)
320                  */

321                 public void controlResized(ControlEvent e) {
322                     setInput(fText.getText());
323                 }
324             });
325         }
326
327         initStyleSheet();
328         listenForFontChanges();
329         getViewSite().setSelectionProvider(new SelectionProvider(getControl()));
330     }
331
332     /**
333      * Registers a listener for the Java editor font.
334      *
335      * @since 3.3
336      */

337     private void listenForFontChanges() {
338         fFontListener= new IPropertyChangeListener() {
339             public void propertyChange(PropertyChangeEvent event) {
340                 if (PreferenceConstants.APPEARANCE_JAVADOC_FONT.equals(event.getProperty())) {
341                     fgStyleSheetLoaded= false;
342                     // trigger reloading, but make sure other listeners have already run, so that
343
// the style sheet gets reloaded only once.
344
final Display display= getSite().getPage().getWorkbenchWindow().getWorkbench().getDisplay();
345                     if (!display.isDisposed()) {
346                         display.asyncExec(new Runnable JavaDoc() {
347                             public void run() {
348                                 if (!display.isDisposed()) {
349                                     initStyleSheet();
350                                     refresh();
351                                 }
352                             }
353                         });
354                     }
355                 }
356             }
357         };
358         JFaceResources.getFontRegistry().addListener(fFontListener);
359     }
360     
361     private static void initStyleSheet() {
362         if (fgStyleSheetLoaded)
363             return;
364         fgStyleSheetLoaded= true;
365         fgStyleSheet= loadStyleSheet();
366     }
367     
368     private static String JavaDoc loadStyleSheet() {
369         Bundle bundle= Platform.getBundle(JavaPlugin.getPluginId());
370         URL JavaDoc styleSheetURL= bundle.getEntry("/JavadocViewStyleSheet.css"); //$NON-NLS-1$
371
if (styleSheetURL == null)
372             return null;
373
374         try {
375             styleSheetURL= FileLocator.toFileURL(styleSheetURL);
376             BufferedReader JavaDoc reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(styleSheetURL.openStream()));
377             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(200);
378             String JavaDoc line= reader.readLine();
379             while (line != null) {
380                 buffer.append(line);
381                 buffer.append('\n');
382                 line= reader.readLine();
383             }
384
385             FontData fontData= JFaceResources.getFontRegistry().getFontData(PreferenceConstants.APPEARANCE_JAVADOC_FONT)[0];
386             return HTMLPrinter.convertTopLevelFont(buffer.toString(), fontData);
387         } catch (IOException JavaDoc ex) {
388             JavaPlugin.log(ex);
389             return null;
390         }
391     }
392
393     /*
394      * @see AbstractInfoView#createActions()
395      */

396     protected void createActions() {
397         super.createActions();
398         fSelectAllAction= new SelectAllAction(getControl(), (SelectionProvider)getSelectionProvider());
399     }
400
401
402     /*
403      * @see org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView#getSelectAllAction()
404      * @since 3.0
405      */

406     protected IAction getSelectAllAction() {
407         // FIXME: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=63022
408
if (fIsUsingBrowserWidget)
409             return null;
410
411         return fSelectAllAction;
412     }
413
414     /*
415      * @see org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView#getCopyToClipboardAction()
416      * @since 3.0
417      */

418     protected IAction getCopyToClipboardAction() {
419         // FIXME: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=63022
420
if (fIsUsingBrowserWidget)
421             return null;
422
423         return super.getCopyToClipboardAction();
424     }
425
426     /*
427      * @see AbstractInfoView#setForeground(Color)
428      */

429     protected void setForeground(Color color) {
430         getControl().setForeground(color);
431     }
432
433     /*
434      * @see AbstractInfoView#setBackground(Color)
435      */

436     protected void setBackground(Color color) {
437         getControl().setBackground(color);
438         fBackgroundColorRGB= color.getRGB();
439         refresh();
440     }
441
442     /**
443      * Refreshes the view.
444      *
445      * @since 3.3
446      */

447     private void refresh() {
448         IJavaElement input= getInput();
449         if (input == null) {
450             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(""); //$NON-NLS-1$
451
HTMLPrinter.insertPageProlog(buffer, 0, fBackgroundColorRGB, fgStyleSheet);
452             setInput(buffer.toString());
453         } else {
454             setInput(computeInput(input));
455         }
456     }
457     
458     /*
459      * @see org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView#getBackgroundColorKey()
460      * @since 3.2
461      */

462     protected String JavaDoc getBackgroundColorKey() {
463         return "org.eclipse.jdt.ui.JavadocView.backgroundColor"; //$NON-NLS-1$
464
}
465
466     /*
467      * @see AbstractInfoView#internalDispose()
468      */

469     protected void internalDispose() {
470         fText= null;
471         fBrowser= null;
472         if (fFontListener != null) {
473             JFaceResources.getFontRegistry().removeListener(fFontListener);
474             fFontListener= null;
475         }
476     }
477
478     /*
479      * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
480      */

481     public void setFocus() {
482         getControl().setFocus();
483     }
484
485     /*
486      * @see AbstractInfoView#computeInput(Object)
487      */

488     protected Object JavaDoc computeInput(Object JavaDoc input) {
489         if (getControl() == null || ! (input instanceof IJavaElement))
490             return null;
491
492         IJavaElement je= (IJavaElement)input;
493         String JavaDoc javadocHtml;
494
495         switch (je.getElementType()) {
496             case IJavaElement.COMPILATION_UNIT:
497                 try {
498                     javadocHtml= getJavadocHtml(((ICompilationUnit)je).getTypes());
499                 } catch (JavaModelException ex) {
500                     javadocHtml= null;
501                 }
502                 break;
503             case IJavaElement.CLASS_FILE:
504                 javadocHtml= getJavadocHtml(new IJavaElement[] {((IClassFile)je).getType()});
505                 break;
506             default:
507                 javadocHtml= getJavadocHtml(new IJavaElement[] { je });
508         }
509         
510         if (javadocHtml == null)
511             return ""; //$NON-NLS-1$
512

513         return javadocHtml;
514     }
515
516     /*
517      * @see AbstractInfoView#setInput(Object)
518      */

519     protected void setInput(Object JavaDoc input) {
520         String JavaDoc javadocHtml= (String JavaDoc)input;
521
522         if (fIsUsingBrowserWidget) {
523             if (javadocHtml != null && javadocHtml.length() > 0) {
524                 boolean RTL= (getSite().getShell().getStyle() & SWT.RIGHT_TO_LEFT) != 0;
525                 if (RTL) {
526                     StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(javadocHtml);
527                     HTMLPrinter.insertStyles(buffer, new String JavaDoc[] { "direction:rtl" } ); //$NON-NLS-1$
528
javadocHtml= buffer.toString();
529                 }
530             }
531             fBrowser.setText(javadocHtml);
532         } else {
533             fPresentation.clear();
534             Rectangle size= fText.getClientArea();
535
536             try {
537                 javadocHtml= ((DefaultInformationControl.IInformationPresenterExtension)fPresenter).updatePresentation(getSite().getShell(), javadocHtml, fPresentation, size.width, size.height);
538             } catch (IllegalArgumentException JavaDoc ex) {
539                 // the javadoc might no longer be valid
540
return;
541             }
542             fText.setText(javadocHtml);
543             TextPresentation.applyTextPresentation(fPresentation, fText);
544         }
545     }
546     
547     /**
548      * Returns the Javadoc in HTML format.
549      *
550      * @param result the Java elements for which to get the Javadoc
551      * @return a string with the Javadoc in HTML format.
552      */

553     private String JavaDoc getJavadocHtml(IJavaElement[] result) {
554         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
555         int nResults= result.length;
556
557         if (nResults == 0)
558             return null;
559
560         if (nResults > 1) {
561
562             for (int i= 0; i < result.length; i++) {
563                 HTMLPrinter.startBulletList(buffer);
564                 IJavaElement curr= result[i];
565                 if (curr instanceof IMember)
566                     HTMLPrinter.addBullet(buffer, getInfoText((IMember) curr));
567                 HTMLPrinter.endBulletList(buffer);
568             }
569
570         } else {
571
572             IJavaElement curr= result[0];
573             if (curr instanceof IMember) {
574                 IMember member= (IMember) curr;
575 // HTMLPrinter.addSmallHeader(buffer, getInfoText(member));
576
Reader JavaDoc reader;
577                 try {
578                     reader= JavadocContentAccess.getHTMLContentReader(member, true, true);
579                     
580                     // Provide hint why there's no Javadoc
581
if (reader == null && member.isBinary()) {
582                         boolean hasAttachedJavadoc= JavaDocLocations.getJavadocBaseLocation(member) != null;
583                         IPackageFragmentRoot root= (IPackageFragmentRoot)member.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
584                         boolean hasAttachedSource= root != null && root.getSourceAttachmentPath() != null;
585                         IOpenable openable= member.getOpenable();
586                         boolean hasSource= openable.getBuffer() != null;
587                         
588                         if (!hasAttachedSource && !hasAttachedJavadoc)
589                             reader= new StringReader JavaDoc(InfoViewMessages.JavadocView_noAttachments);
590                         else if (!hasAttachedJavadoc && !hasSource)
591                             reader= new StringReader JavaDoc(InfoViewMessages.JavadocView_noAttachedJavadoc);
592                         else if (!hasAttachedSource)
593                             reader= new StringReader JavaDoc(InfoViewMessages.JavadocView_noAttachedSource);
594                         else if (!hasSource)
595                             reader= new StringReader JavaDoc(InfoViewMessages.JavadocView_noInformation);
596                     }
597                     
598                 } catch (JavaModelException ex) {
599                     reader= new StringReader JavaDoc(InfoViewMessages.JavadocView_error_gettingJavadoc);
600                     JavaPlugin.log(ex.getStatus());
601                 }
602                 if (reader != null) {
603                     HTMLPrinter.addParagraph(buffer, reader);
604                 }
605             }
606         }
607
608         boolean flushContent= true;
609         if (buffer.length() > 0 || flushContent) {
610             HTMLPrinter.insertPageProlog(buffer, 0, fBackgroundColorRGB, fgStyleSheet);
611             HTMLPrinter.addPageEpilog(buffer);
612             return buffer.toString();
613         }
614
615         return null;
616     }
617
618     /**
619      * Gets the label for the given member.
620      *
621      * @param member the Java member
622      * @return a string containing the member's label
623      */

624     private String JavaDoc getInfoText(IMember member) {
625         return JavaElementLabels.getElementLabel(member, LABEL_FLAGS);
626     }
627
628     /*
629      * @see org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView#isIgnoringNewInput(org.eclipse.jdt.core.IJavaElement, org.eclipse.jface.viewers.ISelection)
630      * @since 3.2
631      */

632     protected boolean isIgnoringNewInput(IJavaElement je, IWorkbenchPart part, ISelection selection) {
633         if (super.isIgnoringNewInput(je, part, selection)
634                 && part instanceof ITextEditor
635                 && selection instanceof ITextSelection) {
636             
637             ITextEditor editor= (ITextEditor)part;
638             IDocumentProvider docProvider= editor.getDocumentProvider();
639             if (docProvider == null)
640                 return false;
641             
642             IDocument document= docProvider.getDocument(editor.getEditorInput());
643             if (!(document instanceof IDocumentExtension3))
644                 return false;
645             
646             try {
647                 int offset= ((ITextSelection)selection).getOffset();
648                 String JavaDoc partition= ((IDocumentExtension3)document).getContentType(IJavaPartitions.JAVA_PARTITIONING, offset, false);
649                 return partition != IJavaPartitions.JAVA_DOC;
650             } catch (BadPartitioningException ex) {
651                 return false;
652             } catch (BadLocationException ex) {
653                 return false;
654             }
655
656         }
657         return false;
658     }
659
660     /*
661      * @see AbstractInfoView#findSelectedJavaElement(IWorkbenchPart)
662      */

663     protected IJavaElement findSelectedJavaElement(IWorkbenchPart part, ISelection selection) {
664         IJavaElement element;
665         try {
666             element= super.findSelectedJavaElement(part, selection);
667
668             if (element == null && part instanceof JavaEditor && selection instanceof ITextSelection) {
669
670                 JavaEditor editor= (JavaEditor)part;
671                 ITextSelection textSelection= (ITextSelection)selection;
672
673                 IDocumentProvider documentProvider= editor.getDocumentProvider();
674                 if (documentProvider == null)
675                     return null;
676
677                 IDocument document= documentProvider.getDocument(editor.getEditorInput());
678                 if (document == null)
679                     return null;
680
681                 ITypedRegion typedRegion= TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, textSelection.getOffset(), false);
682                 if (IJavaPartitions.JAVA_DOC.equals(typedRegion.getType()))
683                     return TextSelectionConverter.getElementAtOffset((JavaEditor)part, textSelection);
684                 else
685                     return null;
686             } else
687                 return element;
688         } catch (JavaModelException e) {
689             return null;
690         } catch (BadLocationException e) {
691             return null;
692         }
693     }
694
695     /*
696      * @see AbstractInfoView#getControl()
697      */

698     protected Control getControl() {
699         if (fIsUsingBrowserWidget)
700             return fBrowser;
701         else
702             return fText;
703     }
704
705     /*
706      * @see org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView#getHelpContextId()
707      * @since 3.1
708      */

709     protected String JavaDoc getHelpContextId() {
710         return IJavaHelpContextIds.JAVADOC_VIEW;
711     }
712 }
713
Popular Tags