]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/propertiesfileeditor/PropertiesFileHover.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / propertiesfileeditor / PropertiesFileHover.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
12
13 import org.eclipse.swt.widgets.Shell;
14
15 import org.eclipse.core.runtime.CoreException;
16
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;
32
33 import org.eclipse.ui.editors.text.EditorsUI;
34
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
36
37 /**
38  * Provides the default text hover info if it exists, else provides the native string as hover info.
39  * 
40  * @since 3.7
41  */
42 public class PropertiesFileHover implements ITextHover, ITextHoverExtension, ITextHoverExtension2 {
43
44         /**
45          * The offset for which the hover request has been issued.
46          */
47         private int fOffset;
48
49         /**
50          * The default text hover.
51          */
52         private final ITextHover fTextHover;
53
54         /**
55          * The hover control creator.
56          */
57         private HoverControlCreator fHoverControlCreator;
58
59         public PropertiesFileHover(ITextHover textHover) {
60                 fTextHover= textHover;
61         }
62
63         /**
64          * Hover control creator.
65          * 
66          * @since 3.7
67          */
68         private static final class HoverControlCreator extends AbstractReusableInformationControlCreator {
69
70                 /*
71                  * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
72                  */
73                 @Override
74                 public IInformationControl doCreateInformationControl(Shell parent) {
75                         return new PropertiesFileHoverControl(parent, EditorsUI.getTooltipAffordanceString());
76                 }
77         }
78
79         /**
80          * The Properties File hover control.
81          * 
82          * @since 3.7
83          */
84         static class PropertiesFileHoverControl extends DefaultInformationControl implements IInformationControlExtension2 {
85
86                 /**
87                  * Creates an Properties File hover control with the given shell as parent.
88                  * 
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
92                  */
93                 public PropertiesFileHoverControl(Shell parent, String tooltipAffordanceString) {
94                         super(parent, tooltipAffordanceString, null);
95                 }
96
97                 /*
98                  * (non-Javadoc)
99                  * @see org.eclipse.jface.text.IInformationControlExtension2#setInput(java.lang.Object)
100                  */
101                 public void setInput(Object input) {
102                         setInformation((String)input);
103                 }
104         }
105
106         /*
107          * (non-Javadoc)
108          * @see org.eclipse.jface.text.ITextHoverExtension2#getHoverInfo2(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
109          */
110         public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
111                 return getHoverInfo(textViewer, hoverRegion);
112         }
113
114         /*
115          * (non-Javadoc)
116          * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
117          */
118         public IInformationControlCreator getHoverControlCreator() {
119                 if (fHoverControlCreator == null)
120                         fHoverControlCreator= new HoverControlCreator();
121                 return fHoverControlCreator;
122         }
123
124         /**
125          * {@inheritDoc}
126          * @deprecated see {@link ITextHover#getHoverInfo(ITextViewer, IRegion)}
127          */
128         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
129                 String hoverInfo= fTextHover.getHoverInfo(textViewer, hoverRegion);
130                 if (hoverInfo != null && hoverInfo.length() > 0) {
131                         return hoverInfo;
132                 }
133
134                 String unescapedString= null;
135                 try {
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)
141                                 return null;
142
143                         String type= partition.getType();
144                         if (!(type.equals(IPropertiesFilePartitions.PROPERTY_VALUE) || type.equals(IDocument.DEFAULT_CONTENT_TYPE))) {
145                                 return null;
146                         }
147                         String escapedString= document.get(partition.getOffset(), partition.getLength());
148                         if (type.equals(IPropertiesFilePartitions.PROPERTY_VALUE)) {
149                                 escapedString= escapedString.substring(1); //see PropertiesFilePartitionScanner()
150                         }
151
152                         try {
153                                 unescapedString= PropertiesFileEscapes.unescape(escapedString);
154                         } catch (CoreException e) {
155                                 return e.getStatus().getMessage();
156                         }
157                         if (escapedString.equals(unescapedString))
158                                 return null;
159                 } catch (BadLocationException e) {
160                         JavaPlugin.log(e);
161                 } catch (BadPartitioningException e) {
162                         JavaPlugin.log(e);
163                 }
164                 return unescapedString;
165         }
166
167         /*
168          * (non-Javadoc)
169          * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
170          */
171         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
172                 fOffset= offset;
173                 return fTextHover.getHoverRegion(textViewer, offset);
174         }
175 }