001 package org.gwtbootstrap3.extras.typeahead.client.base;
002
003 /*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2014 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 * http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024 /**
025 * A Dataset represents a collection of suggestion objects for a Typeahead.
026 * A Typeahead is composed of one or more Datasets. When an end-user modifies
027 * the value of a Typeahead, each Dataset will attempt to render suggestions
028 * for the new value.
029 *
030 * @author Florian Kremser <florian.kremser@sage.com>
031 */
032 public abstract class Dataset<T> {
033 private static long nextId = 0;
034 private String name;
035 private Template emptyTemplate;
036 private Template footerTemplate;
037 private Template headerTemplate;
038 private SuggestionTemplate<T> suggestionTemplate;
039
040 protected Dataset() {
041 this.name = "dataset" + nextId++;
042 }
043
044 public String getName() {
045 return this.name;
046 }
047
048 public void setName(final String name) {
049 this.name = name;
050 }
051
052 /**
053 * Rendered when 0 suggestions are available for the given query.
054 *
055 * @return a template to render in case of 0 results
056 */
057 public Template getEmptyTemplate() {
058 return emptyTemplate;
059 }
060
061 public void setEmptyTemplate(final Template emptyTemplate) {
062 this.emptyTemplate = emptyTemplate;
063 }
064
065 /**
066 * Rendered at the bottom of the dataset.
067 *
068 * @return a template to render at the bottom of the dataset
069 */
070 public Template getFooterTemplate() {
071 return footerTemplate;
072 }
073
074 public void setFooterTemplate(final Template footerTemplate) {
075 this.footerTemplate = footerTemplate;
076 }
077
078 /**
079 * Rendered at the top of the dataset.
080 *
081 * @return a template to render at the top of the dataset
082 */
083 public Template getHeaderTemplate() {
084 return headerTemplate;
085 }
086
087 public void setHeaderTemplate(final Template headerTemplate) {
088 this.headerTemplate = headerTemplate;
089 }
090
091 /**
092 * Renders a single suggestion.
093 *
094 * @return a template for rendering suggestions
095 */
096 public SuggestionTemplate<T> getSuggestionTemplate() {
097 return suggestionTemplate;
098 }
099
100 public void setSuggestionTemplate(final SuggestionTemplate<T> suggestionTemplate) {
101 this.suggestionTemplate = suggestionTemplate;
102 }
103
104 /**
105 * Find all Suggestions matching a search query and pass them to the callback.
106 *
107 * @param query the user input
108 * @param callback callback for suggestions
109 */
110 public abstract void findMatches(final String query, final SuggestionCallback<T> callback);
111
112 @Override
113 public boolean equals(final Object o) {
114 if (this == o) return true;
115 if (o == null || getClass() != o.getClass()) return false;
116 Dataset<?> dataset = (Dataset<?>) o;
117 if (name != null ? !name.equals(dataset.name) : dataset.name != null) return false;
118 return true;
119 }
120
121 @Override
122 public int hashCode() {
123 return name != null ? name.hashCode() : 0;
124 }
125 }