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.diff.ex;
18 import com.intellij.openapi.editor.colors.EditorColorsManager;
19 import com.intellij.openapi.editor.colors.EditorColorsScheme;
20 import com.intellij.ui.IdeBorderFactory;
21 import com.intellij.util.ui.UIUtil;
22 import org.jetbrains.annotations.Nullable;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
33 public class DiffStatusBar extends JPanel {
34 private final Collection<JComponent> myLabels = new ArrayList<JComponent>();
36 private final JLabel myTextLabel = new JLabel("");
37 private static final int COMP_HEIGHT = 30;
38 private EditorColorsScheme myColorScheme = null;
40 public <T extends LegendTypeDescriptor> DiffStatusBar(List<T> types) {
41 for (T differenceType : types) {
42 addDiffType(differenceType);
47 private void addDiffType(final LegendTypeDescriptor diffType){
48 addComponent(diffType);
51 private void addComponent(final LegendTypeDescriptor diffType) {
52 JComponent component = new JPanel() {
53 public void paint(Graphics g) {
54 setBackground(UIUtil.getPanelBackground());
56 FontMetrics metrics = getFontMetrics(getFont());
58 EditorColorsScheme colorScheme = myColorScheme != null
60 : EditorColorsManager.getInstance().getGlobalScheme();
61 g.setColor(diffType.getLegendColor(colorScheme));
62 g.fill3DRect(10, (getHeight() - 10) / 2, 35, 10, true);
64 Font font = g.getFont();
65 if (font.getStyle() != Font.PLAIN) {
66 font = font.deriveFont(Font.PLAIN);
69 g.setColor(UIUtil.getLabelForeground());
70 int textBaseline = (getHeight() - metrics.getHeight()) / 2 + metrics.getAscent();
71 g.drawString(diffType.getDisplayName(), 67, textBaseline);
75 public Dimension getPreferredSize() {
76 FontMetrics metrics = getFontMetrics(getFont());
77 return new Dimension((int)(70 + metrics.getStringBounds(diffType.getDisplayName(), getGraphics()).getWidth()), COMP_HEIGHT);
81 public Dimension getMinimumSize() {
82 return getPreferredSize();
85 myLabels.add(component);
88 public Dimension getMinimumSize() {
89 Dimension p = super.getPreferredSize();
90 Dimension m = super.getMinimumSize();
91 return new Dimension(m.width, p.height);
94 public Dimension getMaximumSize() {
95 Dimension p = super.getPreferredSize();
96 Dimension m = super.getMaximumSize();
97 return new Dimension(m.width, p.height);
100 public void setText(String text) {
101 myTextLabel.setText(text);
104 private void initGui() {
105 JComponent filler = new JComponent() {
107 public Dimension getPreferredSize() {
108 return myTextLabel.getPreferredSize();
111 setLayout(new BorderLayout());
112 setBorder(BorderFactory.createCompoundBorder(IdeBorderFactory.createBorder(),
113 BorderFactory.createEmptyBorder(3, 20, 3, 20)));
115 add(myTextLabel, BorderLayout.WEST);
116 Box box = Box.createHorizontalBox();
117 box.add(Box.createHorizontalGlue());
118 JPanel panel = new JPanel(new GridLayout(1, myLabels.size(), 0, 0));
119 for (final JComponent myLabel : myLabels) {
122 panel.setMaximumSize(panel.getPreferredSize());
124 box.add(Box.createHorizontalGlue());
125 add(box, BorderLayout.CENTER);
127 add(filler, BorderLayout.EAST);
130 public void setColorScheme(EditorColorsScheme colorScheme) {
131 EditorColorsScheme oldScheme = myColorScheme;
132 myColorScheme = colorScheme;
133 if (oldScheme != colorScheme) repaint();
136 public interface LegendTypeDescriptor {
137 String getDisplayName();
139 Color getLegendColor(EditorColorsScheme colorScheme);