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;
018
019import org.apache.wicket.util.io.IClusterable;
020import org.apache.wicket.util.lang.Args;
021
022/**
023 * A Channel used to define how Ajax requests are processed at the client side.
024 * 
025 * Channels are either:
026 * <ul>
027 * <li>queueing - Ajax requests are kept in a Queue at the client side and processed one at a time.
028 * Default.</li>
029 * <li>dropping - only the last Ajax request is processed, all previously scheduled requests are discarded</li>
030 * <li>active - discards any Ajax requests if there is a running Ajax request on the same channel</li>
031 * </ul>
032 * 
033 * @author Martin Dilger
034 */
035public class AjaxChannel implements IClusterable
036{
037        private static final long serialVersionUID = 1L;
038
039        /**
040         * The type of an {@link AjaxChannel}
041         */
042        public static enum Type {
043
044                /**
045                 * Ajax requests are kept in a Queue at the client side and processed one at a time
046                 *
047                 * 's' comes from 'stack', but it really acts as a queue.
048                 */
049                QUEUE("s"),
050
051                /**
052                 * dropping - only the last Ajax request is processed, the others are discarded
053                 */
054                DROP("d"),
055
056                /**
057                 * the ajax call will discarded if there is an active/running request on the same channel
058                 */
059                ACTIVE("a");
060
061                private final String shortType;
062
063                Type(String shortType)
064                {
065                        this.shortType = shortType;
066                }
067        }
068
069        /**
070         * The name of the default channel
071         */
072        public static final String DEFAULT_NAME = "0";
073
074        /**
075         * The type of the default channel
076         */
077        public static final Type DEFAULT_TYPE = Type.QUEUE;
078
079        /**
080         * The default channel for all Ajax calls
081         */
082        public static final AjaxChannel DEFAULT = new AjaxChannel(DEFAULT_NAME, DEFAULT_TYPE);
083
084        private final String name;
085
086        private final Type type;
087
088        /**
089         * Construct.
090         * 
091         * @param name
092         *            the name of the channel
093         */
094        public AjaxChannel(final String name)
095        {
096                this(name, Type.QUEUE);
097        }
098
099        /**
100         * Construct.
101         * 
102         * @param name
103         *            the name of the channel
104         * @param type
105         *            the behavior type of this channel
106         */
107        public AjaxChannel(final String name, final Type type)
108        {
109                this.name = Args.notNull(name, "name");
110                this.type = Args.notNull(type, "type");
111        }
112
113        /**
114         * @return the name
115         */
116        public String getName()
117        {
118                return name;
119        }
120
121        /**
122         * @return the type of this channel
123         * @see AjaxChannel.Type
124         */
125        public Type getType()
126        {
127                return type;
128        }
129
130        /**
131         * Calculates the ChannelName.
132         * 
133         * @return a String in the format channelName|d for DropChannels, channelName|s for Stackable
134         *         Channels.
135         */
136        String getChannelName()
137        {
138                return toString();
139        }
140
141        @Override
142        public String toString()
143        {
144                return name + "|" + type.shortType;
145        }
146
147        @Override
148        public boolean equals(Object o)
149        {
150                if (this == o) return true;
151                if (o == null || getClass() != o.getClass()) return false;
152
153                AjaxChannel that = (AjaxChannel) o;
154
155                if (!name.equals(that.name)) return false;
156                if (type != that.type) return false;
157
158                return true;
159        }
160
161        @Override
162        public int hashCode()
163        {
164                int result = name.hashCode();
165                result = 31 * result + type.hashCode();
166                return result;
167        }
168}