view flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java @ 253:aea3ab5997b2

Added a doubleclick listener to the project list to open a project. flys-client/trunk@1863 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 09 May 2011 10:31:54 +0000
parents 924da6695800
children ad2ba6e2b8bd
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import java.util.Date;

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.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.CellDoubleClickHandler;
import com.smartgwt.client.widgets.grid.events.RowContextClickEvent;
import com.smartgwt.client.widgets.grid.events.RowContextClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.types.VerticalAlignment;

import de.intevation.flys.client.shared.model.Collection;
import de.intevation.flys.client.shared.model.CollectionRecord;
import de.intevation.flys.client.shared.model.User;

import de.intevation.flys.client.client.Config;
import de.intevation.flys.client.client.FLYS;
import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.client.event.CollectionChangeEvent;
import de.intevation.flys.client.client.event.CollectionChangeHandler;
import de.intevation.flys.client.client.services.UserCollectionsService;
import de.intevation.flys.client.client.services.UserCollectionsServiceAsync;


/**
 * The project list shows a list of projects of a specific user.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ProjectList
extends      VLayout
implements   CollectionChangeHandler
{
    /** The interface that provides i18n messages. */
    private FLYSConstants messages = GWT.create(FLYSConstants.class);

    /** The UserService used to retrieve information about the current user. */
    protected UserCollectionsServiceAsync userCollectionsService =
        GWT.create(UserCollectionsService.class);

    /** A pointer to the FLYS instance.*/
    protected FLYS flys;

    /** The user whose projects should be displayed.*/
    protected User user;

    /** The grid that contains the project rows.*/
    protected ListGrid grid;

    /**
     * The default constructor that creates a new ProjectList for a specific
     * user.
     *
     * @param user The user.
     */
    public ProjectList(FLYS flys, User user) {
        this.flys = flys;
        this.user = user;

        grid = new ListGrid();
        initGrid();
        init();

        updateUserCollections();
    }


    protected void initGrid() {
        grid.setEmptyMessage(messages.no_projects());
        grid.setLoadingDataMessage(messages.load_projects());
        grid.setShowRecordComponents(true);
        grid.setShowRecordComponentsByCell(true);
        grid.setCanRemoveRecords(false);
        grid.setShowHeader(false);
        grid.setWidth100();
        grid.setHeight100();
        grid.setSortDirection(SortDirection.DESCENDING);
        grid.setSortField(0);

        ListGridField date = buildDateField();
        ListGridField name = buildNameField();

        grid.setFields(date, name);

        grid.addCellDoubleClickHandler(new CellDoubleClickHandler() {
            public void onCellDoubleClick(CellDoubleClickEvent e) {
                CollectionRecord record = (CollectionRecord) e.getRecord();
                String uuid = record != null
                    ? record.getCollection().identifier()
                    : "";
                getFlys().openProject(uuid);
            }
        });

        grid.addRowContextClickHandler(new RowContextClickHandler() {
            public void onRowContextClick(RowContextClickEvent event) {
                CollectionRecord record = (CollectionRecord) event.getRecord();

                Menu menu = createContextMenu(record);
                grid.setContextMenu(menu);
                menu.showContextMenu();

                event.cancel();
            }
        });
    }


    public FLYS getFlys() {
        return flys;
    }


    /**
     * Creates a new context menu that interacts with a CollectionRecord.
     *
     * @param record The selected record.
     *
     * @return the context menu with operations that interact with
     * <i>record</i>.
     */
    protected Menu createContextMenu(final CollectionRecord record) {
        Menu menu = new Menu();

        MenuItem open = new MenuItem(messages.open_project());
        open.addClickHandler(new ClickHandler() {
            public void onClick(MenuItemClickEvent evt) {
                getFlys().openProject(record.getCollection().identifier());
            }
        });

        MenuItem del = new MenuItem(messages.delete_project());
        del.addClickHandler(new ClickHandler() {
            public void onClick(MenuItemClickEvent evt) {
                SC.warn("Removing projects is not implemented.");
            }
        });

        menu.addItem(open);
        menu.addItem(del);

        return menu;
    }


    /**
     * The init() method handles the layout stuff for this widget.
     */
    protected void init() {
        setWidth(300);
        setHeight100();
        setShowResizeBar(true);
        setShowEdges(false);
        setLayoutMargin(0);
        setLayoutAlign(VerticalAlignment.TOP);

        Label title = new Label(messages.projects());
        title.setHeight("20");
        title.setMargin(5);
        title.setWidth100();
        title.setStyleName("fontLightSmall");

        Canvas titleWrapper = new Canvas();
        titleWrapper.setStyleName("bgBlueDark");
        titleWrapper.setWidth100();
        titleWrapper.setHeight("20px");
        titleWrapper.addChild(title);

        Canvas gridWrapper = new Canvas();
        gridWrapper.setPadding(0);
        titleWrapper.setWidth100();
        gridWrapper.addChild(grid);

        addMember(titleWrapper);
        addMember(gridWrapper);
    }


    public void onCollectionChange(CollectionChangeEvent event) {
        GWT.log("ProjectList.onCollectionChange");

        updateUserCollections();
    }


    protected void updateUserCollections() {
        Config config = Config.getInstance();
        String url    = config.getServerUrl();
        String locale = config.getLocale();

        userCollectionsService.getUserCollections(url, locale, user.identifier(),
            new AsyncCallback<Collection[]>() {
                public void onFailure(Throwable caught) {
                    GWT.log("Could not recieve a list of user collections.");
                    GWT.log(caught.getMessage());
                }

                public void onSuccess(Collection[] collections) {
                    int num = collections != null ? collections.length : 0;

                    GWT.log("Received " + num + " user collections.");

                    if (num == 0) {
                        return;
                    }

                    updateGrid(collections);
                }
            }
        );
    }


    protected void clearGrid() {
        ListGridRecord[] records = grid.getRecords();

        for (ListGridRecord record: records) {
            grid.removeData(record);
        }
    }


    protected void updateGrid(Collection[] collections) {
        clearGrid();

        for (Collection c: collections) {
            grid.addData(new CollectionRecord(c));
        }
    }


    /**
     * Builds the field in the grid that displays the creation time of a
     * project.
     *
     * @return the grid field.
     */
    protected ListGridField buildDateField() {
        ListGridField date = new ListGridField("creationTime", "creationTime");
        date.setType(ListGridFieldType.DATE);

        date.setCellFormatter(new CellFormatter() {
            public String format(Object value, ListGridRecord rec, int r, int c) {
                if (value == null) {
                    return null;
                }

                DateTimeFormat dtf = DateTimeFormat.getFormat(
                    messages.datetime_format());

                return dtf.format((Date)value);
            }
        });

        date.setWidth(105);
        date.setAlign(Alignment.LEFT);

        return date;
    }


    /**
     * Builds the field in the grid that displays the name of a project.
     *
     * @return the grid field.
     */
    protected ListGridField buildNameField() {
        ListGridField name = new ListGridField("name", "name");
        name.setType(ListGridFieldType.TEXT);

        name.setCellFormatter(new CellFormatter() {
            public String format(Object value, ListGridRecord record, int row, int col) {
                String n = (String) value;
                int len  = n.length();
                int sec  = len - 15;
                return n.substring(0, 14) + "..." + n.substring(sec, len-1);
            }
        });

        name.setWidth(195);
        name.setAlign(Alignment.LEFT);

        return name;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org