001: package net.sourceforge.squirrel_sql.client.gui;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023:
024: import javax.swing.JButton;
025: import javax.swing.JPanel;
026: import javax.swing.JRootPane;
027: import javax.swing.event.EventListenerList;
028:
029: import com.jgoodies.forms.factories.ButtonBarFactory;
030:
031: import net.sourceforge.squirrel_sql.fw.util.StringManager;
032: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
033:
034: public class OkClosePanel extends JPanel {
035: /** Internationalized strings for this class. */
036: private static final StringManager s_stringMgr = StringManagerFactory
037: .getStringManager(OkClosePanel.class);
038:
039: private boolean _executingMode;
040:
041: /** Listeners for this object. */
042: private EventListenerList _listenerList = new EventListenerList();
043:
044: private JButton _okBtn;
045: private JButton _closeBtn = new JButton(s_stringMgr
046: .getString("OkClosePanel.close"));
047:
048: public OkClosePanel() {
049: super ();
050: createGUI(s_stringMgr.getString("OkClosePanel.ok"));
051: }
052:
053: public OkClosePanel(String okButtonText) {
054: super ();
055: createGUI(okButtonText != null ? okButtonText : s_stringMgr
056: .getString("OkClosePanel.ok"));
057: }
058:
059: /**
060: * When in <EM>executing mode</EM> the OK button is disabled
061: * and the Close button is retitled as Cancel.
062: */
063: public void setExecuting(boolean executingMode) {
064: if (executingMode != _executingMode) {
065: _executingMode = executingMode;
066: _okBtn.setEnabled(!executingMode);
067: _closeBtn.setText(executingMode ? s_stringMgr
068: .getString("OkClosePanel.cancel") : s_stringMgr
069: .getString("OkClosePanel.close"));
070: if (!executingMode) {
071: _closeBtn.setEnabled(true);
072: }
073: }
074: }
075:
076: /**
077: * Enable/disable the Close/Cancel button.
078: *
079: * @param enable <TT>true</TT> to enable else <TT>false</TT> to disable.
080: */
081: public void enableCloseButton(boolean enable) {
082: _closeBtn.setEnabled(enable);
083: }
084:
085: /**
086: * Adds a listener for actions in this panel.
087: *
088: * @param lis <TT>OkClosePanelListener</TT> that will be notified when
089: * actions are performed in this panel.
090: */
091: public synchronized void addListener(IOkClosePanelListener lis) {
092: _listenerList.add(IOkClosePanelListener.class, lis);
093: }
094:
095: /**
096: * Make the OK button the default. This should be called
097: * <EM>after</EM> you add this panel to a dialog/frame, not
098: * before otherwise you will get an <TT>IllegalStateException</TT>
099: * exception.
100: *
101: * @param IllegalStateException
102: * Thrown if <TT>null</TT> <TT>JRootPane</TT>. I.E. component
103: * hasn't been added to a frame, dialog etc.
104: */
105: public synchronized void makeOKButtonDefault()
106: throws IllegalStateException {
107: JRootPane root = getRootPane();
108: if (root == null) {
109: throw new IllegalStateException(
110: "Null RootPane so cannot set default button");
111: }
112: // root.setDefaultButton(_okBtn);
113: }
114:
115: public JButton getCloseButton() {
116: return _closeBtn;
117: }
118:
119: public JButton getOKButton() {
120: return _okBtn;
121: }
122:
123: private void fireButtonPressed(JButton btn) {
124: // Guaranteed to be non-null.
125: Object[] listeners = _listenerList.getListenerList();
126: // Process the listeners last to first, notifying
127: // those that are interested in this event.
128: OkClosePanelEvent evt = null;
129: for (int i = listeners.length - 2; i >= 0; i -= 2) {
130: if (listeners[i] == IOkClosePanelListener.class) {
131: // Lazily create the event:
132: if (evt == null) {
133: evt = new OkClosePanelEvent(this );
134: }
135: IOkClosePanelListener lis = (IOkClosePanelListener) listeners[i + 1];
136: if (btn == _okBtn) {
137: lis.okPressed(evt);
138: } else if (_executingMode) {
139: lis.cancelPressed(evt);
140: } else {
141: lis.closePressed(evt);
142: }
143: }
144: }
145: }
146:
147: private void createGUI(String okButtonText) {
148: _okBtn = new JButton(okButtonText);
149:
150: JPanel pnl = ButtonBarFactory.buildOKCancelBar(_okBtn,
151: _closeBtn);
152: add(pnl);
153: // add(_okBtn);
154: // add(_closeBtn);
155: _okBtn.addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent evt) {
157: fireButtonPressed(_okBtn);
158: }
159: });
160: _closeBtn.addActionListener(new ActionListener() {
161: public void actionPerformed(ActionEvent evt) {
162: fireButtonPressed(_closeBtn);
163: }
164: });
165: //
166: // GUIUtils.setJButtonSizesTheSame(new JButton[] {_okBtn, _closeBtn, new JButton(i18n.CANCEL)});
167: }
168: }
|