KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > JavaChangeHover


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.text;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.widgets.Shell;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IInformationControl;
22 import org.eclipse.jface.text.IInformationControlCreator;
23 import org.eclipse.jface.text.ITypedRegion;
24 import org.eclipse.jface.text.TextUtilities;
25 import org.eclipse.jface.text.source.ISourceViewer;
26 import org.eclipse.jface.text.source.LineChangeHover;
27
28 import org.eclipse.ui.editors.text.EditorsUI;
29
30 /**
31  * A line change hover for Java source code. Adds a custom information control creator returning a
32  * source viewer with syntax coloring.
33  *
34  * @since 3.0
35  */

36 public class JavaChangeHover extends LineChangeHover {
37
38     /** The last computed partition type. */
39     private String JavaDoc fPartition;
40     /** The last created information control. */
41     private ChangeHoverInformationControl fInformationControl;
42     /** The document partitioning to be used by this hover. */
43     private String JavaDoc fPartitioning;
44     /** The last created information control. */
45     private int fLastScrollIndex= 0;
46     
47     /**
48      * The orientation to be used by this hover.
49      * Allowed values are: SWT#RIGHT_TO_LEFT or SWT#LEFT_TO_RIGHT
50      * @since 3.2
51      */

52     private int fOrientation;
53
54     /**
55      * Creates a new change hover for the given document partitioning.
56      *
57      * @param partitioning the document partitioning
58      * @param orientation the orientation, allowed values are: SWT#RIGHT_TO_LEFT or SWT#LEFT_TO_RIGHT
59      */

60     public JavaChangeHover(String JavaDoc partitioning, int orientation) {
61         Assert.isLegal(orientation == SWT.RIGHT_TO_LEFT || orientation == SWT.LEFT_TO_RIGHT);
62         fPartitioning= partitioning;
63         fOrientation= orientation;
64     }
65
66     /*
67      * @see org.eclipse.ui.internal.editors.text.LineChangeHover#formatSource(java.lang.String)
68      */

69     protected String JavaDoc formatSource(String JavaDoc content) {
70         return content;
71     }
72
73     /*
74      * @see org.eclipse.jface.text.source.IAnnotationHoverExtension#getHoverControlCreator()
75      */

76     public IInformationControlCreator getHoverControlCreator() {
77         return new IInformationControlCreator() {
78             public IInformationControl createInformationControl(Shell parent) {
79                 int shellStyle= SWT.TOOL | SWT.NO_TRIM | fOrientation;
80                 fInformationControl= new ChangeHoverInformationControl(parent, shellStyle, SWT.NONE, fPartition, EditorsUI.getTooltipAffordanceString());
81                 fInformationControl.setHorizontalScrollPixel(fLastScrollIndex);
82                 return fInformationControl;
83             }
84         };
85     }
86
87     /*
88      * @see org.eclipse.jface.text.information.IInformationProviderExtension2#getInformationPresenterControlCreator()
89      * @since 3.2
90      */

91     public IInformationControlCreator getInformationPresenterControlCreator() {
92         return new IInformationControlCreator() {
93             public IInformationControl createInformationControl(Shell parent) {
94                 int shellStyle= SWT.RESIZE | SWT.TOOL | fOrientation;
95                 int style= SWT.V_SCROLL | SWT.H_SCROLL;
96                 fInformationControl= new ChangeHoverInformationControl(parent, shellStyle, style, fPartition, null);
97                 fInformationControl.setHorizontalScrollPixel(fLastScrollIndex);
98                 return fInformationControl;
99             }
100         };
101     }
102     
103     /*
104      * @see org.eclipse.jface.text.source.LineChangeHover#computeLineRange(org.eclipse.jface.text.source.ISourceViewer, int, int, int)
105      */

106     protected Point computeLineRange(ISourceViewer viewer, int line, int first, int number) {
107         Point lineRange= super.computeLineRange(viewer, line, first, number);
108         if (lineRange != null) {
109             fPartition= getPartition(viewer, lineRange.x);
110         } else {
111             fPartition= IDocument.DEFAULT_CONTENT_TYPE;
112         }
113         fLastScrollIndex= viewer.getTextWidget().getHorizontalPixel();
114         if (fInformationControl != null) {
115             fInformationControl.setStartingPartitionType(fPartition);
116             fInformationControl.setHorizontalScrollPixel(fLastScrollIndex);
117         }
118         return lineRange;
119     }
120
121     /**
122      * Returns the partition type of the document displayed in <code>viewer</code> at <code>startLine</code>.
123
124      * @param viewer the viewer
125      * @param startLine the line in the viewer
126      * @return the partition type at the start of <code>startLine</code>, or <code>IDocument.DEFAULT_CONTENT_TYPE</code> if none can be detected
127      */

128     private String JavaDoc getPartition(ISourceViewer viewer, int startLine) {
129         if (viewer == null)
130             return null;
131         IDocument doc= viewer.getDocument();
132         if (doc == null)
133             return null;
134         if (startLine <= 0)
135             return IDocument.DEFAULT_CONTENT_TYPE;
136         try {
137             ITypedRegion region= TextUtilities.getPartition(doc, fPartitioning, doc.getLineOffset(startLine) - 1, true);
138             return region.getType();
139         } catch (BadLocationException e) {
140         }
141         return IDocument.DEFAULT_CONTENT_TYPE;
142     }
143
144
145     /*
146      * @see org.eclipse.jface.text.source.LineChangeHover#getTabReplacement()
147      */

148     protected String JavaDoc getTabReplacement() {
149         return Character.toString('\t');
150     }
151 }
152
Popular Tags