KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > projection > SourceViewerInformationControl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jface.text.source.projection;
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.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Shell;
30
31 import org.eclipse.jface.text.Document;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.IInformationControl;
34 import org.eclipse.jface.text.IInformationControlExtension;
35 import org.eclipse.jface.text.source.SourceViewer;
36 import org.eclipse.jface.text.source.SourceViewerConfiguration;
37
38 import org.eclipse.jface.resource.JFaceResources;
39
40 /**
41  * Source viewer based implementation of {@link org.eclipse.jface.text.IInformationControl}.
42  * Displays information in a source viewer.
43  *
44  * @since 3.0
45  */

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

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

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

178     public void setInput(Object JavaDoc input) {
179         if (input instanceof String JavaDoc)
180             setInformation((String JavaDoc)input);
181         else
182             setInformation(null);
183     }
184
185     /*
186      * @see IInformationControl#setInformation(String)
187      */

188     public void setInformation(String JavaDoc content) {
189         if (content == null) {
190             fViewer.setInput(null);
191             return;
192         }
193
194         IDocument doc= new Document(content);
195         fViewer.setInput(doc);
196     }
197
198     /*
199      * @see IInformationControl#setVisible(boolean)
200      */

201     public void setVisible(boolean visible) {
202             fShell.setVisible(visible);
203     }
204
205     /*
206      * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
207      */

208     public void widgetDisposed(DisposeEvent event) {
209         if (fStatusTextFont != null && !fStatusTextFont.isDisposed())
210             fStatusTextFont.dispose();
211
212         fStatusTextFont= null;
213         fShell= null;
214         fText= null;
215     }
216
217     /*
218      * @see org.eclipse.jface.text.IInformationControl#dispose()
219      */

220     public final void dispose() {
221         if (fShell != null && !fShell.isDisposed())
222             fShell.dispose();
223         else
224             widgetDisposed(null);
225     }
226
227     /*
228      * @see IInformationControl#setSize(int, int)
229      */

230     public void setSize(int width, int height) {
231
232         if (fStatusField != null) {
233             GridData gd= (GridData)fViewer.getTextWidget().getLayoutData();
234             Point statusSize= fStatusField.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
235             Point separatorSize= fSeparator.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
236             gd.heightHint= height - statusSize.y - separatorSize.y;
237         }
238         fShell.setSize(width, height);
239
240         if (fStatusField != null)
241             fShell.pack(true);
242     }
243
244     /*
245      * @see IInformationControl#setLocation(Point)
246      */

247     public void setLocation(Point location) {
248         fShell.setLocation(location);
249     }
250
251     /*
252      * @see IInformationControl#setSizeConstraints(int, int)
253      */

254     public void setSizeConstraints(int maxWidth, int maxHeight) {
255         fMaxWidth= maxWidth;
256         fMaxHeight= maxHeight;
257     }
258
259     /*
260      * @see IInformationControl#computeSizeHint()
261      */

262     public Point computeSizeHint() {
263         // compute the preferred size
264
int x= SWT.DEFAULT;
265         int y= SWT.DEFAULT;
266         Point size= fShell.computeSize(x, y);
267         if (size.x > fMaxWidth)
268             x= fMaxWidth;
269         if (size.y > fMaxHeight)
270             y= fMaxHeight;
271
272         // recompute using the constraints if the preferred size is larger than the constraints
273
if (x != SWT.DEFAULT || y != SWT.DEFAULT)
274             size= fShell.computeSize(x, y, false);
275
276         return size;
277     }
278
279     /*
280      * @see IInformationControl#addDisposeListener(DisposeListener)
281      */

282     public void addDisposeListener(DisposeListener listener) {
283         fShell.addDisposeListener(listener);
284     }
285
286     /*
287      * @see IInformationControl#removeDisposeListener(DisposeListener)
288      */

289     public void removeDisposeListener(DisposeListener listener) {
290         fShell.removeDisposeListener(listener);
291     }
292
293     /*
294      * @see IInformationControl#setForegroundColor(Color)
295      */

296     public void setForegroundColor(Color foreground) {
297         fText.setForeground(foreground);
298     }
299
300     /*
301      * @see IInformationControl#setBackgroundColor(Color)
302      */

303     public void setBackgroundColor(Color background) {
304         fText.setBackground(background);
305     }
306
307     /*
308      * @see IInformationControl#isFocusControl()
309      */

310     public boolean isFocusControl() {
311         return fText.isFocusControl();
312     }
313
314     /*
315      * @see IInformationControl#setFocus()
316      */

317     public void setFocus() {
318         fShell.forceFocus();
319         fText.setFocus();
320     }
321
322     /*
323      * @see IInformationControl#addFocusListener(FocusListener)
324      */

325     public void addFocusListener(FocusListener listener) {
326         fText.addFocusListener(listener);
327     }
328
329     /*
330      * @see IInformationControl#removeFocusListener(FocusListener)
331      */

332     public void removeFocusListener(FocusListener listener) {
333         fText.removeFocusListener(listener);
334     }
335
336     /*
337      * @see IInformationControlExtension#hasContents()
338      */

339     public boolean hasContents() {
340         return fText.getCharCount() > 0;
341     }
342 }
343
Popular Tags