cleanup
[fedora-idea.git] / platform / platform-api / src / com / intellij / ui / ErrorLabel.java
blob3f7df6fb4b60944756d5d70afdca3f5c91a005b3
1 /*
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;
18 import javax.swing.*;
19 import java.awt.*;
21 /**
22 * @author kir
24 public class ErrorLabel extends JLabel {
26 private boolean myUnderline;
28 private Color myForeground;
29 private String myTooltip;
31 public ErrorLabel() {
32 this(null, null);
35 public ErrorLabel(String text) {
36 this(text, null);
39 public ErrorLabel(String text, Icon icon) {
40 super(text, icon, JLabel.LEFT);
41 setOpaque(false);
44 public void setErrorText(String text, Color color) {
45 boolean newUnderline = text != null;
46 myForeground = color;
47 if (newUnderline) {
48 updateLabelView(newUnderline, text);
50 else if (myUnderline) {
51 updateLabelView(newUnderline, myTooltip);
55 public void setToolTipText(String text) {
56 if (myUnderline) {
57 myTooltip = text;
59 else {
60 super.setToolTipText(text);
66 private void updateLabelView(boolean newUnderline, String tooltip) {
67 super.setToolTipText(tooltip);
68 myUnderline = newUnderline;
69 repaint();
72 protected void paintComponent(Graphics g) {
74 super.paintComponent(g);
76 if (getText() != null & myUnderline) {
77 g.setColor(myForeground);
78 int x = 0;
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);
96 y += 2;
98 int width = c.getFontMetrics(c.getFont()).stringWidth(text);
99 int nLines = (width >> 1) + 1;
101 int xCurr = x;
102 int yBottom = y + 1;
103 int []xx = new int[nLines + 1];
104 int []yy = new int[nLines + 1];
105 int line = 0;
106 for (; line < nLines; line += 2) {
107 xx[line] = xCurr;
108 yy[line] = yBottom;
110 xx[line + 1] = xCurr + 2;
111 yy[line + 1] = yBottom - 2;
112 xCurr += 4;
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());