Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f25e5d5589a4ba7049782dbcb641d97459af7dad (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 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.team.internal.ui.synchronize.actions;

import org.eclipse.jface.action.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

public class StatusLineCLabelContribution extends ContributionItem {
	
	public final static int DEFAULT_CHAR_WIDTH = 40; 
	
	private int charWidth;
	private CLabel label;
	private Image image;
	private String text = ""; //$NON-NLS-1$
	private int widthHint = -1;
	private int heightHint = -1;
	
	private Listener listener;
	private int eventType;
	private String tooltip;
	
	public StatusLineCLabelContribution(String id, int charWidth) {
		super(id);
		this.charWidth = charWidth;
		setVisible(false); // no text to start with
	}
	
	public void fill(Composite parent) {	
		Label sep = new Label(parent, SWT.SEPARATOR);
		label = new CLabel(parent, SWT.SHADOW_NONE);
		StatusLineLayoutData statusLineLayoutData = new StatusLineLayoutData();
		
		if (widthHint < 0) {
			GC gc = new GC(parent);
			gc.setFont(parent.getFont());
			FontMetrics fm = gc.getFontMetrics();
			widthHint = fm.getAverageCharWidth() * charWidth;
			heightHint = fm.getHeight();
			gc.dispose();
		}
		
		statusLineLayoutData.widthHint = widthHint;
		label.setLayoutData(statusLineLayoutData);
		label.setText(text);
		label.setImage(image);
		if(listener != null) {
			label.addListener(eventType, listener);
		}
		if(tooltip != null) {
			label.setToolTipText(tooltip);
		}
		
		statusLineLayoutData = new StatusLineLayoutData();
		statusLineLayoutData.heightHint = heightHint;
		sep.setLayoutData(statusLineLayoutData);
	}
	
	public void addListener(int eventType, Listener listener) {
		this.eventType = eventType;
		this.listener = listener;
	}
	
	public void setText(String text) {
		if (text == null)
			throw new NullPointerException();
		
		this.text = text;
		
		if (label != null && !label.isDisposed())
			label.setText(this.text);
		
		if (this.text.length() == 0) {
			if (isVisible()) {
				setVisible(false);
				IContributionManager contributionManager = getParent();
				
				if (contributionManager != null)
					contributionManager.update(true);
			}
		} else {
			if (!isVisible()) {
				setVisible(true);
				IContributionManager contributionManager = getParent();
				
				if (contributionManager != null)
					contributionManager.update(true);
			}
		}
	}
	
	public void setTooltip(String tooltip) {
		if (tooltip == null)
			throw new NullPointerException();
		
		this.tooltip = tooltip;
		
		if (label != null && !label.isDisposed()) {
			label.setToolTipText(this.tooltip);	
		}
	}
	
	public void setImage(Image image) {
		if (image == null)
			throw new NullPointerException();
		
		this.image = image;
		
		if (label != null && !label.isDisposed())
			label.setImage(this.image);
		
		if (!isVisible()) {
			setVisible(true);
			IContributionManager contributionManager = getParent();
			
			if (contributionManager != null)
				contributionManager.update(true);
		}
	}
}

Back to the top