KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > script > JavaScriptListener


1 /*
2  * $Id: JavaScriptListener.java,v 1.7 2005/04/27 09:06:07 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.script;
15
16 import org.wings.SComponent;
17
18 public class JavaScriptListener
19         implements ScriptListener {
20     private String JavaDoc event;
21     private String JavaDoc code;
22     private String JavaDoc script;
23     private SComponent[] components;
24
25     /**
26      * @param event one of 'onclick', 'onmouseover', ..
27      * @param code the code that is written as a value of the event attribute
28      */

29     public JavaScriptListener(String JavaDoc event, String JavaDoc code) {
30         this.event = event;
31         this.code = code;
32     }
33
34     /**
35      * @param event one of 'onclick', 'onmouseover', ..
36      * @param code the code that is written as a value of the event attribute
37      * @param script larger code block (java script functions), that is written
38      * to a combined script file, that is linked in the header
39      */

40     public JavaScriptListener(String JavaDoc event, String JavaDoc code, String JavaDoc script) {
41         this.event = event;
42         this.code = code;
43         this.script = script;
44     }
45
46     /**
47      * The code is parsed and all occurrences of '{0..components.length}' are substituted
48      * with that component's id. You can use this to address elements by id.
49      * This mechanism is highly dependant on the code, the plaf generates for a component.
50      *
51      * @param event one of 'onclick', 'onmouseover', ..
52      * @param code the code that is written as a value of the event attribute
53      * @param components the components whose ids are substituted into the code
54      */

55     public JavaScriptListener(String JavaDoc event, String JavaDoc code, SComponent[] components) {
56         this.event = event;
57         this.code = substituteIds(code, components);
58         this.components = components;
59     }
60
61     /**
62      * The code is parsed and all occurrences of '{0..components.length}' are substituted
63      * with that component's id. You can use this to address elements by id.
64      * This mechanism is highly dependant on the code, the plaf generates for a component.
65      *
66      * @param event one of 'onclick', 'onmouseover', ..
67      * @param code the code that is written as a value of the event attribute
68      * @param script larger code block (java script functions), that is written
69      * to a separate script file, that is linked in the header
70      * @param components the components whose ids are substituted into the code
71      */

72     public JavaScriptListener(String JavaDoc event, String JavaDoc code, String JavaDoc 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 JavaDoc event) {
80         this.event = event;
81     }
82
83     public String JavaDoc getEvent() { return event; }
84
85     public void setCode(String JavaDoc code) {
86         this.code = code;
87     }
88
89     public String JavaDoc getCode() { return code; }
90
91     public void setScript(String JavaDoc script) {
92         this.script = script;
93     }
94
95     public String JavaDoc 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 JavaDoc substituteIds(String JavaDoc code, SComponent[] components) {
104         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc("Expected closing '}' after '{" + varIndex + "'");
125                 }
126             }
127             lastChar = c;
128         }
129         buffer.append(code.substring(startPos));
130
131         return buffer.toString();
132     }
133
134     /* (non-Javadoc)
135      * @see java.lang.Object#equals(java.lang.Object)
136      */

137     public boolean equals(Object JavaDoc 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