001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import java.awt.BorderLayout;
029: import java.awt.FlowLayout;
030: import java.awt.Window;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033:
034: import javax.swing.BorderFactory;
035: import javax.swing.Icon;
036: import javax.swing.JButton;
037: import javax.swing.JComponent;
038: import javax.swing.JDialog;
039: import javax.swing.JFrame;
040: import javax.swing.JLabel;
041: import javax.swing.JPanel;
042: import javax.swing.JSeparator;
043: import javax.swing.SwingUtilities;
044:
045: /**
046: *
047: *
048: * @author $author$
049: * @version $Revision: 1.13 $
050: */
051: public class OptionsDialog extends JDialog implements ActionListener {
052: private Option selectedOption;
053: private OptionCallback callback;
054: private JButton defaultButton;
055:
056: /**
057: * Creates a new OptionsDialog object.
058: *
059: * @param parent
060: * @param options
061: * @param message
062: * @param title
063: * @param defaultOption
064: * @param callback
065: * @param modal
066: * @param icon
067: */
068: public OptionsDialog(JDialog parent, Option[] options,
069: Object message, String title, Option defaultOption,
070: OptionCallback callback, boolean modal, Icon icon) {
071: super (parent, title, modal);
072: init(options, message, defaultOption, callback, icon);
073: }
074:
075: /**
076: * Creates a new OptionsDialog object.
077: *
078: * @param parent
079: * @param options
080: * @param message
081: * @param title
082: * @param defaultOption
083: * @param callback
084: * @param modal
085: * @param icon
086: */
087: public OptionsDialog(JFrame parent, Option[] options,
088: Object message, String title, Option defaultOption,
089: OptionCallback callback, boolean modal, Icon icon) {
090: super (parent, title, modal);
091: init(options, message, defaultOption, callback, icon);
092: }
093:
094: private void init(Option[] options, Object message,
095: Option defaultOption, OptionCallback callback, Icon icon) {
096: //
097: this .callback = callback;
098:
099: JPanel b = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 2));
100: b.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
101:
102: for (int i = 0; i < options.length; i++) {
103: JButton button = new JButton(options[i].getText());
104:
105: if (options[i] == defaultOption) {
106: button.setDefaultCapable(options[i] == defaultOption);
107: defaultButton = button;
108: }
109:
110: button.setMnemonic(options[i].getMnemonic());
111: button.setToolTipText(options[i].getToolTipText());
112: button.putClientProperty("option", options[i]);
113: button.addActionListener(this );
114: b.add(button);
115: }
116:
117: //
118: JPanel s = new JPanel(new BorderLayout());
119: s.setBorder(BorderFactory.createEmptyBorder(4, 0, 0, 0));
120: s
121: .add(new JSeparator(JSeparator.HORIZONTAL),
122: BorderLayout.NORTH);
123: s.add(b, BorderLayout.SOUTH);
124:
125: //
126: JPanel z = new JPanel(new BorderLayout());
127: z.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
128:
129: //
130: if (message instanceof JComponent) {
131: z.add((JComponent) message, BorderLayout.CENTER);
132: } else {
133: z.add(new MultilineLabel(String.valueOf(message)),
134: BorderLayout.CENTER);
135: }
136:
137: // Icon panel
138: JLabel i = null;
139:
140: if (icon != null) {
141: i = new JLabel(icon);
142: i.setVerticalAlignment(JLabel.NORTH);
143: i.setBorder(BorderFactory.createEmptyBorder(4, 4, 0, 4));
144: }
145:
146: // Build this panel
147: getContentPane().setLayout(new BorderLayout());
148: getContentPane().add(z, BorderLayout.CENTER);
149:
150: if (i != null) {
151: getContentPane().add(i, BorderLayout.WEST);
152: }
153:
154: getContentPane().add(s, BorderLayout.SOUTH);
155:
156: //
157: pack();
158: }
159:
160: /**
161: *
162: *
163: * @return
164: */
165: public JButton getDefaultButton() {
166: return defaultButton;
167: }
168:
169: /**
170: *
171: *
172: * @return
173: */
174: public Option getSelectedOption() {
175: return selectedOption;
176: }
177:
178: /**
179: *
180: *
181: * @param evt
182: */
183: public void actionPerformed(ActionEvent evt) {
184: selectedOption = (Option) ((JButton) evt.getSource())
185: .getClientProperty("option");
186:
187: if ((callback == null)
188: || callback.canClose(this , selectedOption)) {
189: setVisible(false);
190: }
191: }
192:
193: /**
194: *
195: *
196: * @param parent
197: * @param options
198: * @param message
199: * @param title
200: * @param defaultOption
201: * @param callback
202: * @param icon
203: *
204: * @return
205: */
206: public static OptionsDialog createOptionDialog(JComponent parent,
207: Option[] options, Object message, String title,
208: Option defaultOption, OptionCallback callback, Icon icon) {
209: //
210: OptionsDialog dialog = null;
211: Window w = (Window) SwingUtilities.getAncestorOfClass(
212: Window.class, parent);
213:
214: if (w instanceof JFrame) {
215: dialog = new OptionsDialog((JFrame) w, options, message,
216: title, defaultOption, callback, true, icon);
217: } else if (w instanceof JDialog) {
218: dialog = new OptionsDialog((JDialog) w, options, message,
219: title, defaultOption, callback, true, icon);
220: } else {
221: dialog = new OptionsDialog((JFrame) null, options, message,
222: title, defaultOption, callback, true, icon);
223: }
224:
225: if (dialog.getDefaultButton() != null) {
226: dialog.getRootPane().setDefaultButton(
227: dialog.getDefaultButton());
228: }
229:
230: return dialog;
231: }
232: }
|