KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > DefaultInformationControl


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
12 package org.eclipse.jface.text;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.StyledText;
16 import org.eclipse.swt.events.DisposeEvent;
17 import org.eclipse.swt.events.DisposeListener;
18 import org.eclipse.swt.events.FocusListener;
19 import org.eclipse.swt.events.KeyEvent;
20 import org.eclipse.swt.events.KeyListener;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Drawable;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Shell;
30
31 import org.eclipse.jface.dialogs.PopupDialog;
32
33
34 /**
35  * Default implementation of {@link org.eclipse.jface.text.IInformationControl}.
36  * <p>
37  * Displays textual information in a {@link org.eclipse.swt.custom.StyledText}
38  * widget. Before displaying, the information set to this information control is
39  * processed by an <code>IInformationPresenter</code>.
40  *
41  * @since 2.0
42  */

43 public class DefaultInformationControl implements IInformationControl, IInformationControlExtension, IInformationControlExtension3, DisposeListener {
44
45     /**
46      * An information presenter determines the style presentation
47      * of information displayed in the default information control.
48      * The interface can be implemented by clients.
49      */

50     public interface IInformationPresenter {
51
52         /**
53          * Updates the given presentation of the given information and
54          * thereby may manipulate the information to be displayed. The manipulation
55          * could be the extraction of textual encoded style information etc. Returns the
56          * manipulated information.
57          *
58          * @param display the display of the information control
59          * @param hoverInfo the information to be presented
60          * @param presentation the presentation to be updated
61          * @param maxWidth the maximal width in pixels
62          * @param maxHeight the maximal height in pixels
63          *
64          * @return the manipulated information
65          * @deprecated As of 3.2, replaced by {@link DefaultInformationControl.IInformationPresenterExtension#updatePresentation(Drawable, String, TextPresentation, int, int)}
66          * see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=38528 for details.
67          */

68         String JavaDoc updatePresentation(Display display, String JavaDoc hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight);
69     }
70     
71     
72     /**
73      * An information presenter determines the style presentation
74      * of information displayed in the default information control.
75      * The interface can be implemented by clients.
76      *
77      * @since 3.2
78      */

79     public interface IInformationPresenterExtension {
80         
81         /**
82          * Updates the given presentation of the given information and
83          * thereby may manipulate the information to be displayed. The manipulation
84          * could be the extraction of textual encoded style information etc. Returns the
85          * manipulated information.
86          * <p>
87          * Replaces {@link DefaultInformationControl.IInformationPresenter#updatePresentation(Display, String, TextPresentation, int, int)}
88          * <em>Make sure that you do not pass in a <code>Display</code></em> until
89          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=38528 is fixed.
90          * </p>
91          *
92          * @param drawable the drawable of the information control
93          * @param hoverInfo the information to be presented
94          * @param presentation the presentation to be updated
95          * @param maxWidth the maximal width in pixels
96          * @param maxHeight the maximal height in pixels
97          *
98          * @return the manipulated information
99          */

100         String JavaDoc updatePresentation(Drawable drawable, String JavaDoc hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight);
101     }
102     
103
104     /**
105      * Inner border thickness in pixels.
106      * @since 3.1
107      */

108     private static final int INNER_BORDER= 1;
109
110     /**
111      * The control's popup dialog.
112      * @since 3.2
113      */

114     private PopupDialog fPopupDialog;
115     /** The control's text widget */
116     private StyledText fText;
117     /** The information presenter */
118     private IInformationPresenter fPresenter;
119     /** A cached text presentation */
120     private TextPresentation fPresentation= new TextPresentation();
121     /** The control width constraint */
122     private int fMaxWidth= -1;
123     /** The control height constraint */
124     private int fMaxHeight= -1;
125     
126
127     /**
128      * Creates a default information control with the given shell as parent. The given
129      * information presenter is used to process the information to be displayed. The given
130      * styles are applied to the created styled text widget.
131      *
132      * @param parent the parent shell
133      * @param shellStyle the additional styles for the shell
134      * @param style the additional styles for the styled text widget
135      * @param presenter the presenter to be used
136      */

137     public DefaultInformationControl(Shell parent, int shellStyle, int style, IInformationPresenter presenter) {
138         this(parent, shellStyle, style, presenter, null);
139     }
140
141     /**
142      * Creates a default information control with the given shell as parent. The given
143      * information presenter is used to process the information to be displayed. The given
144      * styles are applied to the created styled text widget.
145      *
146      * @param parentShell the parent shell
147      * @param shellStyle the additional styles for the shell
148      * @param style the additional styles for the styled text widget
149      * @param presenter the presenter to be used
150      * @param statusFieldText the text to be used in the optional status field
151      * or <code>null</code> if the status field should be hidden
152      * @since 3.0
153      */

154     public DefaultInformationControl(Shell parentShell, int shellStyle, final int style, IInformationPresenter presenter, String JavaDoc statusFieldText) {
155         shellStyle= shellStyle | SWT.NO_FOCUS | SWT.ON_TOP;
156         fPopupDialog= new PopupDialog(parentShell, shellStyle, false, false, false, false, null, statusFieldText) {
157             protected Control createDialogArea(Composite parent) {
158                 // Text field
159
fText= new StyledText(parent, SWT.MULTI | SWT.READ_ONLY | style);
160                 GridData gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH);
161                 gd.horizontalIndent= INNER_BORDER;
162                 gd.verticalIndent= INNER_BORDER;
163                 fText.setLayoutData(gd);
164                 fText.addKeyListener(new KeyListener() {
165
166                     public void keyPressed(KeyEvent e) {
167                         if (e.character == 0x1B) // ESC
168
close();
169                     }
170
171                     public void keyReleased(KeyEvent e) {}
172                 });
173                 return fText;
174             }
175         };
176
177         fPresenter= presenter;
178
179         // Force create early so that listeners can be added at all times with API.
180
fPopupDialog.create();
181     }
182
183     /**
184      * Creates a default information control with the given shell as parent. The given
185      * information presenter is used to process the information to be displayed. The given
186      * styles are applied to the created styled text widget.
187      *
188      * @param parent the parent shell
189      * @param style the additional styles for the styled text widget
190      * @param presenter the presenter to be used
191      */

192     public DefaultInformationControl(Shell parent,int style, IInformationPresenter presenter) {
193         this(parent, SWT.TOOL | SWT.NO_TRIM, style, presenter);
194     }
195
196     /**
197      * Creates a default information control with the given shell as parent. The given
198      * information presenter is used to process the information to be displayed. The given
199      * styles are applied to the created styled text widget.
200      *
201      * @param parent the parent shell
202      * @param style the additional styles for the styled text widget
203      * @param presenter the presenter to be used
204      * @param statusFieldText the text to be used in the optional status field
205      * or <code>null</code> if the status field should be hidden
206      * @since 3.0
207      */

208     public DefaultInformationControl(Shell parent,int style, IInformationPresenter presenter, String JavaDoc statusFieldText) {
209         this(parent, SWT.TOOL | SWT.NO_TRIM, style, presenter, statusFieldText);
210     }
211
212     /**
213      * Creates a default information control with the given shell as parent.
214      * No information presenter is used to process the information
215      * to be displayed. No additional styles are applied to the styled text widget.
216      *
217      * @param parent the parent shell
218      */

219     public DefaultInformationControl(Shell parent) {
220         this(parent, SWT.NONE, null);
221     }
222
223     /**
224      * Creates a default information control with the given shell as parent. The given
225      * information presenter is used to process the information to be displayed.
226      * No additional styles are applied to the styled text widget.
227      *
228      * @param parent the parent shell
229      * @param presenter the presenter to be used
230      */

231     public DefaultInformationControl(Shell parent, IInformationPresenter presenter) {
232         this(parent, SWT.NONE, presenter);
233     }
234
235     /*
236      * @see IInformationControl#setInformation(String)
237      */

238     public void setInformation(String JavaDoc content) {
239         if (fPresenter == null) {
240             fText.setText(content);
241         } else {
242             fPresentation.clear();
243             if (fPresenter instanceof IInformationPresenterExtension)
244                 content= ((IInformationPresenterExtension)fPresenter).updatePresentation(fPopupDialog.getShell(), content, fPresentation, fMaxWidth, fMaxHeight);
245             else
246                 content= fPresenter.updatePresentation(fPopupDialog.getShell().getDisplay(), content, fPresentation, fMaxWidth, fMaxHeight);
247             if (content != null) {
248                 fText.setText(content);
249                 TextPresentation.applyTextPresentation(fPresentation, fText);
250             } else {
251                 fText.setText(""); //$NON-NLS-1$
252
}
253         }
254     }
255
256     /*
257      * @see IInformationControl#setVisible(boolean)
258      */

259     public void setVisible(boolean visible) {
260         if (visible) {
261             if (fText.getWordWrap()) {
262                 Point currentSize= fPopupDialog.getShell().getSize();
263                 fPopupDialog.getShell().pack(true);
264                 Point newSize= fPopupDialog.getShell().getSize();
265                 if (newSize.x > currentSize.x || newSize.y > currentSize.y)
266                     setSize(currentSize.x, currentSize.y); // restore previous size
267
}
268             fPopupDialog.open();
269         } else
270             fPopupDialog.getShell().setVisible(false);
271     }
272
273     /*
274      * @see IInformationControl#dispose()
275      */

276     public void dispose() {
277         fPopupDialog.close();
278         fPopupDialog= null;
279     }
280
281     /*
282      * @see IInformationControl#setSize(int, int)
283      */

284     public void setSize(int width, int height) {
285         fPopupDialog.getShell().setSize(width, height);
286     }
287
288     /*
289      * @see IInformationControl#setLocation(Point)
290      */

291     public void setLocation(Point location) {
292         fPopupDialog.getShell().setLocation(location);
293     }
294
295     /*
296      * @see IInformationControl#setSizeConstraints(int, int)
297      */

298     public void setSizeConstraints(int maxWidth, int maxHeight) {
299         fMaxWidth= maxWidth;
300         fMaxHeight= maxHeight;
301     }
302
303     /*
304      * @see IInformationControl#computeSizeHint()
305      */

306     public Point computeSizeHint() {
307         // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=117602
308
int widthHint= SWT.DEFAULT;
309         if (fMaxWidth > -1 && fText.getWordWrap())
310             widthHint= fMaxWidth;
311         
312         return fPopupDialog.getShell().computeSize(widthHint, SWT.DEFAULT, true);
313     }
314
315     /*
316      * @see org.eclipse.jface.text.IInformationControlExtension3#computeTrim()
317      * @since 3.0
318      */

319     public Rectangle computeTrim() {
320         return fPopupDialog.getShell().computeTrim(0, 0, 0, 0);
321     }
322
323     /*
324      * @see org.eclipse.jface.text.IInformationControlExtension3#getBounds()
325      * @since 3.0
326      */

327     public Rectangle getBounds() {
328         return fPopupDialog.getShell().getBounds();
329     }
330
331     /*
332      * @see org.eclipse.jface.text.IInformationControlExtension3#restoresLocation()
333      * @since 3.0
334      */

335     public boolean restoresLocation() {
336         return false;
337     }
338
339     /*
340      * @see org.eclipse.jface.text.IInformationControlExtension3#restoresSize()
341      * @since 3.0
342      */

343     public boolean restoresSize() {
344         return false;
345     }
346
347     /*
348      * @see IInformationControl#addDisposeListener(DisposeListener)
349      */

350     public void addDisposeListener(DisposeListener listener) {
351         fPopupDialog.getShell().addDisposeListener(listener);
352     }
353
354     /*
355      * @see IInformationControl#removeDisposeListener(DisposeListener)
356      */

357     public void removeDisposeListener(DisposeListener listener) {
358         fPopupDialog.getShell().removeDisposeListener(listener);
359     }
360
361     /*
362      * @see IInformationControl#setForegroundColor(Color)
363      */

364     public void setForegroundColor(Color foreground) {
365         fText.setForeground(foreground);
366     }
367
368     /*
369      * @see IInformationControl#setBackgroundColor(Color)
370      */

371     public void setBackgroundColor(Color background) {
372         fText.setBackground(background);
373     }
374
375     /*
376      * @see IInformationControl#isFocusControl()
377      */

378     public boolean isFocusControl() {
379         return fText.isFocusControl();
380     }
381
382     /*
383      * @see IInformationControl#setFocus()
384      */

385     public void setFocus() {
386         fPopupDialog.getShell().forceFocus();
387         fText.setFocus();
388     }
389
390     /*
391      * @see IInformationControl#addFocusListener(FocusListener)
392      */

393     public void addFocusListener(FocusListener listener) {
394         fText.addFocusListener(listener);
395     }
396
397     /*
398      * @see IInformationControl#removeFocusListener(FocusListener)
399      */

400     public void removeFocusListener(FocusListener listener) {
401         fText.removeFocusListener(listener);
402     }
403
404     /*
405      * @see IInformationControlExtension#hasContents()
406      */

407     public boolean hasContents() {
408         return fText.getCharCount() > 0;
409     }
410
411     /**
412      * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
413      * @since 3.0
414      * @deprecated As of 3.2, no longer used and called
415      */

416     public void widgetDisposed(DisposeEvent event) {
417     }
418 }
419
420
Popular Tags