1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
13 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.BadPartitioningException;
20 import org.eclipse.jface.text.DefaultInformationControl;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentExtension3;
23 import org.eclipse.jface.text.IInformationControl;
24 import org.eclipse.jface.text.IInformationControlCreator;
25 import org.eclipse.jface.text.IInformationControlExtension2;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITextHover;
28 import org.eclipse.jface.text.ITextHoverExtension;
29 import org.eclipse.jface.text.ITextHoverExtension2;
30 import org.eclipse.jface.text.ITextViewer;
31 import org.eclipse.jface.text.ITypedRegion;
33 import org.eclipse.ui.editors.text.EditorsUI;
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 * Provides the default text hover info if it exists, else provides the native string as hover info.
42 public class PropertiesFileHover implements ITextHover, ITextHoverExtension, ITextHoverExtension2 {
45 * The offset for which the hover request has been issued.
50 * The default text hover.
52 private final ITextHover fTextHover;
55 * The hover control creator.
57 private HoverControlCreator fHoverControlCreator;
59 public PropertiesFileHover(ITextHover textHover) {
60 fTextHover= textHover;
64 * Hover control creator.
68 private static final class HoverControlCreator extends AbstractReusableInformationControlCreator {
71 * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
74 public IInformationControl doCreateInformationControl(Shell parent) {
75 return new PropertiesFileHoverControl(parent, EditorsUI.getTooltipAffordanceString());
80 * The Properties File hover control.
84 static class PropertiesFileHoverControl extends DefaultInformationControl implements IInformationControlExtension2 {
87 * Creates an Properties File hover control with the given shell as parent.
89 * @param parent the parent shell
90 * @param tooltipAffordanceString the text to be used in the status field or
91 * <code>null</code> to hide the status field
93 public PropertiesFileHoverControl(Shell parent, String tooltipAffordanceString) {
94 super(parent, tooltipAffordanceString, null);
99 * @see org.eclipse.jface.text.IInformationControlExtension2#setInput(java.lang.Object)
101 public void setInput(Object input) {
102 setInformation((String)input);
108 * @see org.eclipse.jface.text.ITextHoverExtension2#getHoverInfo2(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
110 public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
111 return getHoverInfo(textViewer, hoverRegion);
116 * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
118 public IInformationControlCreator getHoverControlCreator() {
119 if (fHoverControlCreator == null)
120 fHoverControlCreator= new HoverControlCreator();
121 return fHoverControlCreator;
126 * @deprecated see {@link ITextHover#getHoverInfo(ITextViewer, IRegion)}
128 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
129 String hoverInfo= fTextHover.getHoverInfo(textViewer, hoverRegion);
130 if (hoverInfo != null && hoverInfo.length() > 0) {
134 String unescapedString= null;
136 ITypedRegion partition= null;
137 IDocument document= textViewer.getDocument();
138 if (document instanceof IDocumentExtension3)
139 partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, fOffset, false);
140 if (partition == null)
143 String type= partition.getType();
144 if (!(type.equals(IPropertiesFilePartitions.PROPERTY_VALUE) || type.equals(IDocument.DEFAULT_CONTENT_TYPE))) {
147 String escapedString= document.get(partition.getOffset(), partition.getLength());
148 if (type.equals(IPropertiesFilePartitions.PROPERTY_VALUE)) {
149 escapedString= escapedString.substring(1); //see PropertiesFilePartitionScanner()
153 unescapedString= PropertiesFileEscapes.unescape(escapedString);
154 } catch (CoreException e) {
155 return e.getStatus().getMessage();
157 if (escapedString.equals(unescapedString))
159 } catch (BadLocationException e) {
161 } catch (BadPartitioningException e) {
164 return unescapedString;
169 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
171 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
173 return fTextHover.getHoverRegion(textViewer, offset);