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
23
24
25 public class StatusLineContributionItem extends ContributionItem {
26
27
28
29
30
31 private class Listener extends MouseAdapter {
32
33
34
35 public void mouseDoubleClick(MouseEvent e) {
36 if (fActionHandler != null && fActionHandler.isEnabled())
37 fActionHandler.run();
38 }
39 }
40
41
42
43
44
45 private static final int INDENT= 3;
46
47
48
49
50 static final int DEFAULT_WIDTH_IN_CHARS= 14;
51
52
53
54
55 private int fFixedWidth= -1;
56
57
58
59
60 private int fFixedHeight= -1;
61
62 private String fText;
63
64 private Image fImage;
65
66
67
68
69 private String fErrorText;
70
71
72
73
74 private Image fErrorImage;
75
76
77
78
79 private String fToolTipText;
80
81
82
83
84 private int fWidthInChars;
85
86 private CLabel fLabel;
87
88
89
90
91 private IAction fActionHandler;
92
93
94
95
96 private MouseListener fMouseListener;
97
98
99
100
101
102
103
104 public StatusLineContributionItem(String id) {
105 this(id, true, DEFAULT_WIDTH_IN_CHARS);
106 }
107
108
109
110
111
112
113
114
115
116 public StatusLineContributionItem(String id, boolean visible, int widthInChars) {
117 super(id);
118 setVisible(visible);
119 fWidthInChars= widthInChars;
120 }
121
122
123
124
125 public void setText(String text) {
126 fText= text;
127 updateMessageLabel();
128 }
129
130
131
132
133 public void setImage(Image image) {
134 fImage= image;
135 updateMessageLabel();
136 }
137
138
139
140
141
142 public void setErrorText(String text) {
143 fErrorText= text;
144 updateMessageLabel();
145 }
146
147
148
149
150
151 public void setErrorImage(Image image) {
152 fErrorImage= image;
153 updateMessageLabel();
154 }
155
156
157
158
159
160 public void setToolTipText(String string) {
161 fToolTipText= string;
162 updateMessageLabel();
163 }
164
165
166
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
211
212
213
214
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
229
230
231
232
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
246
247
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