changeset 1456:1c2cd46d526d

Have 'area' context menu in Longitudinal-Section diagrams themepanels, too. flys-client/trunk@3486 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 20 Dec 2011 10:54:55 +0000
parents 75a40270efa8
children 3d9f81c3f00d
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java
diffstat 2 files changed, 281 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Tue Dec 20 10:52:52 2011 +0000
+++ b/flys-client/ChangeLog	Tue Dec 20 10:54:55 2011 +0000
@@ -1,3 +1,9 @@
+2011-12-20	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java:
+	  Add area-context menus. Most duplicate of code in
+	  CrossSectionChartThemePanel, which will be merged in near future.
+
 2011-12-20	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties,
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java	Tue Dec 20 10:52:52 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java	Tue Dec 20 10:54:55 2011 +0000
@@ -2,20 +2,43 @@
 
 import com.google.gwt.core.client.GWT;
 
-import com.smartgwt.client.types.ListGridFieldType;
-import com.smartgwt.client.widgets.grid.ListGridField;
-import com.smartgwt.client.widgets.layout.VLayout;
+import com.google.gwt.user.client.rpc.AsyncCallback;
 
+import com.smartgwt.client.types.ListGridFieldType;
+
+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.widgets.menu.events.ClickHandler;
+
+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.util.SC;
+
+import de.intevation.flys.client.shared.model.Artifact;
 import de.intevation.flys.client.shared.model.Collection;
+import de.intevation.flys.client.shared.model.Data;
+import de.intevation.flys.client.shared.model.DefaultData;
+import de.intevation.flys.client.shared.model.DefaultArtifact;
+import de.intevation.flys.client.shared.model.FacetRecord;
+import de.intevation.flys.client.shared.model.OutputMode;
+import de.intevation.flys.client.shared.model.Recommendation;
 import de.intevation.flys.client.shared.model.Theme;
-import de.intevation.flys.client.shared.model.OutputMode;
+import de.intevation.flys.client.shared.model.ThemeList;
 
+import de.intevation.flys.client.client.Config;
 import de.intevation.flys.client.client.FLYSConstants;
 import de.intevation.flys.client.client.ui.ThemePanel;
 
 import de.intevation.flys.client.client.services.FeedService;
 import de.intevation.flys.client.client.services.FeedServiceAsync;
 
+import de.intevation.flys.client.client.services.LoadArtifactService;
+import de.intevation.flys.client.client.services.LoadArtifactServiceAsync;
+
+
 /**
  * ThemePanel on the left in CollectionView.
  * Contains control widgets for "themes", which are plotted in a diagram (chart).
@@ -23,6 +46,9 @@
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
 public class ChartThemePanel extends ThemePanel {
+    /** Artifact Clone/Creation service. */
+    protected LoadArtifactServiceAsync loadService =
+                GWT.create(LoadArtifactService.class);
 
     /** The interface that provides i18n messages. */
     protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
@@ -37,6 +63,7 @@
     FeedServiceAsync feedService = GWT.create(
         de.intevation.flys.client.client.services.FeedService.class);
 
+
     public ChartThemePanel(Collection collection, OutputMode mode) {
         super(collection, mode);
         this.collection = collection;
@@ -59,6 +86,7 @@
         return layout;
     }
 
+
     /**
      * Initializes the layout of this panel.
      */
@@ -100,5 +128,248 @@
     public void activateTheme(Theme theme, boolean active) {
         theme.setActive(active ? 1 : 0);
     }
+
+
+    /**
+     * Tell an area artifact where to get the upper and lower curve from.
+     * @param over if null, something
+     */
+    public void feedTellArea(final String artifact, Theme under, Theme over) {
+
+        Data[] feedData;
+
+        if (over != null && under != null) {
+            feedData = new Data[] {
+                DefaultData.createSimpleStringData("area.curve_under",
+                    under.getArtifact() + under.getIndex()),
+                DefaultData.createSimpleStringData("area.curve_over",
+                    over.getArtifact() + over.getIndex()),
+                DefaultData.createSimpleStringData("area.name",
+                    over.getDescription() + " / " + under.getDescription()),
+                DefaultData.createSimpleStringData("area.facet",
+                    "longitudinal_section.area")
+            };
+            GWT.log("Have over and under");
+        }
+        else if (over == null && under != null) {
+            feedData = new Data[] {
+                DefaultData.createSimpleStringData("area.curve_under",
+                    under.getArtifact() + under.getIndex()),
+                DefaultData.createSimpleStringData("area.name",
+                    under.getDescription() + " / " + MSG.getString("x_axis")),
+                DefaultData.createSimpleStringData("area.facet",
+                    "longitudinal_section.area")
+            };
+            GWT.log("Have under only");
+        }
+        else if (over != null && under == null) {
+            feedData = new Data[] {
+                DefaultData.createSimpleStringData("area.curve_over",
+                    over.getArtifact() + over.getIndex()),
+                DefaultData.createSimpleStringData("area.name",
+                    MSG.getString("x_axis") + " / " + over.getDescription()),
+                DefaultData.createSimpleStringData("area.facet",
+                    "longitudinal_section.area")
+            };
+            GWT.log("Have over only");
+        }
+        else {
+            GWT.log("Missing Data for area painting.");
+            return;
+        }
+
+
+        feedService.feed(
+            Config.getInstance().getLocale(),
+            new DefaultArtifact(artifact, "TODO:hash"),
+            feedData,
+            new AsyncCallback<Artifact>() {
+                public void onFailure(Throwable caught) {
+                    GWT.log("Could not feed artifact (" + artifact
+                            + ") with area info: " + caught.getMessage());
+                    SC.warn(MSG.getString(caught.getMessage()));
+                    enable();
+                }
+                public void onSuccess(Artifact artifact) {
+                    GWT.log("Successfully set area params to " + artifact);
+                    requestRedraw();
+                    updateCollection();
+                    updateGrid();
+                    enable();
+                }
+            });
+    }
+
+
+    /**
+     * Create and parameterize a new area artifact.
+     * @param under
+     * @param over if null, something.
+     */
+    public void createAreaArtifact(
+        final Theme under,
+        final Theme over,
+        boolean between
+    ) {
+        Config config = Config.getInstance();
+        String locale = config.getLocale();
+
+        Recommendation area = new Recommendation(
+            "area",
+            "",
+            "",
+            null);
+        Recommendation[] recommendations = new Recommendation[] {area};
+
+        loadService.loadMany(
+            this.collection,
+            recommendations,
+            null, //use individual factories.
+            locale,
+            new AsyncCallback<Artifact[]>() {
+                public void onFailure(Throwable caught) {
+                    GWT.log("Failed, no area artifact: " + caught.getMessage());
+                    enable();
+                    // TODO SC.warn
+                }
+                public void onSuccess(Artifact[] artifacts) {
+                    GWT.log("Success, created area artifact: "
+                        + artifacts[0].getUuid());
+                    // Now, feed the artifact with the relevant data.
+                    feedTellArea(artifacts[0].getUuid(), under, over);
+                }
+            }
+            );
+    }
+
+    
+    /**
+     * Return true if two themes are canditates for an area being
+     * rendered between them.
+     * TODO join with canArea, generalize to allow easier modification
+     *      in subclasses.
+     */
+    protected boolean areAreaCompatible(Theme a, Theme b) {
+        if (a.equals(b)) {
+            return false;
+        }
+        if (a.getFacet().equals("longitudinal_section.w")) {
+            return b.getFacet().equals("longitudinal_section.w");
+        }
+        else if (a.getFacet().equals("longitudinal_section.q")) {
+            return b.getFacet().equals("longitudinal_section.q");
+        }
+        return false;
+    }
+
+
+    /**
+     * True if context menu should contain 'create area' submenu on
+     * this theme.
+     */
+    protected boolean canArea(Theme a) {
+        return a.getFacet().equals("longitudinal_section.q")
+            || a.getFacet().equals("longitudinal_section.w");
+    }
+
+
+    /**
+     * Include area specific menu items.
+     */
+    protected Menu getSingleContextMenu(final ListGridRecord[] records) {
+        Menu menu = super.getSingleContextMenu(records);
+
+        final Theme facetTheme = ((FacetRecord)records[0]).getTheme();
+        String thisItem = facetTheme.getDescription();
+        if (!canArea(facetTheme)) {
+            return menu;
+        }
+        
+        menu.addItem(createSeparator());
+
+        MenuItem areaMenuItem = new MenuItem(MSG.chart_themepanel_new_area());
+        Menu areaMenu         = new Menu();
+
+        ThemeList themes = getThemeList();
+        int nThemes      = themes.getThemeCount();
+
+        MenuItem underMenuItem = new MenuItem(MSG.chart_themepanel_area_under());
+        Menu underMenu = new Menu();
+        for (int i = 0; i < nThemes; i++)  {
+            final Theme theme = themes.getThemeAt(i+1);
+            if (!areAreaCompatible(facetTheme, theme)) {
+                continue;
+            }
+            MenuItem againster = new MenuItem(theme.getDescription());
+            underMenu.addItem(againster);
+
+            againster.addClickHandler(new ClickHandler() {
+                public void onClick(MenuItemClickEvent evt) {
+                    disable();
+                    createAreaArtifact(theme, facetTheme, false);
+                }
+            });
+        }
+
+        MenuItem overMenuItem = new MenuItem(MSG.chart_themepanel_area_over());
+        Menu overMenu = new Menu();
+        for (int i = 0; i < nThemes; i++)  {
+            final Theme theme = themes.getThemeAt(i+1);
+            if (!areAreaCompatible(facetTheme, theme)) {
+                continue;
+            }
+            MenuItem againster = new MenuItem(theme.getDescription());
+            overMenu.addItem(againster);
+
+            againster.addClickHandler(new ClickHandler() {
+                public void onClick(MenuItemClickEvent evt) {
+                    disable();
+                    createAreaArtifact(facetTheme, theme, false);
+                }
+            });
+        }
+        overMenu.addItem(createSeparator());
+        MenuItem againstAxis = new MenuItem(MSG.getString("x_axis"));
+        againstAxis.addClickHandler(new ClickHandler() {
+            public void onClick(MenuItemClickEvent evt) {
+                disable();
+                createAreaArtifact(facetTheme, null, false);
+            }
+        });
+        overMenu.addItem(againstAxis);
+
+        MenuItem betweenMenuItem = new MenuItem(MSG.chart_themepanel_area_between());
+        Menu betweenMenu = new Menu();
+        for (int i = 0; i < nThemes; i++)  {
+            final Theme theme = themes.getThemeAt(i+1);
+            if (!areAreaCompatible(facetTheme, theme)) {
+                continue;
+            }
+            MenuItem againster = new MenuItem(theme.getDescription());
+            betweenMenu.addItem(againster);
+
+            againster.addClickHandler(new ClickHandler() {
+                public void onClick(MenuItemClickEvent evt) {
+                    disable();
+                    createAreaArtifact(facetTheme, theme, true);
+                }
+            });
+        }
+        betweenMenu.addItem(createSeparator());
+        betweenMenu.addItem(againstAxis);
+
+        overMenuItem.setSubmenu(overMenu);
+        underMenuItem.setSubmenu(underMenu);
+        betweenMenuItem.setSubmenu(betweenMenu);
+
+        areaMenu.addItem(overMenuItem);
+        areaMenu.addItem(underMenuItem);
+        areaMenu.addItem(betweenMenuItem);
+
+        areaMenuItem.setSubmenu(areaMenu);
+        menu.addItem(areaMenuItem);
+
+        return menu;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org