1 14 package org.wings.script; 15 16 import org.wings.SComponent; 17 18 public class JavaScriptListener 19 implements ScriptListener { 20 private String event; 21 private String code; 22 private String script; 23 private SComponent[] components; 24 25 29 public JavaScriptListener(String event, String code) { 30 this.event = event; 31 this.code = code; 32 } 33 34 40 public JavaScriptListener(String event, String code, String script) { 41 this.event = event; 42 this.code = code; 43 this.script = script; 44 } 45 46 55 public JavaScriptListener(String event, String code, SComponent[] components) { 56 this.event = event; 57 this.code = substituteIds(code, components); 58 this.components = components; 59 } 60 61 72 public JavaScriptListener(String event, String code, String script, SComponent[] components) { 73 this.event = event; 74 this.code = substituteIds(code, components); 75 this.script = substituteIds(script, components); 76 this.components = components; 77 } 78 79 public void setEvent(String event) { 80 this.event = event; 81 } 82 83 public String getEvent() { return event; } 84 85 public void setCode(String code) { 86 this.code = code; 87 } 88 89 public String getCode() { return code; } 90 91 public void setScript(String script) { 92 this.script = script; 93 } 94 95 public String getScript() { return script; } 96 97 public void setComponents(SComponent[] components) { 98 this.components = components; 99 } 100 101 public SComponent[] getComponents() { return components; } 102 103 private String substituteIds(String code, SComponent[] components) { 104 StringBuffer buffer = new StringBuffer (); 105 106 int startPos = 0; 107 int endPos = 0; 108 char lastChar = code.charAt(0); 109 for (int i = 1; i < code.length(); ++i) { 110 char c = code.charAt(i); 111 endPos = i; 112 if (lastChar == '{' && Character.isDigit(c)) { 113 int varIndex = (int) (c - '0'); 114 while (Character.isDigit(code.charAt(++i))) { 115 varIndex *= 10; 116 varIndex += (int) (code.charAt(i) - '0'); 117 } 118 c = code.charAt(i); 119 if (c == '}') { 120 buffer.append(code.substring(startPos, endPos - 1)); 121 startPos = i + 1; 122 buffer.append(components[varIndex].getName()); 123 } else { 124 throw new IllegalArgumentException ("Expected closing '}' after '{" + varIndex + "'"); 125 } 126 } 127 lastChar = c; 128 } 129 buffer.append(code.substring(startPos)); 130 131 return buffer.toString(); 132 } 133 134 137 public boolean equals(Object obj) { 138 if (obj.getClass() != JavaScriptListener.class) 139 return false; 140 JavaScriptListener testObj = (JavaScriptListener) obj; 141 142 if (testObj.getEvent() == null) { 143 if (getEvent() != null) 144 return false; 145 } else { 146 if (!testObj.getEvent().equals(getEvent())) 147 return false; 148 } 149 150 if (testObj.getCode() == null) { 151 if (getCode() != null) 152 return false; 153 } else { 154 if (!testObj.getCode().equals(getCode())) 155 return false; 156 } 157 158 if (testObj.getComponents() == null) { 159 if (getComponents() != null) 160 return false; 161 } else { 162 if (!testObj.getComponents().equals(getComponents())) 163 return false; 164 } 165 166 if (testObj.getScript() == null) { 167 if (getScript() != null) 168 return false; 169 } else { 170 if (!testObj.getScript().equals(getScript())) 171 return false; 172 } 173 174 return true; 175 } 176 177 } 178 | Popular Tags |