001: /*
002: * SwingML Copyright (C) 2002 SwingML Team This library is free software; you
003: * can redistribute it and/or modify it under the terms of the GNU Lesser
004: * General Public License as published by the Free Software Foundation; either
005: * version 2 of the License, or (at your option) any later version. This library
006: * is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
007: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
008: * PARTICULAR PURPOSE.
009: *
010: * See the GNU Lesser General Public License for more details.
011: * You should have received a copy of the GNU Lesser General Public
012: * License along with this library; if not, write to the Free Software
013: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
014: *
015: * Authors:
016: * Ezequiel Cuellar <ecuellar@crosslogic.com>
017: * Robert Morris <robertj@morris.net>
018: */
019: package org.swingml.component;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023:
024: import javax.swing.*;
025:
026: import org.swingml.*;
027: import org.swingml.event.*;
028: import org.swingml.model.*;
029: import org.swingml.registry.*;
030:
031: public class JFrameComponent extends JFrame implements WindowListener,
032: XMLTranslatable, XMLStateTranslatable {
033:
034: private EventHandler eventHandler = EventHandler.getInstance();
035: private JFrameModel model = null;
036: private Container owner = null;
037: private boolean postState = false;
038: private StatusBar statusBar = null;
039:
040: public JFrameComponent() {
041: super ();
042: }
043:
044: public JFrameComponent(JFrameModel aModel) {
045: this .model = aModel;
046: String theName = model.getName();
047: String theLayout = model.getLayout();
048: String theText = model.getText();
049: setPostState(model.isPostState());
050: int theRows = model.getRows();
051: int theCols = model.getCols();
052: int theWidth = model.getWidth();
053: int theHeight = model.getHeight();
054: super .setName(theName);
055: super .setTitle(theText);
056: super .setSize(theWidth, theHeight);
057: if (model.isMaximize()) {
058: setExtendedState(MAXIMIZED_BOTH);
059: }
060: if (theLayout.equalsIgnoreCase(Constants.FLOWLAYOUT)) {
061: super .getContentPane().setLayout(new FlowLayout());
062: }
063: if (theLayout.equalsIgnoreCase(Constants.GRIDLAYOUT)) {
064: super .getContentPane().setLayout(
065: new GridLayout(theRows, theCols));
066: }
067: if (theLayout.equalsIgnoreCase(Constants.BORDERLAYOUT)) {
068: super .getContentPane().setLayout(new BorderLayout());
069: }
070: if (theLayout.equalsIgnoreCase(Constants.NONE)) {
071: super .getContentPane().setLayout(null);
072: }
073: super .addWindowListener(this );
074: if (model.showStatusBar()) {
075: createStatusBarArea();
076: }
077:
078: Image icon = IconFactory.getIconImage();
079: if (icon != null) {
080: super .setIconImage(icon);
081: }
082: }
083:
084: private void createStatusBarArea() {
085: statusBar = new StatusBar(this );
086: getContentPane().add(statusBar.getBar(), BorderLayout.SOUTH);
087: }
088:
089: public Container getDialogOwner() {
090: return this .owner;
091: }
092:
093: private JComponent getFirstJComponent(Container aContainer) {
094: JComponent theComponent = null;
095: if (aContainer != null && aContainer.getComponents() != null
096: && aContainer.getComponents().length > 0) {
097: Component[] theComponents = aContainer.getComponents();
098: for (int i = 0; i < theComponents.length; i++) {
099: if (theComponents[i] instanceof JComponent) {
100: theComponent = (JComponent) theComponents[i];
101: break;
102: } else if (theComponents[i] instanceof Container) {
103: theComponent = getFirstJComponent((Container) theComponents[i]);
104: if (theComponent != null) {
105: break;
106: }
107: }
108: }
109: }
110: return theComponent;
111: }
112:
113: public StatusBar getStatusBar() {
114: return this .statusBar;
115: }
116:
117: public String getXMLState() {
118: boolean currentPopState = isPostState();
119: setPostState(true);
120: String xml = getXMLValue();
121: setPostState(currentPopState);
122: return xml;
123: }
124:
125: public String getXMLValue() {
126: StringBuffer buffer = new StringBuffer();
127: if (isPostState()) {
128: buffer.append("<WINDOW ");
129: buffer.append(Constants.NAME);
130: buffer.append("=\"");
131: buffer.append(getName());
132: buffer.append("\" ");
133: buffer.append(Constants.WIDTH);
134: buffer.append("=\"");
135: buffer.append(getWidth());
136: buffer.append("\" ");
137: buffer.append(Constants.HEIGHT);
138: buffer.append("=\"");
139: buffer.append(getHeight());
140: buffer.append("\" ");
141: buffer.append(Constants.XOPEN);
142: buffer.append("=\"");
143: buffer.append(getX());
144: buffer.append("\" ");
145: buffer.append(Constants.YOPEN);
146: buffer.append("=\"");
147: buffer.append(getY());
148: buffer.append("\" ");
149: buffer.append(Constants.MAXIMUM);
150: buffer.append("=\"");
151: buffer
152: .append(getExtendedState() == MAXIMIZED_BOTH ? Constants.TRUE
153: : Constants.FALSE);
154: buffer.append("\" />");
155: }
156: return buffer.toString();
157: }
158:
159: // WindowListener Realization.
160: private void handleEvent(String eventType) {
161: /*
162: * This code makes sure that a JComponent instance exists in the current
163: * DIALOG element. If not, no event is fired. If so, the event is fired
164: * with the first instance of a JComponent class located within the
165: * current dialog's ContentPane.
166: */
167: JComponent jComponent = this .getFirstJComponent(this
168: .getContentPane());
169: if (jComponent != null) {
170: this .eventHandler.handleEvent(this .model, eventType,
171: jComponent);
172: }
173: }
174:
175: public boolean hasState() {
176: return true;
177: }
178:
179: /**
180: * @return Returns the postState.
181: */
182: public boolean isPostState() {
183: return postState;
184: }
185:
186: public void render(String aUrl, String aParent) {
187: // TODO the expression
188: // Component theParent = this.eventHandler.findActionTarget(getOwner(),
189: // aParent);
190: // does not work. getOwner returns a Window without components and the
191: // result will always be null.
192: // Need to find a way to get the correct parent. Current implementation
193: // will always
194: // use "this" as a parent for the rendering component.
195: // JDialog and JFrame dont extend from JComponent and cant pass
196: // super.getTopLevelAncestor()
197: // as a parameter.
198: RenderingThread theThread = new RenderingThread(aUrl, this
199: .getContentPane());
200: theThread.start();
201: }
202:
203: public void setOwner(Container aContainer) {
204: this .owner = aContainer;
205: }
206:
207: /**
208: * @param postState
209: * The postState to set.
210: */
211: public void setPostState(boolean postState) {
212: this .postState = postState;
213: }
214:
215: public void setTitle(String title) {
216: if (title != null && title.startsWith("@")) {
217: // find the component, get the value, call super
218: String name = null;
219: String id = null;
220: if (title.indexOf(":") != -1) {
221: name = title.substring(1, title.indexOf(":"));
222: id = title.substring(title.indexOf(":") + 1);
223: } else {
224: name = title.substring(1);
225: }
226: SwingMLModel theModel = (SwingMLModel) SwingMLModelToContainerRegistry
227: .getModel(name, id);
228: if (theModel != null) {
229: String newTitle = theModel.getText();
230: super .setTitle(newTitle);
231: }
232: } else {
233: super .setTitle(title);
234: }
235: }
236:
237: public void submit(String anAddress) {
238: SubmittingThread theThread = new SubmittingThread(anAddress,
239: this , this , true);
240: theThread.start();
241: }
242:
243: public void submit(String anAddress, String aTargetContainer,
244: boolean aRepaint) {
245: Component theTargetContainer = this .eventHandler
246: .findActionTarget(super .getContentPane(),
247: aTargetContainer);
248: SubmittingThread theThread = new SubmittingThread(anAddress,
249: this , (Container) theTargetContainer, aRepaint);
250: theThread.start();
251: }
252:
253: /**
254: * @see java.awt.event.WindowListener#windowActivated(WindowEvent)
255: */
256: public void windowActivated(WindowEvent aEvt) {
257: this .handleEvent(Constants.WINDOW_ACTIVATED);
258: }
259:
260: /**
261: * @see java.awt.event.WindowListener#windowClosed(WindowEvent)
262: */
263: public void windowClosed(WindowEvent aEvt) {
264: this .handleEvent(Constants.WINDOW_CLOSED);
265: if (this .statusBar != null) {
266: StatusBar.unRegister(this );
267: this .statusBar.disposeOfYourself();
268: this .statusBar = null;
269: }
270: }
271:
272: /**
273: * @see java.awt.event.WindowListener#windowClosing(WindowEvent)
274: */
275: public void windowClosing(WindowEvent aEvt) {
276: this .handleEvent(Constants.WINDOW_CLOSING);
277: }
278:
279: /**
280: * @see java.awt.event.WindowListener#windowDeactivated(WindowEvent)
281: */
282: public void windowDeactivated(WindowEvent aEvt) {
283: this .handleEvent(Constants.WINDOW_DEACTIVATED);
284: }
285:
286: /**
287: * @see java.awt.event.WindowListener#windowDeiconified(WindowEvent)
288: */
289: public void windowDeiconified(WindowEvent aEvt) {
290: this .handleEvent(Constants.WINDOW_DEICONIFIED);
291: }
292:
293: /**
294: * @see java.awt.event.WindowListener#windowIconified(WindowEvent)
295: */
296: public void windowIconified(WindowEvent aEvt) {
297: this .handleEvent(Constants.WINDOW_ICONIFIED);
298: }
299:
300: /**
301: * @see java.awt.event.WindowListener#windowOpened(WindowEvent)
302: */
303: public void windowOpened(WindowEvent aEvt) {
304: this.handleEvent(Constants.WINDOW_OPENED);
305: }
306: }
|