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
;
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
) {
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
];
54 return result
.toString();
59 public final void setText(final String 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(){
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
){
90 final int widthLimit
= getSize().width
- 20;
92 if (getFontMetrics(getFont()).stringWidth(myText
) < widthLimit
) return;
94 final JLabel label
= new JLabel();
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) {