cleanup
[fedora-idea.git] / platform / platform-api / src / com / intellij / ui / SeparatorWithText.java
blob5fa975b445101bda9b25054370169c0488b54b1b
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 org.jetbrains.annotations.NonNls;
20 import javax.swing.*;
21 import java.awt.*;
23 public class SeparatorWithText extends JComponent {
25 private static final int VGAP = 3;
26 private static final int HGAP = 3;
28 private String myCaption = "";
29 private int myPrefWidth;
31 public SeparatorWithText() {
32 setBorder(BorderFactory.createEmptyBorder(VGAP, 0, VGAP, 0));
33 @NonNls final String labelFont = "Label.font";
34 setFont(UIManager.getFont(labelFont));
35 setFont(getFont().deriveFont(Font.BOLD));
38 public Dimension getPreferredSize() {
39 final Dimension size = getPreferredFontSize();
40 size.width = myPrefWidth == -1? size.width : myPrefWidth;
41 return size;
44 public Dimension getPreferredFontSize() {
45 if (hasCaption()) {
46 FontMetrics fm = getFontMetrics(getFont());
47 int preferredHeight = fm.getHeight();
48 int preferredWidth = getPreferredWidth(fm);
50 return new Dimension(preferredWidth, preferredHeight + VGAP * 2);
53 return new Dimension(0, VGAP * 2 + 1);
56 private int getPreferredWidth(FontMetrics fm) {
57 return fm.stringWidth(myCaption) + 2 * HGAP;
60 private boolean hasCaption() {
61 return myCaption != null && !"".equals(myCaption.trim());
64 public Dimension getMinimumSize() {
65 return getPreferredSize();
68 public void setMinimumWidth(int width) {
69 myPrefWidth = width;
72 protected void paintComponent(Graphics g) {
73 g.setColor(GroupedElementsRenderer.POPUP_SEPARATOR_FOREGROUND);
75 if (hasCaption()) {
76 FontMetrics fm = getFontMetrics(getFont());
77 int baseline = VGAP + fm.getAscent();
79 final int fontWidth = getPreferredFontSize().width;
80 final int lineX = (getWidth() - fontWidth) / 2;
81 final int lineY = VGAP + fm.getHeight() / 2;
83 g.drawLine(0, lineY, lineX, lineY);
84 g.drawString(myCaption, lineX + HGAP, baseline);
85 g.drawLine(lineX + fontWidth, lineY, getWidth() - 1, lineY);
87 else {
88 g.drawLine(0, VGAP, getWidth() - 1, VGAP);
92 public void setCaption(String captionAboveOf) {
93 myCaption = captionAboveOf;