com.googlecode.wicket.jquery.ui.form.spinner.Spinner Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googlecode.wicket.jquery.ui.form.spinner;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.lang.Args;
import com.googlecode.wicket.jquery.core.IJQueryCultureWidget;
import com.googlecode.wicket.jquery.core.JQueryBehavior;
import com.googlecode.wicket.jquery.core.Options;
import com.googlecode.wicket.jquery.ui.JQueryUIBehavior;
/**
* Provides a jQuery spinner based on a {@link TextField}
*
* @param the type of the model object
* @author Sebastien Briquet - sebfz1
* @since 1.5.0
*/
public class Spinner extends TextField implements IJQueryCultureWidget
{
private static final long serialVersionUID = 1L;
protected final Options options;
/**
* Constructor
*
* @param id the markup id
*/
public Spinner(final String id)
{
this(id, new Options());
}
/**
* Constructor
*
* @param id the markup id
* @param options the {@link Options}
*/
public Spinner(String id, Options options)
{
this(id, options, null);
}
/**
* Constructor
*
* @param id the markup id
* @param type type for field validation
*/
public Spinner(final String id, final Class type)
{
this(id, new Options(), type);
}
/**
* Constructor
*
* @param id the markup id
* @param options the {@link Options}
* @param type Type for field validation
*/
public Spinner(final String id, Options options, final Class type)
{
super(id, type);
this.options = Args.notNull(options, "options");
}
/**
* Constructor
*
* @param id the markup id
* @param model the {@link IModel}
*/
public Spinner(final String id, final IModel model)
{
this(id, model, new Options(), null);
}
/**
* Constructor
*
* @param id the markup id
* @param model the {@link IModel}
* @param options the {@link Options}
*/
public Spinner(String id, final IModel model, Options options)
{
this(id, model, options, null);
}
/**
* Constructor
*
* @param id the markup id
* @param model the {@link IModel}
* @param type type for field validation
*/
public Spinner(final String id, final IModel model, final Class type)
{
this(id, model, new Options(), type);
}
/**
* Constructor
*
* @param id the markup id
* @param model the {@link IModel}
* @param options the {@link Options}
* @param type Type for field validation
*/
public Spinner(final String id, final IModel model, Options options, final Class type)
{
super(id, model, type);
this.options = Args.notNull(options, "options");
}
// Events //
@Override
protected void onInitialize()
{
super.onInitialize();
this.add(JQueryWidget.newWidgetBehavior(this)); // cannot be in ctor as the markupId may be set manually afterward
}
@Override
protected void onConfigure()
{
super.onConfigure();
this.setDisabled(!this.isEnabledInHierarchy());
}
@Override
public void onConfigure(JQueryBehavior behavior)
{
// noop
}
@Override
public void onBeforeRender(JQueryBehavior behavior)
{
// noop
}
// Options //
/**
* Sets the culture to use for parsing and formatting the value.
* More: https://github.com/jquery/globalize
*
* @param culture the culture to be used
* @return this, for chaining
*
*/
@Override
public Spinner setCulture(final String culture)
{
this.options.set("culture", Options.asString(culture));
return this;
}
@Override
public String getCulture()
{
return this.options.get("culture");
}
/**
* Disables the spinner, if set to true.
*
* @param disabled whether the spinner is (visually) disabled
* @return this, for chaining
*/
private Spinner setDisabled(final boolean disabled)
{
this.options.set("disabled", disabled);
return this;
}
/**
* Sets the min.
*
* @param min the min
* @return this, for chaining
*/
public Spinner setMin(final Number min)
{
this.options.set("min", min);
return this;
}
/**
* Sets the min.
* If Globalize is included, the min option can be passed as a string which will be parsed based on the numberFormat and culture options; otherwise it will fall back to the native parseFloat() method.
* More: https://github.com/jquery/globalize
*
* @param min the min
* @return this, for chaining
*/
public Spinner setMin(final String min)
{
this.options.set("min", Options.asString(min));
return this;
}
/**
* Sets the max.
*
* @param max the max
* @return this, for chaining
*/
public Spinner setMax(final Number max)
{
this.options.set("max", max);
return this;
}
/**
* Sets the max.
* If Globalize is included, the max option canbe passed as a string which will be parsed based on the numberFormat and culture options; otherwise it will fall back to the native parseFloat() method.
* More: https://github.com/jquery/globalize
*
* @param max the max
* @return this, for chaining
*/
public Spinner setMax(final String max)
{
this.options.set("max", Options.asString(max));
return this;
}
// Not activated for now, because of currency issue in Wicket (space before the currency symbol, WICKET-4988) and an issue in Java (space as thousand separator, in fr_FR for instance).
// /**
// * Format of numbers passed to Globalize, if available. Most common are "n" for a decimal number and "C" for a currency value. Also see the culture option.
// * More: https://github.com/jquery/globalize
// * More: http://api.jqueryui.com/spinner/#option-culture
// *
// * @param format the number format
// * @return this, for chaining
// */
// public Spinner setNumberFormat(final String format)
// {
// this.options.set("numberFormat", Options.asString(format));
//
// return this;
// }
/**
* Sets the number of steps to take when paging via the pageUp/pageDown methods.
*
* @param steps the number of steps. Default is 10
* @return this, for chaining
*/
public Spinner setPage(final Number steps)
{
this.options.set("page", steps);
return this;
}
/**
* Sets the size of the step to take when spinning via buttons or via the stepUp()/stepDown() methods. The element's step attribute is used if it exists and the option is not explicitly set.
*
* @param size the size of the step. Default is 1
* @return this, for chaining
*/
public Spinner setStep(final Number size)
{
this.options.set("step", size);
return this;
}
// IJQueryWidget //
@Override
public JQueryUIBehavior newWidgetBehavior(String selector)
{
return new SpinnerBehavior(selector, new SpinnerAdapter(), this.options);
}
}