view flys-client/src/main/java/de/intevation/flys/client/client/ui/AbstractUIProvider.java @ 247:4a684d29404f

Implemented the createOld() of WQAdaptedInputPanel. flys-client/trunk@1832 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 05 May 2011 09:33:15 +0000
parents 137daff2c732
children 9f16ac843dda
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.Button;

import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.client.event.HasStepBackHandlers;
import de.intevation.flys.client.client.event.HasStepForwardHandlers;
import de.intevation.flys.client.client.event.StepBackEvent;
import de.intevation.flys.client.client.event.StepBackHandler;
import de.intevation.flys.client.client.event.StepForwardEvent;
import de.intevation.flys.client.client.event.StepForwardHandler;
import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.DataList;
import de.intevation.flys.client.shared.model.Artifact;

/**
 * An abstract UIProvider that provides some basic methods.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public abstract class AbstractUIProvider
implements   UIProvider, HasStepForwardHandlers, ClickHandler,
             HasStepBackHandlers
{
    /** The message class that provides i18n strings.*/
    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);


    /** The StepForwardHandlers.*/
    protected List<StepForwardHandler> forwardHandlers;

    /** The StepForwardHandlers.*/
    protected List<StepBackHandler> backHandlers;

    /** The container that is used to position helper widgets.*/
    protected Canvas helperContainer;

    /** The artifact that contains status information.*/
    protected Artifact artifact;

    /**
     * Creates a new UIProvider instance of this class.
     */
    public AbstractUIProvider() {
        forwardHandlers = new ArrayList<StepForwardHandler>();
        backHandlers    = new ArrayList<StepBackHandler>();
    }


    /**
     * Appends a StepBackHandler that wants to listen to StepBackEvents.
     *
     * @param handler A new StepBackHandler.
     */
    public void addStepBackHandler(StepBackHandler handler) {
        if (handler != null) {
            backHandlers.add(handler);
        }
    }


    /**
     * Appends a StepForwardHandler that wants to listen to StepForwardEvents.
     *
     * @param handler A new StepForwardHandler.
     */
    public void addStepForwardHandler(StepForwardHandler handler) {
        if (handler != null) {
            forwardHandlers.add(handler);
        }
    }


    /**
     * This method is called after the user has clicked one of the buttons to
     * step back to a previous state.
     *
     * @param e The StepBackEvent.
     */
    protected void fireStepBackEvent(StepBackEvent e) {
        GWT.log("AbstractUIProvider - fireStepBackEvent() handlers: " + backHandlers.size());
        for (StepBackHandler handler: backHandlers) {
            handler.onStepBack(e);
        }
    }


    /**
     * This method is called after the user has clicked on the 'next' button to
     * step to the next state.
     *
     * @param e The StepForwardEvent.
     */
    protected void fireStepForwardEvent(StepForwardEvent e) {
        GWT.log("AbstractUIProvider - fireStepForwardEvent() handlers: " + forwardHandlers.size());
        for (StepForwardHandler handler: forwardHandlers) {
            handler.onStepForward(e);
        }
    }


    /**
     * This method is used to listen to click events on the 'next' button. The
     * fireStepForwardEvent() method is called here.
     *
     * @param e The click event.
     */
    public void onClick(ClickEvent e) {
        Data[] data = getData();

        fireStepForwardEvent(new StepForwardEvent(data));
    }


    /**
     * Creates the 'next' button to step forward to the next state.
     *
     * @return the 'next' button.
     */
    protected Canvas getNextButton() {
        Button next = new Button(MSG.buttonNext());
        next.addClickHandler(this);

        return next;
    }


    /**
     * Creates the 'back' button to step back to a previous state.
     *
     * @param targetState The identifier of the target state.
     *
     * @return the 'back' button.
     */
    protected Canvas getBackButton(final String targetState) {
        String url = GWT.getHostPageBaseURL() + MSG.imageBack();
        Img back   = new Img(url, 16, 16);

        back.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                fireStepBackEvent(new StepBackEvent(targetState));
            }
        });

        return back;
    }


    /**
     * This method injects a container that is used to position helper widgets.
     *
     * @param helperContainer A container that is used to position helper
     * widgets.
     */
    public void setContainer(Canvas helperContainer) {
        this.helperContainer = helperContainer;
    }


    /**
     * This method injects an artifact that contains the status information.
     *
     * @param art An artifact containing status information.
     */
    public void setArtifact(Artifact art) {
        this.artifact = art;
    }


    /**
     * This method greps the Data with name <i>name</i> from the list and
     * returns it.
     *
     * @param items A list of Data.
     * @param name The name of the Data that we are searching for.
     *
     * @return the Data with the name <i>name</i>.
     */
    protected Data getData(List<Data> data, String name) {
        for (Data d: data) {
            if (name.equals(d.getLabel())) {
                return d;
            }
        }

        return null;
    }

    /**
     * This method greps the DataItem with name <i>name</i> from the list and
     * returns it.
     *
     * @param items A list of DataItems.
     * @param name The name of the DataItem that we are searching for.
     *
     * @return the DataItem with the name <i>name</i>.
     */
    protected DataItem getDataItem(DataItem[] items, String name) {
        for (DataItem item: items) {
            if (name.equals(item.getLabel())) {
                return item;
            }
        }

        return null;
    }


    /**
     * This method needs to be implemented by concrete subclasses. It should
     * create a new Canvas object with a representation of <i>data</i>.
     *
     * @param data The data that should be displayed.
     *
     * @return a Canvas object that displays <i>data</i>.
     */
    public abstract Canvas create(DataList data);


    /**
     * This method needs to be implemented by concrete subclasses. It should
     * return the selected data.
     *
     * @return the selected data.
     */
    protected abstract Data[] getData();
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org