KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > hover > 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.java.hover;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.custom.StyledText;
15 import org.eclipse.swt.events.DisposeEvent;
16 import org.eclipse.swt.events.DisposeListener;
17 import org.eclipse.swt.events.FocusListener;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.KeyListener;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.FontData;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.RGB;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31
32 import org.eclipse.jface.preference.IPreferenceStore;
33 import org.eclipse.jface.preference.PreferenceConverter;
34 import org.eclipse.jface.resource.JFaceResources;
35
36 import org.eclipse.jface.text.Document;
37 import org.eclipse.jface.text.IDocument;
38 import org.eclipse.jface.text.IInformationControl;
39 import org.eclipse.jface.text.IInformationControlExtension;
40 import org.eclipse.jface.text.source.ISourceViewer;
41 import org.eclipse.jface.text.source.SourceViewer;
42
43 import org.eclipse.jdt.ui.PreferenceConstants;
44 import org.eclipse.jdt.ui.text.IJavaPartitions;
45
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
48 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
49
50 /**
51  * Source viewer based implementation of <code>IInformationControl</code>.
52  * Displays information in a source viewer.
53  *
54  * @since 3.0
55  */

56 public class SourceViewerInformationControl implements IInformationControl, IInformationControlExtension, DisposeListener {
57
58     /** Border thickness in pixels. */
59     private static final int BORDER= 1;
60     /** The control's shell */
61     private Shell fShell;
62     /** The control's text widget */
63     private StyledText fText;
64     /** The control's source viewer */
65     private SourceViewer fViewer;
66     /**
67      * The optional status field.
68      *
69      * @since 3.0
70      */

71     private Label fStatusField;
72     /**
73      * The separator for the optional status field.
74      *
75      * @since 3.0
76      */

77     private Label fSeparator;
78     /**
79      * The font of the optional status text label.
80      *
81      * @since 3.0
82      */

83     private Font fStatusTextFont;
84     /**
85      * The width size constraint.
86      * @since 3.2
87      */

88     private int fMaxWidth= SWT.DEFAULT;
89     /**
90      * The height size constraint.
91      * @since 3.2
92      */

93     private int fMaxHeight= SWT.DEFAULT;
94     
95     private Color fBackgroundColor;
96     private boolean fIsSystemBackgroundColor= true;
97
98
99     /**
100      * Creates a default information control with the given shell as parent. The given
101      * information presenter is used to process the information to be displayed. The given
102      * styles are applied to the created styled text widget.
103      *
104      * @param parent the parent shell
105      * @param shellStyle the additional styles for the shell
106      * @param style the additional styles for the styled text widget
107      */

108     public SourceViewerInformationControl(Shell parent, int shellStyle, int style) {
109         this(parent, shellStyle, style, null);
110     }
111
112     /**
113      * Creates a default information control with the given shell as parent. The given
114      * information presenter is used to process the information to be displayed. The given
115      * styles are applied to the created styled text widget.
116      *
117      * @param parent the parent shell
118      * @param shellStyle the additional styles for the shell
119      * @param style the additional styles for the styled text widget
120      * @param statusFieldText the text to be used in the optional status field
121      * or <code>null</code> if the status field should be hidden
122      * @since 3.0
123      */

124     public SourceViewerInformationControl(Shell parent, int shellStyle, int style, String JavaDoc statusFieldText) {
125         GridLayout layout;
126         GridData gd;
127
128         fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | shellStyle);
129         Display display= fShell.getDisplay();
130         fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
131         
132         initializeColors();
133         
134         Composite composite= fShell;
135         layout= new GridLayout(1, false);
136         int border= ((shellStyle & SWT.NO_TRIM) == 0) ? 0 : BORDER;
137         layout.marginHeight= border;
138         layout.marginWidth= border;
139         composite.setLayout(layout);
140         gd= new GridData(GridData.FILL_HORIZONTAL);
141         composite.setLayoutData(gd);
142
143         if (statusFieldText != null) {
144             composite= new Composite(composite, SWT.NONE);
145             layout= new GridLayout(1, false);
146             layout.marginHeight= 0;
147             layout.marginWidth= 0;
148             composite.setLayout(layout);
149             gd= new GridData(GridData.FILL_BOTH);
150             composite.setLayoutData(gd);
151             composite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
152             composite.setBackground(fBackgroundColor);
153         }
154
155         // Source viewer
156
IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
157         fViewer= new JavaSourceViewer(composite, null, null, false, style, store);
158         fViewer.configure(new SimpleJavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, IJavaPartitions.JAVA_PARTITIONING, false));
159         fViewer.setEditable(false);
160
161         fText= fViewer.getTextWidget();
162         gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH);
163         fText.setLayoutData(gd);
164         fText.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
165         fText.setBackground(fBackgroundColor);
166         
167         initializeFont();
168
169         fText.addKeyListener(new KeyListener() {
170
171             public void keyPressed(KeyEvent e) {
172                 if (e.character == 0x1B) // ESC
173
fShell.dispose();
174             }
175
176             public void keyReleased(KeyEvent e) {}
177         });
178
179         // Status field
180
if (statusFieldText != null) {
181
182             // Horizontal separator line
183
fSeparator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT);
184             fSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
185
186             // Status field label
187
fStatusField= new Label(composite, SWT.RIGHT);
188             fStatusField.setText(statusFieldText);
189             Font font= fStatusField.getFont();
190             FontData[] fontDatas= font.getFontData();
191             for (int i= 0; i < fontDatas.length; i++)
192                 fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10);
193             fStatusTextFont= new Font(fStatusField.getDisplay(), fontDatas);
194             fStatusField.setFont(fStatusTextFont);
195             GridData gd2= new GridData(GridData.FILL_VERTICAL | GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
196             fStatusField.setLayoutData(gd2);
197
198             // Regarding the color see bug 41128
199
fStatusField.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
200             fStatusField.setBackground(fBackgroundColor);
201         }
202
203         addDisposeListener(this);
204     }
205
206     private void initializeColors() {
207         RGB bgRGB= getHoverBackgroundColorRGB();
208         if (bgRGB != null) {
209             fBackgroundColor= new Color(fShell.getDisplay(), bgRGB);
210             fIsSystemBackgroundColor= false;
211         } else {
212             fBackgroundColor= fShell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
213             fIsSystemBackgroundColor= true;
214         }
215     }
216     
217     private RGB getHoverBackgroundColorRGB() {
218         IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
219         return store.getBoolean(PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR_SYSTEM_DEFAULT)
220             ? null
221             : PreferenceConverter.getColor(store, PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR);
222     }
223
224     /**
225      * Creates a default information control with the given shell as parent. The given
226      * information presenter is used to process the information to be displayed. The given
227      * styles are applied to the created styled text widget.
228      *
229      * @param parent the parent shell
230      * @param style the additional styles for the styled text widget
231      */

232     public SourceViewerInformationControl(Shell parent,int style) {
233         this(parent, SWT.NO_TRIM | SWT.TOOL, style);
234     }
235
236     /**
237      * Creates a default information control with the given shell as parent. The given
238      * information presenter is used to process the information to be displayed. The given
239      * styles are applied to the created styled text widget.
240      *
241      * @param parent the parent shell
242      * @param style the additional styles for the styled text widget
243      * @param statusFieldText the text to be used in the optional status field
244      * or <code>null</code> if the status field should be hidden
245      * @since 3.0
246      */

247     public SourceViewerInformationControl(Shell parent,int style, String JavaDoc statusFieldText) {
248         this(parent, SWT.NO_TRIM | SWT.TOOL, style, statusFieldText);
249     }
250
251     /**
252      * Creates a default information control with the given shell as parent.
253      * No information presenter is used to process the information
254      * to be displayed. No additional styles are applied to the styled text widget.
255      *
256      * @param parent the parent shell
257      */

258     public SourceViewerInformationControl(Shell parent) {
259         this(parent, SWT.NONE);
260     }
261
262     /**
263      * Creates a default information control with the given shell as parent.
264      * No information presenter is used to process the information
265      * to be displayed. No additional styles are applied to the styled text widget.
266      *
267      * @param parent the parent shell
268      * @param statusFieldText the text to be used in the optional status field
269      * or <code>null</code> if the status field should be hidden
270      * @since 3.0
271      */

272     public SourceViewerInformationControl(Shell parent, String JavaDoc statusFieldText) {
273         this(parent, SWT.NONE, statusFieldText);
274     }
275
276     /**
277      * Initialize the font to the Java editor font.
278      *
279      * @since 3.2
280      */

281     private void initializeFont() {
282         Font font= JFaceResources.getFont("org.eclipse.jdt.ui.editors.textfont"); //$NON-NLS-1$
283
StyledText styledText= getViewer().getTextWidget();
284         styledText.setFont(font);
285     }
286
287     /*
288      * @see org.eclipse.jface.text.IInformationControlExtension2#setInput(java.lang.Object)
289      */

290     public void setInput(Object JavaDoc input) {
291         if (input instanceof String JavaDoc)
292             setInformation((String JavaDoc)input);
293         else
294             setInformation(null);
295     }
296
297     /*
298      * @see IInformationControl#setInformation(String)
299      */

300     public void setInformation(String JavaDoc content) {
301         if (content == null) {
302             fViewer.setInput(null);
303             return;
304         }
305
306         IDocument doc= new Document(content);
307         JavaPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(doc, IJavaPartitions.JAVA_PARTITIONING);
308         fViewer.setInput(doc);
309     }
310
311     /*
312      * @see IInformationControl#setVisible(boolean)
313      */

314     public void setVisible(boolean visible) {
315             fShell.setVisible(visible);
316     }
317
318     /**
319      * {@inheritDoc}
320      * @since 3.0
321      */

322     public void widgetDisposed(DisposeEvent event) {
323         if (fStatusTextFont != null && !fStatusTextFont.isDisposed())
324             fStatusTextFont.dispose();
325
326         fStatusTextFont= null;
327         fShell= null;
328         fText= null;
329     }
330
331     /**
332      * {@inheritDoc}
333      */

334     public final void dispose() {
335         if (!fIsSystemBackgroundColor)
336             fBackgroundColor.dispose();
337         if (fShell != null && !fShell.isDisposed())
338             fShell.dispose();
339         else
340             widgetDisposed(null);
341     }
342
343     /*
344      * @see IInformationControl#setSize(int, int)
345      */

346     public void setSize(int width, int height) {
347
348         if (fStatusField != null) {
349             GridData gd= (GridData)fViewer.getTextWidget().getLayoutData();
350             Point statusSize= fStatusField.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
351             Point separatorSize= fSeparator.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
352             gd.heightHint= height - statusSize.y - separatorSize.y;
353         }
354         fShell.setSize(width, height);
355
356         if (fStatusField != null)
357             fShell.pack(true);
358     }
359
360     /*
361      * @see IInformationControl#setLocation(Point)
362      */

363     public void setLocation(Point location) {
364         fShell.setLocation(location);
365     }
366
367     /*
368      * @see IInformationControl#setSizeConstraints(int, int)
369      */

370     public void setSizeConstraints(int maxWidth, int maxHeight) {
371         fMaxWidth= maxWidth;
372         fMaxHeight= maxHeight;
373     }
374
375     /*
376      * @see IInformationControl#computeSizeHint()
377      */

378     public Point computeSizeHint() {
379         // compute the preferred size
380
int x= SWT.DEFAULT;
381         int y= SWT.DEFAULT;
382         Point size= fShell.computeSize(x, y);
383         if (size.x > fMaxWidth)
384             x= fMaxWidth;
385         if (size.y > fMaxHeight)
386             y= fMaxHeight;
387
388         // recompute using the constraints if the preferred size is larger than the constraints
389
if (x != SWT.DEFAULT || y != SWT.DEFAULT)
390             size= fShell.computeSize(x, y, false);
391
392         return size;
393     }
394
395     /*
396      * @see IInformationControl#addDisposeListener(DisposeListener)
397      */

398     public void addDisposeListener(DisposeListener listener) {
399         fShell.addDisposeListener(listener);
400     }
401
402     /*
403      * @see IInformationControl#removeDisposeListener(DisposeListener)
404      */

405     public void removeDisposeListener(DisposeListener listener) {
406         fShell.removeDisposeListener(listener);
407     }
408
409     /*
410      * @see IInformationControl#setForegroundColor(Color)
411      */

412     public void setForegroundColor(Color foreground) {
413         fText.setForeground(foreground);
414     }
415
416     /*
417      * @see IInformationControl#setBackgroundColor(Color)
418      */

419     public void setBackgroundColor(Color background) {
420         fText.setBackground(background);
421     }
422
423     /*
424      * @see IInformationControl#isFocusControl()
425      */

426     public boolean isFocusControl() {
427         return fText.isFocusControl();
428     }
429
430     /*
431      * @see IInformationControl#setFocus()
432      */

433     public void setFocus() {
434         fShell.forceFocus();
435         fText.setFocus();
436     }
437
438     /*
439      * @see IInformationControl#addFocusListener(FocusListener)
440      */

441     public void addFocusListener(FocusListener listener) {
442         fText.addFocusListener(listener);
443     }
444
445     /*
446      * @see IInformationControl#removeFocusListener(FocusListener)
447      */

448     public void removeFocusListener(FocusListener listener) {
449         fText.removeFocusListener(listener);
450     }
451
452     /*
453      * @see IInformationControlExtension#hasContents()
454      */

455     public boolean hasContents() {
456         return fText.getCharCount() > 0;
457     }
458
459     protected ISourceViewer getViewer() {
460         return fViewer;
461     }
462 }
463
Popular Tags