View Javadoc

1   /*
2    * $Id: AbstractScriptListener.java,v 1.17 2011/06/16 09:29:41 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.listener;
6   
7   import javax.faces.FacesException;
8   import javax.faces.component.StateHolder;
9   import javax.faces.context.FacesContext;
10  
11  import org.rcfaces.core.internal.renderkit.IProcessContext;
12  
13  /**
14   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
15   * @version $Revision: 1.17 $ $Date: 2011/06/16 09:29:41 $
16   */
17  public abstract class AbstractScriptListener implements IScriptListener,
18          StateHolder {
19  
20      private String scriptType;
21  
22      private String command;
23  
24      private int hashCode = 0;
25  
26      public AbstractScriptListener(String scriptType, String command) {
27          if (command == null) {
28              throw new NullPointerException(
29                      "Can not create AbstractScriptListener: parameter is null.");
30          }
31  
32          this.scriptType = scriptType;
33          this.command = command;
34          this.hashCode = computeHashCode();
35      }
36  
37      public AbstractScriptListener() {
38      }
39  
40      public final String getScriptType(IProcessContext processContext) {
41          if (scriptType == null) {
42              scriptType = processContext.getScriptType();
43  
44              if (scriptType == null) {
45                  throw new FacesException(
46                          "No script type defined ! (You may use an init tag to resolve this problem)");
47              }
48  
49          }
50          return scriptType;
51      }
52  
53      public final String getCommand() {
54          return command;
55      }
56  
57      protected int computeHashCode() {
58          int hashCode = 0;
59  
60          if (scriptType != null) {
61              hashCode ^= scriptType.hashCode();
62          }
63  
64          if (command != null) {
65              hashCode ^= command.hashCode();
66          }
67  
68          return hashCode;
69      }
70  
71      /*
72       * (non-Javadoc)
73       * 
74       * @see java.lang.Object#hashCode()
75       */
76      public int hashCode() {
77          return hashCode;
78      }
79  
80      public boolean equals(Object object) {
81          if (object == null
82                  || (object instanceof AbstractScriptListener) == false) {
83              return false;
84          }
85  
86          AbstractScriptListener s = (AbstractScriptListener) object;
87  
88          if (scriptType != s.scriptType) {
89              if (scriptType == null || scriptType.equals(s.scriptType) == false) {
90                  return false;
91              }
92          }
93  
94          if (command != s.command) {
95              if (command == null || command.equals(s.command) == false) {
96                  return false;
97              }
98          }
99  
100         return true;
101     }
102 
103     /*
104      * (non-Javadoc)
105      * 
106      * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext,
107      *      java.lang.Object)
108      */
109     public final void restoreState(FacesContext context, Object state) {
110         String s[] = (String[]) state;
111 
112         scriptType = s[0];
113         command = s[1];
114         hashCode = computeHashCode();
115     }
116 
117     /*
118      * (non-Javadoc)
119      * 
120      * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
121      */
122     public final Object saveState(FacesContext context) {
123         return new String[] { scriptType, command };
124     }
125 
126     /*
127      * (non-Javadoc)
128      * 
129      * @see javax.faces.component.StateHolder#isTransient()
130      */
131     public boolean isTransient() {
132         return false;
133     }
134 
135     /*
136      * (non-Javadoc)
137      * 
138      * @see javax.faces.component.StateHolder#setTransient(boolean)
139      */
140     public void setTransient(boolean newTransientValue) {
141     }
142 }