001: /* SwingML
002: * Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: *
019: * Authors:
020: * Ezequiel Cuellar <ecuellar@crosslogic.com>
021: * Robert Morris <robertj@morris.net>
022: * Bram Stieperaere <bramez@users.sourceforge.net>
023: */
024:
025: package org.swingml.component;
026:
027: import java.awt.*;
028: import java.awt.event.*;
029:
030: import javax.swing.*;
031:
032: import org.swingml.*;
033: import org.swingml.event.*;
034: import org.swingml.model.*;
035:
036: public class JDialogComponent extends JDialog implements WindowListener {
037:
038: private EventHandler eventHandler = EventHandler.getInstance();
039: private JDialogModel model = null;
040: private Container owner = null;
041: private StatusBar statusBar;
042:
043: public JDialogComponent(JDialogModel aModel) {
044: this .model = aModel;
045: String theName = model.getName();
046: String theLayout = model.getLayout();
047: String theText = model.getText();
048: int theRows = model.getRows();
049: int theCols = model.getCols();
050: int theWidth = model.getWidth();
051: int theHeight = model.getHeight();
052: super .setName(theName);
053: super .setTitle(theText);
054: super .setSize(theWidth, theHeight);
055: super .setModal(model.isModal());
056: if (theLayout.equalsIgnoreCase(Constants.FLOWLAYOUT)) {
057: super .getContentPane().setLayout(new FlowLayout());
058: }
059: if (theLayout.equalsIgnoreCase(Constants.GRIDLAYOUT)) {
060: super .getContentPane().setLayout(
061: new GridLayout(theRows, theCols));
062: }
063: if (theLayout.equalsIgnoreCase(Constants.BORDERLAYOUT)) {
064: super .getContentPane().setLayout(new BorderLayout());
065: }
066: if (theLayout.equalsIgnoreCase(Constants.NONE)) {
067: super .getContentPane().setLayout(null);
068: }
069:
070: if (model.showStatusBar()) {
071: createStatusBarArea();
072: }
073:
074: // special case for JDialog, since the rendering holds up the thread.
075: model.setContainer(this );
076: super .addWindowListener(this );
077: }
078:
079: private void createStatusBarArea() {
080: statusBar = new StatusBar(this );
081: getContentPane().add(statusBar.getBar(), BorderLayout.SOUTH);
082: }
083:
084: public Container getDialogOwner() {
085: return this .owner;
086: }
087:
088: private JComponent getFirstJComponent(Container aContainer) {
089: JComponent theComponent = null;
090: if (aContainer != null && aContainer.getComponents() != null
091: && aContainer.getComponents().length > 0) {
092: Component[] theComponents = aContainer.getComponents();
093: for (int i = 0; i < theComponents.length; i++) {
094: if (theComponents[i] instanceof JComponent) {
095: theComponent = (JComponent) theComponents[i];
096: break;
097: } else if (theComponents[i] instanceof Container) {
098: theComponent = getFirstJComponent((Container) theComponents[i]);
099: if (theComponent != null) {
100: break;
101: }
102: }
103: }
104: }
105: return theComponent;
106: }
107:
108: public StatusBar getStatusBar() {
109: return this .statusBar;
110: }
111:
112: // WindowListener Realization.
113: private void handleEvent(String eventType) {
114: /*
115: * This code makes sure that a JComponent instance exists in the current DIALOG
116: * element. If not, no event is fired. If so, the event is fired with the
117: * first instance of a JComponent class located within the current dialog's
118: * ContentPane.
119: */
120: JComponent theComponent = this .getFirstJComponent(this
121: .getContentPane());
122: if (theComponent != null) {
123: this .eventHandler.handleEvent(this .model, eventType,
124: theComponent);
125: }
126: }
127:
128: public void render(String aUrl, String aParent) {
129: //TODO the expression
130: //Component theParent = this.eventHandler.findActionTarget(getOwner(), aParent);
131: //does not work. getOwner returns a Window without components and the result will always be null.
132: //Need to find a way to get the correct parent. Current implementation will always
133: //use "this" as a parent for the rendering component.
134: //JDialog and JFrame dont extend from JComponent and cant pass super.getTopLevelAncestor()
135: //as a parameter.
136: RenderingThread theThread = new RenderingThread(aUrl, this
137: .getContentPane());
138: theThread.start();
139: }
140:
141: public void setOwner(Container aContainer) {
142: this .owner = aContainer;
143: }
144:
145: public void submit(String anAddress) {
146: SubmittingThread theThread = new SubmittingThread(anAddress,
147: this , this , true);
148: theThread.start();
149: }
150:
151: public void submit(String anAddress, String aTargetContainer,
152: boolean aRepaint) {
153: Component theTargetContainer = this .eventHandler
154: .findActionTarget(super .getContentPane(),
155: aTargetContainer);
156: SubmittingThread theThread = new SubmittingThread(anAddress,
157: this , (Container) theTargetContainer, aRepaint);
158: theThread.start();
159: }
160:
161: /**
162: * @see java.awt.event.WindowListener#windowActivated(WindowEvent)
163: */
164: public void windowActivated(WindowEvent aEvt) {
165: this .handleEvent(Constants.WINDOW_ACTIVATED);
166: }
167:
168: /**
169: * @see java.awt.event.WindowListener#windowClosed(WindowEvent)
170: */
171: public void windowClosed(WindowEvent aEvt) {
172: this .handleEvent(Constants.WINDOW_CLOSED);
173: if (this .statusBar != null) {
174: StatusBar.unRegister(this );
175: this .statusBar.disposeOfYourself();
176: this .statusBar = null;
177: }
178: }
179:
180: /**
181: * @see java.awt.event.WindowListener#windowClosing(WindowEvent)
182: */
183: public void windowClosing(WindowEvent aEvt) {
184: this .handleEvent(Constants.WINDOW_CLOSING);
185: }
186:
187: /**
188: * @see java.awt.event.WindowListener#windowDeactivated(WindowEvent)
189: */
190: public void windowDeactivated(WindowEvent aEvt) {
191: this .handleEvent(Constants.WINDOW_DEACTIVATED);
192: }
193:
194: /**
195: * @see java.awt.event.WindowListener#windowDeiconified(WindowEvent)
196: */
197: public void windowDeiconified(WindowEvent aEvt) {
198: this .handleEvent(Constants.WINDOW_DEICONIFIED);
199: }
200:
201: /**
202: * @see java.awt.event.WindowListener#windowIconified(WindowEvent)
203: */
204: public void windowIconified(WindowEvent aEvt) {
205: this .handleEvent(Constants.WINDOW_ICONIFIED);
206: }
207:
208: /**
209: * @see java.awt.event.WindowListener#windowOpened(WindowEvent)
210: */
211: public void windowOpened(WindowEvent aEvt) {
212: this.handleEvent(Constants.WINDOW_OPENED);
213: }
214: }
|