Commiting more changes to fix RSE issues with PHP projects.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / dialogs / MessageLine.java
1 package net.sourceforge.phpdt.internal.ui.dialogs;
2
3 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
4
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.custom.CLabel;
8 import org.eclipse.swt.graphics.Color;
9 import org.eclipse.swt.graphics.Image;
10 import org.eclipse.swt.graphics.RGB;
11 import org.eclipse.swt.widgets.Composite;
12
13 /**
14  * A message line displaying a status.
15  */
16 public class MessageLine extends CLabel {
17
18         private static final RGB ERROR_BACKGROUND_RGB = new RGB(230, 226, 221);
19
20         private Color fNormalMsgAreaBackground;
21
22         private Color fErrorMsgAreaBackground;
23
24         /**
25          * Creates a new message line as a child of the given parent.
26          */
27         public MessageLine(Composite parent) {
28                 this(parent, SWT.LEFT);
29         }
30
31         /**
32          * Creates a new message line as a child of the parent and with the given
33          * SWT stylebits.
34          */
35         public MessageLine(Composite parent, int style) {
36                 super(parent, style);
37                 fNormalMsgAreaBackground = getBackground();
38                 fErrorMsgAreaBackground = null;
39         }
40
41         private Image findImage(IStatus status) {
42                 if (status.isOK()) {
43                         return null;
44                 } else if (status.matches(IStatus.ERROR)) {
45                         return PHPUiImages.get(PHPUiImages.IMG_OBJS_ERROR);
46                 } else if (status.matches(IStatus.WARNING)) {
47                         return PHPUiImages.get(PHPUiImages.IMG_OBJS_WARNING);
48                 } else if (status.matches(IStatus.INFO)) {
49                         return PHPUiImages.get(PHPUiImages.IMG_OBJS_INFO);
50                 }
51                 return null;
52         }
53
54         /**
55          * Sets the message and image to the given status. <code>null</code> is a
56          * valid argument and will set the empty text and no image
57          */
58         public void setErrorStatus(IStatus status) {
59                 if (status != null) {
60                         String message = status.getMessage();
61                         if (message != null && message.length() > 0) {
62                                 setText(message);
63                                 setImage(findImage(status));
64                                 if (fErrorMsgAreaBackground == null) {
65                                         fErrorMsgAreaBackground = new Color(getDisplay(),
66                                                         ERROR_BACKGROUND_RGB);
67                                 }
68                                 setBackground(fErrorMsgAreaBackground);
69                                 return;
70                         }
71                 }
72                 setText("");
73                 setImage(null);
74                 setBackground(fNormalMsgAreaBackground);
75         }
76
77         /*
78          * @see Widget#dispose()
79          */
80         public void dispose() {
81                 if (fErrorMsgAreaBackground != null) {
82                         fErrorMsgAreaBackground.dispose();
83                         fErrorMsgAreaBackground = null;
84                 }
85                 super.dispose();
86         }
87 }