view flys-client/src/main/java/de/intevation/flys/client/client/ui/CollectionView.java @ 26:c19985f75118

Implemented a service that triggers the artifact-collection creation in the artifact server. flys-client/trunk@1413 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 07 Mar 2011 13:47:55 +0000
parents dc086030e6a3
children 5709bd8f4d7c
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.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

import de.intevation.flys.client.shared.model.Artifact;
import de.intevation.flys.client.shared.model.ArtifactDescription;
import de.intevation.flys.client.shared.model.Collection;
import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DefaultCollection;

import de.intevation.flys.client.client.Config;
import de.intevation.flys.client.client.FLYS;
import de.intevation.flys.client.client.FLYSMessages;
import de.intevation.flys.client.client.event.HasCollectionChangeHandlers;
import de.intevation.flys.client.client.event.HasStepForwardHandlers;
import de.intevation.flys.client.client.event.CollectionChangeEvent;
import de.intevation.flys.client.client.event.StepForwardEvent;
import de.intevation.flys.client.client.event.StepForwardHandler;
import de.intevation.flys.client.client.event.CollectionChangeHandler;
import de.intevation.flys.client.client.services.ArtifactService;
import de.intevation.flys.client.client.services.ArtifactServiceAsync;
import de.intevation.flys.client.client.services.CreateCollectionService;
import de.intevation.flys.client.client.services.CreateCollectionServiceAsync;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class CollectionView
extends      Window
implements   CollectionChangeHandler, HasCollectionChangeHandlers,
             StepForwardHandler
{
    /** The ArtifactService used to communicate with the Artifact server. */
    protected ArtifactServiceAsync artifactService =
        GWT.create(ArtifactService.class);

    /** The ArtifactService used to communicate with the Artifact server. */
    protected CreateCollectionServiceAsync createCollectionService =
        GWT.create(CreateCollectionService.class);

    /** The message class that provides i18n strings.*/
    FLYSMessages messages = GWT.create(FLYSMessages.class);

    /** The FLYS instance used to call services.*/
    protected FLYS flys;

    /** The ParameterList.*/
    protected ParameterList parameterList;

    /** The list of ValueChangeHandlers.*/
    protected List<CollectionChangeHandler> handlers;

    /** The collection to be displayed.*/
    protected Collection collection;

    protected TabSet tabs;

    /** The parameter tab.*/
    protected Tab parameterTab;

    /** The output tab.*/
    protected Tab outputTab;

    /** The layout.*/
    protected Layout layout;


    /**
     * This constructor creates a new CollectionView that is used to display the
     * <i>collection</i>.
     *
     * @param collection The collection to be displayed.
     */
    public CollectionView(FLYS flys, Collection collection) {
        this.flys       = flys;
        this.collection = collection;

        this.tabs          = new TabSet();
        this.parameterTab  = new Tab(messages.winfo());
        this.parameterList = new ParameterList();
        this.handlers      = new ArrayList<CollectionChangeHandler>();
        this.layout        = new VLayout();

        addCollectionChangeHandler(this);

        init();
    }


    /**
     * This method handles the initial layout stuff.
     */
    protected void init() {
        setWidth(600);
        setHeight(400);

        layout.setWidth100();

        setCanDragResize(true);

        DateTimeFormat dtf = DateTimeFormat.getFormat(messages.date_format());
        String lastAccess  = dtf.format(collection.getLastAccess());
        setTitle(lastAccess + " - " + collection.getName());

        addItem(layout);

        layout.addMember(tabs);
        tabs.addTab(parameterTab);

        if (isNew()) {
            tabs.setTabTitle(0, "MODUL");
            tabs.updateTab(0, renderNew());
        }
    }


    /**
     * This method triggers the CreateCollectionService to create a new
     * collection in the artifact server.
     *
     * @param ownerId The uuid of the user that should own the new collection.
     */
    protected void createNewCollection(String ownerId) {
        Config config    = Config.getInstance();
        String serverUrl = config.getServerUrl();

        createCollectionService.create(
            serverUrl, ownerId,
            new AsyncCallback<String>() {
                public void onFailure(Throwable caught) {
                    GWT.log("Could not create the new collection.");
                    GWT.log(caught.getMessage());
                }

                public void onSuccess(String uuid) {
                    GWT.log("Successfully created a new collection.");
                    GWT.log("NEW collection uuid: " + uuid);
                }
            });
    }


    protected FLYS getFlys() {
        return flys;
    }



    /**
     * This method registers a new ValueChangeHandler.
     *
     * @param handler The new ValueChangeHandler.
     */
    public void addCollectionChangeHandler(CollectionChangeHandler handler) {
        if (handler != null) {
            handlers.add(handler);
        }
    }


    /**
     * This method calls the <code>onValueChange()</code> method of all
     * registered ValueChangeHanders.
     */
    protected void fireCollectionChangeEvent(
        Collection old, Collection newCol)
    {
        for (CollectionChangeHandler handler: handlers) {
            handler.onCollectionChange(new CollectionChangeEvent(old, newCol));
        }
    }


    /**
     * This method returns true, if the Collection is new and no plugins has
     * been chosen.
     *
     * @return true, if the Collection is new.
     */
    public boolean isNew() {
        return collection.getArtifactLength() == 0 ? true : false;
    }


    /**
     * This method creates a Canvas displaying the plugins of FLYS.
     *
     * @return a Canvas that displays the plugins of FLYS.
     */
    protected Canvas renderNew() {
        VLayout newLayout = new VLayout();

        DynamicForm    form  = new DynamicForm();
        RadioGroupItem radio = new RadioGroupItem("plugin");

        radio.setShowTitle(false);
        radio.setVertical(false);
        radio.setValueMap(
            messages.winfo(),
            messages.minfo(),
            messages.map(),
            messages.fix());

        form.setFields(radio);

        IButton go = new IButton(messages.next());
        go.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                String serverUrl = Config.getInstance().getServerUrl();

                artifactService.create(
                    serverUrl, "winfo",
                    new AsyncCallback<Artifact>() {
                        public void onFailure(Throwable caught) {
                            GWT.log("Could not create the new artifact.");
                            GWT.log(caught.getMessage());
                        }

                        public void onSuccess(Artifact artifact) {
                            GWT.log("Successfully created a new artifact.");
                            Collection c = new DefaultCollection("TODO");

                            c.addArtifact(artifact);
                            setCollection(c);
                        }
                });
            }
        });

        newLayout.addMember(form);
        newLayout.addMember(go);

        return newLayout;
    }


    /**
     * Set the current collection.
     *
     * @param collection The new collection.
     */
    protected void setCollection(Collection collection) {
        Collection tmp  = this.collection;
        this.collection = collection;

        fireCollectionChangeEvent(tmp, this.collection);
    }


    public void onCollectionChange(CollectionChangeEvent event) {
        updateView();
    }


    public void onStepForward(StepForwardEvent event) {
        GWT.log("CollectionView - onStepForward()");
        GWT.log("TODO: IMPLEMENT FEED!");
    }


    /**
     * Update the view (refresh the list of old and current data).
     */
    protected void updateView() {
        GWT.log("Update view of the collection: " + collection.identifier());
        Artifact artifact        = collection.getArtifact(0);
        ArtifactDescription desc = artifact.getArtifactDescription();

        Data currentData = desc.getCurrentData();
        if (currentData != null) {
            String uiProvider   = currentData.getUIProvider();
            UIProvider provider = UIProviderFactory.getProvider(uiProvider);

            HasStepForwardHandlers handler = (HasStepForwardHandlers) provider;
            handler.addStepForwardHandler(this);

            parameterList.setCurrentData(currentData, provider);
        }

        parameterList.addOldDatas(desc.getOldData());

        tabs.setTabTitle(0, messages.winfo());
        tabs.updateTab(0, parameterList);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org