KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > StatusLineContributionItem


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.ui.texteditor;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.CLabel;
16 import org.eclipse.swt.events.DisposeEvent;
17 import org.eclipse.swt.events.DisposeListener;
18 import org.eclipse.swt.events.MouseAdapter;
19 import org.eclipse.swt.events.MouseEvent;
20 import org.eclipse.swt.events.MouseListener;
21 import org.eclipse.swt.graphics.GC;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Label;
26
27 import org.eclipse.jface.action.ContributionItem;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.action.StatusLineLayoutData;
30 import org.eclipse.jface.resource.JFaceColors;
31
32 /**
33  * Contribution item for the status line.
34  * @since 2.0
35  */

36 public class StatusLineContributionItem extends ContributionItem implements IStatusField, IStatusFieldExtension {
37
38     /**
39      * Internal mouse listener to track double clicking the status line item.
40      * @since 3.0
41      */

42     private class Listener extends MouseAdapter {
43         /*
44          * @see org.eclipse.swt.events.MouseAdapter#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
45          */

46         public void mouseDoubleClick(MouseEvent e) {
47             if (fActionHandler != null && fActionHandler.isEnabled())
48                 fActionHandler.run();
49         }
50     }
51
52     /**
53      * Left and right margin used in CLabel.
54      * @since 2.1
55      */

56     private static final int INDENT= 3;
57     /**
58      * Default number of characters that should fit into the item.
59      * @since 3.0
60      */

61     static final int DEFAULT_WIDTH_IN_CHARS= 14;
62     /**
63      * Pre-computed label width hint.
64      * @since 2.1
65      */

66     private int fFixedWidth= -1;
67     /**
68      * Pre-computed label height hint.
69      * @since 3.0
70      */

71     private int fFixedHeight= -1;
72     /** The text */
73     private String JavaDoc fText;
74     /** The image */
75     private Image fImage;
76     /**
77      * The error text.
78      * @since 3.0
79      */

80     private String JavaDoc fErrorText;
81     /**
82      * The error image.
83      * @since 3.0
84      */

85     private Image fErrorImage;
86     /**
87      * The tool tip text.
88      * @since 3.0
89      */

90     private String JavaDoc fToolTipText;
91     /**
92      * Number of characters that should fit into the item.
93      * @since 3.0
94      */

95     private int fWidthInChars;
96     /** The status line label widget */
97     private CLabel fLabel;
98     /**
99      * The action handler.
100      * @since 3.0
101      */

102     private IAction fActionHandler;
103     /**
104      * The mouse listener
105      * @since 3.0
106      */

107     private MouseListener fMouseListener;
108
109
110     /**
111      * Creates a new item with the given id.
112      *
113      * @param id the item's id
114      */

115     public StatusLineContributionItem(String JavaDoc id) {
116         this(id, true, DEFAULT_WIDTH_IN_CHARS);
117     }
118
119     /**
120      * Creates a new item with the given attributes.
121      *
122      * @param id the item's id
123      * @param visible the visibility of this item
124      * @param widthInChars the width in characters
125      * @since 3.0
126      */

127     public StatusLineContributionItem(String JavaDoc id, boolean visible, int widthInChars) {
128         super(id);
129         setVisible(visible);
130         fWidthInChars= widthInChars;
131     }
132
133     /*
134      * @see IStatusField#setText(String)
135      */

136     public void setText(String JavaDoc text) {
137         fText= text;
138         updateMessageLabel();
139     }
140
141     /*
142      * @see IStatusField#setImage(Image)
143      */

144     public void setImage(Image image) {
145         fImage= image;
146         updateMessageLabel();
147     }
148
149     /*
150      * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setErrorText(java.lang.String)
151      * @since 3.0
152      */

153     public void setErrorText(String JavaDoc text) {
154         fErrorText= text;
155         updateMessageLabel();
156     }
157
158     /*
159      * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setErrorImage(org.eclipse.swt.graphics.Image)
160      * @since 3.0
161      */

162     public void setErrorImage(Image image) {
163         fErrorImage= image;
164         updateMessageLabel();
165     }
166
167     /*
168      * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setToolTipText(java.lang.String)
169      * @since 3.0
170      */

171     public void setToolTipText(String JavaDoc string) {
172         fToolTipText= string;
173         updateMessageLabel();
174     }
175
176     /*
177      * @see IContributionItem#fill(Composite)
178      */

179     public void fill(Composite parent) {
180
181         Label sep= new Label(parent, SWT.SEPARATOR);
182         fLabel= new CLabel(parent, SWT.SHADOW_NONE);
183
184         fLabel.addDisposeListener(new DisposeListener() {
185             public void widgetDisposed(DisposeEvent e) {
186                 fMouseListener= null;
187             }
188         });
189         if (fActionHandler != null) {
190             fMouseListener= new Listener();
191             fLabel.addMouseListener(fMouseListener);
192         }
193
194         StatusLineLayoutData data= new StatusLineLayoutData();
195         data.widthHint= getWidthHint(parent);
196         fLabel.setLayoutData(data);
197
198         data= new StatusLineLayoutData();
199         data.heightHint= getHeightHint(parent);
200         sep.setLayoutData(data);
201
202         updateMessageLabel();
203     }
204
205     public void setActionHandler(IAction actionHandler) {
206         if (fActionHandler != null && actionHandler == null && fMouseListener != null) {
207             if (!fLabel.isDisposed())
208                 fLabel.removeMouseListener(fMouseListener);
209             fMouseListener= null;
210         }
211
212         fActionHandler= actionHandler;
213
214         if (fLabel != null && !fLabel.isDisposed() && fMouseListener == null && fActionHandler != null) {
215             fMouseListener= new Listener();
216             fLabel.addMouseListener(fMouseListener);
217         }
218     }
219
220     /**
221      * Returns the width hint for this label.
222      *
223      * @param control the root control of this label
224      * @return the width hint for this label
225      * @since 2.1
226      */

227     private int getWidthHint(Composite control) {
228         if (fFixedWidth < 0) {
229             GC gc= new GC(control);
230             gc.setFont(control.getFont());
231             fFixedWidth= gc.getFontMetrics().getAverageCharWidth() * fWidthInChars;
232             fFixedWidth += INDENT * 2;
233             gc.dispose();
234         }
235         return fFixedWidth;
236     }
237
238     /**
239      * Returns the height hint for this label.
240      *
241      * @param control the root control of this label
242      * @return the height hint for this label
243      * @since 3.0
244      */

245     private int getHeightHint(Composite control) {
246         if (fFixedHeight < 0) {
247             GC gc= new GC(control);
248             gc.setFont(control.getFont());
249             fFixedHeight= gc.getFontMetrics().getHeight();
250             gc.dispose();
251         }
252         return fFixedHeight;
253     }
254
255     /**
256      * Updates the message label widget.
257      *
258      * @since 3.0
259      */

260     private void updateMessageLabel() {
261         if (fLabel != null && !fLabel.isDisposed()) {
262             Display display= fLabel.getDisplay();
263             if ((fErrorText != null && fErrorText.length() > 0) || fErrorImage != null) {
264                 fLabel.setForeground(JFaceColors.getErrorText(display));
265                 fLabel.setText(fErrorText);
266                 fLabel.setImage(fErrorImage);
267                 if (fToolTipText != null)
268                     fLabel.setToolTipText(fToolTipText);
269                 else if (fErrorText.length() > fWidthInChars)
270                     fLabel.setToolTipText(fErrorText);
271                 else
272                     fLabel.setToolTipText(null);
273             }
274             else {
275                 fLabel.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
276                 fLabel.setText(fText);
277                 fLabel.setImage(fImage);
278                 if (fToolTipText != null)
279                     fLabel.setToolTipText(fToolTipText);
280                 else if (fText != null && fText.length() > fWidthInChars)
281                     fLabel.setToolTipText(fText);
282                 else
283                     fLabel.setToolTipText(null);
284             }
285         }
286     }
287 }
288
289
Popular Tags