com.pietschy.gwt.pectin.client.form.AbstractFieldModelBase.java Source code

Java tutorial

Introduction

Here is the source code for com.pietschy.gwt.pectin.client.form.AbstractFieldModelBase.java

Source

/*
 * Copyright 2010 Andrew Pietsch
 *
 * Licensed 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.pietschy.gwt.pectin.client.form;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.pietschy.gwt.pectin.client.value.MutableValueModel;
import com.pietschy.gwt.pectin.client.value.ValueModel;

/**
 * Created by IntelliJ IDEA.
 * User: andrew
 * Date: Jun 30, 2009
 * Time: 7:32:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class AbstractFieldModelBase<T> extends AbstractField<T> implements FieldModelBase<T> {
    private SourceModelListener sourceListener = new SourceModelListener();
    private ValueModel<T> source;
    private T cachedValue;

    public AbstractFieldModelBase(FormModel formModel, ValueModel<T> source, Class<T> valueType) {
        super(formModel, valueType);
        this.source = source;

        source.addValueChangeHandler(sourceListener);
        // read our state from our source.
        onSourceModelChange(source.getValue());
    }

    protected void writeToSource(T value) {
        try {
            sourceListener.setIgnoreEvents(true);

            getMutableSource().setValue(value);
        } finally {
            sourceListener.setIgnoreEvents(false);
        }
    }

    protected void onSourceModelChange(T value) {
        cachedValue = value;
        fireValueChangeEvent(value);
    }

    public T getValue() {
        return cachedValue;
    }

    public void setValue(T value) {
        T oldValue = cachedValue;

        cachedValue = value;
        writeToSource(value);

        fireValueChangeEvent(oldValue, value);
    }

    public boolean isMutableSource() {
        return getSource() instanceof MutableValueModel;
    }

    public ValueModel<T> getSource() {
        return source;
    }

    public MutableValueModel<T> getMutableSource() {
        verifyMutableSource();
        return (MutableValueModel<T>) source;
    }

    protected void fireValueChangeEvent(T newValue) {
        ValueChangeEvent.fire(this, newValue);
    }

    protected void fireValueChangeEvent(T oldValue, T newValue) {
        ValueChangeEvent.fireIfNotEqual(this, oldValue, newValue);
    }

    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<T> handler) {
        return addHandler(handler, ValueChangeEvent.getType());
    }

    /**
     * Probably nice to move this to a buffer strategy.
     */
    private class SourceModelListener implements ValueChangeHandler<T> {
        private boolean ignoreEvents = false;

        public void onValueChange(ValueChangeEvent<T> event) {
            if (!ignoreEvents) {
                onSourceModelChange(event.getValue());
            }
        }

        public void setIgnoreEvents(boolean ignoreEvents) {
            this.ignoreEvents = ignoreEvents;
        }
    }
}