f531af01596d76b63f02c13f6f8551ea55ecb0d3
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / fileEditor / impl / FileEditorInfoPane.java
blobf531af01596d76b63f02c13f6f8551ea55ecb0d3
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.
17 package com.intellij.openapi.fileEditor.impl;
19 import com.intellij.ui.IdeBorderFactory;
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.ArrayList;
27 /**
28 * @author max
30 public class FileEditorInfoPane extends JPanel {
31 private int myCounter = 0;
32 private final JPanel myCards;
33 private final JButton myPrevButton;
34 private final JButton myNextButton;
35 private final java.util.List<JComponent> myComponents;
36 private final JPanel myButtonsPanel;
38 public FileEditorInfoPane() {
39 super(new BorderLayout());
40 final CardLayout layout = new CardLayout();
41 myCards = new JPanel(layout);
42 myComponents = new ArrayList<JComponent>();
43 add(myCards, BorderLayout.CENTER);
44 myPrevButton = new JButton("<");
45 myNextButton = new JButton(">");
47 myButtonsPanel = new JPanel(new GridLayout(1, 2));
48 myButtonsPanel.add(myPrevButton);
49 myButtonsPanel.add(myNextButton);
51 myPrevButton.addActionListener(new ActionListener() {
52 public void actionPerformed(ActionEvent e) {
53 layout.previous(myCards);
54 updateButtons();
56 });
58 myNextButton.addActionListener(new ActionListener() {
59 public void actionPerformed(ActionEvent e) {
60 layout.next(myCards);
61 updateButtons();
63 });
64 setBorder(IdeBorderFactory.createBorder());
66 add(myButtonsPanel, BorderLayout.EAST);
67 myButtonsPanel.setVisible(false);
68 setVisible(false);
71 public void addInfo(JComponent component) {
72 myComponents.add(component);
73 myCards.add(component, String.valueOf(myCounter++));
74 updateButtons();
75 validate();
78 public void removeInfo(JComponent component) {
79 myComponents.remove(component);
80 myCards.remove(component);
81 updateButtons();
82 validate();
85 private int getCurrentCard() {
86 for (int i = 0; i < myComponents.size(); i++) {
87 if (myComponents.get(i).isVisible()) return i;
89 return -1;
92 private void updateButtons() {
93 int count = myComponents.size();
94 if (count > 0) {
95 setVisible(true);
96 if (count == 1) {
97 myButtonsPanel.setVisible(false);
99 else {
100 myButtonsPanel.setVisible(true);
101 int currentCard = getCurrentCard();
102 myNextButton.setEnabled(currentCard + 1 < count);
103 myPrevButton.setEnabled(currentCard - 1 >= 0);
106 else {
107 setVisible(false);