org.geoserver.wps.web.RangePanel.java Source code

Java tutorial

Introduction

Here is the source code for org.geoserver.wps.web.RangePanel.java

Source

/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */
package org.geoserver.wps.web;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.geoserver.web.wicket.DecimalTextField;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.util.NumberRange;

/**
 * A form component for a {@link Range} object.
 * 
 * @author Andrea Aime, OpenGeo
 */
@SuppressWarnings("serial")
public class RangePanel extends FormComponentPanel<NumberRange> {

    protected Double min, max;

    protected Label minLabel, maxLabel;

    protected DecimalTextField minInput, maxInput;

    public RangePanel(String id) {
        super(id);

        initComponents();
    }

    public RangePanel(String id, ReferencedEnvelope e) {
        this(id, new Model(e));
    }

    public RangePanel(String id, IModel model) {
        super(id, model);

        initComponents();
    }

    void initComponents() {
        updateFields();

        add(minLabel = new Label("minLabel", new ResourceModel("min")));
        add(maxLabel = new Label("maxLabel", new ResourceModel("max")));

        add(minInput = new DecimalTextField("min", new PropertyModel(this, "min")));
        add(maxInput = new DecimalTextField("max", new PropertyModel(this, "max")));
    }

    @Override
    protected void onBeforeRender() {
        updateFields();
        super.onBeforeRender();
    }

    private void updateFields() {
        NumberRange range = getModelObject();
        if (range != null) {
            this.min = range.getMinimum();
            this.max = range.getMaximum();
        }
    }

    public RangePanel setReadOnly(final boolean readOnly) {
        visitChildren(TextField.class, new org.apache.wicket.Component.IVisitor() {
            public Object component(Component component) {
                component.setEnabled(!readOnly);
                return null;
            }
        });
        return this;
    }

    @Override
    protected void convertInput() {
        visitChildren(TextField.class, new org.apache.wicket.Component.IVisitor() {

            public Object component(Component component) {
                ((TextField) component).processInput();
                return null;
            }
        });

        // update the envelope model
        if (min != null && max != null) {
            setConvertedInput(new NumberRange<>(Double.class, min, max));
        } else {
            setConvertedInput(null);
        }
    }

    @Override
    protected void onModelChanged() {
        // when the client programmatically changed the model, update the fields
        // so that the textfields will change too
        updateFields();
        visitChildren(TextField.class, new Component.IVisitor() {

            public Object component(Component component) {
                ((TextField) component).clearInput();
                return CONTINUE_TRAVERSAL;
            }
        });
    }

}