KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > operations > SourceViewerInformationControl


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  * Eugene Kuleshov <eu@md.pp.ru> - Bug 173959 add mechanism for navigating from team annotation to corresponding task
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.ui.operations;
13
14 import java.util.Collections JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.text.*;
19 import org.eclipse.jface.text.source.ISourceViewer;
20 import org.eclipse.jface.text.source.SourceViewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.StyleRange;
23 import org.eclipse.swt.custom.StyledText;
24 import org.eclipse.swt.events.*;
25 import org.eclipse.swt.graphics.*;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.*;
29 import org.eclipse.ui.editors.text.EditorsUI;
30 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
31
32 /**
33  * Source viewer based implementation of {@link org.eclipse.jface.text.IInformationControl}.
34  * Displays information in a source viewer.
35  *
36  * @since 3.0
37  *
38  * This class is copied from org.eclipse.jface.text.source.projection.SourceViewerInformationControl
39  * Several changes are made in order to handle hover for CVS annotations
40  */

41 class SourceViewerInformationControl implements IInformationControl, IInformationControlExtension, DisposeListener {
42
43     /** Border thickness in pixels. */
44     private static final int BORDER= 1;
45
46
47     /** The control's shell */
48     private Shell fShell;
49     /** The control's text widget */
50     private StyledText fText;
51     /** The control's source viewer */
52     private SourceViewer fViewer;
53     /** The optional status field. */
54     private Label fStatusField;
55     /** The separator for the optional status field. */
56     private Label fSeparator;
57     /** The font of the optional status text label.*/
58     private Font fStatusTextFont;
59     /** The maximal widget width. */
60     private int fMaxWidth;
61     /** The maximal widget height. */
62     private int fMaxHeight;
63
64
65     /**
66      * Creates a source viewer information control with the given shell as
67      * parent and the given font.
68      *
69      * @param parent the parent shell
70      * @param symbolicFontName the symbolic font name
71      */

72     public SourceViewerInformationControl(Shell parent, String JavaDoc symbolicFontName) {
73         this(parent, SWT.NO_TRIM | SWT.TOOL, SWT.NONE, symbolicFontName, null);
74     }
75
76     /**
77      * Creates a source viewer information control with the given shell as
78      * parent. The given shell styles are applied to the created shell. The
79      * given styles are applied to the created styled text widget. The text
80      * widget will be initialized with the given font. The status field will
81      * contain the given text or be hidden.
82      *
83      * @param parent the parent shell
84      * @param shellStyle the additional styles for the shell
85      * @param style the additional styles for the styled text widget
86      * @param symbolicFontName the symbolic font name
87      * @param statusFieldText the text to be used in the optional status field
88      * or <code>null</code> if the status field should be hidden
89      */

90     public SourceViewerInformationControl(Shell parent, int shellStyle, int style, String JavaDoc symbolicFontName, String JavaDoc statusFieldText) {
91         GridLayout layout;
92         GridData gd;
93
94         fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | shellStyle);
95         Display display= fShell.getDisplay();
96         fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
97
98         Composite composite= fShell;
99         layout= new GridLayout(1, false);
100         int border= ((shellStyle & SWT.NO_TRIM) == 0) ? 0 : BORDER;
101         layout.marginHeight= border;
102         layout.marginWidth= border;
103         composite.setLayout(layout);
104         gd= new GridData(GridData.FILL_HORIZONTAL);
105         composite.setLayoutData(gd);
106
107         if (statusFieldText != null) {
108             composite= new Composite(composite, SWT.NONE);
109             layout= new GridLayout(1, false);
110             layout.marginHeight= 0;
111             layout.marginWidth= 0;
112             composite.setLayout(layout);
113             gd= new GridData(GridData.FILL_BOTH);
114             composite.setLayoutData(gd);
115             composite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
116             composite.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
117         }
118
119         // Source viewer
120
fViewer= new SourceViewer(composite, null, style);
121         fViewer.setEditable(false);
122         
123         // configure hyperlink detectors
124
// fViewer.configure(new SourceViewerConfiguration());
125
fViewer.configure(new TextSourceViewerConfiguration(EditorsUI.getPreferenceStore()) {
126       protected Map JavaDoc getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
127         return Collections.singletonMap("org.eclipse.ui.DefaultTextEditor", //$NON-NLS-1$
128
null);
129 // new IAdaptable() {
130
// public Object getAdapter(Class adapter) {
131
// // return Platform.getAdapterManager().getAdapter(CVSHistoryPage.this, adapter);
132
// return null;
133
// }
134
// });
135
}
136     });
137
138         fText= fViewer.getTextWidget();
139         gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH);
140         fText.setLayoutData(gd);
141         fText.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
142         fText.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
143         fText.setFont(JFaceResources.getFont(symbolicFontName));
144
145         fText.addKeyListener(new KeyListener() {
146
147             public void keyPressed(KeyEvent e) {
148                 if (e.character == 0x1B) // ESC
149
fShell.dispose();
150             }
151
152             public void keyReleased(KeyEvent e) {}
153         });
154
155         // Status field
156
if (statusFieldText != null) {
157
158             // Horizontal separator line
159
fSeparator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT);
160             fSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
161
162             // Status field label
163
fStatusField= new Label(composite, SWT.RIGHT);
164             fStatusField.setText(statusFieldText);
165             Font font= fStatusField.getFont();
166             FontData[] fontDatas= font.getFontData();
167             for (int i= 0; i < fontDatas.length; i++)
168                 fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10);
169             fStatusTextFont= new Font(fStatusField.getDisplay(), fontDatas);
170             fStatusField.setFont(fStatusTextFont);
171             GridData gd2= new GridData(GridData.FILL_VERTICAL | GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
172             fStatusField.setLayoutData(gd2);
173
174             // Regarding the color see bug 41128
175
fStatusField.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
176
177             fStatusField.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
178         }
179
180         addDisposeListener(this);
181     }
182
183     /**
184      * @see org.eclipse.jface.text.IInformationControlExtension2#setInput(java.lang.Object)
185      * @param input the input object
186      */

187     public void setInput(Object JavaDoc input) {
188         if (input instanceof String JavaDoc)
189             setInformation((String JavaDoc)input);
190         else
191             setInformation(null);
192     }
193
194     /*
195      * @see IInformationControl#setInformation(String)
196      */

197     public void setInformation(String JavaDoc content) {
198         if (content == null) {
199             fViewer.setInput(null);
200             return;
201         }
202
203         IDocument doc= new Document(content);
204         fViewer.setInput(doc);
205         
206         // decorate text
207
StyleRange styleRange = new StyleRange();
208         styleRange.start = 0;
209         styleRange.length = content.indexOf('\n');
210         styleRange.fontStyle = SWT.BOLD;
211     fViewer.getTextWidget().setStyleRange(styleRange);
212     }
213
214     /*
215      * @see IInformationControl#setVisible(boolean)
216      */

217     public void setVisible(boolean visible) {
218             fShell.setVisible(visible);
219     }
220
221     /*
222      * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
223      */

224     public void widgetDisposed(DisposeEvent event) {
225         if (fStatusTextFont != null && !fStatusTextFont.isDisposed())
226             fStatusTextFont.dispose();
227
228         fStatusTextFont= null;
229         fShell= null;
230         fText= null;
231     }
232
233     /*
234      * @see org.eclipse.jface.text.IInformationControl#dispose()
235      */

236     public final void dispose() {
237         if (fShell != null && !fShell.isDisposed())
238             fShell.dispose();
239         else
240             widgetDisposed(null);
241     }
242
243     /*
244      * @see IInformationControl#setSize(int, int)
245      */

246     public void setSize(int width, int height) {
247
248         if (fStatusField != null) {
249             GridData gd= (GridData)fViewer.getTextWidget().getLayoutData();
250             Point statusSize= fStatusField.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
251             Point separatorSize= fSeparator.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
252             gd.heightHint= height - statusSize.y - separatorSize.y;
253         }
254         fShell.setSize(width, height);
255
256         if (fStatusField != null)
257             fShell.pack(true);
258     }
259
260     /*
261      * @see IInformationControl#setLocation(Point)
262      */

263     public void setLocation(Point location) {
264         fShell.setLocation(location);
265     }
266
267     /*
268      * @see IInformationControl#setSizeConstraints(int, int)
269      */

270     public void setSizeConstraints(int maxWidth, int maxHeight) {
271         fMaxWidth= maxWidth;
272         fMaxHeight= maxHeight;
273     }
274
275     /*
276      * @see IInformationControl#computeSizeHint()
277      */

278     public Point computeSizeHint() {
279         // compute the preferred size
280
int x= SWT.DEFAULT;
281         int y= SWT.DEFAULT;
282         Point size= fShell.computeSize(x, y);
283         if (size.x > fMaxWidth)
284             x= fMaxWidth;
285         if (size.y > fMaxHeight)
286             y= fMaxHeight;
287
288         // recompute using the constraints if the preferred size is larger than the constraints
289
if (x != SWT.DEFAULT || y != SWT.DEFAULT)
290             size= fShell.computeSize(x, y, false);
291
292         return size;
293     }
294
295     /*
296      * @see IInformationControl#addDisposeListener(DisposeListener)
297      */

298     public void addDisposeListener(DisposeListener listener) {
299         fShell.addDisposeListener(listener);
300     }
301
302     /*
303      * @see IInformationControl#removeDisposeListener(DisposeListener)
304      */

305     public void removeDisposeListener(DisposeListener listener) {
306         fShell.removeDisposeListener(listener);
307     }
308
309     /*
310      * @see IInformationControl#setForegroundColor(Color)
311      */

312     public void setForegroundColor(Color foreground) {
313         fText.setForeground(foreground);
314     }
315
316     /*
317      * @see IInformationControl#setBackgroundColor(Color)
318      */

319     public void setBackgroundColor(Color background) {
320         fText.setBackground(background);
321     }
322
323     /*
324      * @see IInformationControl#isFocusControl()
325      */

326     public boolean isFocusControl() {
327         return fText.isFocusControl();
328     }
329
330     /*
331      * @see IInformationControl#setFocus()
332      */

333     public void setFocus() {
334         fShell.forceFocus();
335         fText.setFocus();
336     }
337
338     /*
339      * @see IInformationControl#addFocusListener(FocusListener)
340      */

341     public void addFocusListener(FocusListener listener) {
342         fText.addFocusListener(listener);
343     }
344
345     /*
346      * @see IInformationControl#removeFocusListener(FocusListener)
347      */

348     public void removeFocusListener(FocusListener listener) {
349         fText.removeFocusListener(listener);
350     }
351
352     /*
353      * @see IInformationControlExtension#hasContents()
354      */

355     public boolean hasContents() {
356         return fText.getCharCount() > 0;
357     }
358 }
Popular Tags