KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > AntSourceViewerInformationControl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12 package org.eclipse.ant.internal.ui.editor;
13
14 import org.eclipse.ant.internal.ui.AntSourceViewerConfiguration;
15 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.text.Document;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IInformationControl;
20 import org.eclipse.jface.text.IInformationControlExtension;
21 import org.eclipse.jface.text.source.SourceViewer;
22 import org.eclipse.jface.text.source.SourceViewerConfiguration;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.StyledText;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.events.FocusListener;
28 import org.eclipse.swt.events.KeyEvent;
29 import org.eclipse.swt.events.KeyListener;
30 import org.eclipse.swt.graphics.Color;
31 import org.eclipse.swt.graphics.Font;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.graphics.Rectangle;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Shell;
39
40 public class AntSourceViewerInformationControl implements IInformationControl, IInformationControlExtension, DisposeListener {
41     /** The control's shell */
42     private Shell fShell;
43     
44     /** Border thickness in pixels. */
45     private static final int BORDER= 1;
46     
47     /** The control's source viewer */
48     private SourceViewer fViewer;
49     
50     /** The control's text widget */
51     private StyledText fText;
52     
53     public AntSourceViewerInformationControl(Shell parent) {
54         GridLayout layout;
55         GridData gd;
56
57         fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | SWT.NO_TRIM);
58         Display display= fShell.getDisplay();
59         fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
60
61         Composite composite= fShell;
62         layout= new GridLayout(1, false);
63         int border= ((SWT.NO_TRIM & SWT.NO_TRIM) == 0) ? 0 : BORDER;
64         layout.marginHeight= border;
65         layout.marginWidth= border;
66         composite.setLayout(layout);
67         gd= new GridData(GridData.FILL_HORIZONTAL);
68         composite.setLayoutData(gd);
69         fViewer= createViewer(composite);
70         
71         fText= fViewer.getTextWidget();
72         gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH);
73         fText.setLayoutData(gd);
74         fText.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
75         fText.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
76             
77         fText.addKeyListener(new KeyListener() {
78                 
79             public void keyPressed(KeyEvent e) {
80                 if (e.character == 0x1B) // ESC
81
fShell.dispose();
82             }
83                 
84             public void keyReleased(KeyEvent e) {}
85         });
86     }
87     
88     
89     private SourceViewer createViewer(Composite parent) {
90         SourceViewer viewer = new SourceViewer(parent, null, SWT.NONE);
91           
92         SourceViewerConfiguration configuration = new AntSourceViewerConfiguration();
93         viewer.configure(configuration);
94         viewer.setEditable(false);
95         Font font= JFaceResources.getFont(JFaceResources.TEXT_FONT);
96         viewer.getTextWidget().setFont(font);
97                 
98         return viewer;
99     }
100     
101     /* (non-Javadoc)
102      * @see org.eclipse.jface.text.IInformationControl#setInformation(java.lang.String)
103      */

104     public void setInformation(String JavaDoc content) {
105         if (content == null) {
106             fViewer.setInput(null);
107             return;
108         }
109         IDocument document = new Document(content);
110         new AntDocumentSetupParticipant().setup(document);
111         fViewer.setDocument(document);
112     }
113
114     /* (non-Javadoc)
115      * @see org.eclipse.jface.text.IInformationControl#setSizeConstraints(int, int)
116      */

117     public void setSizeConstraints(int maxWidth, int maxHeight) {
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
122      */

123     public Point computeSizeHint() {
124         return fShell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.jface.text.IInformationControl#setVisible(boolean)
129      */

130     public void setVisible(boolean visible) {
131         fShell.setVisible(visible);
132     }
133
134     /* (non-Javadoc)
135      * @see org.eclipse.jface.text.IInformationControl#setSize(int, int)
136      */

137     public void setSize(int width, int height) {
138         fShell.setSize(width, height);
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.jface.text.IInformationControl#setLocation(org.eclipse.swt.graphics.Point)
143      */

144     public void setLocation(Point location) {
145         Rectangle trim= fShell.computeTrim(0, 0, 0, 0);
146         Point textLocation= fText.getLocation();
147         location.x += trim.x - textLocation.x;
148         location.y += trim.y - textLocation.y;
149         fShell.setLocation(location);
150     }
151
152     /* (non-Javadoc)
153      * @see org.eclipse.jface.text.IInformationControl#dispose()
154      */

155     public void dispose() {
156         if (fShell != null && !fShell.isDisposed()) {
157             fShell.dispose();
158         } else {
159             widgetDisposed(null);
160         }
161         
162     }
163
164     /* (non-Javadoc)
165      * @see org.eclipse.jface.text.IInformationControl#addDisposeListener(org.eclipse.swt.events.DisposeListener)
166      */

167     public void addDisposeListener(DisposeListener listener) {
168         fShell.addDisposeListener(listener);
169         
170     }
171
172     /* (non-Javadoc)
173      * @see org.eclipse.jface.text.IInformationControl#removeDisposeListener(org.eclipse.swt.events.DisposeListener)
174      */

175     public void removeDisposeListener(DisposeListener listener) {
176         fShell.removeDisposeListener(listener);
177     }
178
179     /* (non-Javadoc)
180      * @see org.eclipse.jface.text.IInformationControl#setForegroundColor(org.eclipse.swt.graphics.Color)
181      */

182     public void setForegroundColor(Color foreground) {
183         fText.setForeground(foreground);
184     }
185
186     /* (non-Javadoc)
187      * @see org.eclipse.jface.text.IInformationControl#setBackgroundColor(org.eclipse.swt.graphics.Color)
188      */

189     public void setBackgroundColor(Color background) {
190         fText.setBackground(background);
191     }
192
193     /* (non-Javadoc)
194      * @see org.eclipse.jface.text.IInformationControl#isFocusControl()
195      */

196     public boolean isFocusControl() {
197         return fText.isFocusControl();
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.jface.text.IInformationControl#setFocus()
202      */

203     public void setFocus() {
204         fShell.forceFocus();
205         fText.setFocus();
206         
207     }
208
209     /* (non-Javadoc)
210      * @see org.eclipse.jface.text.IInformationControl#addFocusListener(org.eclipse.swt.events.FocusListener)
211      */

212     public void addFocusListener(FocusListener listener) {
213         fText.addFocusListener(listener);
214     }
215
216     /* (non-Javadoc)
217      * @see org.eclipse.jface.text.IInformationControl#removeFocusListener(org.eclipse.swt.events.FocusListener)
218      */

219     public void removeFocusListener(FocusListener listener) {
220         fText.removeFocusListener(listener);
221         
222     }
223
224     /* (non-Javadoc)
225      * @see org.eclipse.jface.text.IInformationControlExtension#hasContents()
226      */

227     public boolean hasContents() {
228         return fText.getCharCount() > 0;
229     }
230
231     /* (non-Javadoc)
232      * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
233      */

234     public void widgetDisposed(DisposeEvent e) {
235         fShell= null;
236         fText= null;
237     }
238 }
239
Popular Tags