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.stationinfo; bjoern@4956: bjoern@4956: import com.google.gwt.core.client.GWT; rrenkert@8313: import com.google.gwt.user.client.rpc.AsyncCallback; bjoern@4956: rrenkert@8313: import com.smartgwt.client.util.SC; bjoern@4956: import com.smartgwt.client.widgets.Canvas; bjoern@4956: import com.smartgwt.client.widgets.grid.ListGridField; bjoern@4956: import com.smartgwt.client.widgets.grid.ListGridRecord; rrenkert@5503: import com.smartgwt.client.widgets.grid.events.RecordClickEvent; rrenkert@5503: import com.smartgwt.client.widgets.grid.events.RecordClickHandler; bjoern@4956: rrenkert@8313: import org.dive4elements.river.client.client.Config; teichmann@5835: import org.dive4elements.river.client.client.FLYS; rrenkert@8313: import org.dive4elements.river.client.client.services.ArtifactService; rrenkert@8313: import org.dive4elements.river.client.client.services.ArtifactServiceAsync; rrenkert@8313: import org.dive4elements.river.client.client.services.CreateCollectionService; rrenkert@8313: import org.dive4elements.river.client.client.services.CreateCollectionServiceAsync; rrenkert@8313: import org.dive4elements.river.client.client.services.StepForwardService; rrenkert@8313: import org.dive4elements.river.client.client.services.StepForwardServiceAsync; rrenkert@8313: import org.dive4elements.river.client.client.ui.CollectionView; rrenkert@8313: import org.dive4elements.river.client.shared.model.Artifact; rrenkert@8313: import org.dive4elements.river.client.shared.model.Collection; rrenkert@8313: import org.dive4elements.river.client.shared.model.Data; rrenkert@8313: import org.dive4elements.river.client.shared.model.DataItem; rrenkert@8313: import org.dive4elements.river.client.shared.model.DefaultData; rrenkert@8313: import org.dive4elements.river.client.shared.model.DefaultDataItem; teichmann@5835: import org.dive4elements.river.client.shared.model.MeasurementStation; teichmann@5835: import org.dive4elements.river.client.shared.model.RiverInfo; bjoern@4956: bjoern@4956: import java.util.ArrayList; bjoern@4956: import java.util.List; bjoern@4956: bjoern@4956: /** bjoern@4956: * @author Björn Ricks bjoern@4956: */ rrenkert@5503: public class MeasurementStationListGrid rrenkert@5503: extends InfoListGrid rrenkert@5503: implements RecordClickHandler { bjoern@4956: rrenkert@8313: /** The ArtifactService used to communicate with the Artifact server. */ rrenkert@8313: protected ArtifactServiceAsync artifactService = rrenkert@8313: GWT.create(ArtifactService.class); rrenkert@8313: rrenkert@8313: /** The StepForwardService used to put data into an existing artifact. */ rrenkert@8313: protected StepForwardServiceAsync forwardService = rrenkert@8313: GWT.create(StepForwardService.class); rrenkert@8313: rrenkert@8313: /** The ArtifactService used to communicate with the Artifact server. */ rrenkert@8313: protected CreateCollectionServiceAsync createCollectionService = rrenkert@8313: GWT.create(CreateCollectionService.class); rrenkert@8313: bjoern@4956: public MeasurementStationListGrid(FLYS flys) { bjoern@4956: super(flys); bjoern@4956: ListGridField nfield = new ListGridField("name", "Messtelle"); teichmann@7859: ListGridField mfield = new ListGridField("measurementtype", "Messstellenart"); bjoern@4956: ListGridField sfield = new ListGridField("kmstart", "Start [km]", 60); bjoern@4956: ListGridField efield = new ListGridField("kmend", "Ende [km]", 60); bjoern@4956: ListGridField stfield = new ListGridField("station", "Station [km]"); teichmann@7873: ListGridField lfield = new ListGridField("infolink", "Info"); teichmann@7873: ListGridField cfield = new ListGridField("curvelink", "Feststofftransport-Abfluss-Beziehung"); rrenkert@5503: cfield.addRecordClickHandler(this); rrenkert@6272: rrenkert@6272: this.setShowRecordComponents(true); rrenkert@6272: this.setShowRecordComponentsByCell(true); teichmann@7859: this.setFields(nfield, mfield, sfield, efield, stfield, lfield, cfield); bjoern@4956: } bjoern@4956: bjoern@4956: /** bjoern@4956: * Resets the items of the tree. bjoern@4956: * If the list of gauges is empty or null the tree will be empty. bjoern@4956: */ bjoern@4956: @Override bjoern@4956: public void setRiverInfo(RiverInfo riverinfo) { bjoern@4956: List stations = riverinfo.getMeasurementStations(); bjoern@4956: GWT.log("MeasurmentStationListGrid - setRiverInfo " + stations); bjoern@4956: bjoern@4956: if (stations != null && !stations.isEmpty()) { bjoern@4956: bjoern@4956: ArrayList emptystations = bjoern@4956: new ArrayList(); bjoern@4956: bjoern@4956: if (!riverinfo.isKmUp()) { bjoern@4956: for (MeasurementStation station : stations) { bjoern@4956: addStation(station, emptystations); bjoern@4956: } bjoern@4956: } bjoern@4956: else { bjoern@4956: for (int i = stations.size()-1; i >= 0; i--) { bjoern@4956: MeasurementStation station = stations.get(i); bjoern@4956: addStation(station, emptystations); bjoern@4956: } bjoern@4956: } bjoern@4956: bjoern@4956: // put empty stations to the end bjoern@4956: for (MeasurementStation station : emptystations) { bjoern@4956: addStation(station); bjoern@4956: } bjoern@4956: } bjoern@4956: } bjoern@4956: bjoern@4956: private void addStation(MeasurementStation station, bjoern@4956: List empty) { tom@8243: /* switch off sorting stations without range to the end bjoern@4956: if (station.getKmStart() != null && station.getKmEnd() != null) { bjoern@4956: addStation(station); bjoern@4956: } bjoern@4956: else { bjoern@4956: empty.add(station); bjoern@4956: } tom@8243: */ tom@8243: addStation(station); bjoern@4956: } bjoern@4956: bjoern@4956: private void addStation(MeasurementStation station) { bjoern@4956: ListGridRecord record = new MeasurementStationRecord(station); bjoern@4956: this.addData(record); bjoern@4956: } bjoern@4956: bjoern@4956: @Override bjoern@4956: public void open() { bjoern@4956: } bjoern@4956: bjoern@4956: @Override bjoern@4956: protected Canvas getExpandPanel(ListGridRecord record) { bjoern@4956: MeasurementStationRecord station = (MeasurementStationRecord)record; rrenkert@7935: return new MeasurementStationInfoPanel(station); bjoern@4956: } rrenkert@5503: rrenkert@5503: @Override rrenkert@5503: public void onRecordClick(RecordClickEvent event) { rrenkert@8313: final MeasurementStationRecord station = rrenkert@5503: (MeasurementStationRecord)event.getRecord(); rrenkert@8313: Config config = Config.getInstance(); rrenkert@8313: final String locale = config.getLocale(); rrenkert@8313: createCollectionService.create( rrenkert@8313: locale, rrenkert@8313: flys.getCurrentUser().identifier(), rrenkert@8313: new AsyncCallback() { rrenkert@8313: @Override rrenkert@8313: public void onFailure(Throwable caught) { rrenkert@8313: GWT.log("Could not create the new collection."); rrenkert@8313: SC.warn(FLYS.getExceptionString(MSG, caught)); rrenkert@8313: } rrenkert@8313: rrenkert@8313: @Override rrenkert@8313: public void onSuccess(Collection collection) { rrenkert@8313: GWT.log("Successfully created a new collection."); rrenkert@8313: createArtifact(collection, locale, station); rrenkert@8313: } rrenkert@8313: } rrenkert@8313: ); rrenkert@8313: } rrenkert@8313: rrenkert@8313: private void createArtifact( rrenkert@8313: final Collection collection, rrenkert@8313: final String locale, rrenkert@8313: final MeasurementStationRecord station rrenkert@8313: ) { rrenkert@8313: artifactService.create( rrenkert@8313: locale, "staticsqrelation", null, rrenkert@8313: new AsyncCallback() { rrenkert@8313: @Override rrenkert@8313: public void onFailure(Throwable caught) { rrenkert@8313: GWT.log("Could not create the new artifact."); rrenkert@8313: SC.warn(FLYS.getExceptionString(MSG, caught)); rrenkert@8313: } rrenkert@8313: rrenkert@8313: @Override rrenkert@8313: public void onSuccess(Artifact artifact) { rrenkert@8313: GWT.log("Successfully created a new artifact."); rrenkert@8313: rrenkert@8313: DataItem riverItem = new DefaultDataItem( rrenkert@8313: "river", rrenkert@8313: "river", rrenkert@8313: station.getRiverName()); rrenkert@8313: Data river = new DefaultData( rrenkert@8313: "river", rrenkert@8313: null, rrenkert@8313: null, rrenkert@8313: new DataItem[]{riverItem}); rrenkert@8313: rrenkert@8313: DataItem refItem = new DefaultDataItem( rrenkert@8313: "station", rrenkert@8313: "station", rrenkert@8313: station.getID().toString()); rrenkert@8313: Data ref = new DefaultData( rrenkert@8313: "station", rrenkert@8313: null, rrenkert@8313: null, rrenkert@8313: new DataItem[]{refItem}); rrenkert@8313: rrenkert@8313: DataItem nameItem = new DefaultDataItem( rrenkert@8313: "station_name", rrenkert@8313: "station_name", rrenkert@8313: station.getID().toString()); rrenkert@8313: Data name = new DefaultData( rrenkert@8313: "station_name", rrenkert@8313: null, rrenkert@8313: null, rrenkert@8313: new DataItem[]{nameItem}); rrenkert@8313: rrenkert@8313: Data[] data = new Data[]{river, ref, name}; rrenkert@8313: forwardService.go(locale, artifact, data, rrenkert@8313: new AsyncCallback() { rrenkert@8313: @Override rrenkert@8313: public void onFailure(Throwable caught) { rrenkert@8313: GWT.log("Could not feed the artifact."); rrenkert@8313: SC.warn(caught.getMessage()); rrenkert@8313: } rrenkert@8313: rrenkert@8313: @Override rrenkert@8313: public void onSuccess(Artifact artifact) { rrenkert@8313: GWT.log("Successfully feed the artifact."); rrenkert@8313: CollectionView view = new CollectionView( rrenkert@8313: flys, rrenkert@8313: collection, rrenkert@8313: artifact); rrenkert@8313: flys.getWorkspace().addView( rrenkert@8313: collection.identifier(), rrenkert@8313: view); rrenkert@8313: view.addArtifactToCollection(artifact); rrenkert@8313: } rrenkert@8313: }); rrenkert@8313: } rrenkert@8313: }); rrenkert@5503: } rrenkert@5503: rrenkert@6272: @Override rrenkert@6272: public String getCellCSSText(ListGridRecord record, int rowNum, rrenkert@6272: int colNum) { rrenkert@8285: if (colNum == 7) { rrenkert@6272: return "text-decoration: underline; color: #0000EE; cursor: pointer;"; rrenkert@6272: } rrenkert@6272: else { rrenkert@6272: return super.getCellCSSText(record, rowNum, colNum); rrenkert@6272: } rrenkert@6272: } rrenkert@6272: bjoern@4956: }