org.wicketstuff.jquery.ui.widget.progressbar.ProgressBar 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 org.wicketstuff.jquery.ui.widget.progressbar;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.lang.Args;
import org.wicketstuff.jquery.core.JQueryBehavior;
import org.wicketstuff.jquery.core.JQueryEvent;
import org.wicketstuff.jquery.core.JQueryGenericContainer;
import org.wicketstuff.jquery.core.ajax.IJQueryAjaxAware;
import org.wicketstuff.jquery.core.event.IValueChangedListener;
import org.wicketstuff.jquery.ui.ajax.OnChangeAjaxBehavior.ChangeEvent;
/**
* Provides a jQuery progress-bar based on a {@link JQueryGenericContainer}
*
* @author Sebastien Briquet - sebfz1
* @since 1.0
*/
public class ProgressBar extends JQueryGenericContainer implements IJQueryAjaxAware, IValueChangedListener
{
private static final long serialVersionUID = 1L;
private static final int MIN = 0;
private static final int MAX = 100;
/**
* Constructor
*
* @param id the markup id
*/
public ProgressBar(String id)
{
super(id);
}
/**
* Constructor
*
* @param id the markup id
* @param model the {@link IModel}
*/
public ProgressBar(String id, IModel model)
{
super(id, model);
}
// Properties //
/**
* Sets the progress-bar value
*
* @param value value which should be greater than or equals to {@link #MIN} and less than or equals to {@link #MAX}
*/
@Override
public JQueryGenericContainer setModelObject(Integer value)
{
Integer v = Args.notNull(value, "value");
if (v < MIN)
{
v = MIN;
}
else if (v > MAX)
{
v = MAX;
}
return super.setModelObject(v);
}
// Methods //
/**
* Increments the progress-bar value by 1
*
* @param handler the {@link IPartialPageRequestHandler}
*/
public void forward(IPartialPageRequestHandler handler)
{
this.forward(handler, 1);
}
/**
* Increments the progress-bar value by the specified step value
*
* @param handler the {@link IPartialPageRequestHandler}
* @param step the value
*/
public final void forward(IPartialPageRequestHandler handler, int step)
{
this.setModelObject(this.getModelObject() + step);
this.refresh(handler);
}
/**
* Decrements the progress-bar value by 1
*
* @param handler the {@link IPartialPageRequestHandler}
*/
public final void backward(IPartialPageRequestHandler handler)
{
this.backward(handler, 1);
}
/**
* Decrements the progress-bar value by the specified step value
*
* @param handler the {@link IPartialPageRequestHandler}
* @param step the value
*/
public final void backward(IPartialPageRequestHandler handler, int step)
{
this.setModelObject(this.getModelObject() - step);
this.refresh(handler);
}
/**
* Refreshes the ProgressBar.
* This method needs to be called after the model object changes.
* But It is not required to be called when calling forward or backward methods.
*
* @param handler the {@link IPartialPageRequestHandler}
*/
public final void refresh(IPartialPageRequestHandler handler)
{
handler.appendJavaScript(this.widgetBehavior.toString()); // change the value ui-side so the change-event will be fired
}
// Events //
@Override
public void onAjax(AjaxRequestTarget target, JQueryEvent event)
{
if (event instanceof ChangeEvent)
{
this.onValueChanged(target);
if (this.getModelObject() == ProgressBar.MAX)
{
this.onComplete(target);
}
}
}
@Override
public void onValueChanged(IPartialPageRequestHandler handler)
{
// noop
}
/**
* Triggered when the value reach {@link #MAX}
*
* @param target the {@link AjaxRequestTarget}
*/
protected void onComplete(AjaxRequestTarget target)
{
// noop
}
@Override
protected void onModelChanged()
{
this.widgetBehavior.setOption("value", this.getModelObject()); // cannot be null?
}
// IJQueryWidget //
@Override
public JQueryBehavior newWidgetBehavior(String selector)
{
return new ProgressBarBehavior(selector) {
private static final long serialVersionUID = 1L;
@Override
public void onAjax(AjaxRequestTarget target, JQueryEvent event)
{
ProgressBar.this.onAjax(target, event);
}
};
}
}