diff flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java @ 97:0bec0112c8b3

Integrated the ProjectList into the client. Now, the Collections of a user are displayed in the list. flys-client/trunk@1610 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 29 Mar 2011 13:53:24 +0000
parents 17815a7354bc
children b92281182c6b
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java	Tue Mar 29 13:49:04 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java	Tue Mar 29 13:53:24 2011 +0000
@@ -1,20 +1,26 @@
 package de.intevation.flys.client.client.ui;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.user.client.rpc.AsyncCallback;
 
 import com.smartgwt.client.widgets.Canvas;
 import com.smartgwt.client.widgets.Label;
 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.layout.VLayout;
 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.DefaultCollection;
+import de.intevation.flys.client.shared.model.User;
 
+import de.intevation.flys.client.client.Config;
 import de.intevation.flys.client.client.FLYSMessages;
-import de.intevation.flys.client.shared.model.User;
+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;
 
 
 /**
@@ -22,11 +28,17 @@
  *
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
-public class ProjectList extends VLayout {
-
+public class ProjectList
+extends      VLayout
+implements   CollectionChangeHandler
+{
     /** The interface that provides i18n messages. */
     private FLYSMessages messages = GWT.create(FLYSMessages.class);
 
+    /** The UserService used to retrieve information about the current user. */
+    protected UserCollectionsServiceAsync userCollectionsService =
+        GWT.create(UserCollectionsService.class);
+
     /** The user whose projects should be displayed.*/
     protected User user;
 
@@ -44,19 +56,9 @@
 
         grid = new CollectionGrid();
         initGrid();
+        init();
 
-        // TODO Remove the following code block after a service to fetch the
-        // user collections has been implemented! Instead of these static lines,
-        // a callback mechanism should be implemented that updates this widget
-        // when the current user changes.
-        Collection c1 = new DefaultCollection("uu-1");
-        Collection c2 = new DefaultCollection("uu-2");
-        CollectionRecord rec1 = new CollectionRecord(c1);
-        CollectionRecord rec2 = new CollectionRecord(c2);
-        grid.addData(rec1);
-        grid.addData(rec2);
-
-        init();
+        updateUserCollections();
     }
 
 
@@ -68,18 +70,15 @@
         grid.setCanRemoveRecords(false);
         grid.setShowHeader(false);
         grid.setWidth100();
+        grid.setHeight100();
 
         ListGridField date = new ListGridField("date", "date");
         ListGridField name = new ListGridField("name", "name");
-        ListGridField pub  = new ListGridField("publish", "publish");
-        ListGridField del  = new ListGridField("delete", "delete");
 
-        date.setWidth(70);
-        name.setWidth(155);
-        pub.setWidth(35);
-        del.setWidth(35);
+        date.setWidth(100);
+        name.setWidth(195);
 
-        grid.setFields(date, name, pub, del);
+        grid.setFields(date, name);
     }
 
 
@@ -114,5 +113,57 @@
         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();
+
+        userCollectionsService.getUserCollections(url, 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));
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org