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 com
.intellij
.util
.text
.StringTokenizer
;
19 import com
.intellij
.util
.ui
.UIUtil
;
25 * @author Vladimir Kondratyev
27 public class HorizontalLabeledIcon
implements Icon
{
28 private final Icon myIcon
;
29 private final String
[] myStrings
;
30 private final String myMnemonic
;
33 * @param icon not <code>null</code> icon.
34 * @param text to be painted under the <code>icon<code>. This parameter can
35 * be <code>null</code> if text isn't specified. In that case <code>LabeledIcon</code>
37 public HorizontalLabeledIcon(Icon icon
, String text
, String mnemonic
) {
40 StringTokenizer tokenizer
= new StringTokenizer(text
, "\n");
41 myStrings
= new String
[tokenizer
.countTokens()];
42 for (int i
= 0; tokenizer
.hasMoreTokens(); i
++) {
43 myStrings
[i
] = tokenizer
.nextToken();
49 myMnemonic
= mnemonic
;
52 public int getIconHeight() {
53 return Math
.max(myIcon
.getIconHeight(), getTextHeight());
56 public int getIconWidth() {
57 return myIcon
.getIconWidth() + getTextWidth() + 5;
60 private int getTextHeight() {
61 if (myStrings
!= null) {
62 Font font
= UIUtil
.getLabelFont();
63 FontMetrics fontMetrics
= Toolkit
.getDefaultToolkit().getFontMetrics(font
);
64 return fontMetrics
.getHeight() * myStrings
.length
;
71 private int getTextWidth() {
72 if (myStrings
!= null) {
74 Font font
= UIUtil
.getLabelFont();
75 FontMetrics fontMetrics
= Toolkit
.getDefaultToolkit().getFontMetrics(font
);
76 for (int i
= 0; i
< myStrings
.length
; i
++) {
77 String string
= myStrings
[i
];
78 if (myMnemonic
!= null && i
== myStrings
.length
-1) {
79 string
+= " "+myMnemonic
;
81 width
= Math
.max(width
, fontMetrics
.stringWidth(string
));
91 public void paintIcon(Component c
, Graphics g
, int x
, int y
) {
93 int height
= getIconHeight();
94 int iconHeight
= myIcon
.getIconHeight();
95 if (height
> iconHeight
) {
96 myIcon
.paintIcon(c
, g
, x
, y
+ (height
- iconHeight
) / 2);
99 myIcon
.paintIcon(c
, g
, x
, y
);
103 if (myStrings
!= null) {
104 Font font
= UIUtil
.getLabelFont();
105 FontMetrics fontMetrics
= Toolkit
.getDefaultToolkit().getFontMetrics(font
);
106 g
.setFont(fontMetrics
.getFont());
107 g
.setColor(UIUtil
.getLabelForeground());
109 x
+= myIcon
.getIconWidth() + 5;
110 y
+= (height
- getTextHeight()) / 2 + fontMetrics
.getHeight() - fontMetrics
.getDescent();
111 for (int i
= 0; i
< myStrings
.length
; i
++) {
112 String string
= myStrings
[i
];
113 g
.drawString(string
, x
, y
);
114 y
+= fontMetrics
.getHeight();
116 if (myMnemonic
!= null) {
117 g
.setColor(UIUtil
.getTextInactiveTextColor());
118 int offset
= fontMetrics
.stringWidth(myStrings
[myStrings
.length
-1]+" ");
119 y
-= fontMetrics
.getHeight();
120 g
.drawString(myMnemonic
, x
+ offset
, y
);