2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.ui
;
24 public class ErrorLabel
extends JLabel
{
26 private boolean myUnderline
;
28 private Color myForeground
;
29 private String myTooltip
;
35 public ErrorLabel(String text
) {
39 public ErrorLabel(String text
, Icon icon
) {
40 super(text
, icon
, JLabel
.LEFT
);
44 public void setErrorText(String text
, Color color
) {
45 boolean newUnderline
= text
!= null;
48 updateLabelView(newUnderline
, text
);
50 else if (myUnderline
) {
51 updateLabelView(newUnderline
, myTooltip
);
55 public void setToolTipText(String text
) {
60 super.setToolTipText(text
);
66 private void updateLabelView(boolean newUnderline
, String tooltip
) {
67 super.setToolTipText(tooltip
);
68 myUnderline
= newUnderline
;
72 protected void paintComponent(Graphics g
) {
74 super.paintComponent(g
);
76 if (getText() != null & myUnderline
) {
77 g
.setColor(myForeground
);
80 if (getIcon() != null) {
81 x
= getIcon().getIconWidth() + getIconTextGap();
84 if (getHorizontalAlignment() == CENTER
) {
85 int w
= g
.getFontMetrics().stringWidth(getText());
86 x
+= (getWidth() - x
- w
) >> 1;
89 drawWave(this, g
, x
, getText());
93 public static void drawWave(Component c
, Graphics g
, int x
, String text
) {
94 int y
= getTextBaseLine(c
);
98 int width
= c
.getFontMetrics(c
.getFont()).stringWidth(text
);
99 int nLines
= (width
>> 1) + 1;
103 int []xx
= new int[nLines
+ 1];
104 int []yy
= new int[nLines
+ 1];
106 for (; line
< nLines
; line
+= 2) {
110 xx
[line
+ 1] = xCurr
+ 2;
111 yy
[line
+ 1] = yBottom
- 2;
115 g
.drawPolyline(xx
, yy
, line
);
118 private static int getTextBaseLine(Component c
) {
119 FontMetrics fm
= c
.getFontMetrics(c
.getFont());
120 return (c
.getHeight() >> 1) + ((fm
.getHeight() >> 1) - fm
.getDescent());