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.wings.script;
015:
016: import org.wings.SComponent;
017: import org.wings.util.SStringBuilder;
018:
019: /**
020: * Specialized ScriptListener for DOM events. The original JavaScriptListener
021: * uses inline events (e.g. <p onclick="alert('huzza!')">), this one the events of
022: * the DOM.
023: * @author Christian Schyma
024: */
025: public class JavaScriptDOMListener extends JavaScriptListener {
026:
027: /**
028: * object to override default event scope
029: */
030: private String customObject = null;
031:
032: /**
033: * the component this listener is associated with
034: */
035: private SComponent component;
036:
037: /**
038: * @param event one of JavaScriptDOMEvent (e.g. JavaScriptDOMEvent.ON_CLICK) to act on
039: * @param code callback function fired on event
040: * @param component the component this listener is associated with
041: */
042: public JavaScriptDOMListener(String event, String code,
043: SComponent component) {
044: super (event, code);
045: this .component = component;
046: }
047:
048: /**
049: * @param event one of JavaScriptDOMEvent (e.g. JavaScriptDOMEvent.ON_CLICK) to act on
050: * @param code callback function fired on event
051: * @param customObject object that is used for the execution scope (so it becomes "this" in the callback) instead of
052: * the default execution scope of the element the event was fired upon
053: * @param component the component this listener is associated with
054: */
055: public JavaScriptDOMListener(String event, String code,
056: String customObject, SComponent component) {
057: super (event, code);
058: this .customObject = customObject;
059: this .component = component;
060: }
061:
062: /**
063: * Returns executable code to initialize the JavaScript listener for the
064: * given component.
065: * @return init code
066: */
067: public String getScript() {
068: SStringBuilder elementId = new SStringBuilder();
069: elementId.append("'").append(component.getName()).append("'");
070:
071: // special Yahoo UI event, see JavaScriptDOMEvent.ON_AVAILABLE docu for more
072: if (this .getEvent().compareTo(JavaScriptEvent.ON_AVAILABLE) == 0) {
073:
074: SStringBuilder initCode = new SStringBuilder();
075: initCode.append("YAHOO.util.Event.onAvailable(").append(
076: elementId).append(", ").append(this .getCode())
077: .append("); ");
078: return initCode.toString();
079:
080: } else {
081:
082: // the 'on' has to be removed for W3C DOM Event Handling
083: // e.g. 'onload' becomes 'load''
084: String modifiedEventName = this .getEvent();
085: if (modifiedEventName.startsWith("on")) {
086: modifiedEventName = modifiedEventName.substring(2);
087: }
088:
089: // some events are only registerable to special browser objects
090: if (modifiedEventName.compareTo("load") == 0)
091: elementId.delete(0, elementId.length())
092: .append("window");
093: else if (modifiedEventName.compareTo("resize") == 0)
094: elementId.delete(0, elementId.length())
095: .append("window");
096: else if (modifiedEventName.compareTo("scroll") == 0)
097: elementId.delete(0, elementId.length())
098: .append("window");
099: else if (modifiedEventName.compareTo("focus") == 0)
100: elementId.delete(0, elementId.length()).append(
101: "document");
102:
103: SStringBuilder initCode = new SStringBuilder();
104: initCode.append("YAHOO.util.Event.addListener(").append(
105: elementId).append(", ").append("'").append(
106: modifiedEventName).append("', ").append(
107: this .getCode());
108:
109: // modifies the scope of the callback function
110: if (this .customObject != null) {
111: initCode.append(", ").append(customObject).append(
112: ", true");
113: }
114:
115: initCode.append(");");
116:
117: return initCode.toString();
118:
119: }
120: }
121:
122: }
|