changedUpdate exception
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ui / DialogButtonGroup.java
bloba67de6cad3d5d44adcbdd3208e7c6c78dd0a6a5f
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 javax.swing.*;
19 import java.awt.*;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.KeyEvent;
23 public class DialogButtonGroup extends JPanel {
24 public static final int TOP = 1;
25 public static final int BOTTOM = 2;
26 private int myPreferredH = 0;
27 private int myPreferredW = 0;
29 public DialogButtonGroup() {
30 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
31 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
32 registerKeyboardAction(new AbstractAction() {
33 public void actionPerformed(ActionEvent e) {
34 upPressed();
37 KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),
38 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
39 registerKeyboardAction(new AbstractAction() {
40 public void actionPerformed(ActionEvent e) {
41 downPressed();
44 KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
45 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
48 public void addButton(AbstractButton button) {
49 addButton(button, BOTTOM);
52 public void addButton(AbstractButton button, int position) {
53 if (TOP == position) {
54 add(button, 0);
55 add(Box.createVerticalStrut(5), 1);
57 else {
58 add(Box.createVerticalStrut(5));
59 add(button);
61 Dimension prefSize = button.getPreferredSize();
62 if (prefSize.height > myPreferredH) {
63 myPreferredH = prefSize.height;
65 if (prefSize.width > myPreferredW) {
66 myPreferredW = prefSize.width;
68 updateButtonSizes();
71 public void remove(AbstractButton button) {
72 super.remove(button);
73 updateButtonSizes();
76 public void grabFocus() {
77 ((JComponent)getComponent(0)).grabFocus();
80 private void updateButtonSizes() {
81 Dimension dim = new Dimension(myPreferredW, myPreferredH);
82 Component[] components = getComponents();
83 if (components == null) return;
84 for (int i = 0; i < components.length; i++) {
85 if (components[i] instanceof AbstractButton) {
86 AbstractButton button = (AbstractButton)components[i];
87 button.setPreferredSize(dim);
88 button.setMaximumSize(dim);
89 button.setMinimumSize(dim);
94 private void upPressed() {
95 Component[] components = getComponents();
96 for (int i = 0; i < components.length; i++) {
97 if (components[i].hasFocus()) {
98 if (i == 0) {
99 components[components.length - 1].requestFocus();
100 return;
102 components[i - 1].requestFocus();
103 return;
108 private void downPressed() {
109 Component[] components = getComponents();
110 for (int i = 0; i < components.length; i++) {
111 if (components[i].hasFocus()) {
112 if (i == components.length - 1) {
113 components[0].requestFocus();
114 return;
116 components[i + 1].requestFocus();
117 return;