~ubuntu-branches/ubuntu/utopic/389-admin-console/utopic

« back to all changes in this revision

Viewing changes to src/com/netscape/management/admserv/config/ButtonBar.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2011-10-12 11:05:09 UTC
  • Revision ID: package-import@ubuntu.com-20111012110509-ybd1jr5xeat2ug1i
Tags: upstream-1.1.8
ImportĀ upstreamĀ versionĀ 1.1.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc.  Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
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
 
9
 * of the License.
 
10
 * 
 
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.
 
15
 * 
 
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.
 
19
 * 
 
20
 * END COPYRIGHT BLOCK **/
 
21
package com.netscape.management.admserv.config;
 
22
 
 
23
import java.awt.*;
 
24
import java.awt.event.*;
 
25
import java.util.Vector;
 
26
import javax.swing.*;
 
27
import javax.swing.border.*;
 
28
import com.netscape.management.client.util.*;
 
29
import com.netscape.management.nmclf.SuiLookAndFeel;
 
30
 
 
31
/**
 
32
  *
 
33
  * @version 0.1 11/28/97
 
34
  * @author miodrag@netscape.com
 
35
  */
 
36
 
 
37
/**
 
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.
 
44
 */
 
45
public class ButtonBar extends JPanel {
 
46
 
 
47
    static ResourceSet _resource;
 
48
    static public String _i18nOKButton, _i18nCancelButton,
 
49
        _i18nCloseButton, _i18nHelpButton;
 
50
    static public String _i18nSaveButton, _i18nResetButton, 
 
51
        _i18nSaveToolTip, _i18nResetToolTip, _i18nHelpToolTip;
 
52
    
 
53
    static {
 
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");
 
64
    }
 
65
 
 
66
    /**
 
67
      * Standard Command Buttons
 
68
     */
 
69
    public static final String cmdOK = "ok", cmdCancel = "cancel",
 
70
    cmdClose = "close", cmdHelp = "help";
 
71
 
 
72
    protected Vector _buttons = new Vector(5);
 
73
 
 
74
    // Standard panel commands buttons OK, Cancel, Help
 
75
    static private String[] stdCommands = { cmdOK, cmdCancel, null,
 
76
                                            cmdHelp};
 
77
    static private String[] stdLabels = { _i18nSaveButton,
 
78
                                          _i18nResetButton, null, _i18nHelpButton };
 
79
 
 
80
    static private String[] stdToolTips = { _i18nSaveToolTip, 
 
81
                                            _i18nResetToolTip, null,  _i18nHelpToolTip};
 
82
 
 
83
    // Standard panel command buttons when running inside a dialog. All standard + Close button
 
84
    static private String[] stdDialogCommands = { cmdOK, cmdCancel,
 
85
                                                  null, cmdHelp};
 
86
    static private String[] stdDialogLabels = { _i18nOKButton,
 
87
                                                _i18nCancelButton, null, _i18nHelpButton };
 
88
 
 
89
    /**
 
90
     * No-Arg constructor, create default button bar, horizontal orientation
 
91
     */
 
92
    public ButtonBar() {
 
93
        this(stdCommands, stdLabels, true);
 
94
    }
 
95
 
 
96
    /**
 
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
 
99
      */
 
100
    public ButtonBar(boolean isDialog) {
 
101
        this(isDialog ? stdDialogCommands : stdCommands,
 
102
             isDialog ? stdDialogLabels : stdLabels, true);
 
103
    }
 
104
 
 
105
    /**
 
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
 
109
      */
 
110
    public ButtonBar(boolean isDialog, String[] toolTips) {
 
111
        this(isDialog ? stdDialogCommands : stdCommands,
 
112
             isDialog ? stdDialogLabels : stdLabels, toolTips, true);
 
113
    }
 
114
 
 
115
    /**
 
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
 
125
      */
 
126
    public ButtonBar(String[] commands, String[] labels, boolean isHorizontal) {
 
127
        this(commands, labels, stdToolTips, isHorizontal);
 
128
    }
 
129
 
 
130
    /**
 
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
 
141
      */
 
142
    public ButtonBar(String[] commands, String[] labels, String[] toolTips, boolean isHorizontal) {
 
143
        
 
144
        JButton button;
 
145
        JPanel p = new JPanel();
 
146
        GBC gbc = new GBC();
 
147
 
 
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);
 
154
        }
 
155
 
 
156
        setLayout(new GridBagLayout());
 
157
        p.setLayout (new GridBagLayout());
 
158
 
 
159
        int gridx = 0, gridy = 0, insetTop = 0, insetLeft = 0,
 
160
        insetBottom = 0, insetRight = 0;
 
161
        boolean newButtonGroup = false;
 
162
 
 
163
        JButton[] factoryButtons = JButtonFactory.create(labels);
 
164
 
 
165
        for (int i = 0; i < labels.length; i++) {
 
166
 
 
167
            if (labels[i] == null) {
 
168
                newButtonGroup = true;
 
169
                continue;
 
170
            }
 
171
 
 
172
            _buttons.addElement(button = factoryButtons[i]);
 
173
            button.setActionCommand(commands[i]);
 
174
            JButtonFactory.initializeMnemonic(button);
 
175
            button.setToolTipText(toolTips[i]);
 
176
 
 
177
            /*
 
178
             * Apply the proper button spacing. If start of a new button group
 
179
             * use SEPARATED_COMPONENT_SPACE
 
180
            */
 
181
            insetTop = insetLeft = insetBottom = insetRight = 0;
 
182
            if (isHorizontal) {
 
183
                if (i != 0) {
 
184
                    if (newButtonGroup) {
 
185
                        insetLeft =
 
186
                                SuiLookAndFeel.SEPARATED_COMPONENT_SPACE;
 
187
                    } else {
 
188
                        insetLeft = SuiLookAndFeel.COMPONENT_SPACE;
 
189
                    }
 
190
                }
 
191
            } else {
 
192
                if (i != 0) {
 
193
                    if (newButtonGroup) {
 
194
                        insetTop = SuiLookAndFeel.SEPARATED_COMPONENT_SPACE;
 
195
                    } else {
 
196
                        insetTop = SuiLookAndFeel.COMPONENT_SPACE;
 
197
                    }
 
198
                }
 
199
            }
 
200
 
 
201
            gbc.setInsets(insetTop, insetLeft, insetBottom, insetRight);
 
202
            gbc.setGrid(gridx, gridy, 1, 1);
 
203
            gbc.setSpace(1.0, 0.0, /*anchor=*/GBC.CENTER,
 
204
                    /*fill=*/GBC.NONE);
 
205
            p.add(button, gbc);
 
206
 
 
207
            if (isHorizontal) {
 
208
                gridx++;
 
209
            } else {
 
210
                gridy++;
 
211
            }
 
212
            newButtonGroup = false;
 
213
        }
 
214
 
 
215
        /**
 
216
          * Add the buttons to the panel right left conner
 
217
          */
 
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);
 
221
        add(p, gbc);
 
222
    }
 
223
 
 
224
    /**
 
225
      * Get a button with the command string <i>command</i>
 
226
      */
 
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)) {
 
231
                return button;
 
232
            }
 
233
        }
 
234
        return null;
 
235
    }
 
236
 
 
237
    /**
 
238
      * Add listener too all buttons
 
239
     */
 
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);
 
244
        }
 
245
    }
 
246
 
 
247
    /**
 
248
      * Remove action listener on all buttons
 
249
     */
 
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);
 
254
        }
 
255
    }
 
256
}