1 /*******************************************************************************
2  *  Copyright (c) 2007, 2017 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.ui.sdk.scheduler;
15 
16 import org.eclipse.jface.action.*;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.CLabel;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Listener;
22 
23 /**
24  * @since 3.4
25  */
26 public class StatusLineCLabelContribution extends ContributionItem {
27 
28 	public final static int DEFAULT_CHAR_WIDTH = 40;
29 
30 	private int charWidth;
31 	private CLabel label;
32 	private Image image;
33 	private String text = ""; //$NON-NLS-1$
34 	private int widthHint = -1;
35 	private int heightHint = -1;
36 
37 	private Listener listener;
38 	private int eventType;
39 	private String tooltip;
40 
StatusLineCLabelContribution(String id, int charWidth)41 	public StatusLineCLabelContribution(String id, int charWidth) {
42 		super(id);
43 		this.charWidth = charWidth;
44 		setVisible(false); // no text to start with
45 	}
46 
47 	@Override
fill(Composite parent)48 	public void fill(Composite parent) {
49 		label = new CLabel(parent, SWT.DEFAULT);
50 		StatusLineLayoutData statusLineLayoutData = new StatusLineLayoutData();
51 
52 		if (widthHint < 0) {
53 			GC gc = new GC(parent);
54 			gc.setFont(parent.getFont());
55 			FontMetrics fm = gc.getFontMetrics();
56 			widthHint = fm.getAverageCharWidth() * charWidth;
57 			heightHint = fm.getHeight();
58 			gc.dispose();
59 		}
60 
61 		statusLineLayoutData.widthHint = widthHint;
62 		label.setLayoutData(statusLineLayoutData);
63 		label.setText(text);
64 		label.setImage(image);
65 		if (listener != null) {
66 			label.addListener(eventType, listener);
67 		}
68 		if (tooltip != null) {
69 			label.setToolTipText(tooltip);
70 		}
71 
72 		statusLineLayoutData = new StatusLineLayoutData();
73 		statusLineLayoutData.heightHint = heightHint;
74 	}
75 
addListener(int type, Listener labelListener)76 	public void addListener(int type, Listener labelListener) {
77 		this.eventType = type;
78 		this.listener = labelListener;
79 	}
80 
setText(String text)81 	public void setText(String text) {
82 		if (text == null)
83 			throw new NullPointerException();
84 
85 		this.text = text;
86 
87 		if (label != null && !label.isDisposed())
88 			label.setText(this.text);
89 
90 		if (this.text.length() == 0) {
91 			if (isVisible()) {
92 				setVisible(false);
93 				IContributionManager contributionManager = getParent();
94 
95 				if (contributionManager != null)
96 					contributionManager.update(true);
97 			}
98 		} else {
99 			if (!isVisible()) {
100 				setVisible(true);
101 				IContributionManager contributionManager = getParent();
102 
103 				if (contributionManager != null)
104 					contributionManager.update(true);
105 			}
106 		}
107 	}
108 
setTooltip(String tooltip)109 	public void setTooltip(String tooltip) {
110 		if (tooltip == null)
111 			throw new NullPointerException();
112 
113 		this.tooltip = tooltip;
114 
115 		if (label != null && !label.isDisposed()) {
116 			label.setToolTipText(this.tooltip);
117 		}
118 	}
119 
setImage(Image image)120 	public void setImage(Image image) {
121 		if (image == null)
122 			throw new NullPointerException();
123 
124 		this.image = image;
125 
126 		if (label != null && !label.isDisposed())
127 			label.setImage(this.image);
128 
129 		if (!isVisible()) {
130 			setVisible(true);
131 			IContributionManager contributionManager = getParent();
132 
133 			if (contributionManager != null)
134 				contributionManager.update(true);
135 		}
136 	}
137 }
138