01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.parts;
11:
12: import org.eclipse.core.runtime.IStatus;
13: import org.eclipse.pde.internal.ui.PDELabelProvider;
14: import org.eclipse.pde.internal.ui.PDEPlugin;
15: import org.eclipse.pde.internal.ui.PDEPluginImages;
16: import org.eclipse.swt.SWT;
17: import org.eclipse.swt.custom.CLabel;
18: import org.eclipse.swt.graphics.Color;
19: import org.eclipse.swt.graphics.Image;
20: import org.eclipse.swt.graphics.RGB;
21: import org.eclipse.swt.widgets.Composite;
22:
23: /**
24: * A message line displaying a status.
25: */
26: public class MessageLine extends CLabel {
27:
28: private static final RGB ERROR_BACKGROUND_RGB = new RGB(230, 226,
29: 221);
30:
31: private Color fNormalMsgAreaBackground;
32: private Color fErrorMsgAreaBackground;
33:
34: /**
35: * Creates a new message line as a child of the given parent.
36: */
37: public MessageLine(Composite parent) {
38: this (parent, SWT.LEFT);
39: }
40:
41: /**
42: * Creates a new message line as a child of the parent and with the given SWT stylebits.
43: */
44: public MessageLine(Composite parent, int style) {
45: super (parent, style);
46: fNormalMsgAreaBackground = getBackground();
47: fErrorMsgAreaBackground = null;
48: }
49:
50: private Image findImage(IStatus status) {
51: PDELabelProvider provider = PDEPlugin.getDefault()
52: .getLabelProvider();
53: if (status.isOK()) {
54: return null;
55: } else if (status.matches(IStatus.ERROR)) {
56: return provider.get(PDEPluginImages.DESC_ERROR_ST_OBJ);
57: } else if (status.matches(IStatus.WARNING)) {
58: return provider.get(PDEPluginImages.DESC_WARNING_ST_OBJ);
59: } else if (status.matches(IStatus.INFO)) {
60: return provider.get(PDEPluginImages.DESC_INFO_ST_OBJ);
61: }
62: return null;
63: }
64:
65: /**
66: * Sets the message and image to the given status.
67: * <code>null</code> is a valid argument and will set the empty text and no image
68: */
69: public void setErrorStatus(IStatus status) {
70: if (status != null) {
71: String message = status.getMessage();
72: if (message != null && message.length() > 0) {
73: setText(message);
74: setImage(findImage(status));
75: if (fErrorMsgAreaBackground == null) {
76: fErrorMsgAreaBackground = new Color(getDisplay(),
77: ERROR_BACKGROUND_RGB);
78: }
79: setBackground(fErrorMsgAreaBackground);
80: return;
81: }
82: }
83: setText(""); //$NON-NLS-1$
84: setImage(null);
85: setBackground(fNormalMsgAreaBackground);
86: }
87:
88: /*
89: * @see Widget#dispose()
90: */
91: public void dispose() {
92: if (fErrorMsgAreaBackground != null) {
93: fErrorMsgAreaBackground.dispose();
94: fErrorMsgAreaBackground = null;
95: }
96: super.dispose();
97: }
98: }
|