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 org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import java.awt.BorderLayout;
032: import java.awt.Component;
033: import java.awt.FlowLayout;
034: import java.awt.GridBagConstraints;
035: import java.awt.GridBagLayout;
036: import java.awt.GridLayout;
037: import java.awt.Insets;
038: import java.awt.Window;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041:
042: import javax.swing.BorderFactory;
043: import javax.swing.JButton;
044: import javax.swing.JDialog;
045: import javax.swing.JFrame;
046: import javax.swing.JPanel;
047: import javax.swing.SwingConstants;
048: import javax.swing.SwingUtilities;
049:
050: /**
051: *
052: *
053: * @author $author$
054: * @version $Revision: 1.14 $
055: */
056: public class OptionsPanel extends JPanel {
057: //
058:
059: /** */
060: protected Log log = LogFactory.getLog(OptionsPanel.class);
061:
062: //
063: private Tabber tabber;
064: private boolean cancelled;
065:
066: /**
067: * Creates a new OptionsPanel object.
068: *
069: * @param optionalTabs
070: */
071: public OptionsPanel(OptionsTab[] optionalTabs) {
072: super ();
073: tabber = new Tabber();
074:
075: if (optionalTabs != null) {
076: for (int i = 0; i < optionalTabs.length; i++) {
077: optionalTabs[i].reset();
078: addTab(optionalTabs[i]);
079: }
080: }
081:
082: // Add the common tabs
083: addTab(new GlobalOptionsTab());
084:
085: // Build this panel
086: setLayout(new GridLayout(1, 1));
087: add(tabber);
088: }
089:
090: /**
091: *
092: *
093: * @return
094: */
095: public boolean validateTabs() {
096: return tabber.validateTabs();
097: }
098:
099: /**
100: *
101: */
102: public void applyTabs() {
103: tabber.applyTabs();
104: }
105:
106: /**
107: *
108: *
109: * @param tab
110: */
111: public void addTab(OptionsTab tab) {
112: tabber.addTab(tab);
113: }
114:
115: /**
116: *
117: */
118: public void reset() {
119: for (int i = 0; i < tabber.getTabCount(); i++) {
120: ((OptionsTab) tabber.getTabAt(i)).reset();
121: }
122: }
123:
124: /**
125: *
126: *
127: * @param parent
128: * @param optionalTabs
129: *
130: * @return
131: */
132: public static boolean showOptionsDialog(Component parent,
133: OptionsTab[] optionalTabs) {
134: final OptionsPanel opts = new OptionsPanel(optionalTabs);
135: opts.reset();
136:
137: JDialog d = null;
138: Window w = (Window) SwingUtilities.getAncestorOfClass(
139: Window.class, parent);
140:
141: if (w instanceof JDialog) {
142: d = new JDialog((JDialog) w, "Options", true);
143: } else if (w instanceof JFrame) {
144: d = new JDialog((JFrame) w, "Options", true);
145: } else {
146: d = new JDialog((JFrame) null, "Options", true);
147: }
148:
149: final JDialog dialog = d;
150:
151: // Create the bottom button panel
152: final JButton cancel = new JButton("Cancel");
153: cancel.setMnemonic('c');
154: cancel.addActionListener(new ActionListener() {
155: public void actionPerformed(ActionEvent evt) {
156: opts.cancelled = true;
157: dialog.setVisible(false);
158: }
159: });
160:
161: final JButton ok = new JButton("Ok");
162: ok.setMnemonic('o');
163: ok.addActionListener(new ActionListener() {
164: public void actionPerformed(ActionEvent evt) {
165: if (opts.validateTabs()) {
166: dialog.setVisible(false);
167: }
168: }
169: });
170: dialog.getRootPane().setDefaultButton(ok);
171:
172: JPanel buttonPanel = new JPanel(new GridBagLayout());
173: GridBagConstraints gbc = new GridBagConstraints();
174: gbc.fill = GridBagConstraints.HORIZONTAL;
175: gbc.anchor = GridBagConstraints.CENTER;
176: gbc.insets = new Insets(6, 6, 0, 0);
177: gbc.weighty = 1.0;
178: UIUtil.jGridBagAdd(buttonPanel, ok, gbc,
179: GridBagConstraints.RELATIVE);
180: UIUtil.jGridBagAdd(buttonPanel, cancel, gbc,
181: GridBagConstraints.REMAINDER);
182:
183: JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,
184: 0, 0));
185: southPanel.add(buttonPanel);
186:
187: //
188: JPanel mainPanel = new JPanel(new BorderLayout());
189: mainPanel
190: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
191: mainPanel.add(opts, BorderLayout.CENTER);
192: mainPanel.add(southPanel, BorderLayout.SOUTH);
193:
194: // Show the dialog
195: dialog.getContentPane().setLayout(new GridLayout(1, 1));
196: dialog.getContentPane().add(mainPanel);
197: dialog.pack();
198: dialog.setResizable(true);
199: UIUtil.positionComponent(SwingConstants.CENTER, dialog);
200: dialog.setVisible(true);
201:
202: if (!opts.cancelled) {
203: opts.applyTabs();
204: }
205:
206: return !opts.cancelled;
207: }
208: }
|