001: /*
002: * $Id: LinksPage.java 4633 2006-02-25 16:22:21 -0800 (Sat, 25 Feb 2006)
003: * dashorst $ $Revision: 461552 $ $Date: 2006-02-25 16:22:21 -0800 (Sat, 25 Feb
004: * 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.examples.ajax.builtin;
020:
021: import wicket.WicketRuntimeException;
022: import wicket.ajax.AjaxRequestTarget;
023: import wicket.ajax.calldecorator.AjaxCallDecorator;
024: import wicket.ajax.markup.html.AjaxFallbackLink;
025: import wicket.ajax.markup.html.AjaxLink;
026: import wicket.extensions.ajax.markup.html.IndicatingAjaxLink;
027: import wicket.markup.html.basic.Label;
028: import wicket.model.PropertyModel;
029:
030: /**
031: * Ajax links demo.
032: *
033: * @author ivaynberg
034: */
035: public class LinksPage extends BasePage {
036: private int counter1 = 0;
037: private int counter2 = 0;
038: private int counter3 = 0;
039:
040: /**
041: * @return Value of counter1
042: */
043: public int getCounter1() {
044: return counter1;
045: }
046:
047: /**
048: * @return Value for counter2
049: */
050: public int getCounter2() {
051: return counter2;
052: }
053:
054: /**
055: * @return Value of counter3
056: */
057: public int getCounter3() {
058: return counter3;
059: }
060:
061: /**
062: * Constructor
063: */
064: public LinksPage() {
065: final Label c1 = new Label("c1", new PropertyModel(this ,
066: "counter1"));
067: c1.setOutputMarkupId(true);
068: add(c1);
069:
070: final Label c2 = new Label("c2", new PropertyModel(this ,
071: "counter2"));
072: c2.setOutputMarkupId(true);
073: add(c2);
074:
075: final Label c3 = new Label("c3", new PropertyModel(this ,
076: "counter3"));
077: c3.setOutputMarkupId(true);
078: add(c3);
079:
080: add(new AjaxLink("c1-link") {
081: public void onClick(AjaxRequestTarget target) {
082: counter1++;
083: target.addComponent(c1);
084: }
085: });
086:
087: add(new AjaxFallbackLink("c2-link") {
088: public void onClick(AjaxRequestTarget target) {
089: counter2++;
090: // notice that for a fallback link we need to makesure the
091: // target is not null. if the target is null ajax failed and the
092: // fallback was used, so there is no need to do any ajax-related
093: // processing.
094: if (target != null) {
095: target.addComponent(c2);
096: }
097: }
098: });
099:
100: add(new IndicatingAjaxLink("c3-link") {
101: public void onClick(AjaxRequestTarget target) {
102: counter3++;
103: target.addComponent(c3);
104: // sleep for 5 seconds to show off the busy indicator
105: try {
106: Thread.sleep(5000);
107: } catch (InterruptedException e) {
108: // noop
109: }
110: }
111: });
112:
113: add(new AjaxLink("success-link") {
114: public void onClick(AjaxRequestTarget target) {
115: }
116:
117: protected wicket.ajax.IAjaxCallDecorator getAjaxCallDecorator() {
118: return new AjaxCallDecorator() {
119: public CharSequence decorateOnSuccessScript(
120: CharSequence script) {
121: return "alert('Success');";
122: }
123:
124: public CharSequence decorateOnFailureScript(
125: CharSequence script) {
126: return "alert('Failure');";
127: }
128:
129: public CharSequence decorateScript(
130: CharSequence script) {
131: return "alert('Before ajax call');" + script;
132: }
133: };
134: };
135: });
136:
137: add(new AjaxLink("failure-link") {
138: public void onClick(AjaxRequestTarget target) {
139: throw new WicketRuntimeException("Failure link clicked");
140: }
141:
142: protected wicket.ajax.IAjaxCallDecorator getAjaxCallDecorator() {
143: return new AjaxCallDecorator() {
144: public CharSequence decorateOnSuccessScript(
145: CharSequence script) {
146: return "alert('Success');";
147: }
148:
149: public CharSequence decorateOnFailureScript(
150: CharSequence script) {
151: return "alert('Failure');";
152: }
153:
154: public CharSequence decorateScript(
155: CharSequence script) {
156: return "alert('Before ajax call');" + script;
157: }
158: };
159: };
160: });
161:
162: add(new AjaxLink("set-response-page") {
163: public void onClick(AjaxRequestTarget target) {
164: setResponsePage(LinksPage.class);
165: }
166: });
167: }
168: }
|