sticky documentation popup [take 1]
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / wm / impl / status / TextPanel.java
blobd550eda90291cd59460ac02fc60ecdf9dd763c9b
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.openapi.wm.impl.status;
18 import com.intellij.codeInsight.hint.HintUtil;
19 import com.intellij.openapi.ui.MultiLineLabelUI;
20 import com.intellij.ui.LightweightHint;
21 import com.intellij.ui.SplittingUtil;
23 import javax.swing.*;
24 import java.awt.*;
25 import java.awt.event.MouseAdapter;
26 import java.awt.event.MouseEvent;
28 public class TextPanel extends JLabel {
29 private String myText;
30 private final String[] myPossibleStrings;
32 public TextPanel(final boolean shouldShowTooltip, final String... possibleStrings) {
33 myText = "";
34 myPossibleStrings = possibleStrings;
36 if (shouldShowTooltip) {
37 addMouseListener(new MyMouseListener());
41 private static String splitText(final JLabel label, final String text, final int widthLimit){
42 final FontMetrics fontMetrics = label.getFontMetrics(label.getFont());
44 final String[] lines = SplittingUtil.splitText(text, fontMetrics, widthLimit, ' ');
46 final StringBuilder result = new StringBuilder();
47 for (int i = 0; i < lines.length; i++) {
48 final String line = lines[i];
49 if (i > 0) {
50 result.append('\n');
52 result.append(line);
54 return result.toString();
59 public final void setText(final String text) {
60 super.setText(text);
61 myText = text;
64 public void setDisplayedMnemonic(int key) {
65 // status bar may not have mnemonics
68 public void setDisplayedMnemonicIndex(int index) throws IllegalArgumentException {
69 // status bar may not have mnemonics
70 super.setDisplayedMnemonicIndex(-1);
73 public final Dimension getPreferredSize(){
74 int max = 0;
75 for (String possibleString : myPossibleStrings) {
76 max = Math.max(max, getFontMetrics(getFont()).stringWidth(possibleString));
78 return new Dimension(20 + max, getMinimumSize().height);
81 private final class MyMouseListener extends MouseAdapter {
82 private LightweightHint myHint;
84 public void mouseEntered(final MouseEvent e){
85 if (myHint != null) {
86 myHint.hide();
87 myHint = null;
90 final int widthLimit = getSize().width - 20;
92 if (getFontMetrics(getFont()).stringWidth(myText) < widthLimit) return;
94 final JLabel label = new JLabel();
95 label.setBorder(
96 BorderFactory.createCompoundBorder(
97 BorderFactory.createLineBorder(Color.black),
98 BorderFactory.createEmptyBorder(0,5,0,5)
101 label.setForeground(Color.black);
102 label.setBackground(HintUtil.INFORMATION_COLOR);
103 label.setOpaque(true);
104 label.setUI(new MultiLineLabelUI());
106 final JLayeredPane layeredPane = getRootPane().getLayeredPane();
109 label.setText(splitText(label, myText, layeredPane.getWidth() - 10));
111 final Point p = SwingUtilities.convertPoint(TextPanel.this, 1, getHeight() - 1, layeredPane);
112 p.y -= label.getPreferredSize().height;
114 myHint = new LightweightHint(label);
115 myHint.show(layeredPane, p.x, p.y, null);
118 public void mouseExited(final MouseEvent e){
119 if (myHint != null) {
120 myHint.hide();
121 myHint = null;