001: /* MilComponent.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: May 31, 2007 4:25:10 PM, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019:
020: package org.zkoss.mil;
021:
022: import org.zkoss.mil.au.in.CommandCommand;
023: import org.zkoss.xml.HTMLs;
024: import org.zkoss.zk.ui.AbstractComponent;
025: import org.zkoss.zk.ui.event.EventListener;
026: import org.zkoss.zk.ui.event.Events;
027:
028: /**
029: * Super class of MIL component.
030: *
031: * @author henrichen
032: */
033: abstract public class MilComponent extends AbstractComponent {
034: /** Returns whether to send back the request of the specified event
035: * immediately -- i.e., non-deferrable.
036: * Returns true if you want the component (on the server)
037: * to process the event immediately.
038: *
039: * <p>Default: return true if any non-deferable event listener of
040: * the specified event is found. In other words, it returns
041: * {@link Events#isListened} with asap = true.
042: */
043: protected boolean isAsapRequired(String evtnm) {
044: return Events.isListened(this , evtnm, true);
045: }
046:
047: /** Appends the HTML attribute for the specified event name, say, onChange.
048: * It is called by derived's {@link #getOuterAttrs}.
049: *
050: * @param sb the string buffer to hold the HTML attribute. If null and
051: * {@link #isAsapRequired} is true, a string buffer is created and returned.
052: * @return the string buffer. If sb is null and {@link #isAsapRequired}
053: * returns false, null is returned.
054: * If the caller passed non-null sb, the returned value must be the same
055: * as sb (so it usually ignores the returned value).
056: */
057: protected StringBuffer appendAsapAttr(StringBuffer sb, String evtnm) {
058: if (isAsapRequired(evtnm)) {
059: if (sb == null)
060: sb = new StringBuffer(80);
061: HTMLs.appendAttribute(sb, getAttrOfEvent(evtnm), "t");
062: }
063: return sb;
064: }
065:
066: private static String getAttrOfEvent(String evtnm) {
067: return "onBack".equals(evtnm) ? "bc"
068: : "onCancel".equals(evtnm) ? "cc" : "onExit"
069: .equals(evtnm) ? "ec"
070: : "onHelp".equals(evtnm) ? "hc" : "onItem"
071: .equals(evtnm) ? "in" : "onOK"
072: .equals(evtnm) ? "oc" : "onScreen"
073: .equals(evtnm) ? "sc" : "onStop"
074: .equals(evtnm) ? "sn" : "onChange"
075: .equals(evtnm) ? "on" : "onChanging"
076: .equals(evtnm) ? "og" : "onSelect"
077: .equals(evtnm) ? "os" : "onSlide"
078: .equals(evtnm) ? "ol" : "";
079: }
080:
081: /**
082: * Encode text to make it complies to XML standard.
083: * @param str the input string
084: * @return the output string that complies to XML standard.
085: */
086: protected String encodeString(String str) {
087: if (str == null)
088: return null;
089: final int len = str.length();
090: final StringBuffer sb = new StringBuffer(len + 100);
091: for (int j = 0; j < len; ++j) {
092: final char c = str.charAt(j);
093: switch (c) {
094: case '"':
095: sb.append(""");
096: break;
097: case '&':
098: sb.append("&");
099: break;
100: case '<':
101: sb.append("<");
102: break;
103: default:
104: sb.append(c);
105: }
106: }
107: return sb.toString();
108: }
109:
110: //-- Component --//
111: public boolean addEventListener(String evtnm, EventListener listener) {
112: final boolean asap = isAsapRequired(evtnm);
113: final boolean ret = super .addEventListener(evtnm, listener);
114: if (ret && !asap && isAsapRequired(evtnm))
115: smartUpdate(getAttrOfEvent(evtnm), "t");
116: return ret;
117: }
118:
119: public boolean removeEventListener(String evtnm,
120: EventListener listener) {
121: final boolean asap = isAsapRequired(evtnm);
122: final boolean ret = super .removeEventListener(evtnm, listener);
123: if (ret && asap && !isAsapRequired(evtnm))
124: smartUpdate(getAttrOfEvent(evtnm), null);
125: return ret;
126: }
127:
128: public String getOuterAttrs() {
129: return "";
130: }
131:
132: //register the MIL related event
133: static {
134: new CommandCommand("onCommand",
135: org.zkoss.zk.au.Command.IGNORE_OLD_EQUIV);
136: }
137: }
|