blob: abcbc8d9b4cf59d617f890c24425c2f30357c80d [file] [log] [blame]
jeffliuec1c4782006-05-24 14:16:24 +00001/*******************************************************************************
amywuecebb042007-04-10 20:07:35 +00002 * Copyright (c) 2005, 2006 IBM Corporation and others.
jeffliuec1c4782006-05-24 14:16:24 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
david_williamscfdb2cd2004-11-11 08:37:49 +00005 * which accompanies this distribution, and is available at
jeffliuec1c4782006-05-24 14:16:24 +00006 * http://www.eclipse.org/legal/epl-v10.html
amywuecebb042007-04-10 20:07:35 +00007 *
david_williamscfdb2cd2004-11-11 08:37:49 +00008 * Contributors:
jeffliuec1c4782006-05-24 14:16:24 +00009 * IBM Corporation - initial API and implementation
10 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
david_williamsf3680f02005-04-13 22:43:54 +000013package org.eclipse.wst.sse.ui.internal.taginfo;
david_williamscfdb2cd2004-11-11 08:37:49 +000014
15import java.util.ArrayList;
16import java.util.Iterator;
17import java.util.List;
18
amywu0f875372006-01-16 17:31:34 +000019import org.eclipse.jface.text.IInformationControlCreator;
david_williamscfdb2cd2004-11-11 08:37:49 +000020import org.eclipse.jface.text.IRegion;
21import org.eclipse.jface.text.ITextHover;
amywu0f875372006-01-16 17:31:34 +000022import org.eclipse.jface.text.ITextHoverExtension;
david_williamscfdb2cd2004-11-11 08:37:49 +000023import org.eclipse.jface.text.ITextViewer;
david_williams425ffe72004-12-07 21:46:39 +000024import org.eclipse.wst.sse.ui.internal.Logger;
david_williamscfdb2cd2004-11-11 08:37:49 +000025
david_williamscfdb2cd2004-11-11 08:37:49 +000026/**
27 * Provides the best hover help documentation (by using other hover help
28 * processors) Priority of hover help processors is: ProblemHoverProcessor,
29 * TagInfoProcessor, AnnotationHoverProcessor
30 */
amywu0f875372006-01-16 17:31:34 +000031public class BestMatchHover implements ITextHover, ITextHoverExtension {
david_williamscfdb2cd2004-11-11 08:37:49 +000032 private ITextHover fBestMatchHover; // current best match text hover
nitindf8e77632005-09-07 23:49:25 +000033 private ITextHover fTagInfoHover; // documentation/information hover
34 private List fTextHovers; // list of text hovers to consider in best
amywu0f875372006-01-16 17:31:34 +000035 // match
david_williamscfdb2cd2004-11-11 08:37:49 +000036
nitindf8e77632005-09-07 23:49:25 +000037 public BestMatchHover(ITextHover infotaghover) {
38 fTagInfoHover = infotaghover;
39 }
david_williamscfdb2cd2004-11-11 08:37:49 +000040
41 /**
42 * Create a list of text hovers applicable to this best match hover
43 * processor
44 *
45 * @return List of ITextHover - in abstract class this is empty list
46 */
nitindf8e77632005-09-07 23:49:25 +000047 private List createTextHoversList() {
david_williamscfdb2cd2004-11-11 08:37:49 +000048 List hoverList = new ArrayList();
49 // if currently debugging, then add the debug hover to the list of
50 // best match
51 if (Logger.isTracing(DebugInfoHoverProcessor.TRACEFILTER)) {
52 hoverList.add(new DebugInfoHoverProcessor());
53 }
54
55 hoverList.add(new ProblemAnnotationHoverProcessor());
nitindf8e77632005-09-07 23:49:25 +000056 if (fTagInfoHover != null) {
57 hoverList.add(fTagInfoHover);
david_williamscfdb2cd2004-11-11 08:37:49 +000058 }
59 hoverList.add(new AnnotationHoverProcessor());
60 return hoverList;
61 }
62
amywu0f875372006-01-16 17:31:34 +000063 public IInformationControlCreator getHoverControlCreator() {
64 IInformationControlCreator creator = null;
65
66 if (fBestMatchHover instanceof ITextHoverExtension) {
67 creator = ((ITextHoverExtension) fBestMatchHover).getHoverControlCreator();
68 }
69 return creator;
70 }
71
david_williamscfdb2cd2004-11-11 08:37:49 +000072 /*
73 * (non-Javadoc)
74 *
75 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
76 * org.eclipse.jface.text.IRegion)
77 */
78 public String getHoverInfo(ITextViewer viewer, IRegion hoverRegion) {
79 String displayText = null;
80
81 // already have a best match hover picked out from getHoverRegion call
82 if (fBestMatchHover != null) {
83 displayText = fBestMatchHover.getHoverInfo(viewer, hoverRegion);
84 }
85 // either had no best match hover or best match hover returned null
86 if (displayText == null) {
87 // go through list of text hovers and return first display string
88 Iterator i = getTextHovers().iterator();
89 while ((i.hasNext()) && (displayText == null)) {
90 ITextHover hover = (ITextHover) i.next();
91 displayText = hover.getHoverInfo(viewer, hoverRegion);
92 }
93 }
94 return displayText;
95 }
96
97 /*
98 * (non-Javadoc)
99 *
100 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
101 * int)
102 */
103 public IRegion getHoverRegion(ITextViewer viewer, int offset) {
104 IRegion hoverRegion = null;
105
106 // go through list of text hovers and return first hover region
107 ITextHover hover = null;
108 Iterator i = getTextHovers().iterator();
109 while ((i.hasNext()) && (hoverRegion == null)) {
110 hover = (ITextHover) i.next();
111 hoverRegion = hover.getHoverRegion(viewer, offset);
112 }
113
114 // store the text hover processor that found region
115 if (hoverRegion != null)
116 fBestMatchHover = hover;
117 else
118 fBestMatchHover = null;
119
120 return hoverRegion;
121 }
122
nitindf8e77632005-09-07 23:49:25 +0000123 private List getTextHovers() {
david_williamscfdb2cd2004-11-11 08:37:49 +0000124 if (fTextHovers == null) {
125 fTextHovers = createTextHoversList();
126 }
127 return fTextHovers;
128 }
129}