KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > parts > MessageLine


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.parts;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.pde.internal.ui.PDELabelProvider;
15 import org.eclipse.pde.internal.ui.PDEPlugin;
16 import org.eclipse.pde.internal.ui.PDEPluginImages;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.CLabel;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.RGB;
22 import org.eclipse.swt.widgets.Composite;
23
24 /**
25  * A message line displaying a status.
26  */

27 public class MessageLine extends CLabel {
28     
29     private static final RGB ERROR_BACKGROUND_RGB = new RGB(230, 226, 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     
51     private Image findImage(IStatus status) {
52         PDELabelProvider provider = PDEPlugin.getDefault().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 JavaDoc 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(), ERROR_BACKGROUND_RGB);
77                 }
78                 setBackground(fErrorMsgAreaBackground);
79                 return;
80             }
81         }
82         setText(""); //$NON-NLS-1$
83
setImage(null);
84         setBackground(fNormalMsgAreaBackground);
85     }
86     
87     /*
88      * @see Widget#dispose()
89      */

90     public void dispose() {
91         if (fErrorMsgAreaBackground != null) {
92             fErrorMsgAreaBackground.dispose();
93             fErrorMsgAreaBackground= null;
94         }
95         super.dispose();
96     }
97 }
98
99
Popular Tags