teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.client.ui; ingo@41: ingo@41: import com.google.gwt.core.client.GWT; christian@4131: ingo@562: import com.smartgwt.client.util.SC; ingo@788: import com.smartgwt.client.widgets.Button; ingo@41: import com.smartgwt.client.widgets.Canvas; ingo@57: import com.smartgwt.client.widgets.Img; ingo@41: import com.smartgwt.client.widgets.events.ClickEvent; ingo@41: import com.smartgwt.client.widgets.events.ClickHandler; ingo@788: import com.smartgwt.client.widgets.layout.VLayout; ingo@41: teichmann@5835: import org.dive4elements.river.client.client.FLYSConstants; aheinecke@6232: import org.dive4elements.river.client.client.FLYS; teichmann@5835: import org.dive4elements.river.client.client.event.HasStepBackHandlers; teichmann@5835: import org.dive4elements.river.client.client.event.HasStepForwardHandlers; teichmann@5835: import org.dive4elements.river.client.client.event.StepBackEvent; teichmann@5835: import org.dive4elements.river.client.client.event.StepBackHandler; teichmann@5835: import org.dive4elements.river.client.client.event.StepForwardEvent; teichmann@5835: import org.dive4elements.river.client.client.event.StepForwardHandler; teichmann@5835: import org.dive4elements.river.client.shared.model.Artifact; teichmann@5835: import org.dive4elements.river.client.shared.model.ArtifactDescription; teichmann@5835: import org.dive4elements.river.client.shared.model.Collection; teichmann@5835: import org.dive4elements.river.client.shared.model.Data; teichmann@5835: import org.dive4elements.river.client.shared.model.DataItem; teichmann@5835: import org.dive4elements.river.client.shared.model.DataList; teichmann@5835: import org.dive4elements.river.client.shared.model.DefaultData; teichmann@5835: import org.dive4elements.river.client.shared.model.DefaultDataItem; felix@1591: christian@3384: import java.util.ArrayList; christian@3384: import java.util.List; christian@3384: felix@1591: ingo@41: /** ingo@41: * An abstract UIProvider that provides some basic methods. ingo@41: * ingo@41: * @author Ingo Weinzierl ingo@41: */ ingo@41: public abstract class AbstractUIProvider ingo@60: implements UIProvider, HasStepForwardHandlers, ClickHandler, ingo@60: HasStepBackHandlers ingo@41: { christian@4131: private static final long serialVersionUID = -1610874613377494184L; christian@4131: felix@5189: /** The message class that provides i18n strings. */ ingo@211: protected FLYSConstants MSG = GWT.create(FLYSConstants.class); ingo@57: felix@5189: /** The StepForwardHandlers. */ ingo@41: protected List forwardHandlers; ingo@41: felix@5189: /** The StepForwardHandlers. */ ingo@60: protected List backHandlers; ingo@60: felix@5189: /** The container that is used to position helper widgets. */ ingo@788: protected VLayout helperContainer; ingo@83: felix@5189: /** The artifact that contains status information. */ raimund@233: protected Artifact artifact; ingo@41: felix@5189: /** The Collection. */ ingo@909: protected Collection collection; ingo@909: felix@5189: /** The ParameterList. */ raimund@915: protected ParameterList parameterList; raimund@915: ingo@41: /** ingo@41: * Creates a new UIProvider instance of this class. ingo@41: */ ingo@41: public AbstractUIProvider() { ingo@41: forwardHandlers = new ArrayList(); ingo@60: backHandlers = new ArrayList(); ingo@60: } ingo@60: ingo@60: ingo@60: /** ingo@60: * Appends a StepBackHandler that wants to listen to StepBackEvents. ingo@60: * ingo@60: * @param handler A new StepBackHandler. ingo@60: */ christian@3384: @Override ingo@60: public void addStepBackHandler(StepBackHandler handler) { ingo@60: if (handler != null) { ingo@60: backHandlers.add(handler); ingo@60: } ingo@41: } ingo@41: ingo@41: ingo@41: /** ingo@41: * Appends a StepForwardHandler that wants to listen to StepForwardEvents. ingo@41: * ingo@41: * @param handler A new StepForwardHandler. ingo@41: */ christian@3384: @Override ingo@41: public void addStepForwardHandler(StepForwardHandler handler) { ingo@41: if (handler != null) { ingo@41: forwardHandlers.add(handler); ingo@41: } ingo@41: } ingo@41: ingo@41: ingo@41: /** ingo@60: * This method is called after the user has clicked one of the buttons to ingo@60: * step back to a previous state. ingo@60: * ingo@60: * @param e The StepBackEvent. ingo@60: */ ingo@60: protected void fireStepBackEvent(StepBackEvent e) { tom@8856: GWT.log("AbstractUIProvider - fireStepBackEvent() handlers: " tom@8856: + backHandlers.size()); ingo@60: for (StepBackHandler handler: backHandlers) { ingo@60: handler.onStepBack(e); ingo@60: } ingo@60: } ingo@60: ingo@60: ingo@60: /** ingo@41: * This method is called after the user has clicked on the 'next' button to ingo@41: * step to the next state. ingo@41: * ingo@41: * @param e The StepForwardEvent. ingo@41: */ ingo@41: protected void fireStepForwardEvent(StepForwardEvent e) { tom@8856: GWT.log("AbstractUIProvider - fireStepForwardEvent() handlers: " tom@8856: + forwardHandlers.size()); ingo@41: for (StepForwardHandler handler: forwardHandlers) { ingo@41: handler.onStepForward(e); ingo@41: } ingo@41: } ingo@41: ingo@41: ingo@41: /** ingo@41: * This method is used to listen to click events on the 'next' button. The ingo@41: * fireStepForwardEvent() method is called here. ingo@41: * ingo@41: * @param e The click event. ingo@41: */ christian@3384: @Override ingo@41: public void onClick(ClickEvent e) { ingo@562: List errors = validate(); ingo@562: if (errors == null || errors.isEmpty()) { ingo@562: Data[] data = getData(); ingo@562: fireStepForwardEvent(new StepForwardEvent(data)); ingo@562: } ingo@562: else { ingo@909: showErrors(errors); ingo@909: } ingo@909: } ingo@41: ingo@562: ingo@909: protected void showErrors(List errors) { ingo@909: StringBuilder sb = new StringBuilder(); ingo@909: ingo@909: for (String error: errors) { ingo@909: sb.append(error); ingo@909: sb.append("
"); ingo@562: } ingo@909: ingo@909: SC.warn(sb.toString()); ingo@41: } ingo@41: ingo@41: ingo@58: /** ingo@58: * Creates the 'next' button to step forward to the next state. ingo@58: * ingo@58: * @return the 'next' button. ingo@58: */ ingo@57: protected Canvas getNextButton() { raimund@81: Button next = new Button(MSG.buttonNext()); raimund@81: next.addClickHandler(this); ingo@57: raimund@81: return next; ingo@57: } ingo@57: ingo@57: ingo@2500: @Override aheinecke@6232: public Canvas createHelpLink(DataList dataList, Data data, FLYS instance) { ingo@2500: String iUrl = GWT.getHostPageBaseURL() + MSG.getFeatureInfo(); ingo@2500: String helpUrl = dataList.getHelpText(); ingo@2500: aheinecke@6232: return new WikiImgLink(iUrl, helpUrl, 30, 30, instance); ingo@2500: } ingo@2500: ingo@2500: ingo@41: /** ingo@58: * Creates the 'back' button to step back to a previous state. ingo@58: * ingo@58: * @param targetState The identifier of the target state. ingo@58: * ingo@58: * @return the 'back' button. ingo@58: */ ingo@58: protected Canvas getBackButton(final String targetState) { ingo@89: String url = GWT.getHostPageBaseURL() + MSG.imageBack(); ingo@89: Img back = new Img(url, 16, 16); ingo@58: ingo@58: back.addClickHandler(new ClickHandler() { christian@3384: @Override ingo@58: public void onClick(ClickEvent event) { ingo@60: fireStepBackEvent(new StepBackEvent(targetState)); ingo@58: } ingo@58: }); ingo@58: ingo@58: return back; ingo@58: } ingo@58: ingo@58: ingo@58: /** ingo@83: * This method injects a container that is used to position helper widgets. ingo@83: * ingo@83: * @param helperContainer A container that is used to position helper ingo@83: * widgets. ingo@83: */ christian@3384: @Override ingo@788: public void setContainer(VLayout helperContainer) { ingo@83: this.helperContainer = helperContainer; ingo@83: } ingo@83: ingo@83: ingo@83: /** raimund@233: * This method injects an artifact that contains the status information. raimund@233: * raimund@233: * @param art An artifact containing status information. raimund@233: */ christian@3384: @Override raimund@233: public void setArtifact(Artifact art) { raimund@233: this.artifact = art; raimund@233: } raimund@233: raimund@233: christian@3384: @Override ingo@909: public void setCollection(Collection collection) { ingo@909: this.collection = collection; ingo@909: } ingo@909: ingo@909: christian@3384: @Override raimund@915: public void setParameterList(ParameterList list) { raimund@915: this.parameterList = list; raimund@915: } raimund@915: raimund@915: ingo@909: public Collection getCollection() { ingo@909: return collection; ingo@909: } ingo@909: ingo@909: raimund@233: /** ingo@247: * This method greps the Data with name name from the list and ingo@247: * returns it. ingo@247: * ingo@247: * @param items A list of Data. ingo@247: * @param name The name of the Data that we are searching for. ingo@247: * ingo@247: * @return the Data with the name name. ingo@247: */ ingo@247: protected Data getData(List data, String name) { ingo@247: for (Data d: data) { ingo@247: if (name.equals(d.getLabel())) { ingo@247: return d; ingo@247: } ingo@247: } ingo@247: ingo@247: return null; ingo@247: } ingo@247: ingo@873: ingo@873: protected String getDataValue(String state, String name) { ingo@873: ArtifactDescription desc = artifact.getArtifactDescription(); ingo@873: ingo@873: DataList[] old = desc.getOldData(); ingo@873: ingo@873: for (DataList list: old) { rrenkert@5308: if (list == null) { rrenkert@5308: continue; rrenkert@5308: } ingo@873: Data d = getData(list.getAll(), name); ingo@873: ingo@873: if (d != null) { ingo@873: return d.getItems()[0].getStringValue(); ingo@873: } ingo@873: } ingo@873: ingo@873: return null; ingo@873: } ingo@873: ingo@247: /** ingo@247: * This method greps the DataItem with name name from the list and ingo@247: * returns it. ingo@247: * ingo@247: * @param items A list of DataItems. ingo@247: * @param name The name of the DataItem that we are searching for. ingo@247: * ingo@247: * @return the DataItem with the name name. ingo@247: */ ingo@247: protected DataItem getDataItem(DataItem[] items, String name) { ingo@247: for (DataItem item: items) { ingo@247: if (name.equals(item.getLabel())) { ingo@247: return item; ingo@247: } ingo@247: } ingo@247: ingo@247: return null; ingo@247: } ingo@247: ingo@247: ingo@562: public List validate() { christian@5658: return new ArrayList(); // FIXME: What's this? ingo@562: } ingo@562: ingo@562: felix@1591: /** Create simple DefaultData with single DataItem inside. */ felix@1591: public static DefaultData createDataArray(String name, String value) { felix@1591: DataItem item = new DefaultDataItem( felix@1591: name, felix@1591: name, felix@1591: value); felix@1591: felix@1591: return new DefaultData(name, felix@1591: null, felix@1591: null, felix@1591: new DataItem[] {item}); felix@1591: } felix@1591: felix@1591: ingo@247: /** ingo@41: * This method needs to be implemented by concrete subclasses. It should ingo@41: * create a new Canvas object with a representation of data. ingo@41: * ingo@41: * @param data The data that should be displayed. ingo@41: * ingo@41: * @return a Canvas object that displays data. ingo@41: */ christian@3384: @Override ingo@51: public abstract Canvas create(DataList data); ingo@41: ingo@41: ingo@41: /** ingo@41: * This method needs to be implemented by concrete subclasses. It should ingo@41: * return the selected data. ingo@41: * ingo@41: * @return the selected data. ingo@41: */ ingo@41: protected abstract Data[] getData(); ingo@41: } ingo@41: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :