001: /*
002: * @(#)ButtonPanel.java
003: *
004: * Copyright 2002-2003 JIDE Software. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import javax.swing.*;
009: import java.awt.*;
010:
011: /**
012: * ButtonPanel can have a collection of buttons. And it will make all buttons to
013: * have the same size to make them looks better.
014: *
015: * @deprecated replaced by ButtonPanel under com.jidesoft.dialog.
016: */
017:
018: public class ButtonPanel extends JPanel {
019:
020: /**
021: * The default horizontal spacing.
022: */
023: public static final int DEFAULT_SPACING = 4;
024:
025: /**
026: * The actual panel that have those buttons.
027: */
028: private JPanel _buttons;
029:
030: /**
031: * Constructs a new <code>ButtonPanel</code> with the default horizontal
032: * spacing and right alignment.
033: */
034:
035: public ButtonPanel() {
036: this (SwingConstants.RIGHT, DEFAULT_SPACING);
037: }
038:
039: /**
040: * Constructs a new <code>ButtonPanel</code> with default horizontal
041: * spacing and the given alignment.
042: *
043: * @param alignment the alignment of the buttons. It can be one of <code>SwingConstants.LEFT</code> or
044: * <code>SwingConstants.RIGHT</code> or <code>SwingConstants.TOP</code> or
045: * <code>SwingConstants.BOTTOM</code>.
046: */
047:
048: public ButtonPanel(int alignment) {
049: this (alignment, DEFAULT_SPACING);
050: }
051:
052: /**
053: * Constructs a new <code>ButtonPanel</code> with the given horizontal
054: * spacing and alignment.
055: *
056: * @param spacing The gridSize of the gap (in pixels) to place between buttons
057: * horizontally.
058: * @param alignment the alignment of the buttons. It can be one of <code>SwingConstants.LEFT</code> or
059: * <code>SwingConstants.RIGHT</code> or <code>SwingConstants.TOP</code> or
060: * <code>SwingConstants.BOTTOM</code>.
061: */
062:
063: public ButtonPanel(int alignment, int spacing) {
064: if (alignment != SwingConstants.LEFT
065: && alignment != SwingConstants.RIGHT
066: && alignment != SwingConstants.TOP
067: && alignment != SwingConstants.BOTTOM) {
068: throw new IllegalArgumentException("Invalid alignment");
069: }
070:
071: setLayout(new BorderLayout());
072:
073: _buttons = new NullPanel();
074:
075: if (alignment == SwingConstants.TOP
076: || alignment == SwingConstants.BOTTOM)
077: _buttons.setLayout(new GridLayout(0, 1, 0, spacing));
078: else
079: _buttons.setLayout(new GridLayout(1, 0, spacing, 0));
080:
081: switch (alignment) {
082: case SwingConstants.LEFT:
083: add(_buttons, BorderLayout.WEST);
084: break;
085: case SwingConstants.RIGHT:
086: add(_buttons, BorderLayout.EAST);
087: break;
088: case SwingConstants.TOP:
089: add(_buttons, BorderLayout.NORTH);
090: break;
091: case SwingConstants.BOTTOM:
092: add(_buttons, BorderLayout.SOUTH);
093: break;
094: }
095: }
096:
097: /**
098: * Adds a button to <code>ButtonPanel</code>.
099: *
100: * @param button button to be added.
101: */
102:
103: public void addButton(AbstractButton button) {
104: if (button.getParent() != _buttons) {
105: _buttons.add(button);
106: }
107: }
108:
109: /**
110: * Adds a button to <code>ButtonPanel</code> at the specified position.
111: *
112: * @param button button to add.
113: * @param pos position at which to add the button. The value 0 denotes
114: * the first position, and -1 denotes the last position.
115: * @throws IllegalArgumentException If the value of
116: * <code>pos</code> is invalid.
117: */
118:
119: public void addButton(AbstractButton button, int pos)
120: throws IllegalArgumentException {
121: if (button.getParent() != _buttons) {
122: _buttons.add(button, pos);
123: }
124: }
125:
126: /**
127: * Removes a button from the <code>ButtonPanel</code>.
128: *
129: * @param button button to remove.
130: */
131:
132: public void removeButton(AbstractButton button) {
133: if (button.getParent() == _buttons) {
134: _buttons.remove(button);
135: }
136: }
137:
138: /**
139: * Removes a button from the <code>ButtonPanel</code> at the specified position.
140: *
141: * @param position position of the button to remove, where 0 denotes the
142: * first position.
143: */
144:
145: public void removeButton(int position) {
146: _buttons.remove(position);
147: }
148:
149: /**
150: * Gets the button at the specified position in the
151: * <code>ButtonPanel</code>.
152: *
153: * @param position position of the button.
154: * @return button at the specified position.
155: */
156:
157: public AbstractButton getButton(int position) {
158: return ((AbstractButton) _buttons.getComponent(position));
159: }
160:
161: /**
162: * Gets the number of buttons in this <code>ButtonPanel</code>.
163: *
164: * @return the number of buttons.
165: */
166:
167: public int getButtonCount() {
168: return (_buttons.getComponentCount());
169: }
170:
171: /**
172: * Gets the actual panel that has the buttons. We expose this method so
173: * that user can customize the panel such as the background. Please don't
174: * try to add non-button component to it.
175: *
176: * @return the actual button panel.
177: */
178: public JPanel getButtons() {
179: return _buttons;
180: }
181: }
|