ingo@23: package de.intevation.flys.client.client.ui; ingo@23: ingo@23: import java.util.ArrayList; ingo@23: import java.util.List; ingo@23: ingo@38: import com.google.gwt.core.client.GWT; ingo@73: import com.google.gwt.user.client.rpc.AsyncCallback; ingo@38: ingo@23: import com.smartgwt.client.types.VerticalAlignment; ingo@215: import com.smartgwt.client.util.SC; ingo@53: import com.smartgwt.client.widgets.Canvas; ingo@23: import com.smartgwt.client.widgets.layout.HLayout; ingo@23: import com.smartgwt.client.widgets.layout.VLayout; ingo@90: import com.smartgwt.client.widgets.tab.Tab; ingo@23: ingo@72: import de.intevation.flys.client.shared.model.Artifact; ingo@72: import de.intevation.flys.client.shared.model.ArtifactDescription; ingo@23: import de.intevation.flys.client.shared.model.Data; ingo@73: import de.intevation.flys.client.shared.model.DataItem; ingo@51: import de.intevation.flys.client.shared.model.DataList; ingo@73: import de.intevation.flys.client.shared.model.DefaultData; ingo@73: import de.intevation.flys.client.shared.model.DefaultDataItem; ingo@73: import de.intevation.flys.client.shared.model.River; ingo@73: import de.intevation.flys.client.client.Config; ingo@73: import de.intevation.flys.client.client.FLYS; ingo@211: import de.intevation.flys.client.client.FLYSConstants; ingo@73: import de.intevation.flys.client.client.event.HasParameterChangeHandler; ingo@60: import de.intevation.flys.client.client.event.HasStepBackHandlers; ingo@72: import de.intevation.flys.client.client.event.HasStepForwardHandlers; ingo@72: import de.intevation.flys.client.client.event.ParameterChangeEvent; ingo@72: import de.intevation.flys.client.client.event.ParameterChangeHandler; ingo@60: import de.intevation.flys.client.client.event.StepBackEvent; ingo@60: import de.intevation.flys.client.client.event.StepBackHandler; ingo@73: import de.intevation.flys.client.client.event.StepForwardEvent; ingo@73: import de.intevation.flys.client.client.event.StepForwardHandler; ingo@73: import de.intevation.flys.client.client.services.AdvanceService; ingo@73: import de.intevation.flys.client.client.services.AdvanceServiceAsync; ingo@73: import de.intevation.flys.client.client.services.ArtifactService; ingo@73: import de.intevation.flys.client.client.services.ArtifactServiceAsync; ingo@73: import de.intevation.flys.client.client.services.StepForwardService; ingo@73: import de.intevation.flys.client.client.services.StepForwardServiceAsync; ingo@23: ingo@23: ingo@60: public class ParameterList ingo@90: extends Tab ingo@73: implements StepBackHandler, StepForwardHandler, ParameterChangeHandler, ingo@73: HasParameterChangeHandler ingo@60: { ingo@73: /** The message class that provides i18n strings.*/ ingo@211: protected FLYSConstants MSG = GWT.create(FLYSConstants.class); ingo@73: ingo@73: /** The ArtifactService used to communicate with the Artifact server. */ ingo@73: protected ArtifactServiceAsync artifactService = ingo@73: GWT.create(ArtifactService.class); ingo@73: ingo@73: /** The StepForwardService used to put data into an existing artifact. */ ingo@73: protected StepForwardServiceAsync forwardService = ingo@73: GWT.create(StepForwardService.class); ingo@73: ingo@73: /** The StepForwardService used to put data into an existing artifact. */ ingo@73: protected AdvanceServiceAsync advanceService = ingo@73: GWT.create(AdvanceService.class); ingo@73: ingo@73: ingo@73: /** The list of ParameterizationChangeHandler.*/ ingo@73: protected List parameterHandlers; ingo@73: ingo@73: protected FLYS flys; ingo@73: ingo@60: protected CollectionView cView; ingo@23: ingo@73: protected Artifact artifact; ingo@73: ingo@51: protected List old; ingo@51: protected DataList current; ingo@23: ingo@23: protected UIProvider uiProvider; ingo@23: raimund@256: protected VLayout topLayout; ingo@23: protected VLayout oldItems; ingo@23: protected VLayout currentItems; ingo@83: protected Canvas helperPanel; raimund@260: protected Canvas tablePanel; ingo@23: ingo@90: public ParameterList(FLYS flys, CollectionView cView, String title) { ingo@90: super(title); ingo@90: ingo@73: this.cView = cView; ingo@73: this.flys = flys; ingo@73: ingo@73: parameterHandlers = new ArrayList(); ingo@73: old = new ArrayList(); raimund@256: topLayout = new VLayout(); ingo@73: oldItems = new VLayout(); ingo@73: currentItems = new VLayout(); ingo@23: ingo@224: addParameterChangeHandler(this); ingo@224: ingo@23: init(); ingo@23: } ingo@23: ingo@23: ingo@224: public ParameterList( ingo@224: FLYS flys, ingo@224: CollectionView cView, ingo@224: String title, ingo@224: Artifact artifact) ingo@224: { ingo@224: super(title); ingo@224: ingo@224: this.cView = cView; ingo@224: this.flys = flys; ingo@224: this.artifact = artifact; ingo@224: ingo@224: parameterHandlers = new ArrayList(); ingo@224: old = new ArrayList(); raimund@256: topLayout = new VLayout(); ingo@224: oldItems = new VLayout(); ingo@224: currentItems = new VLayout(); ingo@224: ingo@224: init(); ingo@224: ingo@224: addParameterChangeHandler(this); ingo@224: ingo@224: setArtifact(artifact); ingo@224: } ingo@224: ingo@224: ingo@23: protected void init() { ingo@90: HLayout rootLayout = new HLayout(); raimund@260: tablePanel = new Canvas(); ingo@90: rootLayout.setMembersMargin(20); ingo@73: ingo@73: VLayout left = new VLayout(); ingo@53: ingo@83: if (old == null || old.size() == 0) { ingo@83: oldItems.setHeight(0); ingo@83: } ingo@83: ingo@53: oldItems.setMembersMargin(10); ingo@23: currentItems.setAlign(VerticalAlignment.TOP); ingo@23: ingo@73: left.setMembersMargin(20); ingo@73: left.setWidth(300); ingo@50: ingo@73: left.addMember(oldItems); ingo@73: left.addMember(currentItems); ingo@73: ingo@73: // This canvas is used to render helper widgets ingo@83: helperPanel = new Canvas(); ingo@73: ingo@90: rootLayout.addMember(left); ingo@90: rootLayout.addMember(helperPanel); ingo@73: raimund@256: topLayout.addMember(rootLayout); ingo@224: if (artifact == null) { ingo@224: Canvas moduleSelection = renderNew(); ingo@224: moduleSelection.setLayoutAlign(VerticalAlignment.TOP); ingo@224: currentItems.addMember(moduleSelection); ingo@224: } ingo@90: raimund@256: setPane(topLayout); ingo@73: } ingo@73: ingo@73: ingo@73: protected void setArtifact(Artifact artifact) { ingo@73: Artifact tmp = this.artifact; ingo@73: this.artifact = artifact; ingo@73: ingo@73: fireParameterChangeEvent(tmp, this.artifact); ingo@73: } ingo@73: ingo@73: ingo@73: /** ingo@73: * This method registers a new ParameterChangeHandler. ingo@73: * ingo@73: * @param handler The new ParameterChangeHandler. ingo@73: */ ingo@73: public void addParameterChangeHandler(ParameterChangeHandler handler) { ingo@73: if (handler != null) { ingo@73: parameterHandlers.add(handler); ingo@73: } ingo@73: } ingo@73: ingo@73: ingo@73: /** ingo@73: * This method calls the onParameterChange() method of all ingo@73: * registered ParameterChangeHandler. ingo@73: */ ingo@73: protected void fireParameterChangeEvent(Artifact old, Artifact newArt) { ingo@73: for (ParameterChangeHandler handler: parameterHandlers) { ingo@73: handler.onParameterChange(new ParameterChangeEvent(old, newArt)); ingo@73: } ingo@73: } ingo@73: ingo@73: ingo@73: /** ingo@73: * This method creates a Canvas displaying the plugins of FLYS combined with ingo@73: * a widget to select a river. ingo@73: * ingo@73: * @return a Canvas that displays the supported plugins and rivers of FLYS. ingo@73: */ ingo@73: protected Canvas renderNew() { ingo@73: River[] rivers = flys.getRivers(); ingo@73: DataItem[] items = new DataItem[rivers.length]; ingo@73: ingo@73: int i = 0; ingo@73: for (River river: rivers) { ingo@73: String name = river.getName(); ingo@73: items[i++] = new DefaultDataItem(name, null, name); ingo@73: } ingo@73: ingo@73: Data data = new DefaultData( ingo@73: "river", ingo@73: MSG.river_selection(), ingo@73: null, ingo@73: items); ingo@73: ingo@73: ModuleSelection widget = new ModuleSelection(); ingo@73: HasStepForwardHandlers handler = (HasStepForwardHandlers) widget; ingo@73: ingo@83: widget.setContainer(helperPanel); ingo@83: ingo@73: handler.addStepForwardHandler(new StepForwardHandler() { ingo@73: public void onStepForward(StepForwardEvent event) { ingo@73: Data[] data = event.getData(); ingo@73: ingo@73: DataItem[] moduleItems = data[0].getItems(); ingo@73: DataItem[] riversItems = data[1].getItems(); ingo@73: ingo@73: String module = moduleItems[0].getStringValue(); ingo@73: String river = riversItems[0].getStringValue(); ingo@73: ingo@90: if (module == null) { ingo@90: // TODO throw / show error! ingo@90: return; ingo@90: } ingo@90: ingo@102: String newTitle = MSG.getString(module); ingo@102: setTitle(newTitle); ingo@90: ingo@229: Config config = Config.getInstance(); ingo@229: final String url = config.getServerUrl(); ingo@229: final String locale = config.getLocale(); ingo@229: ingo@73: final Data[] feedData = new Data[] { data[1] }; ingo@229: ingo@73: artifactService.create( ingo@229: url, locale, module.toLowerCase(), ingo@73: new AsyncCallback() { ingo@73: public void onFailure(Throwable caught) { ingo@73: GWT.log("Could not create the new artifact."); ingo@215: SC.warn(MSG.getString(caught.getMessage())); ingo@73: } ingo@73: ingo@73: public void onSuccess(Artifact artifact) { ingo@73: GWT.log("Successfully created a new artifact."); ingo@73: ingo@229: forwardService.go(url, locale, artifact, feedData, ingo@73: new AsyncCallback() { ingo@73: public void onFailure(Throwable caught) { ingo@73: GWT.log("Could not feed the artifact."); ingo@215: SC.warn(caught.getMessage()); ingo@73: } ingo@73: ingo@73: public void onSuccess(Artifact artifact) { ingo@73: GWT.log("Successfully feed the artifact."); ingo@73: setArtifact(artifact); ingo@73: } ingo@73: }); ingo@73: } ingo@73: }); ingo@73: } ingo@73: }); ingo@73: ingo@73: DataList list = new DataList(); ingo@73: list.add(data); ingo@73: ingo@73: return widget.create(list); ingo@23: } ingo@23: ingo@23: ingo@38: protected void clearOldData() { ingo@38: old.clear(); ingo@38: } ingo@38: ingo@38: ingo@51: public void addOldData(DataList old) { ingo@23: if (old != null) { ingo@23: this.old.add(old); ingo@23: } ingo@23: ingo@23: refreshOld(); ingo@23: } ingo@23: ingo@23: ingo@51: public void addOldDatas(DataList[] old) { ingo@23: if (old != null && old.length > 0) { ingo@51: for (DataList o: old) { ingo@38: if (!exists(o)) { ingo@38: GWT.log("Data '" + o.getLabel() + "' is new."); ingo@38: addOldData(o); ingo@38: } ingo@23: } ingo@23: ingo@23: return; ingo@23: } ingo@23: ingo@23: addOldData(null); ingo@23: } ingo@23: ingo@23: ingo@51: public boolean exists(DataList data) { ingo@51: String stateName = data.getState(); ingo@38: ingo@51: for (DataList o: old) { ingo@51: if (stateName.equals(o.getState())) { ingo@38: return true; ingo@38: } ingo@38: } ingo@38: ingo@38: return false; ingo@38: } ingo@38: ingo@38: ingo@51: public void setCurrentData(DataList current, UIProvider uiProvider) { ingo@23: this.current = current; ingo@23: this.uiProvider = uiProvider; ingo@23: ingo@23: refreshCurrent(); ingo@23: } ingo@23: ingo@23: ingo@23: public void refreshOld() { ingo@23: // XXX maybe we should not remove all items but just add the newest item ingo@23: // to the list. ingo@23: oldItems.removeMembers(oldItems.getMembers()); ingo@23: ingo@51: for (DataList dataList: old) { ingo@23: HLayout h = new HLayout(); ingo@23: h.setAlign(VerticalAlignment.TOP); ingo@23: h.setHeight(20); ingo@23: ingo@53: String provider = dataList.getUIProvider(); ingo@53: UIProvider uiprovider = UIProviderFactory.getProvider(provider); ingo@60: ((HasStepBackHandlers) uiprovider).addStepBackHandler(this); ingo@23: ingo@53: oldItems.addMember(uiprovider.createOld(dataList)); ingo@23: } ingo@23: ingo@23: int minHeight = oldItems.getMinHeight(); ingo@23: if (minHeight <= 20) { ingo@23: oldItems.setHeight(20); ingo@23: } ingo@23: else { ingo@23: oldItems.setHeight(minHeight); ingo@23: } ingo@23: } ingo@23: ingo@23: ingo@23: /** ingo@23: * This method refreshes the part displaying the data of the current state. ingo@23: * The UI is created using the UIProvider stored in the Data object. ingo@23: */ ingo@23: public void refreshCurrent() { ingo@40: currentItems.removeMembers(currentItems.getMembers()); ingo@53: ingo@54: if (current != null && uiProvider != null) { ingo@54: Canvas c = uiProvider.create(current); ingo@54: c.setLayoutAlign(VerticalAlignment.TOP); ingo@54: ingo@54: currentItems.addMember(c); ingo@54: } ingo@23: } ingo@60: ingo@60: ingo@60: /** ingo@73: * This method is called if the user clicks on the 'next' button to advance ingo@73: * to the next state. ingo@73: * ingo@73: * @param event The StepForwardEvent. ingo@73: */ ingo@73: public void onStepForward(StepForwardEvent event) { ingo@73: GWT.log("CollectionView - onStepForward()"); ingo@73: ingo@229: Config config = Config.getInstance(); ingo@229: String serverUrl = config.getServerUrl(); ingo@229: String locale = config.getLocale(); ingo@229: ingo@229: forwardService.go(serverUrl, locale, artifact, event.getData(), ingo@73: new AsyncCallback() { ingo@73: public void onFailure(Throwable caught) { ingo@73: GWT.log("Could not feed the artifact."); ingo@215: SC.warn(MSG.getString(caught.getMessage())); ingo@73: } ingo@73: ingo@73: public void onSuccess(Artifact artifact) { ingo@73: GWT.log("Successfully feed the artifact."); ingo@73: setArtifact(artifact); ingo@73: } ingo@73: }); ingo@73: } ingo@73: ingo@73: ingo@73: /** ingo@60: * This method is used to remove all old items from this list after the user ingo@60: * has clicked the step back button. ingo@60: * ingo@73: * @param e The StepBackEvent that holds the identifier of the target state. ingo@60: */ ingo@60: public void onStepBack(StepBackEvent e) { ingo@73: final String target = e.getTarget(); ingo@73: ingo@229: Config config = Config.getInstance(); ingo@229: final String serverUrl = config.getServerUrl(); ingo@229: final String locale = config.getLocale(); ingo@229: ingo@229: advanceService.advance(serverUrl, locale, artifact, target, ingo@73: new AsyncCallback() { ingo@73: public void onFailure(Throwable caught) { ingo@73: GWT.log("Could not go back to '" + target + "'"); ingo@215: SC.warn(MSG.getString(caught.getMessage())); ingo@73: } ingo@73: ingo@73: public void onSuccess(Artifact artifact) { ingo@73: GWT.log("Successfully step back to '" + target + "'"); ingo@73: ingo@73: old.clear(); ingo@73: oldItems.removeMembers(oldItems.getMembers()); ingo@73: ingo@73: setArtifact(artifact); ingo@73: } ingo@73: } ingo@73: ); ingo@60: } ingo@72: ingo@72: ingo@72: /** ingo@72: * Implements the onCollectionChange() method to do update the GUI after the ingo@72: * parameterization has changed. ingo@72: * ingo@72: * @param event The ParameterChangeEvent. ingo@72: */ ingo@72: public void onParameterChange(ParameterChangeEvent event) { ingo@72: GWT.log("ParameterList.onParameterChange"); ingo@72: ingo@83: for (Canvas c: helperPanel.getChildren()) { ingo@83: helperPanel.removeChild(c); ingo@83: } ingo@83: ingo@72: Artifact art = event.getNewValue(); ingo@72: ArtifactDescription desc = art.getArtifactDescription(); ingo@72: ingo@72: DataList currentData = desc.getCurrentData(); ingo@72: if (currentData != null) { ingo@72: // the user has to enter some attributes ingo@72: String uiProvider = currentData.getUIProvider(); ingo@72: UIProvider provider = UIProviderFactory.getProvider(uiProvider); ingo@72: ingo@83: provider.setContainer(helperPanel); raimund@233: provider.setArtifact(art); ingo@83: ingo@73: ((HasStepForwardHandlers) provider).addStepForwardHandler(this); ingo@73: ((HasStepBackHandlers) provider).addStepBackHandler(this); ingo@72: ingo@72: setCurrentData(currentData, provider); ingo@72: } ingo@72: else { ingo@72: // we have reached a final state with no more user input ingo@72: setCurrentData(null, null); ingo@72: } ingo@72: ingo@72: addOldDatas(desc.getOldData()); ingo@72: } raimund@256: raimund@256: /** raimund@256: * Adds a table to the parameterlist to show calculated data. raimund@256: * raimund@256: * @param table The table data panel. raimund@256: */ raimund@256: public void setPanel (TableDataPanel table) { raimund@260: tablePanel = table.create(); raimund@260: tablePanel.setHeight(400); raimund@260: topLayout.addMember(tablePanel); raimund@260: } raimund@260: raimund@260: raimund@260: /** raimund@260: * Removes the table from the parameter list. raimund@260: */ raimund@260: public void removePanel() { raimund@260: topLayout.removeMember(tablePanel); raimund@260: } raimund@260: raimund@260: raimund@260: public boolean hasPanel() { raimund@260: return topLayout.hasMember(tablePanel); raimund@256: } ingo@23: } ingo@23: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :