1
/** BEGIN COPYRIGHT BLOCK
2
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3
* Copyright (C) 2005 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; version 2
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
* END COPYRIGHT BLOCK **/
21
package com.netscape.management.admserv.config;
24
import java.awt.event.*;
25
import java.util.Vector;
27
import javax.swing.border.*;
28
import com.netscape.management.client.util.*;
29
import com.netscape.management.nmclf.SuiLookAndFeel;
33
* @version 0.1 11/28/97
34
* @author miodrag@netscape.com
38
* Create a command button bar. The main purpose of this class is to privide consistent
39
* look for buutons. By default, button bar has OK, Cancel and Help buttons. Inside
40
* a dialog, Close button is also added. The button bar can have horizonal or vertical
41
* orientation. A reference to a button can be obtained by it's command string. Action
42
* Listener on the ButtonBar receives action events triggered by any of the buttons.
43
* The particular button command string should be used to differenciate the buttons.
45
public class ButtonBar extends JPanel {
47
static ResourceSet _resource;
48
static public String _i18nOKButton, _i18nCancelButton,
49
_i18nCloseButton, _i18nHelpButton;
50
static public String _i18nSaveButton, _i18nResetButton,
51
_i18nSaveToolTip, _i18nResetToolTip, _i18nHelpToolTip;
54
_resource = new ResourceSet("com.netscape.management.admserv.config.config");
55
_i18nOKButton = _resource.getString("common", "OKButton");
56
_i18nCancelButton = _resource.getString("common", "CancelButton");
57
_i18nCloseButton = _resource.getString("common", "CloseButton");
58
_i18nSaveButton = _resource.getString("common", "SaveButton");
59
_i18nResetButton = _resource.getString("common", "ResetButton");
60
_i18nHelpButton = _resource.getString("common", "HelpButton");
61
_i18nSaveToolTip = _resource.getString("common", "SaveToolTip");
62
_i18nResetToolTip = _resource.getString("common", "ResetToolTip");
63
_i18nHelpToolTip = _resource.getString("common", "HelpToolTip");
67
* Standard Command Buttons
69
public static final String cmdOK = "ok", cmdCancel = "cancel",
70
cmdClose = "close", cmdHelp = "help";
72
protected Vector _buttons = new Vector(5);
74
// Standard panel commands buttons OK, Cancel, Help
75
static private String[] stdCommands = { cmdOK, cmdCancel, null,
77
static private String[] stdLabels = { _i18nSaveButton,
78
_i18nResetButton, null, _i18nHelpButton };
80
static private String[] stdToolTips = { _i18nSaveToolTip,
81
_i18nResetToolTip, null, _i18nHelpToolTip};
83
// Standard panel command buttons when running inside a dialog. All standard + Close button
84
static private String[] stdDialogCommands = { cmdOK, cmdCancel,
86
static private String[] stdDialogLabels = { _i18nOKButton,
87
_i18nCancelButton, null, _i18nHelpButton };
90
* No-Arg constructor, create default button bar, horizontal orientation
93
this(stdCommands, stdLabels, true);
97
* Construct default button bar for dailog panel, horizontal orientation
98
* @param isDialog A flag whether the panel that create ButtonBar runs inside a dialog
100
public ButtonBar(boolean isDialog) {
101
this(isDialog ? stdDialogCommands : stdCommands,
102
isDialog ? stdDialogLabels : stdLabels, true);
106
* Construct default button bar for dailog panel, horizontal orientation
107
* @param isDialog A flag whether the panel that create ButtonBar runs inside a dialog
108
* @param toolTips Customized tool tips
110
public ButtonBar(boolean isDialog, String[] toolTips) {
111
this(isDialog ? stdDialogCommands : stdCommands,
112
isDialog ? stdDialogLabels : stdLabels, toolTips, true);
116
* The generic button bar constrator. Enabels for constructon of custom button bars.
117
* <i>null</i> as a command/label has a special meaning: Start a new button group. In
118
* that case, a bigger spacing is aplied to the next button's left (horizontal bar) or
119
* top (vertical bar) inset in accordance with the Kingpin L&F style guide
120
* @param commands Array of command string for the buttons
121
* @param labels Array of labels for the buttons
122
* @param isHorizontal A flag whether the button bar has horzontal orientation
123
* @exception IllegalArgumentException Length of <i>commands</i> and <i>lebels</i>
124
* arrays are not the same
126
public ButtonBar(String[] commands, String[] labels, boolean isHorizontal) {
127
this(commands, labels, stdToolTips, isHorizontal);
131
* The generic button bar constrator. Enabels for constructon of custom button bars.
132
* <i>null</i> as a command/label has a special meaning: Start a new button group. In
133
* that case, a bigger spacing is aplied to the next button's left (horizontal bar) or
134
* top (vertical bar) inset in accordance with the Kingpin L&F style guide
135
* @param commands Array of command string for the buttons
136
* @param labels Array of labels for the buttons
137
* @param toolTips Array of tool tips for the buttons
138
* @param isHorizontal A flag whether the button bar has horzontal orientation
139
* @exception IllegalArgumentException Length of <i>commands</i> and <i>labels</i>
140
* arrays are not the same
142
public ButtonBar(String[] commands, String[] labels, String[] toolTips, boolean isHorizontal) {
145
JPanel p = new JPanel();
148
if ((commands.length != labels.length)
149
&& (commands.length != toolTips.length)) {
150
throw new IllegalArgumentException(
151
"In Constructor ButtonBar(): commands.length=" +
152
commands.length + ",labels.length=" + labels.length
153
+ ",toolTips.length=" + toolTips.length);
156
setLayout(new GridBagLayout());
157
p.setLayout (new GridBagLayout());
159
int gridx = 0, gridy = 0, insetTop = 0, insetLeft = 0,
160
insetBottom = 0, insetRight = 0;
161
boolean newButtonGroup = false;
163
JButton[] factoryButtons = JButtonFactory.create(labels);
165
for (int i = 0; i < labels.length; i++) {
167
if (labels[i] == null) {
168
newButtonGroup = true;
172
_buttons.addElement(button = factoryButtons[i]);
173
button.setActionCommand(commands[i]);
174
JButtonFactory.initializeMnemonic(button);
175
button.setToolTipText(toolTips[i]);
178
* Apply the proper button spacing. If start of a new button group
179
* use SEPARATED_COMPONENT_SPACE
181
insetTop = insetLeft = insetBottom = insetRight = 0;
184
if (newButtonGroup) {
186
SuiLookAndFeel.SEPARATED_COMPONENT_SPACE;
188
insetLeft = SuiLookAndFeel.COMPONENT_SPACE;
193
if (newButtonGroup) {
194
insetTop = SuiLookAndFeel.SEPARATED_COMPONENT_SPACE;
196
insetTop = SuiLookAndFeel.COMPONENT_SPACE;
201
gbc.setInsets(insetTop, insetLeft, insetBottom, insetRight);
202
gbc.setGrid(gridx, gridy, 1, 1);
203
gbc.setSpace(1.0, 0.0, /*anchor=*/GBC.CENTER,
212
newButtonGroup = false;
216
* Add the buttons to the panel right left conner
218
gbc.setInsets(0, 0, 0, 0);
219
gbc.setGrid(0, 0, 1, 1);
220
gbc.setSpace(1.0, 0.0, /*anchor=*/GBC.EAST, /*fill=*/GBC.NONE);
225
* Get a button with the command string <i>command</i>
227
public JButton getButton(String command) {
228
for (int i = 0; i < _buttons.size(); i++) {
229
JButton button = (JButton)_buttons.elementAt(i);
230
if (button.getActionCommand().equals(command)) {
238
* Add listener too all buttons
240
public void addActionListener(ActionListener listener) {
241
for (int i = 0; i < _buttons.size(); i++) {
242
JButton button = (JButton)_buttons.elementAt(i);
243
button.addActionListener(listener);
248
* Remove action listener on all buttons
250
public void removeActionListener(ActionListener listener) {
251
for (int i = 0; i < _buttons.size(); i++) {
252
JButton button = (JButton)_buttons.elementAt(i);
253
button.removeActionListener(listener);