001: /*
002: * Copyright 2000,2006 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.plaf.css;
014:
015: import java.util.ArrayList;
016: import java.util.HashSet;
017: import java.util.List;
018:
019: import org.wings.SComponent;
020: import org.wings.SPopup;
021: import org.wings.SPopupInterface;
022: import org.wings.header.SessionHeaders;
023: import org.wings.io.Device;
024: import java.io.IOException;
025: import org.wings.io.StringBuilderDevice;
026: import org.wings.plaf.css.dwr.CallableManager;
027: import org.wings.plaf.css.script.OnHeadersLoadedScript;
028: import org.wings.util.SStringBuilder;
029:
030: /**
031: * CG for SPopup instances.
032: *
033: * @author Christian Schyma
034: */
035: public final class PopupCG extends AbstractComponentCG implements
036: org.wings.plaf.PopupCG, SPopupInterface {
037:
038: private static final long serialVersionUID = 1L;
039:
040: protected final List headers = new ArrayList();
041:
042: private SPopup popup;
043:
044: private String popupJSObject;
045: private String dwrJsObject;
046:
047: private String showFunction;
048: private String hideFunction;
049:
050: public PopupCG(SPopup popup) {
051: this .popup = popup;
052:
053: headers
054: .add(Utils
055: .createExternalizedJSHeaderFromProperty(Utils.JS_ETC_POPUP));
056: SessionHeaders.getInstance().registerHeaders(headers);
057:
058: String name = this .popup.getComponent().getName();
059: this .popupJSObject = name + "_popup";
060: this .dwrJsObject = name + "_popup_dwr";
061: this .showFunction = "function() {" + popupJSObject + ".show()}";
062: this .hideFunction = "function() {" + popupJSObject + ".hide()}";
063:
064: // expose data source to java script by using DWR
065: CallableManager.getInstance().registerCallable(
066: this .dwrJsObject, this , SPopupInterface.class);
067:
068: attachJavaScript();
069: }
070:
071: public void attachJavaScript() {
072: popup.getSession().getScriptManager().addScriptListener(
073: new OnHeadersLoadedScript(generateInitScript()));
074: }
075:
076: public void tidyUp() {
077: SessionHeaders.getInstance().deregisterHeaders(headers);
078: CallableManager.getInstance().unregisterCallable(dwrJsObject);
079: }
080:
081: private String generateInitScript() {
082: SStringBuilder code = new SStringBuilder();
083:
084: if (this .popup.isAnchored()) {
085: code.append(popupJSObject).append(" = new wingS.Popup(")
086: .append("'").append(popupJSObject).append("', ")
087: .append(this .dwrJsObject).append(", ")
088: .append("0, ").append("0, ").append(
089: this .popup.getWidth()).append(", ").append(
090: this .popup.getHeight()).append(", ")
091: .append("'").append(
092: this .popup.getContext().getName()).append(
093: "', ").append("'").append(
094: this .popup.getContentsCorner()).append(
095: "', ").append("'").append(
096: this .popup.getContextCorner()).append("'")
097: .append(")");
098: } else {
099: code.append(this .popupJSObject).append(
100: " = new wingS.Popup(").append("'").append(
101: popupJSObject).append("', ").append(
102: this .dwrJsObject).append(", ").append(
103: this .popup.getX()).append(", ").append(
104: this .popup.getY()).append(", ").append(
105: this .popup.getWidth()).append(", ").append(
106: this .popup.getHeight()).append(")");
107: }
108:
109: return code.toString();
110: }
111:
112: /**
113: * @return JavaScript function to use a the client side to show the popup
114: */
115: public String getJsShowFunction() {
116: return this .showFunction;
117: }
118:
119: /**
120: * @return JavaScript function to use a the client side to hide the popup
121: */
122: public String getJsHideFunction() {
123: return this .hideFunction;
124: }
125:
126: public String getRenderedContent() {
127: StringBuilderDevice device = new StringBuilderDevice(128);
128:
129: if (this .popup.getComponent() != null) {
130: try {
131: this .popup.getComponent().write(device);
132: } catch (IOException ex) {
133: ex.printStackTrace();
134: }
135: }
136:
137: return device.toString();
138: }
139:
140: public void writeInternal(Device device, SComponent component)
141: throws IOException {
142: }
143:
144: }
|