View | Details | Raw Unified | Return to bug 236850 | Differences between
and this patch

Collapse All | Expand All

(-)ui/org/eclipse/jdt/internal/ui/infoviews/JavadocView.java (-3 / +31 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 76-81 Link Here
76
import org.eclipse.jface.text.IDocumentExtension3;
76
import org.eclipse.jface.text.IDocumentExtension3;
77
import org.eclipse.jface.text.ITextSelection;
77
import org.eclipse.jface.text.ITextSelection;
78
import org.eclipse.jface.text.ITypedRegion;
78
import org.eclipse.jface.text.ITypedRegion;
79
import org.eclipse.jface.text.Region;
79
import org.eclipse.jface.text.TextPresentation;
80
import org.eclipse.jface.text.TextPresentation;
80
import org.eclipse.jface.text.TextSelection;
81
import org.eclipse.jface.text.TextSelection;
81
import org.eclipse.jface.text.TextUtilities;
82
import org.eclipse.jface.text.TextUtilities;
Lines 103-114 Link Here
103
import org.eclipse.jdt.core.IField;
104
import org.eclipse.jdt.core.IField;
104
import org.eclipse.jdt.core.IJavaElement;
105
import org.eclipse.jdt.core.IJavaElement;
105
import org.eclipse.jdt.core.IJavaProject;
106
import org.eclipse.jdt.core.IJavaProject;
107
import org.eclipse.jdt.core.ILocalVariable;
106
import org.eclipse.jdt.core.IMember;
108
import org.eclipse.jdt.core.IMember;
107
import org.eclipse.jdt.core.IOpenable;
109
import org.eclipse.jdt.core.IOpenable;
108
import org.eclipse.jdt.core.IPackageFragmentRoot;
110
import org.eclipse.jdt.core.IPackageFragmentRoot;
111
import org.eclipse.jdt.core.ISourceRange;
109
import org.eclipse.jdt.core.ITypeRoot;
112
import org.eclipse.jdt.core.ITypeRoot;
110
import org.eclipse.jdt.core.JavaCore;
113
import org.eclipse.jdt.core.JavaCore;
111
import org.eclipse.jdt.core.JavaModelException;
114
import org.eclipse.jdt.core.JavaModelException;
115
import org.eclipse.jdt.core.SourceRange;
112
import org.eclipse.jdt.core.dom.AST;
116
import org.eclipse.jdt.core.dom.AST;
113
import org.eclipse.jdt.core.dom.ASTNode;
117
import org.eclipse.jdt.core.dom.ASTNode;
114
import org.eclipse.jdt.core.dom.ASTParser;
118
import org.eclipse.jdt.core.dom.ASTParser;
Lines 893-898 Link Here
893
	 * @see AbstractInfoView#computeInput(Object)
897
	 * @see AbstractInfoView#computeInput(Object)
894
	 */
898
	 */
895
	protected Object computeInput(Object input) {
899
	protected Object computeInput(Object input) {
900
		//TODO: never used?
896
		if (getControl() == null || ! (input instanceof IJavaElement))
901
		if (getControl() == null || ! (input instanceof IJavaElement))
897
			return null;
902
			return null;
898
903
Lines 1062-1067 Link Here
1062
				}
1067
				}
1063
1068
1064
				HTMLPrinter.addSmallHeader(buffer, getInfoText(member, constantValue, true));
1069
				HTMLPrinter.addSmallHeader(buffer, getInfoText(member, constantValue, true));
1070
1071
				try {
1072
					ISourceRange nameRange= ((IMember)curr).getNameRange();
1073
					if (SourceRange.isAvailable(nameRange)) {
1074
						ITypeRoot typeRoot= ((IMember)curr).getTypeRoot();
1075
						Region hoverRegion= new Region(nameRange.getOffset(), nameRange.getLength());
1076
						buffer.append("<br>"); //$NON-NLS-1$
1077
						JavadocHover.addAnnotations(buffer, curr, typeRoot, hoverRegion);
1078
					}
1079
				} catch (JavaModelException e) {
1080
					// no annotations this time...
1081
				}
1082
				
1065
				Reader reader;
1083
				Reader reader;
1066
				try {
1084
				try {
1067
					String content= JavadocContentAccess2.getHTMLContent(member, true);
1085
					String content= JavadocContentAccess2.getHTMLContent(member, true);
Lines 1095-1102 Link Here
1095
				if (reader != null) {
1113
				if (reader != null) {
1096
					HTMLPrinter.addParagraph(buffer, reader);
1114
					HTMLPrinter.addParagraph(buffer, reader);
1097
				}
1115
				}
1116
1098
			} else if (curr.getElementType() == IJavaElement.LOCAL_VARIABLE || curr.getElementType() == IJavaElement.TYPE_PARAMETER) {
1117
			} else if (curr.getElementType() == IJavaElement.LOCAL_VARIABLE || curr.getElementType() == IJavaElement.TYPE_PARAMETER) {
1099
				HTMLPrinter.addSmallHeader(buffer, getInfoText(curr, null, true));
1118
				HTMLPrinter.addSmallHeader(buffer, getInfoText(curr, null, true));
1119
				if (curr instanceof ILocalVariable) {
1120
					ISourceRange nameRange= ((ILocalVariable) curr).getNameRange();
1121
					ITypeRoot typeRoot= ((IMember) curr.getParent()).getTypeRoot();
1122
					Region hoverRegion= new Region(nameRange.getOffset(), nameRange.getLength());
1123
					buffer.append("<br>"); //$NON-NLS-1$
1124
					JavadocHover.addAnnotations(buffer, curr, typeRoot, hoverRegion);
1125
				}
1100
			}
1126
			}
1101
		}
1127
		}
1102
1128
Lines 1137-1143 Link Here
1137
		}
1163
		}
1138
1164
1139
		StringBuffer buf= new StringBuffer();
1165
		StringBuffer buf= new StringBuffer();
1140
		JavadocHover.addImageAndLabel(buf, imageName, 16, 16, 8, 5, label.toString(), 22, 0);
1166
		JavadocHover.addImageAndLabel(buf, imageName, 16, 16, label.toString(), 20, 2);
1141
		return buf.toString();
1167
		return buf.toString();
1142
	}
1168
	}
1143
1169
Lines 1288-1293 Link Here
1288
					return null;
1314
					return null;
1289
1315
1290
				VariableDeclarationFragment fieldDecl= ASTNodeSearchUtil.getFieldDeclarationFragmentNode(constantField, ast);
1316
				VariableDeclarationFragment fieldDecl= ASTNodeSearchUtil.getFieldDeclarationFragmentNode(constantField, ast);
1317
				if (fieldDecl == null)
1318
					return null;
1291
				Expression initializer= fieldDecl.getInitializer();
1319
				Expression initializer= fieldDecl.getInitializer();
1292
				if (initializer == null)
1320
				if (initializer == null)
1293
					return null;
1321
					return null;
Lines 1491-1497 Link Here
1491
			 * @see org.eclipse.jdt.internal.ui.viewsupport.JavaElementLinks.ILinkHandler#handleExternalLink(java.net.URL, org.eclipse.swt.widgets.Display)
1519
			 * @see org.eclipse.jdt.internal.ui.viewsupport.JavaElementLinks.ILinkHandler#handleExternalLink(java.net.URL, org.eclipse.swt.widgets.Display)
1492
			 */
1520
			 */
1493
			public boolean handleExternalLink(final URL url, Display display) {
1521
			public boolean handleExternalLink(final URL url, Display display) {
1494
				if (fCurrent == null || !url.equals(fCurrent.getInputElement())) {
1522
				if (fCurrent == null || (fCurrent.getInputElement() instanceof URL && !url.toExternalForm().equals(((URL) fCurrent.getInputElement()).toExternalForm()))) {
1495
					fCurrent= new URLBrowserInput(fCurrent, url);
1523
					fCurrent= new URLBrowserInput(fCurrent, url);
1496
1524
1497
					if (fBackAction != null) {
1525
					if (fBackAction != null) {
(-)ui/org/eclipse/jdt/internal/ui/text/java/hover/JavadocHover.java (-71 / +251 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 16-21 Link Here
16
import java.io.InputStreamReader;
16
import java.io.InputStreamReader;
17
import java.io.Reader;
17
import java.io.Reader;
18
import java.io.StringReader;
18
import java.io.StringReader;
19
import java.net.URISyntaxException;
19
import java.net.URL;
20
import java.net.URL;
20
21
21
import org.osgi.framework.Bundle;
22
import org.osgi.framework.Bundle;
Lines 25-30 Link Here
25
import org.eclipse.swt.widgets.Display;
26
import org.eclipse.swt.widgets.Display;
26
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Shell;
27
28
29
import org.eclipse.core.runtime.OperationCanceledException;
28
import org.eclipse.core.runtime.Platform;
30
import org.eclipse.core.runtime.Platform;
29
31
30
import org.eclipse.jface.action.Action;
32
import org.eclipse.jface.action.Action;
Lines 54-59 Link Here
54
56
55
import org.eclipse.ui.editors.text.EditorsUI;
57
import org.eclipse.ui.editors.text.EditorsUI;
56
58
59
import org.eclipse.jdt.core.IAnnotatable;
57
import org.eclipse.jdt.core.IField;
60
import org.eclipse.jdt.core.IField;
58
import org.eclipse.jdt.core.IJavaElement;
61
import org.eclipse.jdt.core.IJavaElement;
59
import org.eclipse.jdt.core.IJavaProject;
62
import org.eclipse.jdt.core.IJavaProject;
Lines 63-74 Link Here
63
import org.eclipse.jdt.core.ITypeRoot;
66
import org.eclipse.jdt.core.ITypeRoot;
64
import org.eclipse.jdt.core.JavaCore;
67
import org.eclipse.jdt.core.JavaCore;
65
import org.eclipse.jdt.core.JavaModelException;
68
import org.eclipse.jdt.core.JavaModelException;
69
import org.eclipse.jdt.core.dom.AST;
66
import org.eclipse.jdt.core.dom.ASTNode;
70
import org.eclipse.jdt.core.dom.ASTNode;
71
import org.eclipse.jdt.core.dom.ASTParser;
72
import org.eclipse.jdt.core.dom.CharacterLiteral;
67
import org.eclipse.jdt.core.dom.CompilationUnit;
73
import org.eclipse.jdt.core.dom.CompilationUnit;
74
import org.eclipse.jdt.core.dom.IAnnotationBinding;
68
import org.eclipse.jdt.core.dom.IBinding;
75
import org.eclipse.jdt.core.dom.IBinding;
76
import org.eclipse.jdt.core.dom.IMemberValuePairBinding;
77
import org.eclipse.jdt.core.dom.ITypeBinding;
69
import org.eclipse.jdt.core.dom.IVariableBinding;
78
import org.eclipse.jdt.core.dom.IVariableBinding;
70
import org.eclipse.jdt.core.dom.NodeFinder;
79
import org.eclipse.jdt.core.dom.NodeFinder;
71
import org.eclipse.jdt.core.dom.SimpleName;
80
import org.eclipse.jdt.core.dom.SimpleName;
81
import org.eclipse.jdt.core.dom.StringLiteral;
72
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
82
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
73
83
74
import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations;
84
import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations;
Lines 472-478 Link Here
472
			 * @see org.eclipse.jdt.internal.ui.viewsupport.JavaElementLinks.ILinkHandler#handleInlineJavadocLink(org.eclipse.jdt.core.IJavaElement)
482
			 * @see org.eclipse.jdt.internal.ui.viewsupport.JavaElementLinks.ILinkHandler#handleInlineJavadocLink(org.eclipse.jdt.core.IJavaElement)
473
			 */
483
			 */
474
			public void handleInlineJavadocLink(IJavaElement linkTarget) {
484
			public void handleInlineJavadocLink(IJavaElement linkTarget) {
475
				JavadocBrowserInformationControlInput hoverInfo= getHoverInfo(new IJavaElement[] { linkTarget }, null, (JavadocBrowserInformationControlInput) control.getInput());
485
				JavadocBrowserInformationControlInput hoverInfo= getHoverInfo(new IJavaElement[] { linkTarget }, null, null, (JavadocBrowserInformationControlInput) control.getInput());
476
				if (control.hasDelayedInputChangeListener())
486
				if (control.hasDelayedInputChangeListener())
477
					control.notifyDelayedInputChange(hoverInfo);
487
					control.notifyDelayedInputChange(hoverInfo);
478
				else
488
				else
Lines 533-560 Link Here
533
		if (elements == null || elements.length == 0)
543
		if (elements == null || elements.length == 0)
534
			return null;
544
			return null;
535
545
536
		String constantValue;
546
		return getHoverInfo(elements, getEditorInputJavaElement(), hoverRegion, null);
537
		if (elements.length == 1 && elements[0].getElementType() == IJavaElement.FIELD) {
538
			constantValue= getConstantValue((IField) elements[0], hoverRegion);
539
			if (constantValue != null)
540
				constantValue= HTMLPrinter.convertToHTMLContent(constantValue);
541
		} else {
542
			constantValue= null;
543
		}
544
545
		return getHoverInfo(elements, constantValue, null);
546
	}
547
	}
547
548
548
	/**
549
	/**
549
	 * Computes the hover info.
550
	 * Computes the hover info.
550
	 *
551
	 *
551
	 * @param elements the resolved elements
552
	 * @param elements the resolved elements
552
	 * @param constantValue a constant value iff result contains exactly 1 constant field, or <code>null</code>
553
	 * @param editorInputElement the editor input, or <code>null</code>
554
	 * @param hoverRegion the text range of the hovered word, or <code>null</code>
553
	 * @param previousInput the previous input, or <code>null</code>
555
	 * @param previousInput the previous input, or <code>null</code>
554
	 * @return the HTML hover info for the given element(s) or <code>null</code> if no information is available
556
	 * @return the HTML hover info for the given element(s) or <code>null</code> if no information is available
555
	 * @since 3.4
557
	 * @since 3.4
556
	 */
558
	 */
557
	private static JavadocBrowserInformationControlInput getHoverInfo(IJavaElement[] elements, String constantValue, JavadocBrowserInformationControlInput previousInput) {
559
	private static JavadocBrowserInformationControlInput getHoverInfo(IJavaElement[] elements, ITypeRoot editorInputElement, IRegion hoverRegion, JavadocBrowserInformationControlInput previousInput) {
558
		int nResults= elements.length;
560
		int nResults= elements.length;
559
		StringBuffer buffer= new StringBuffer();
561
		StringBuffer buffer= new StringBuffer();
560
		boolean hasContents= false;
562
		boolean hasContents= false;
Lines 569-576 Link Here
569
				HTMLPrinter.startBulletList(buffer);
571
				HTMLPrinter.startBulletList(buffer);
570
				IJavaElement curr= elements[i];
572
				IJavaElement curr= elements[i];
571
				if (curr instanceof IMember || curr.getElementType() == IJavaElement.LOCAL_VARIABLE) {
573
				if (curr instanceof IMember || curr.getElementType() == IJavaElement.LOCAL_VARIABLE) {
572
					//FIXME: provide links
574
					String label= JavaElementLabels.getElementLabel(curr, getHeaderFlags(curr));
573
					HTMLPrinter.addBullet(buffer, getInfoText(curr, constantValue, false));
575
					String link;
576
					try {
577
						String uri= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, curr);
578
						link= JavaElementLinks.createLink(uri, label);
579
					} catch (URISyntaxException e) {
580
						JavaPlugin.log(e);
581
						link= label;
582
					}
583
					HTMLPrinter.addBullet(buffer, link);
574
					hasContents= true;
584
					hasContents= true;
575
				}
585
				}
576
				HTMLPrinter.endBulletList(buffer);
586
				HTMLPrinter.endBulletList(buffer);
Lines 580-587 Link Here
580
590
581
			element= elements[0];
591
			element= elements[0];
582
			if (element instanceof IMember) {
592
			if (element instanceof IMember) {
593
				HTMLPrinter.addSmallHeader(buffer, getInfoText(element, editorInputElement, hoverRegion, true));
594
				buffer.append("<br>"); //$NON-NLS-1$
595
				addAnnotations(buffer, element, editorInputElement, hoverRegion);
583
				IMember member= (IMember) element;
596
				IMember member= (IMember) element;
584
				HTMLPrinter.addSmallHeader(buffer, getInfoText(member, constantValue, true));
585
				Reader reader;
597
				Reader reader;
586
				try {
598
				try {
587
//					reader= JavadocContentAccess.getHTMLContentReader(member, true, true);
599
//					reader= JavadocContentAccess.getHTMLContentReader(member, true, true);
Lines 620-626 Link Here
620
				hasContents= true;
632
				hasContents= true;
621
633
622
			} else if (element.getElementType() == IJavaElement.LOCAL_VARIABLE || element.getElementType() == IJavaElement.TYPE_PARAMETER) {
634
			} else if (element.getElementType() == IJavaElement.LOCAL_VARIABLE || element.getElementType() == IJavaElement.TYPE_PARAMETER) {
623
				HTMLPrinter.addSmallHeader(buffer, getInfoText(element, constantValue, true));
635
				addAnnotations(buffer, element, editorInputElement, hoverRegion);
636
				HTMLPrinter.addSmallHeader(buffer, getInfoText(element, editorInputElement, hoverRegion, true));
624
				hasContents= true;
637
				hasContents= true;
625
			}
638
			}
626
			leadingImageWidth= 20;
639
			leadingImageWidth= 20;
Lines 642-663 Link Here
642
		return null;
655
		return null;
643
	}
656
	}
644
657
645
	private static String getInfoText(IJavaElement element, String constantValue, boolean allowImage) {
658
	private static String getInfoText(IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion, boolean allowImage) {
646
		long flags;
659
		long flags= getHeaderFlags(element);
647
		switch (element.getElementType()) {
648
			case IJavaElement.LOCAL_VARIABLE:
649
				flags= LOCAL_VARIABLE_FLAGS;
650
				break;
651
			case IJavaElement.TYPE_PARAMETER:
652
				flags= TYPE_PARAMETER_FLAGS;
653
				break;
654
			default:
655
				flags= LABEL_FLAGS;
656
				break;
657
		}
658
		StringBuffer label= new StringBuffer(JavaElementLinks.getElementLabel(element, flags));
660
		StringBuffer label= new StringBuffer(JavaElementLinks.getElementLabel(element, flags));
661
		
659
		if (element.getElementType() == IJavaElement.FIELD) {
662
		if (element.getElementType() == IJavaElement.FIELD) {
663
			String constantValue= getConstantValue((IField) element, editorInputElement, hoverRegion);
660
			if (constantValue != null) {
664
			if (constantValue != null) {
665
				constantValue= HTMLPrinter.convertToHTMLContent(constantValue);
661
				IJavaProject javaProject= element.getJavaProject();
666
				IJavaProject javaProject= element.getJavaProject();
662
				if (JavaCore.INSERT.equals(javaProject.getOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR, true)))
667
				if (JavaCore.INSERT.equals(javaProject.getOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR, true)))
663
					label.append(' ');
668
					label.append(' ');
Lines 667-672 Link Here
667
				label.append(constantValue);
672
				label.append(constantValue);
668
			}
673
			}
669
		}
674
		}
675
		
676
//		if (element.getElementType() == IJavaElement.METHOD) {
677
//			IMethod method= (IMethod)element;
678
//			//TODO: add default value for annotation type members, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=249016
679
//		}
670
680
671
		String imageName= null;
681
		String imageName= null;
672
		if (allowImage) {
682
		if (allowImage) {
Lines 677-686 Link Here
677
		}
687
		}
678
688
679
		StringBuffer buf= new StringBuffer();
689
		StringBuffer buf= new StringBuffer();
680
		addImageAndLabel(buf, imageName, 16, 16, 2, 2, label.toString(), 20, 2);
690
		addImageAndLabel(buf, element, imageName, 16, 16, label.toString(), 20, 2);
681
		return buf.toString();
691
		return buf.toString();
682
	}
692
	}
683
693
694
	private static long getHeaderFlags(IJavaElement element) {
695
		switch (element.getElementType()) {
696
			case IJavaElement.LOCAL_VARIABLE:
697
				return LOCAL_VARIABLE_FLAGS;
698
			case IJavaElement.TYPE_PARAMETER:
699
				return TYPE_PARAMETER_FLAGS;
700
			default:
701
				return LABEL_FLAGS;
702
		}
703
	}
704
684
	/*
705
	/*
685
	 * @since 3.4
706
	 * @since 3.4
686
	 */
707
	 */
Lines 697-722 Link Here
697
	 * Returns the constant value for the given field.
718
	 * Returns the constant value for the given field.
698
	 *
719
	 *
699
	 * @param field the field
720
	 * @param field the field
700
	 * @param hoverRegion the hover region
721
	 * @param editorInputElement the editor input element
722
	 * @param hoverRegion the hover region in the editor
701
	 * @return the constant value for the given field or <code>null</code> if none
723
	 * @return the constant value for the given field or <code>null</code> if none
702
	 * @since 3.4
724
	 * @since 3.4
703
	 */
725
	 */
704
	private String getConstantValue(IField field, IRegion hoverRegion) {
726
	private static String getConstantValue(IField field, ITypeRoot editorInputElement, IRegion hoverRegion) {
705
		if (!isStaticFinal(field))
727
		if (!isStaticFinal(field))
706
			return null;
728
			return null;
707
729
708
		ITypeRoot typeRoot= getEditorInputJavaElement();
730
		ASTNode node= getHoveredASTNode(editorInputElement, hoverRegion);
709
		if (typeRoot == null)
731
		if (node == null)
710
			return null;
732
			return null;
711
733
		
712
		Object constantValue= null;
734
		Object constantValue= null;
713
735
		if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
714
		CompilationUnit unit= SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
715
		if (unit == null)
716
			return null;
717
718
		ASTNode node= NodeFinder.perform(unit, hoverRegion.getOffset(), hoverRegion.getLength());
719
		if (node != null && node.getNodeType() == ASTNode.SIMPLE_NAME) {
720
			IBinding binding= ((SimpleName)node).resolveBinding();
736
			IBinding binding= ((SimpleName)node).resolveBinding();
721
			if (binding != null && binding.getKind() == IBinding.VARIABLE) {
737
			if (binding != null && binding.getKind() == IBinding.VARIABLE) {
722
				IVariableBinding variableBinding= (IVariableBinding)binding;
738
				IVariableBinding variableBinding= (IVariableBinding)binding;
Lines 729-748 Link Here
729
			return null;
745
			return null;
730
746
731
		if (constantValue instanceof String) {
747
		if (constantValue instanceof String) {
732
			StringBuffer result= new StringBuffer();
748
			return getEscapedStringLiteral((String) constantValue);
733
			result.append('"');
734
			String stringConstant= (String)constantValue;
735
			if (stringConstant.length() > 80) {
736
				result.append(stringConstant.substring(0, 80));
737
				result.append(JavaElementLabels.ELLIPSIS_STRING);
738
			} else {
739
				result.append(stringConstant);
740
			}
741
			result.append('"');
742
			return result.toString();
743
749
744
		} else if (constantValue instanceof Character) {
750
		} else if (constantValue instanceof Character) {
745
			String constantResult= '\'' + constantValue.toString() + '\'';
751
			String constantResult= getEscapedCharacterLiteral(((Character) constantValue).charValue());
746
752
747
			char charValue= ((Character) constantValue).charValue();
753
			char charValue= ((Character) constantValue).charValue();
748
			String hexString= Integer.toHexString(charValue);
754
			String hexString= Integer.toHexString(charValue);
Lines 774-779 Link Here
774
		}
780
		}
775
	}
781
	}
776
782
783
	private static ASTNode getHoveredASTNode(ITypeRoot editorInputElement, IRegion hoverRegion) {
784
		if (editorInputElement == null)
785
			return null;
786
787
		CompilationUnit unit= SharedASTProvider.getAST(editorInputElement, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
788
		if (unit == null)
789
			return null;
790
		
791
		return NodeFinder.perform(unit, hoverRegion.getOffset(),	hoverRegion.getLength());
792
	}
793
794
	private static String getEscapedStringLiteral(String stringValue) {
795
		StringLiteral stringLiteral= AST.newAST(AST.JLS3).newStringLiteral();
796
		stringLiteral.setLiteralValue(stringValue);
797
		String stringConstant= stringLiteral.getEscapedValue();
798
		if (stringConstant.length() > 80) {
799
			return stringConstant.substring(0, 80) + JavaElementLabels.ELLIPSIS_STRING;
800
		} else {
801
			return stringConstant;
802
		}
803
	}
804
805
	private static String getEscapedCharacterLiteral(char ch) {
806
		CharacterLiteral characterLiteral= AST.newAST(AST.JLS3).newCharacterLiteral();
807
		characterLiteral.setCharValue(ch);
808
		return characterLiteral.getEscapedValue();
809
	}
810
	
777
	/**
811
	/**
778
	 * Creates and returns a formatted message for the given
812
	 * Creates and returns a formatted message for the given
779
	 * constant with its hex value.
813
	 * constant with its hex value.
Lines 838-872 Link Here
838
		return null;
872
		return null;
839
	}
873
	}
840
874
841
	public static void addImageAndLabel(StringBuffer buf, String imageName, int imageWidth, int imageHeight, int imageLeft, int imageTop, String label, int labelLeft, int labelTop) {
875
	public static void addImageAndLabel(StringBuffer buf, String imageSrcPath, int imageWidth, int imageHeight, String label, int labelLeft, int labelTop) {
876
		addImageAndLabel(buf, null, imageSrcPath, imageWidth, imageHeight, label, labelLeft, labelTop);
877
	}
878
	
879
	public static void addImageAndLabel(StringBuffer buf, IJavaElement element, String imageSrcPath, int imageWidth, int imageHeight, String label, int labelLeft, int labelTop) {
880
		buf.append("<div style='word-wrap: break-word; position: relative; "); //$NON-NLS-1$
881
		
882
		if (imageSrcPath != null) {
883
			buf.append("margin-left: ").append(labelLeft).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
884
			buf.append("padding-top: ").append(labelTop).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
885
		}
842
886
843
		if (imageName != null) {
887
		buf.append("'>"); //$NON-NLS-1$
844
			StringBuffer imageStyle= new StringBuffer("position: absolute; "); //$NON-NLS-1$
888
		if (imageSrcPath != null) {
889
			if (element != null) {
890
				try {
891
					String uri= JavaElementLinks.createURI(JavaElementLinks.OPEN_LINK_SCHEME, element);
892
					buf.append("<a href='").append(uri).append("'>");  //$NON-NLS-1$//$NON-NLS-2$
893
				} catch (URISyntaxException e) {
894
					element= null; // no link
895
				}
896
			}
897
			StringBuffer imageStyle= new StringBuffer("border:none; position: absolute; "); //$NON-NLS-1$
845
			imageStyle.append("width: ").append(imageWidth).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
898
			imageStyle.append("width: ").append(imageWidth).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
846
			imageStyle.append("height: ").append(imageHeight).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
899
			imageStyle.append("height: ").append(imageHeight).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
847
			imageStyle.append("top: ").append(imageTop).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
900
			imageStyle.append("left: ").append(- labelLeft - 1).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
848
			imageStyle.append("left: ").append(imageLeft).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
849
901
902
			// hack for broken transparent PNG support in IE 6, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=223900 :
850
			buf.append("<!--[if lte IE 6]><![if gte IE 5.5]>\n"); //$NON-NLS-1$
903
			buf.append("<!--[if lte IE 6]><![if gte IE 5.5]>\n"); //$NON-NLS-1$
851
			buf.append("<span style=\"").append(imageStyle).append("filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='").append(imageName).append("')\"></span>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
904
			String tooltip= JavaHoverMessages.JavadocHover_openDeclaration;
905
			buf.append("<span alt='").append(tooltip).append("' style=\"").append(imageStyle). //$NON-NLS-1$ //$NON-NLS-2$
906
					append("filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='").append(imageSrcPath).append("')\"></span>\n"); //$NON-NLS-1$ //$NON-NLS-2$
852
			buf.append("<![endif]><![endif]-->\n"); //$NON-NLS-1$
907
			buf.append("<![endif]><![endif]-->\n"); //$NON-NLS-1$
853
908
854
			buf.append("<!--[if !IE]>-->\n"); //$NON-NLS-1$
909
			buf.append("<!--[if !IE]>-->\n"); //$NON-NLS-1$
855
			buf.append("<img style='").append(imageStyle).append("' src='").append(imageName).append("'/>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
910
			buf.append("<img alt='").append(tooltip).append("' style='").append(imageStyle).append("' src='").append(imageSrcPath).append("'/>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
856
			buf.append("<!--<![endif]-->\n"); //$NON-NLS-1$
911
			buf.append("<!--<![endif]-->\n"); //$NON-NLS-1$
857
			buf.append("<!--[if gte IE 7]>\n"); //$NON-NLS-1$
912
			buf.append("<!--[if gte IE 7]>\n"); //$NON-NLS-1$
858
			buf.append("<img style='").append(imageStyle).append("' src='").append(imageName).append("'/>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
913
			buf.append("<img alt='").append(tooltip).append("' style='").append(imageStyle).append("' src='").append(imageSrcPath).append("'/>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
859
			buf.append("<![endif]-->\n"); //$NON-NLS-1$
914
			buf.append("<![endif]-->\n"); //$NON-NLS-1$
915
			if (element != null) {
916
				buf.append("</a>"); //$NON-NLS-1$
917
			}
860
		}
918
		}
861
919
		
862
		buf.append("<div style='word-wrap:break-word;"); //$NON-NLS-1$
863
		if (imageName != null) {
864
			buf.append("margin-left: ").append(labelLeft).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
865
			buf.append("margin-top: ").append(labelTop).append("px; "); //$NON-NLS-1$ //$NON-NLS-2$
866
		}
867
		buf.append("'>"); //$NON-NLS-1$
868
		buf.append(label);
920
		buf.append(label);
921
		
869
		buf.append("</div>"); //$NON-NLS-1$
922
		buf.append("</div>"); //$NON-NLS-1$
870
	}
923
	}
871
924
925
	public static void addAnnotations(StringBuffer buf, IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) {
926
		if (element instanceof IAnnotatable) {
927
			try {
928
				String annotationString= getAnnotations(element, editorInputElement, hoverRegion);
929
				if (annotationString != null) {
930
					buf.append("<div style='margin-bottom: 5px;'>"); //$NON-NLS-1$
931
					buf.append(annotationString);
932
					buf.append("</div>"); //$NON-NLS-1$
933
				}
934
			} catch (JavaModelException e) {
935
				// no annotations this time...
936
				buf.append("<br>"); //$NON-NLS-1$
937
			} catch (URISyntaxException e) {
938
				// no annotations this time...
939
				buf.append("<br>"); //$NON-NLS-1$
940
			}
941
		}
942
	}
943
944
	private static String getAnnotations(IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) throws URISyntaxException, JavaModelException {
945
		if (!(element instanceof IAnnotatable))
946
			return null;
947
		
948
		if (((IAnnotatable)element).getAnnotations().length == 0)
949
			return null;
950
		
951
		IBinding binding;
952
		ASTNode node= getHoveredASTNode(editorInputElement, hoverRegion);
953
		
954
		if (node == null) {
955
			ASTParser p= ASTParser.newParser(AST.JLS3);
956
			p.setProject(element.getJavaProject());
957
			try {
958
				binding= p.createBindings(new IJavaElement[] { element }, null)[0];
959
			} catch (OperationCanceledException e) {
960
				return null;
961
			}
962
			
963
		} else {
964
			if (node.getNodeType() != ASTNode.SIMPLE_NAME)
965
				return null;
966
			
967
			binding= ((SimpleName)node).resolveBinding();
968
		}
969
		
970
		if (binding == null)
971
			return null;
972
		
973
		IAnnotationBinding[] annotations= binding.getAnnotations();
974
		if (annotations.length == 0)
975
			return null;
976
		
977
		StringBuffer buf= new StringBuffer();
978
		for (int i= 0; i < annotations.length; i++) {
979
			//TODO: skip annotations that don't have an @Documented annotation?
980
			addAnnotation(buf, element, annotations[i]);
981
			buf.append("<br>"); //$NON-NLS-1$
982
		}
983
		
984
		return buf.toString();
985
	}
986
987
	private static void addAnnotation(StringBuffer buf, IJavaElement element, IAnnotationBinding annotation) throws URISyntaxException {
988
		String uri= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, annotation.getAnnotationType().getJavaElement());
989
		buf.append('@');
990
		addLink(buf, uri, annotation.getName());
991
		
992
		IMemberValuePairBinding[] mvPairs= annotation.getDeclaredMemberValuePairs();
993
		if (mvPairs.length > 0) {
994
			buf.append('(');
995
			for (int j= 0; j < mvPairs.length; j++) {
996
				if (j > 0) {
997
					buf.append(JavaElementLabels.COMMA_STRING);
998
				}
999
				IMemberValuePairBinding mvPair= mvPairs[j];
1000
				String memberURI= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, mvPair.getMethodBinding().getJavaElement());
1001
				addLink(buf, memberURI, mvPair.getName());
1002
				buf.append('=');
1003
				addValue(buf, element, mvPair.getValue());
1004
			}
1005
			buf.append(')');
1006
		}
1007
	}
1008
1009
	private static void addValue(StringBuffer buf, IJavaElement element, Object value) throws URISyntaxException {
1010
		if (value instanceof ITypeBinding) {
1011
			ITypeBinding typeBinding= (ITypeBinding)value;
1012
			IJavaElement type= typeBinding.getJavaElement();
1013
			String typeLabel= JavaElementLinks.getElementLabel(type, LABEL_FLAGS, true);
1014
			buf.append(typeLabel).append(".class"); //$NON-NLS-1$
1015
			
1016
		} else if (value instanceof IVariableBinding) { // only enum constants
1017
			IVariableBinding variableBinding= (IVariableBinding)value;
1018
			IJavaElement variable= variableBinding.getJavaElement();
1019
			String uri= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, variable);
1020
			String name= variable.getElementName();
1021
			addLink(buf, uri, name);
1022
				
1023
		} else if (value instanceof IAnnotationBinding) {
1024
			IAnnotationBinding annotationBinding= (IAnnotationBinding)value;
1025
			addAnnotation(buf, element, annotationBinding);
1026
			
1027
		} else if (value instanceof String) {
1028
			buf.append(getEscapedStringLiteral((String)value));
1029
			
1030
		} else if (value instanceof Character) {
1031
			buf.append(getEscapedCharacterLiteral(((Character)value).charValue()));
1032
			
1033
		} else if (value instanceof Object[]) {
1034
			Object[] values= (Object[])value;
1035
			buf.append('{');
1036
			for (int i= 0; i < values.length; i++) {
1037
				if (i > 0) {
1038
					buf.append(JavaElementLabels.COMMA_STRING);
1039
				}
1040
				addValue(buf, element, values[i]);
1041
			}
1042
			buf.append('}');
1043
			
1044
		} else { // primitive types (except char) or null
1045
			buf.append(String.valueOf(value));
1046
		}
1047
	}
1048
1049
	private static StringBuffer addLink(StringBuffer buf, String uri, String label) {
1050
		return buf.append(JavaElementLinks.createLink(uri, label));
1051
	}
872
}
1052
}
(-)ui/org/eclipse/jdt/internal/ui/viewsupport/JavaElementLinks.java (-10 / +50 lines)
Lines 24-29 Link Here
24
24
25
import org.eclipse.core.runtime.IProgressMonitor;
25
import org.eclipse.core.runtime.IProgressMonitor;
26
26
27
import org.eclipse.jdt.core.IAnnotation;
27
import org.eclipse.jdt.core.IField;
28
import org.eclipse.jdt.core.IField;
28
import org.eclipse.jdt.core.IJavaElement;
29
import org.eclipse.jdt.core.IJavaElement;
29
import org.eclipse.jdt.core.ILocalVariable;
30
import org.eclipse.jdt.core.ILocalVariable;
Lines 103-109 Link Here
103
104
104
		public String getElementName(IJavaElement element) {
105
		public String getElementName(IJavaElement element) {
105
			String elementName= element.getElementName();
106
			String elementName= element.getElementName();
106
			if (fElement.equals(element)) { // linking to the member itself would be a no-op
107
			if (element.equals(fElement)) { // linking to the member itself would be a no-op
107
				return elementName;
108
				return elementName;
108
			}
109
			}
109
			if (elementName.length() == 0) { // anonymous
110
			if (elementName.length() == 0) { // anonymous
Lines 111-127 Link Here
111
			}
112
			}
112
			try {
113
			try {
113
				String uri= createURI(JAVADOC_SCHEME, element);
114
				String uri= createURI(JAVADOC_SCHEME, element);
114
				return createLink(uri, elementName);
115
				return createHeaderLink(uri, elementName);
115
			} catch (URISyntaxException e) {
116
			} catch (URISyntaxException e) {
116
				JavaPlugin.log(e);
117
				JavaPlugin.log(e);
117
				return elementName;
118
				return elementName;
118
			}
119
			}
119
		}
120
		}
120
121
121
		private String createLink(String uri, String elementName) {
122
			return "<a class='header' href='" + uri + ("'>" + elementName + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123
		}
124
125
		protected String getGT() {
122
		protected String getGT() {
126
			return "&gt;"; //$NON-NLS-1$
123
			return "&gt;"; //$NON-NLS-1$
127
		}
124
		}
Lines 134-140 Link Here
134
			String typeName= super.getSimpleTypeName(enclosingElement, typeSig);
131
			String typeName= super.getSimpleTypeName(enclosingElement, typeSig);
135
			try {
132
			try {
136
				String uri= createURI(JAVADOC_SCHEME, enclosingElement, typeName, null, null);
133
				String uri= createURI(JAVADOC_SCHEME, enclosingElement, typeName, null, null);
137
				return createLink(uri, typeName);
134
				return createHeaderLink(uri, typeName);
138
			} catch (URISyntaxException e) {
135
			} catch (URISyntaxException e) {
139
				JavaPlugin.log(e);
136
				JavaPlugin.log(e);
140
				return typeName;
137
				return typeName;
Lines 319-325 Link Here
319
					JavaPlugin.log(e);
316
					JavaPlugin.log(e);
320
				}
317
				}
321
			}
318
			}
322
319
			if (element instanceof IAnnotation) {
320
				element= element.getParent();
321
			}
322
			
323
			if (element instanceof ILocalVariable) {
323
			if (element instanceof ILocalVariable) {
324
				element= element.getAncestor(IJavaElement.TYPE);
324
				element= element.getAncestor(IJavaElement.TYPE);
325
			} else if (element instanceof ITypeParameter) {
325
			} else if (element instanceof ITypeParameter) {
Lines 459-466 Link Here
459
	}
459
	}
460
460
461
	/**
461
	/**
462
	 * Creates a link with the given URI and label text.
463
	 * 
464
	 * @param uri the URI
465
	 * @param label the label
466
	 * @return the HTML link
467
	 * @since 3.6
468
	 */
469
	public static String createLink(String uri, String label) {
470
		return "<a href='" + uri + "'>" + label + "</a>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
471
	}
472
	
473
	/**
474
	 * Creates a header link with the given URI and label text.
475
	 * 
476
	 * @param uri the URI
477
	 * @param label the label
478
	 * @return the HTML link
479
	 * @since 3.6
480
	 */
481
	public static String createHeaderLink(String uri, String label) {
482
		return "<a class='header' href='" + uri + "'>" + label + "</a>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
483
	}
484
	
485
	/**
462
	 * Returns the label for a Java element with the flags as defined by {@link JavaElementLabels}.
486
	 * Returns the label for a Java element with the flags as defined by {@link JavaElementLabels}.
463
	 * Referenced element names in the label (except the given element's name) are rendered as Javadoc links.
487
	 * Referenced element names in the label (except the given element's name) are rendered as 
488
	 * header links.
464
	 *
489
	 *
465
	 * @param element the element to render
490
	 * @param element the element to render
466
	 * @param flags the rendering flags
491
	 * @param flags the rendering flags
Lines 468-477 Link Here
468
	 * @since 3.5
493
	 * @since 3.5
469
	 */
494
	 */
470
	public static String getElementLabel(IJavaElement element, long flags) {
495
	public static String getElementLabel(IJavaElement element, long flags) {
496
		return getElementLabel(element, flags, false);
497
	}
498
499
	/**
500
	 * Returns the label for a Java element with the flags as defined by {@link JavaElementLabels}.
501
	 * Referenced element names in the label are rendered as header links.
502
	 * If <code>linkAllNames</code> is <code>false</code>, don't link the name of the given element
503
	 *
504
	 * @param element the element to render
505
	 * @param flags the rendering flags
506
	 * @param linkAllNames if <code>true</code>, link all names; if <code>false</code>, link all names except original element's name
507
	 * @return the label of the Java element
508
	 * @since 3.6
509
	 */
510
	public static String getElementLabel(IJavaElement element, long flags, boolean linkAllNames) {
471
		StringBuffer buf= new StringBuffer();
511
		StringBuffer buf= new StringBuffer();
472
512
473
		if (!Strings.USE_TEXT_PROCESSOR) {
513
		if (!Strings.USE_TEXT_PROCESSOR) {
474
			new JavaElementLinkedLabelComposer(element, buf).appendElementLabel(element, flags);
514
			new JavaElementLinkedLabelComposer(linkAllNames ? null : element, buf).appendElementLabel(element, flags);
475
			return Strings.markJavaElementLabelLTR(buf.toString());
515
			return Strings.markJavaElementLabelLTR(buf.toString());
476
		} else {
516
		} else {
477
			String label= JavaElementLabels.getElementLabel(element, flags);
517
			String label= JavaElementLabels.getElementLabel(element, flags);

Return to bug 236850