001: /*
002: * BaseDialog.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.gui;
023:
024: import java.awt.BorderLayout;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.Insets;
028: import java.awt.event.FocusEvent;
029: import java.awt.event.FocusListener;
030: import javax.swing.JDialog;
031: import javax.swing.JPanel;
032: import javax.swing.SwingUtilities;
033: import org.executequery.GUIUtilities;
034: import org.executequery.ActiveComponent;
035: import org.underworldlabs.swing.GUIUtils;
036: import org.underworldlabs.swing.GlassPanePanel;
037:
038: /* ----------------------------------------------------------
039: * CVS NOTE: Changes to the CVS repository prior to the
040: * release of version 3.0.0beta1 has meant a
041: * resetting of CVS revision numbers.
042: * ----------------------------------------------------------
043: */
044:
045: /**
046: * Base dialog to be extended.
047: *
048: * @author Takis Diakoumis
049: * @version $Revision: 1.6 $
050: * @date $Date: 2006/06/07 15:06:47 $
051: */
052: public class BaseDialog extends JDialog implements FocusListener,
053: ActionContainer {
054:
055: /** the content panel */
056: private JPanel contentPanel;
057:
058: /** Creates a new instance of BaseDialog */
059: public BaseDialog(String name, boolean modal) {
060: this (name, modal, null);
061: }
062:
063: /** Creates a new instance of BaseDialog */
064: public BaseDialog(String name, boolean modal, boolean resizeable) {
065: this (name, modal, null);
066: setResizable(resizeable);
067: }
068:
069: /** Creates a new instance of BaseDialog */
070: public BaseDialog(String name, boolean modal, JPanel panel) {
071: super (GUIUtilities.getParentFrame(), name, modal);
072: addDisplayComponentWithEmptyBorder(panel);
073: addFocusListener(this );
074: getRootPane().setGlassPane(new GlassPanePanel());
075: setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
076: }
077:
078: /**
079: * Indicates that a [long-running] process has begun or ended
080: * as specified. This will trigger the glass pane on or off
081: * and set the cursor appropriately.
082: *
083: * @param inProcess - true | false
084: */
085: public void setInProcess(final boolean inProcess) {
086: GUIUtils.invokeLater(new Runnable() {
087: public void run() {
088: if (getRootPane().getGlassPane().isVisible() == inProcess) {
089: return;
090: }
091: getRootPane().getGlassPane().setVisible(inProcess);
092: }
093: });
094: if (inProcess) {
095: GUIUtilities.showWaitCursor();
096: } else {
097: GUIUtilities.showNormalCursor();
098: }
099: }
100:
101: // ------------------------------------------
102: // FocusListener implementation
103:
104: public void focusGained(FocusEvent e) {
105: dialogFocusChanged(true);
106: }
107:
108: public void focusLost(FocusEvent e) {
109: dialogFocusChanged(false);
110: }
111:
112: // ------------------------------------------
113:
114: /**
115: * Removes this dialog from the application
116: * controller <code>GUIUtilities</code> object before
117: * a call to <code>super.dispose()</code>.
118: */
119: public void dispose() {
120: if (contentPanel instanceof ActiveComponent) {
121: ((ActiveComponent) contentPanel).cleanup();
122: }
123: contentPanel = null;
124: GUIUtilities.deregisterDialog(this );
125: GUIUtils.scheduleGC();
126:
127: SwingUtilities.invokeLater(new Runnable() {
128: public void run() {
129: BaseDialog.this .super Dispose();
130: }
131: });
132: }
133:
134: private void super Dispose() {
135: super .dispose();
136: }
137:
138: /**
139: * Indicates the process has completed.
140: */
141: public void finished() {
142: dispose();
143: }
144:
145: /**
146: * Indicates whether this is a dialog.
147: *
148: * @return true | false
149: */
150: public boolean isDialog() {
151: return true;
152: }
153:
154: /**
155: * Called for a change in focus as specified. This
156: * method will pass this object into <code>GUIUtilities</code>
157: * methods <code>setFocusedDialog(JDialog)</code> and
158: * <code>removeFocusedDialog(JDialog)</code> depending on
159: * the focus parameter specified.
160: *
161: * @param whether this dialog has focus
162: */
163: private void dialogFocusChanged(boolean hasFocus) {
164: if (hasFocus) {
165: GUIUtilities.setFocusedDialog(this );
166: } else {
167: GUIUtilities.removeFocusedDialog(this );
168: }
169: }
170:
171: /**
172: * Adds the primary panel for display in this dialog.
173: *
174: * @param the main panel display
175: */
176: public void addDisplayComponentWithEmptyBorder(JPanel panel) {
177: if (panel == null) {
178: return;
179: }
180: contentPanel = panel;
181: getContentPane().setLayout(new GridBagLayout());
182: getContentPane().add(
183: panel,
184: new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
185: GridBagConstraints.SOUTHEAST,
186: GridBagConstraints.BOTH,
187: new Insets(5, 5, 5, 5), 0, 0));
188: }
189:
190: /**
191: * Adds the primary panel for display in this dialog.
192: *
193: * @param the main panel display
194: */
195: public void addDisplayComponent(JPanel panel) {
196: if (panel == null) {
197: return;
198: }
199: contentPanel = panel;
200: getContentPane().setLayout(new BorderLayout());
201: getContentPane().add(panel, BorderLayout.CENTER);
202: }
203:
204: /**
205: * Returns the content panel of this dialog.
206: *
207: * @return the dialog's content panel
208: */
209: public JPanel getConentPanel() {
210: return contentPanel;
211: }
212:
213: /**
214: * Packs, positions and displays the dialog.
215: */
216: public void display() {
217: // check for multiple calls
218: if (isVisible()) {
219: return;
220: }
221: pack();
222: setLocation(GUIUtilities.getLocationForDialog(getSize()));
223: GUIUtilities.registerDialog(this );
224: setVisible(true);
225:
226: if (contentPanel instanceof FocusComponentPanel) {
227: GUIUtils
228: .requestFocusInWindow(((FocusComponentPanel) contentPanel)
229: .getDefaultFocusComponent());
230: }
231:
232: }
233:
234: }
|