Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 51396e6ef50e81379d96ac15e8cb31a7077cf649 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.LegacyActionTools;
import org.eclipse.jface.action.StatusLineLayoutData;
import org.eclipse.jface.resource.JFaceColors;


/**
 * Contribution item for the status line.
 * @since 2.0
 */
public class StatusLineContributionItem extends ContributionItem implements IStatusField, IStatusFieldExtension {

	/**
	 * Internal mouse listener to track double clicking the status line item.
	 * @since 3.0
	 */
	private class Listener extends MouseAdapter {
		@Override
		public void mouseDoubleClick(MouseEvent e) {
			if (fActionHandler != null && fActionHandler.isEnabled())
				fActionHandler.run();
		}
	}

	/**
	 * Left and right margin used in CLabel.
	 * @since 2.1
	 */
	private static final int INDENT= 3;
	/**
	 * Default number of characters that should fit into the item.
	 * @since 3.0
	 */
	static final int DEFAULT_WIDTH_IN_CHARS= 14;
	/**
	 * Pre-computed label width hint.
	 * @since 2.1
	 */
	private int fFixedWidth= -1;
	/**
	 * Pre-computed label height hint.
	 * @since 3.0
	 */
	private int fFixedHeight= -1;
	/** The text */
	private String fText;
	/** The image */
	private Image fImage;
	/**
	 * The error text.
	 * @since 3.0
	 */
	private String fErrorText;
	/**
	 * The error image.
	 * @since 3.0
	 */
	private Image fErrorImage;
	/**
	 * The tool tip text.
	 * @since 3.0
	 */
	private String fToolTipText;
	/**
	 * Number of characters that should fit into the item.
	 * @since 3.0
	 */
	private int fWidthInChars;
	/** The status line label widget */
	private CLabel fLabel;
	/**
	 * The action handler.
	 * @since 3.0
	 */
	private IAction fActionHandler;
	/**
	 * The mouse listener
	 * @since 3.0
	 */
	private MouseListener fMouseListener;


	/**
	 * Creates a new item with the given id.
	 *
	 * @param id the item's id
	 */
	public StatusLineContributionItem(String id) {
		this(id, true, DEFAULT_WIDTH_IN_CHARS);
	}

	/**
	 * Creates a new item with the given attributes.
	 *
	 * @param id the item's id
	 * @param visible the visibility of this item
	 * @param widthInChars the width in characters
	 * @since 3.0
	 */
	public StatusLineContributionItem(String id, boolean visible, int widthInChars) {
		super(id);
		setVisible(visible);
		fWidthInChars= widthInChars;
	}

	@Override
	public void setText(String text) {
		fText= text;
		updateMessageLabel();
	}

	@Override
	public void setImage(Image image) {
		fImage= image;
		updateMessageLabel();
	}

	@Override
	public void setErrorText(String text) {
		fErrorText= text;
		updateMessageLabel();
	}

	@Override
	public void setErrorImage(Image image) {
		fErrorImage= image;
		updateMessageLabel();
	}

	@Override
	public void setToolTipText(String string) {
		fToolTipText= string;
		updateMessageLabel();
	}

	@Override
	public void fill(Composite parent) {

		Label sep= new Label(parent, SWT.SEPARATOR);
		fLabel= new CLabel(parent, SWT.SHADOW_NONE);

		fLabel.addDisposeListener(e -> fMouseListener = null);
		if (fActionHandler != null) {
			fMouseListener= new Listener();
			fLabel.addMouseListener(fMouseListener);
		}

		StatusLineLayoutData data= new StatusLineLayoutData();
		data.widthHint= getWidthHint(parent);
		fLabel.setLayoutData(data);

		data= new StatusLineLayoutData();
		data.heightHint= getHeightHint(parent);
		sep.setLayoutData(data);

		updateMessageLabel();
	}

	public void setActionHandler(IAction actionHandler) {
		if (fActionHandler != null && actionHandler == null && fMouseListener != null) {
			if (!fLabel.isDisposed())
				fLabel.removeMouseListener(fMouseListener);
			fMouseListener= null;
		}

		fActionHandler= actionHandler;

		if (fLabel != null && !fLabel.isDisposed() && fMouseListener == null && fActionHandler != null) {
			fMouseListener= new Listener();
			fLabel.addMouseListener(fMouseListener);
		}
	}

	/**
	 * Returns the width hint for this label.
	 *
	 * @param control the root control of this label
	 * @return the width hint for this label
	 * @since 2.1
	 */
	private int getWidthHint(Composite control) {
		if (fFixedWidth < 0) {
			GC gc= new GC(control);
			gc.setFont(control.getFont());
			fFixedWidth= gc.getFontMetrics().getAverageCharWidth() * fWidthInChars;
			fFixedWidth += INDENT * 2;
			gc.dispose();
		}
		return fFixedWidth;
	}

	/**
	 * Returns the height hint for this label.
	 *
	 * @param control the root control of this label
	 * @return the height hint for this label
	 * @since 3.0
	 */
	private int getHeightHint(Composite control) {
		if (fFixedHeight < 0) {
			GC gc= new GC(control);
			gc.setFont(control.getFont());
			fFixedHeight= gc.getFontMetrics().getHeight();
			gc.dispose();
		}
		return fFixedHeight;
	}

	/**
	 * Updates the message label widget.
	 *
	 * @since 3.0
	 */
	private void updateMessageLabel() {
		if (fLabel != null && !fLabel.isDisposed()) {
			Display display= fLabel.getDisplay();
			if ((fErrorText != null && fErrorText.length() > 0) || fErrorImage != null) {
				String escapedErrorText= escape(fErrorText);
				fLabel.setForeground(JFaceColors.getErrorText(display));
				fLabel.setText(escapedErrorText);
				fLabel.setImage(fErrorImage);
				if (fToolTipText != null)
					fLabel.setToolTipText(escape(fToolTipText));
				else if (fErrorText.length() > fWidthInChars)
					fLabel.setToolTipText(escapedErrorText);
				else
					fLabel.setToolTipText(null);

			} else {
				String escapedText= escape(fText);
				fLabel.setForeground(fLabel.getParent().getForeground());
				fLabel.setText(escapedText);
				fLabel.setImage(fImage);
				if (fToolTipText != null)
					fLabel.setToolTipText(escape(fToolTipText));
				else if (fText != null && fText.length() > fWidthInChars)
					fLabel.setToolTipText(escapedText);
				else
					fLabel.setToolTipText(null);
			}
		}
	}

	/**
	 * Escapes '&' with '&' in the given text.
	 *
	 * @param text the text to escape, can be <code>null</code>
	 * @return the escaped string or <code>null</code> if text was <code>null</code>
	 * @since 3.4
	 */
	private String escape(String text) {
		if (text == null)
			return text;
		return LegacyActionTools.escapeMnemonics(text);
	}

}

Back to the top