Mercurial > dive4elements > river
diff flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterList.java @ 73:39210a74db07
Moved parameterization specific code from CollectionView to ParameterList.
flys-client/trunk@1576 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 28 Mar 2011 06:48:44 +0000 |
parents | 9b726350ab07 |
children | 4784ca718476 |
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterList.java Sat Mar 26 18:07:41 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterList.java Mon Mar 28 06:48:44 2011 +0000 @@ -4,6 +4,7 @@ import java.util.List; import com.google.gwt.core.client.GWT; +import com.google.gwt.user.client.rpc.AsyncCallback; import com.smartgwt.client.types.VerticalAlignment; import com.smartgwt.client.widgets.Canvas; @@ -13,21 +14,61 @@ import de.intevation.flys.client.shared.model.Artifact; import de.intevation.flys.client.shared.model.ArtifactDescription; 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.DefaultData; +import de.intevation.flys.client.shared.model.DefaultDataItem; +import de.intevation.flys.client.shared.model.River; +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.HasParameterChangeHandler; import de.intevation.flys.client.client.event.HasStepBackHandlers; import de.intevation.flys.client.client.event.HasStepForwardHandlers; import de.intevation.flys.client.client.event.ParameterChangeEvent; import de.intevation.flys.client.client.event.ParameterChangeHandler; 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.client.services.AdvanceService; +import de.intevation.flys.client.client.services.AdvanceServiceAsync; +import de.intevation.flys.client.client.services.ArtifactService; +import de.intevation.flys.client.client.services.ArtifactServiceAsync; +import de.intevation.flys.client.client.services.StepForwardService; +import de.intevation.flys.client.client.services.StepForwardServiceAsync; public class ParameterList -extends VLayout -implements StepBackHandler, ParameterChangeHandler +extends HLayout +implements StepBackHandler, StepForwardHandler, ParameterChangeHandler, + HasParameterChangeHandler { + /** The message class that provides i18n strings.*/ + protected FLYSMessages MSG = GWT.create(FLYSMessages.class); + + /** The ArtifactService used to communicate with the Artifact server. */ + protected ArtifactServiceAsync artifactService = + GWT.create(ArtifactService.class); + + /** The StepForwardService used to put data into an existing artifact. */ + protected StepForwardServiceAsync forwardService = + GWT.create(StepForwardService.class); + + /** The StepForwardService used to put data into an existing artifact. */ + protected AdvanceServiceAsync advanceService = + GWT.create(AdvanceService.class); + + + /** The list of ParameterizationChangeHandler.*/ + protected List<ParameterChangeHandler> parameterHandlers; + + protected FLYS flys; + protected CollectionView cView; + protected Artifact artifact; + protected List<DataList> old; protected DataList current; @@ -36,28 +77,147 @@ protected VLayout oldItems; protected VLayout currentItems; - public ParameterList(CollectionView cView) { - this.cView = cView; - old = new ArrayList<DataList>(); - oldItems = new VLayout(); - currentItems = new VLayout(); + public ParameterList(FLYS flys, CollectionView cView) { + this.cView = cView; + this.flys = flys; + + parameterHandlers = new ArrayList<ParameterChangeHandler>(); + old = new ArrayList<DataList>(); + oldItems = new VLayout(); + currentItems = new VLayout(); init(); } protected void init() { - setWidth(300); + setMembersMargin(20); + + VLayout left = new VLayout(); oldItems.setHeight(20); oldItems.setMembersMargin(10); currentItems.setHeight("*"); currentItems.setAlign(VerticalAlignment.TOP); - setMembersMargin(20); + left.setMembersMargin(20); + left.setWidth(300); - addMember(oldItems); - addMember(currentItems); + left.addMember(oldItems); + left.addMember(currentItems); + + // This canvas is used to render helper widgets + Canvas right = new Canvas(); + + addMember(left); + addMember(right); + + Canvas moduleSelection = renderNew(); + moduleSelection.setLayoutAlign(VerticalAlignment.TOP); + currentItems.addMember(moduleSelection); + } + + + protected void setArtifact(Artifact artifact) { + Artifact tmp = this.artifact; + this.artifact = artifact; + + fireParameterChangeEvent(tmp, this.artifact); + } + + + /** + * This method registers a new ParameterChangeHandler. + * + * @param handler The new ParameterChangeHandler. + */ + public void addParameterChangeHandler(ParameterChangeHandler handler) { + if (handler != null) { + parameterHandlers.add(handler); + } + } + + + /** + * This method calls the <code>onParameterChange()</code> method of all + * registered ParameterChangeHandler. + */ + protected void fireParameterChangeEvent(Artifact old, Artifact newArt) { + for (ParameterChangeHandler handler: parameterHandlers) { + handler.onParameterChange(new ParameterChangeEvent(old, newArt)); + } + } + + + /** + * This method creates a Canvas displaying the plugins of FLYS combined with + * a widget to select a river. + * + * @return a Canvas that displays the supported plugins and rivers of FLYS. + */ + protected Canvas renderNew() { + River[] rivers = flys.getRivers(); + DataItem[] items = new DataItem[rivers.length]; + + int i = 0; + for (River river: rivers) { + String name = river.getName(); + items[i++] = new DefaultDataItem(name, null, name); + } + + Data data = new DefaultData( + "river", + MSG.river_selection(), + null, + items); + + ModuleSelection widget = new ModuleSelection(); + HasStepForwardHandlers handler = (HasStepForwardHandlers) widget; + + handler.addStepForwardHandler(new StepForwardHandler() { + public void onStepForward(StepForwardEvent event) { + Data[] data = event.getData(); + + DataItem[] moduleItems = data[0].getItems(); + DataItem[] riversItems = data[1].getItems(); + + String module = moduleItems[0].getStringValue(); + String river = riversItems[0].getStringValue(); + + final String serverUrl = Config.getInstance().getServerUrl(); + final Data[] feedData = new Data[] { data[1] }; + artifactService.create( + serverUrl, module.toLowerCase(), + 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."); + + forwardService.go(serverUrl, artifact, feedData, + new AsyncCallback<Artifact>() { + public void onFailure(Throwable caught) { + GWT.log("Could not feed the artifact."); + GWT.log(caught.getMessage()); + } + + public void onSuccess(Artifact artifact) { + GWT.log("Successfully feed the artifact."); + setArtifact(artifact); + } + }); + } + }); + } + }); + + DataList list = new DataList(); + list.add(data); + + return widget.create(list); } @@ -124,7 +284,6 @@ String provider = dataList.getUIProvider(); UIProvider uiprovider = UIProviderFactory.getProvider(provider); - ((HasStepBackHandlers) uiprovider).addStepBackHandler(cView); ((HasStepBackHandlers) uiprovider).addStepBackHandler(this); oldItems.addMember(uiprovider.createOld(dataList)); @@ -157,14 +316,57 @@ /** + * This method is called if the user clicks on the 'next' button to advance + * to the next state. + * + * @param event The StepForwardEvent. + */ + public void onStepForward(StepForwardEvent event) { + GWT.log("CollectionView - onStepForward()"); + String serverUrl = Config.getInstance().getServerUrl(); + + forwardService.go(serverUrl, artifact, event.getData(), + new AsyncCallback<Artifact>() { + public void onFailure(Throwable caught) { + GWT.log("Could not feed the artifact."); + GWT.log(caught.getMessage()); + } + + public void onSuccess(Artifact artifact) { + GWT.log("Successfully feed the artifact."); + setArtifact(artifact); + } + }); + } + + + /** * This method is used to remove all old items from this list after the user * has clicked the step back button. * - * @param e The StepBackEvent. + * @param e The StepBackEvent that holds the identifier of the target state. */ public void onStepBack(StepBackEvent e) { - old.clear(); - oldItems.removeMembers(oldItems.getMembers()); + final String target = e.getTarget(); + final String serverUrl = Config.getInstance().getServerUrl(); + + advanceService.advance(serverUrl, artifact, target, + new AsyncCallback<Artifact>() { + public void onFailure(Throwable caught) { + GWT.log("Could not go back to '" + target + "'"); + GWT.log(caught.getMessage()); + } + + public void onSuccess(Artifact artifact) { + GWT.log("Successfully step back to '" + target + "'"); + + old.clear(); + oldItems.removeMembers(oldItems.getMembers()); + + setArtifact(artifact); + } + } + ); } @@ -186,8 +388,8 @@ String uiProvider = currentData.getUIProvider(); UIProvider provider = UIProviderFactory.getProvider(uiProvider); - ((HasStepForwardHandlers) provider).addStepForwardHandler(cView); - ((HasStepBackHandlers) provider).addStepBackHandler(cView); + ((HasStepForwardHandlers) provider).addStepForwardHandler(this); + ((HasStepBackHandlers) provider).addStepBackHandler(this); setCurrentData(currentData, provider); }