Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f44338a9cb4da99544222865534d257d87558707 (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.examples.xml.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;

/**
 * A message line. It distinguishs between "normal" messages and errors. 
 * Setting an error message hides a currently displayed message until 
 * <code>clearErrorMessage</code> is called.
 */
public class MessageLine extends CLabel {

	public static final RGB RED= new RGB(200, 0, 0);
	private static RGB fgDefaultErrorRGB= RED;

	private String fMessageText;
	private String fErrorText;

	private Color fDefaultColor;
	private RGB fErrorRGB;
	private Color fErrorColor;

	/*
	 * Creates a new message line as a child of the given parent.
	 * Error message will be shown in <code>RED</code>.
	 */
	public MessageLine(Composite parent) {
		this(parent, SWT.LEFT);
	}

	/*
	 * Creates a new message line as a child of the parent and with the given SWT stylebits.
	 * Error message will be shown in  <code>RED</code>.
	 */
	public MessageLine(Composite parent, int style) {
		super(parent, style);
		fDefaultColor= getForeground();
		fErrorRGB= fgDefaultErrorRGB;
	}

	/*
	 * Creates a new message line as a child of the parent and with the given SWT stylebits.
	 * Error message will be shown with in the given rgb color.
	 */
	public MessageLine(Composite parent, int style, RGB errorRGB) {
		super(parent, style);
		fDefaultColor= getForeground();
		fErrorRGB= errorRGB;
	}

	/**
	 * Clears the currently displayed error message and redisplayes
	 * the message which was active before the error message was set.
	 */
	public void clearErrorMessage() {
		setErrorMessage(null);
	}

	/**
	 * Clears the currently displayed message.
	 */
	public void clearMessage() {
		setMessage(null);
	}

	/**
	 * Get the currently displayed error text.
	 * @return The error message. If no error message is displayed <code>null</code> is returned.
	 */
	public String getErrorMessage() {
		return fErrorText;
	}

	/**
	 * Get the currently displayed message.
	 * @return The message. If no message is displayed <code>null</code> is returned.
	 */
	public String getMessage() {
		return fMessageText;
	}

	/*
	 * Sets the default error color used by all message lines.
	 * Note: a call to this method only affects newly created MessageLines not existing ones. 
	 */
	public static void setDefaultErrorColor(RGB color) {
		fgDefaultErrorRGB= color;
	}

	/*
	 * Display the given error message. A currently displayed message
	 * is saved and will be redisplayed when the error message is cleared.
	 */
	public void setErrorMessage(String message) {
		fErrorText= message;

		if (message == null) {
			setMessage(fMessageText);
		} else {
			if (fErrorColor == null) {
				fErrorColor= new Color(getDisplay(), fErrorRGB);
			}
			setForeground(fErrorColor);
			setText(message);
		}
	}

	/*
	 * Set the message text. If the message line currently displays an error,
	 * the message is stored and will be shown after a call to clearErrorMessage
	 */
	public void setMessage(String message) {
		fMessageText= message;
		if (message == null)
			message= ""; //$NON-NLS-1$
		if (fErrorText == null) {
			setForeground(fDefaultColor);
			setText(message);
		}
	}

}

Back to the top