jeffliu | ec1c478 | 2006-05-24 14:16:24 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2005, 2006 IBM Corporation and others. |
jeffliu | ec1c478 | 2006-05-24 14:16:24 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 5 | * which accompanies this distribution, and is available at |
jeffliu | ec1c478 | 2006-05-24 14:16:24 +0000 | [diff] [blame] | 6 | * http://www.eclipse.org/legal/epl-v10.html |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 8 | * Contributors: |
jeffliu | ec1c478 | 2006-05-24 14:16:24 +0000 | [diff] [blame] | 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
david_williams | f3680f0 | 2005-04-13 22:43:54 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.sse.ui.internal.taginfo; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.Iterator; |
| 17 | import java.util.List; |
| 18 | |
amywu | 0f87537 | 2006-01-16 17:31:34 +0000 | [diff] [blame] | 19 | import org.eclipse.jface.text.IInformationControlCreator; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 20 | import org.eclipse.jface.text.IRegion; |
| 21 | import org.eclipse.jface.text.ITextHover; |
amywu | 0f87537 | 2006-01-16 17:31:34 +0000 | [diff] [blame] | 22 | import org.eclipse.jface.text.ITextHoverExtension; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 23 | import org.eclipse.jface.text.ITextViewer; |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 24 | import org.eclipse.wst.sse.ui.internal.Logger; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 25 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 26 | /** |
| 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 | */ |
amywu | 0f87537 | 2006-01-16 17:31:34 +0000 | [diff] [blame] | 31 | public class BestMatchHover implements ITextHover, ITextHoverExtension { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 32 | private ITextHover fBestMatchHover; // current best match text hover |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 33 | private ITextHover fTagInfoHover; // documentation/information hover |
| 34 | private List fTextHovers; // list of text hovers to consider in best |
amywu | 0f87537 | 2006-01-16 17:31:34 +0000 | [diff] [blame] | 35 | // match |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 36 | |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 37 | public BestMatchHover(ITextHover infotaghover) { |
| 38 | fTagInfoHover = infotaghover; |
| 39 | } |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 40 | |
| 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 | */ |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 47 | private List createTextHoversList() { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 48 | 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()); |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 56 | if (fTagInfoHover != null) { |
| 57 | hoverList.add(fTagInfoHover); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 58 | } |
| 59 | hoverList.add(new AnnotationHoverProcessor()); |
| 60 | return hoverList; |
| 61 | } |
| 62 | |
amywu | 0f87537 | 2006-01-16 17:31:34 +0000 | [diff] [blame] | 63 | 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_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 72 | /* |
| 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 | |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 123 | private List getTextHovers() { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 124 | if (fTextHovers == null) { |
| 125 | fTextHovers = createTextHoversList(); |
| 126 | } |
| 127 | return fTextHovers; |
| 128 | } |
| 129 | } |