ingo@41: package de.intevation.flys.client.client.ui;
ingo@41:
ingo@41: import java.util.ArrayList;
ingo@41: import java.util.List;
ingo@41:
ingo@41: import com.google.gwt.core.client.GWT;
ingo@41:
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;
raimund@81: import com.smartgwt.client.widgets.Button;
ingo@41:
ingo@211: import de.intevation.flys.client.client.FLYSConstants;
ingo@60: import de.intevation.flys.client.client.event.HasStepBackHandlers;
ingo@41: import de.intevation.flys.client.client.event.HasStepForwardHandlers;
ingo@60: import de.intevation.flys.client.client.event.StepBackEvent;
ingo@60: import de.intevation.flys.client.client.event.StepBackHandler;
ingo@41: import de.intevation.flys.client.client.event.StepForwardEvent;
ingo@41: import de.intevation.flys.client.client.event.StepForwardHandler;
ingo@41: import de.intevation.flys.client.shared.model.Data;
ingo@247: import de.intevation.flys.client.shared.model.DataItem;
ingo@51: import de.intevation.flys.client.shared.model.DataList;
raimund@233: import de.intevation.flys.client.shared.model.Artifact;
ingo@41:
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: {
ingo@57: /** The message class that provides i18n strings.*/
ingo@211: protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
ingo@57:
ingo@57:
ingo@41: /** The StepForwardHandlers.*/
ingo@41: protected List forwardHandlers;
ingo@41:
ingo@60: /** The StepForwardHandlers.*/
ingo@60: protected List backHandlers;
ingo@60:
ingo@83: /** The container that is used to position helper widgets.*/
ingo@83: protected Canvas helperContainer;
ingo@83:
raimund@233: /** The artifact that contains status information.*/
raimund@233: protected Artifact artifact;
ingo@41:
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: */
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: */
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) {
ingo@60: GWT.log("AbstractUIProvider - fireStepBackEvent() handlers: " + 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) {
ingo@41: GWT.log("AbstractUIProvider - fireStepForwardEvent() handlers: " + 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: */
ingo@41: public void onClick(ClickEvent e) {
ingo@41: Data[] data = getData();
ingo@41:
ingo@41: fireStepForwardEvent(new StepForwardEvent(data));
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@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() {
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: */
ingo@83: public void setContainer(Canvas 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: */
raimund@233: public void setArtifact(Artifact art) {
raimund@233: this.artifact = art;
raimund@233: }
raimund@233:
raimund@233:
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@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@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: */
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 :