001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.ajax.attributes; 018 019import org.apache.wicket.Component; 020import org.apache.wicket.markup.head.IHeaderResponse; 021import org.apache.wicket.markup.html.IComponentAwareHeaderContributor; 022import org.apache.wicket.util.string.Strings; 023 024/** 025 * An adapter for implementations of IAjaxCallListener. 026 * 027 * @since 6.0 028 */ 029public class AjaxCallListener implements IAjaxCallListener, IComponentAwareHeaderContributor 030{ 031 private StringBuilder init; 032 private StringBuilder success; 033 private StringBuilder failure; 034 private StringBuilder before; 035 private StringBuilder beforeSend; 036 private StringBuilder after; 037 private StringBuilder complete; 038 private StringBuilder precondition; 039 private StringBuilder done; 040 041 /** 042 * Sets the JavaScript code that will be returned by {@link #getInitHandler(Component)}. 043 * If this code was already set, the new one will be appended to the existing one. 044 * 045 * @param init 046 * the JavaScript code for the corresponding handler 047 * @return This 048 */ 049 public AjaxCallListener onInit(final CharSequence init) 050 { 051 if (Strings.isEmpty(init) == false) 052 { 053 if (this.init == null) 054 { 055 this.init = new StringBuilder(); 056 } 057 this.init.append(init); 058 } 059 return this; 060 } 061 062 /** 063 * Sets the JavaScript code that will be returned by {@link #getDoneHandler(Component)}. 064 * If this code was already set, the new one will be appended to the existing one. 065 * 066 * @param done 067 * the JavaScript code for the corresponding handler 068 * @return This 069 */ 070 public AjaxCallListener onDone(final CharSequence done) 071 { 072 if (Strings.isEmpty(done) == false) 073 { 074 if (this.done == null) 075 { 076 this.done = new StringBuilder(); 077 } 078 this.done.append(done); 079 } 080 return this; 081 } 082 083 /** 084 * Sets the JavaScript code that will be returned by {@link #getBeforeHandler(Component)}. 085 * If this code was already set, the new one will be appended to the existing one. 086 * 087 * @param before 088 * the JavaScript code for the corresponding handler 089 * @return This 090 */ 091 public AjaxCallListener onBefore(final CharSequence before) 092 { 093 if (Strings.isEmpty(before) == false) 094 { 095 if (this.before == null) 096 { 097 this.before = new StringBuilder(); 098 } 099 this.before.append(before); 100 } 101 return this; 102 } 103 104 /** 105 * Sets the JavaScript code that will be returned by {@link #getBeforeSendHandler(Component)}. 106 * If this code was already set, the new one will be appended to the existing one. 107 * 108 * @param beforeSend 109 * the JavaScript code for the corresponding handler 110 * @return This 111 */ 112 public AjaxCallListener onBeforeSend(final CharSequence beforeSend) 113 { 114 if (Strings.isEmpty(beforeSend) == false) 115 { 116 if (this.beforeSend == null) 117 { 118 this.beforeSend = new StringBuilder(); 119 } 120 this.beforeSend.append(beforeSend); 121 } 122 return this; 123 } 124 125 /** 126 * Sets the JavaScript code that will be returned by {@link #getAfterHandler(Component)}. 127 * If this code was already set, the new one will be appended to the existing one. 128 * 129 * @param after 130 * the JavaScript code for the corresponding handler 131 * @return This 132 */ 133 public AjaxCallListener onAfter(final CharSequence after) 134 { 135 if (Strings.isEmpty(after) == false) 136 { 137 if (this.after == null) 138 { 139 this.after = new StringBuilder(); 140 } 141 this.after.append(after); 142 } 143 return this; 144 } 145 146 /** 147 * Sets the JavaScript code that will be returned by {@link #getSuccessHandler(Component)}. 148 * If this code was already set, the new one will be appended to the existing one. 149 * 150 * @param success 151 * the JavaScript code for the corresponding handler 152 * @return This 153 */ 154 public AjaxCallListener onSuccess(final CharSequence success) 155 { 156 if (Strings.isEmpty(success) == false) 157 { 158 if (this.success == null) 159 { 160 this.success = new StringBuilder(); 161 } 162 this.success.append(success); 163 } 164 return this; 165 } 166 167 /** 168 * Sets the JavaScript code that will be returned by {@link #getFailureHandler(Component)}. 169 * If this code was already set, the new one will be appended to the existing one. 170 * 171 * @param failure 172 * the JavaScript code for the corresponding handler 173 * @return This 174 */ 175 public AjaxCallListener onFailure(final CharSequence failure) 176 { 177 if (Strings.isEmpty(failure) == false) 178 { 179 if (this.failure == null) 180 { 181 this.failure = new StringBuilder(); 182 } 183 this.failure.append(failure); 184 } 185 return this; 186 } 187 188 /** 189 * Sets the JavaScript code that will be returned by {@link #getCompleteHandler(Component)}. 190 * If this code was already set, the new one will be appended to the existing one. 191 * 192 * @param complete 193 * the JavaScript code for the corresponding handler 194 * @return This 195 */ 196 public AjaxCallListener onComplete(final CharSequence complete) 197 { 198 if (Strings.isEmpty(complete) == false) 199 { 200 if (this.complete == null) 201 { 202 this.complete = new StringBuilder(); 203 } 204 this.complete.append(complete); 205 } 206 return this; 207 } 208 209 /** 210 * Sets the JavaScript code that will be returned by {@link #getPrecondition(Component)}. 211 * If this code was already set, the new one will be appended to the existing one. 212 * 213 * @param precondition 214 * the JavaScript code for the precondition 215 * @return This 216 */ 217 public AjaxCallListener onPrecondition(final CharSequence precondition) 218 { 219 if (Strings.isEmpty(precondition) == false) 220 { 221 if (this.precondition == null) 222 { 223 this.precondition = new StringBuilder(); 224 } 225 this.precondition.append(precondition); 226 } 227 return this; 228 } 229 230 @Override 231 public CharSequence getSuccessHandler(Component component) 232 { 233 return success; 234 } 235 236 @Override 237 public CharSequence getFailureHandler(Component component) 238 { 239 return failure; 240 } 241 242 @Override 243 public CharSequence getInitHandler(Component component) 244 { 245 return init; 246 } 247 248 @Override 249 public CharSequence getBeforeHandler(Component component) 250 { 251 return before; 252 } 253 254 @Override 255 public CharSequence getBeforeSendHandler(Component component) 256 { 257 return beforeSend; 258 } 259 260 @Override 261 public CharSequence getAfterHandler(Component component) 262 { 263 return after; 264 } 265 266 @Override 267 public CharSequence getCompleteHandler(Component component) 268 { 269 return complete; 270 } 271 272 @Override 273 public CharSequence getPrecondition(Component component) 274 { 275 return precondition; 276 } 277 278 @Override 279 public CharSequence getDoneHandler(Component component) { 280 return done; 281 } 282 283 @Override 284 public void renderHead(Component component, IHeaderResponse response) 285 { 286 } 287}