1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * ident "%Z%%M% %I% %E% SMI"
24 *
25 * Copyright 1998-2002 by Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
27 */
28 package com.sun.dhcpmgr.ui;
29
30 import javax.swing.*;
31 import java.awt.event.*;
32 import java.util.*;
33
34 /**
35 * A simple panel with the buttons commonly used in a dialog. Register as
36 * a ButtonPanelListener in order to receive the events from this class.
37 * @see ButtonPanelListener
38 */
39 public class ButtonPanel extends JPanel {
40
41 // Convert action events on each button to button panel notifications
42 class ButtonAdaptor implements ActionListener {
43 public void actionPerformed(ActionEvent e) {
44 int buttonId = -1;
45 Object source = e.getSource();
46 if (source == okButton) {
47 buttonId = ButtonPanelListener.OK;
48 } else if (source == resetButton) {
49 buttonId = ButtonPanelListener.RESET;
50 } else if (source == cancelButton) {
51 buttonId = ButtonPanelListener.CANCEL;
52 } else if (source == helpButton) {
53 buttonId = ButtonPanelListener.HELP;
54 }
55 Enumeration en = listeners.elements();
56 while (en.hasMoreElements()) {
57 ButtonPanelListener l = (ButtonPanelListener)en.nextElement();
58 l.buttonPressed(buttonId);
59 }
60 }
61 }
62
63 JButton okButton, resetButton, cancelButton, helpButton;
64 ButtonAdaptor adaptor;
65 Vector listeners;
66
67 /**
68 * Construct a ButtonPanel with OK, Cancel, and Help buttons, and
69 * the reset button display controlled by the parameter.
70 * @param showReset true if Reset button is to be included
71 */
72 public ButtonPanel(boolean showReset) {
73 this(showReset, true);
74 }
75
76 /**
77 * Construct a ButtonPanel with reset and help buttons controlled
78 * by the parameters passed, and always showing OK and Cancel.
79 * @param showReset true if Reset button is to be included
80 * @param showHelp true if Help button is to be included
81 */
82 public ButtonPanel(boolean showReset, boolean showHelp) {
83 super();
84 setLayout(new ButtonLayout(ALIGNMENT.RIGHT));
85 // Create event handler
86 adaptor = new ButtonAdaptor();
87 listeners = new Vector();
88
89 Mnemonic mn = new Mnemonic(ResourceStrings.getString("ok_button"));
90 okButton = new JButton(mn.getString());
91 okButton.setToolTipText(mn.getString());
92 okButton.setMnemonic(mn.getMnemonic());
93
94 okButton.setEnabled(false);
95 okButton.addActionListener(adaptor);
96 add(okButton);
97
98 // Only show reset if requested
99 if (showReset) {
100 Mnemonic mnReset =
101 new Mnemonic(ResourceStrings.getString("reset_button"));
102 resetButton = new JButton(mnReset.getString());
103 resetButton.setToolTipText(mnReset.getString());
104 resetButton.setMnemonic(mnReset.getMnemonic());
105
106 resetButton.addActionListener(adaptor);
107 add(resetButton);
108 } else {
109 resetButton = null;
110 }
111
112 Mnemonic mnCancel =
113 new Mnemonic(ResourceStrings.getString("cancel_button"));
114 cancelButton = new JButton(mnCancel.getString());
115 cancelButton.setToolTipText(mnCancel.getString());
116 cancelButton.setMnemonic(mnCancel.getMnemonic());
117
118 cancelButton.addActionListener(adaptor);
119 add(cancelButton);
120
121 if (showHelp) {
122 Mnemonic mnHelp =
123 new Mnemonic(ResourceStrings.getString("help_button"));
124 helpButton = new JButton(mnHelp.getString());
125 helpButton.setToolTipText(mnHelp.getString());
126 helpButton.setMnemonic(mnHelp.getMnemonic());
127
128 helpButton.addActionListener(adaptor);
129 add(helpButton);
130 } else {
131 helpButton = null;
132 }
133 }
134
135 public void addButtonPanelListener(ButtonPanelListener l) {
136 listeners.addElement(l);
137 }
138
139 public void removeButtonPanelListener(ButtonPanelListener l) {
140 listeners.removeElement(l);
141 }
142
143 public void setOkEnabled(boolean state) {
144 okButton.setEnabled(state);
145 }
146
147 public boolean isOkEnabled() {
148 return okButton.isEnabled();
149 }
150
151 }