KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > MessageLine


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.preferences;
12
13
14 import org.eclipse.core.runtime.IStatus;
15
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 import org.eclipse.ui.ISharedImages;
23 import org.eclipse.ui.PlatformUI;
24
25 /**
26  * A message line displaying a status.
27  */

28 public class MessageLine extends CLabel {
29
30     private static final RGB ERROR_BACKGROUND_RGB = new RGB(230, 226, 221);
31
32     private Color fNormalMsgAreaBackground;
33     private Color fErrorMsgAreaBackground;
34
35     /**
36      * Creates a new message line as a child of the given parent.
37      */

38     public MessageLine(Composite parent) {
39         this(parent, SWT.LEFT);
40     }
41
42     /**
43      * Creates a new message line as a child of the parent and with the given SWT stylebits.
44      */

45     public MessageLine(Composite parent, int style) {
46         super(parent, style);
47         fNormalMsgAreaBackground= getBackground();
48         fErrorMsgAreaBackground= null;
49     }
50
51
52     private Image findImage(IStatus status) {
53         if (status.isOK()) {
54             return null;
55         } else if (status.matches(IStatus.ERROR)) {
56             return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
57         } else if (status.matches(IStatus.WARNING)) {
58             return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_WARN_TSK);
59         } else if (status.matches(IStatus.INFO)) {
60             return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_INFO_TSK);
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
Popular Tags