001: /*
002: * Copyright 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:
014: package org.wingx.plaf.css;
015:
016: import org.wings.SComponent;
017: import org.wings.SIcon;
018: import org.wings.event.SParentFrameEvent;
019: import org.wings.event.SParentFrameListener;
020: import org.wings.header.SessionHeaders;
021: import org.wings.io.Device;
022: import org.wings.plaf.css.LabelCG;
023: import org.wings.plaf.css.Utils;
024: import org.wings.plaf.css.dwr.CallableManager;
025: import org.wings.plaf.css.script.OnHeadersLoadedScript;
026: import org.wings.resource.ResourceManager;
027: import org.wings.session.ScriptManager;
028: import org.wings.util.SStringBuilder;
029: import org.wingx.XInplaceEditor;
030: import org.wingx.XInplaceEditorInterface;
031:
032: import java.io.IOException;
033: import java.util.ArrayList;
034: import java.util.List;
035:
036: /**
037: *
038: * @author Christian Schyma
039: */
040: public class InplaceEditorCG extends LabelCG implements
041: SParentFrameListener {
042:
043: protected final List headers = new ArrayList();
044:
045: // these graphics are needed for inplaceeditor.js
046: static {
047: ((SIcon) ResourceManager.getObject("InplaceEditorCG.okButton",
048: SIcon.class)).getURL();
049: ((SIcon) ResourceManager.getObject(
050: "InplaceEditorCG.cancelButton", SIcon.class)).getURL();
051: }
052:
053: public InplaceEditorCG() {
054: // TODO move to properties file
055: headers
056: .add(Utils
057: .createExternalizedJSHeader("org/wingx/inplaceeditor/inplaceeditor.js"));
058: headers
059: .add(Utils
060: .createExternalizedCSSHeader("org/wingx/inplaceeditor/inplaceeditor.css"));
061: headers
062: .add(Utils
063: .createExternalizedJSHeader("org/wingx/js/MochiKit/Base.js"));
064: headers
065: .add(Utils
066: .createExternalizedJSHeader("org/wingx/js/MochiKit/Iter.js"));
067: headers
068: .add(Utils
069: .createExternalizedJSHeader("org/wingx/js/MochiKit/DOM.js"));
070: headers
071: .add(Utils
072: .createExternalizedJSHeader("org/wingx/js/MochiKit/Style.js"));
073: }
074:
075: public void installCG(final SComponent comp) {
076: super .installCG(comp);
077: comp.addParentFrameListener(this );
078: }
079:
080: public void writeInternal(Device device, SComponent component)
081: throws IOException {
082: super .writeInternal(device, component);
083:
084: String script = generateInitScript((XInplaceEditor) component);
085: ScriptManager.getInstance().addScriptListener(
086: new OnHeadersLoadedScript(script, false));
087: }
088:
089: private String generateInitScript(XInplaceEditor inplace) {
090: SStringBuilder options = new SStringBuilder("{");
091: options.append("cols: ").append(inplace.getCols()).append(",")
092: .append("rows: ").append(inplace.getRows()).append(",")
093: .append("timeout: ").append(inplace.getTimeout())
094: .append(",").append("clickNotificationText: '").append(
095: inplace.getClickNotificationText())
096: .append("'}");
097:
098: SStringBuilder script = new SStringBuilder(
099: "function () {new wingS.InplaceEditor(\"");
100: script.append(inplace.getName()).append("\", ").append(
101: inplace.getClientProperty("DWR_JS_OBJECT"))
102: .append(", ").append(options.toString()).append(");}");
103:
104: return script.toString();
105: }
106:
107: public void parentFrameAdded(SParentFrameEvent e) {
108: SessionHeaders.getInstance().registerHeaders(headers);
109:
110: SComponent c = e.getComponent();
111: String DWR_JS_OBJECT = c.getName() + "_inplace";
112: c.putClientProperty("DWR_JS_OBJECT", DWR_JS_OBJECT);
113:
114: // expose data source to java script by using DWR
115: CallableManager.getInstance().registerCallable(DWR_JS_OBJECT,
116: c, XInplaceEditorInterface.class);
117: }
118:
119: public void parentFrameRemoved(SParentFrameEvent e) {
120: SessionHeaders.getInstance().deregisterHeaders(headers);
121:
122: String DWR_JS_OBJECT = (String) e.getComponent()
123: .getClientProperty("DWR_JS_OBJECT");
124: CallableManager.getInstance().unregisterCallable(DWR_JS_OBJECT);
125: }
126:
127: }
|