View Javadoc

1   package org.eparapher.rcp.tools;
2   
3   import org.eclipse.swt.SWT;
4   import org.eclipse.swt.custom.CLabel;
5   import org.eclipse.swt.events.DisposeEvent;
6   import org.eclipse.swt.events.DisposeListener;
7   import org.eclipse.swt.events.MouseAdapter;
8   import org.eclipse.swt.events.MouseEvent;
9   import org.eclipse.swt.events.MouseListener;
10  import org.eclipse.swt.graphics.GC;
11  import org.eclipse.swt.graphics.Image;
12  import org.eclipse.swt.widgets.Composite;
13  import org.eclipse.swt.widgets.Display;
14  import org.eclipse.swt.widgets.Label;
15  
16  import org.eclipse.jface.action.ContributionItem;
17  import org.eclipse.jface.action.IAction;
18  import org.eclipse.jface.action.StatusLineLayoutData;
19  import org.eclipse.jface.resource.JFaceColors;
20  
21  /**
22   * Contribution item for the status line.
23   * @since 2.0
24   */
25  public class StatusLineContributionItem extends ContributionItem {
26  
27  	/**
28  	 * Internal mouse listener to track double clicking the status line item.
29  	 * @since 3.0
30  	 */
31  	private class Listener extends MouseAdapter {
32  		/*
33  		 * @see org.eclipse.swt.events.MouseAdapter#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
34  		 */
35  		public void mouseDoubleClick(MouseEvent e) {
36  			if (fActionHandler != null && fActionHandler.isEnabled())
37  				fActionHandler.run();
38  		}
39  	}
40  
41  	/**
42  	 * Left and right margin used in CLabel.
43  	 * @since 2.1
44  	 */
45  	private static final int INDENT= 3;
46  	/**
47  	 * Default number of characters that should fit into the item.
48  	 * @since 3.0
49  	 */
50  	static final int DEFAULT_WIDTH_IN_CHARS= 14;
51  	/**
52  	 * Pre-computed label width hint.
53  	 * @since 2.1
54  	 */
55  	private int fFixedWidth= -1;
56  	/**
57  	 * Pre-computed label height hint.
58  	 * @since 3.0
59  	 */
60  	private int fFixedHeight= -1;
61  	/** The text */
62  	private String fText;
63  	/** The image */
64  	private Image fImage;
65  	/**
66  	 * The error text.
67  	 * @since 3.0
68  	 */
69  	private String fErrorText;
70  	/**
71  	 * The error image.
72  	 * @since 3.0
73  	 */
74  	private Image fErrorImage;
75  	/**
76  	 * The tool tip text.
77  	 * @since 3.0
78  	 */
79  	private String fToolTipText;
80  	/**
81  	 * Number of characters that should fit into the item.
82  	 * @since 3.0
83  	 */
84  	private int fWidthInChars;
85  	/** The status line label widget */
86  	private CLabel fLabel;
87  	/**
88  	 * The action handler.
89  	 * @since 3.0
90  	 */
91  	private IAction fActionHandler;
92  	/**
93  	 * The mouse listener
94  	 * @since 3.0
95  	 */
96  	private MouseListener fMouseListener;
97  
98  
99  	/**
100 	 * Creates a new item with the given id.
101 	 *
102 	 * @param id the item's id
103 	 */
104 	public StatusLineContributionItem(String id) {
105 		this(id, true, DEFAULT_WIDTH_IN_CHARS);
106 	}
107 
108 	/**
109 	 * Creates a new item with the given attributes.
110 	 *
111 	 * @param id the item's id
112 	 * @param visible the visibility of this item
113 	 * @param widthInChars the width in characters
114 	 * @since 3.0
115 	 */
116 	public StatusLineContributionItem(String id, boolean visible, int widthInChars) {
117 		super(id);
118 		setVisible(visible);
119 		fWidthInChars= widthInChars;
120 	}
121 
122 	/*
123 	 * @see IStatusField#setText(String)
124 	 */
125 	public void setText(String text) {
126 		fText= text;
127 		updateMessageLabel();
128 	}
129 
130 	/*
131 	 * @see IStatusField#setImage(Image)
132 	 */
133 	public void setImage(Image image) {
134 		fImage= image;
135 		updateMessageLabel();
136 	}
137 
138 	/*
139 	 * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setErrorText(java.lang.String)
140 	 * @since 3.0
141 	 */
142 	public void setErrorText(String text) {
143 		fErrorText= text;
144 		updateMessageLabel();
145 	}
146 
147 	/*
148 	 * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setErrorImage(org.eclipse.swt.graphics.Image)
149 	 * @since 3.0
150 	 */
151 	public void setErrorImage(Image image) {
152 		fErrorImage= image;
153 		updateMessageLabel();
154 	}
155 
156 	/*
157 	 * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setToolTipText(java.lang.String)
158 	 * @since 3.0
159 	 */
160 	public void setToolTipText(String string) {
161 		fToolTipText= string;
162 		updateMessageLabel();
163 	}
164 
165 	/*
166 	 * @see IContributionItem#fill(Composite)
167 	 */
168 	public void fill(Composite parent) {
169 
170 		Label sep= new Label(parent, SWT.SEPARATOR);
171 		fLabel= new CLabel(parent, SWT.SHADOW_NONE);
172 
173 		fLabel.addDisposeListener(new DisposeListener() {
174 			public void widgetDisposed(DisposeEvent e) {
175 				fMouseListener= null;
176 			}
177 		});
178 		if (fActionHandler != null) {
179 			fMouseListener= new Listener();
180 			fLabel.addMouseListener(fMouseListener);
181 		}
182 
183 		StatusLineLayoutData data= new StatusLineLayoutData();
184 		data.widthHint= getWidthHint(parent);
185 		fLabel.setLayoutData(data);
186 
187 		data= new StatusLineLayoutData();
188 		data.heightHint= getHeightHint(parent);
189 		sep.setLayoutData(data);
190 
191 		updateMessageLabel();
192 	}
193 
194 	public void setActionHandler(IAction actionHandler) {
195 		if (fActionHandler != null && actionHandler == null && fMouseListener != null) {
196 			if (!fLabel.isDisposed())
197 				fLabel.removeMouseListener(fMouseListener);
198 			fMouseListener= null;
199 		}
200 
201 		fActionHandler= actionHandler;
202 
203 		if (fLabel != null && !fLabel.isDisposed() && fMouseListener == null && fActionHandler != null) {
204 			fMouseListener= new Listener();
205 			fLabel.addMouseListener(fMouseListener);
206 		}
207 	}
208 
209 	/**
210 	 * Returns the width hint for this label.
211 	 *
212 	 * @param control the root control of this label
213 	 * @return the width hint for this label
214 	 * @since 2.1
215 	 */
216 	private int getWidthHint(Composite control) {
217 		if (fFixedWidth < 0) {
218 			GC gc= new GC(control);
219 			gc.setFont(control.getFont());
220 			fFixedWidth= gc.getFontMetrics().getAverageCharWidth() * fWidthInChars;
221 			fFixedWidth += INDENT * 2;
222 			gc.dispose();
223 		}
224 		return fFixedWidth;
225 	}
226 
227 	/**
228 	 * Returns the height hint for this label.
229 	 *
230 	 * @param control the root control of this label
231 	 * @return the height hint for this label
232 	 * @since 3.0
233 	 */
234 	private int getHeightHint(Composite control) {
235 		if (fFixedHeight < 0) {
236 			GC gc= new GC(control);
237 			gc.setFont(control.getFont());
238 			fFixedHeight= gc.getFontMetrics().getHeight();
239 			gc.dispose();
240 		}
241 		return fFixedHeight;
242 	}
243 
244 	/**
245 	 * Updates the message label widget.
246 	 *
247 	 * @since 3.0
248 	 */
249 	private void updateMessageLabel() {
250 		if (fLabel != null && !fLabel.isDisposed()) {
251 			Display display= fLabel.getDisplay();
252 			if ((fErrorText != null && fErrorText.length() > 0) || fErrorImage != null) {
253 				fLabel.setForeground(JFaceColors.getErrorText(display));
254 				fLabel.setText(fErrorText);
255 				fLabel.setImage(fErrorImage);
256 				if (fToolTipText != null)
257 					fLabel.setToolTipText(fToolTipText);
258 				else if (fErrorText.length() > fWidthInChars)
259 					fLabel.setToolTipText(fErrorText);
260 				else
261 					fLabel.setToolTipText(null);
262 			}
263 			else {
264 				fLabel.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
265 				fLabel.setText(fText);
266 				fLabel.setImage(fImage);
267 				if (fToolTipText != null)
268 					fLabel.setToolTipText(fToolTipText);
269 				else if (fText != null && fText.length() > fWidthInChars)
270 					fLabel.setToolTipText(fText);
271 				else
272 					fLabel.setToolTipText(null);
273 			}
274 		}
275 	}
276 }
277