raimund@238: package de.intevation.flys.client.client.ui; raimund@238: raimund@238: import java.util.List; raimund@238: raimund@238: import com.google.gwt.core.client.GWT; raimund@238: import com.google.gwt.user.client.rpc.AsyncCallback; raimund@238: raimund@238: import com.smartgwt.client.widgets.Canvas; raimund@238: import com.smartgwt.client.widgets.Label; raimund@238: import com.smartgwt.client.widgets.form.fields.events.BlurHandler; raimund@238: import com.smartgwt.client.widgets.form.fields.events.BlurEvent; raimund@238: import com.smartgwt.client.widgets.form.fields.FormItem; raimund@238: import com.smartgwt.client.widgets.layout.HLayout; raimund@238: import com.smartgwt.client.widgets.layout.VLayout; raimund@238: import com.smartgwt.client.widgets.grid.ListGrid; raimund@238: import com.smartgwt.client.widgets.grid.ListGridField; raimund@238: import com.smartgwt.client.widgets.grid.ListGridRecord; raimund@238: import com.smartgwt.client.widgets.grid.events.RecordClickHandler; raimund@238: import com.smartgwt.client.widgets.grid.events.RecordClickEvent; raimund@238: raimund@238: import com.smartgwt.client.types.ListGridFieldType; raimund@238: raimund@238: import de.intevation.flys.client.shared.model.Data; raimund@238: import de.intevation.flys.client.shared.model.DataItem; raimund@238: import de.intevation.flys.client.shared.model.DataList; raimund@238: import de.intevation.flys.client.shared.model.DefaultData; raimund@238: import de.intevation.flys.client.shared.model.DefaultDataItem; raimund@238: import de.intevation.flys.client.shared.model.DistanceInfoObject; raimund@238: import de.intevation.flys.client.shared.model.DistanceInfoRecord; raimund@238: import de.intevation.flys.client.shared.model.ArtifactDescription; raimund@238: raimund@238: import de.intevation.flys.client.client.services.DistanceInfoService; raimund@238: import de.intevation.flys.client.client.services.DistanceInfoServiceAsync; raimund@238: import de.intevation.flys.client.client.FLYSConstants; raimund@238: import de.intevation.flys.client.client.FLYSImages; raimund@238: import de.intevation.flys.client.client.Config; raimund@238: raimund@238: raimund@238: /** raimund@238: * This UIProvider creates a widget to enter locations. raimund@238: * raimund@238: * @author Raimund Renkert raimund@238: */ raimund@238: public class SingleLocationPanel raimund@238: extends AbstractUIProvider raimund@238: { raimund@238: /** The message class that provides i18n strings.*/ raimund@238: protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); raimund@238: raimund@238: /** The interface that provides the image resources. */ raimund@238: private FLYSImages IMAGES = GWT.create(FLYSImages.class); raimund@238: raimund@238: /** The DistanceInfoService used to retrieve locations about rivers.*/ raimund@238: protected DistanceInfoServiceAsync distanceInfoService = raimund@238: GWT.create(DistanceInfoService.class); raimund@238: raimund@238: /** A container that will contain the location or the distance panel.*/ raimund@238: protected HLayout container; raimund@238: raimund@238: /** The values entered in the location mode.*/ raimund@238: protected double[] values; raimund@238: raimund@238: /** The input panel for locations */ raimund@238: protected DoubleArrayPanel locationPanel; raimund@238: raimund@238: /** The locations table */ raimund@238: protected ListGrid locationTable; raimund@238: raimund@238: /** The table data. */ raimund@238: protected DistanceInfoObject[] tableData; raimund@238: raimund@238: /** raimund@238: * Creates a new LocationDistancePanel instance. raimund@238: */ raimund@238: public SingleLocationPanel() { raimund@238: locationTable = new ListGrid(); raimund@238: } raimund@238: raimund@238: raimund@238: /** raimund@238: * This method creates a widget that contains a label, a panel with raimund@238: * checkboxes to switch the input mode between location and distance input, raimund@238: * and a the mode specific panel. raimund@238: * raimund@238: * @param data The data that might be inserted. raimund@238: * raimund@238: * @return a panel. raimund@238: */ raimund@238: public Canvas create(DataList data) { raimund@238: VLayout layout = new VLayout(); raimund@238: layout.setMembersMargin(10); raimund@238: raimund@238: initDefaults(data); raimund@238: raimund@238: Label label = new Label(MESSAGES.location ()); raimund@238: Canvas widget = createWidget(data); raimund@238: Canvas submit = getNextButton(); raimund@238: raimund@238: createLocationTable(); raimund@238: raimund@238: widget.setHeight(50); raimund@238: label.setHeight(25); raimund@238: raimund@238: layout.addMember(label); raimund@238: layout.addMember(widget); raimund@238: layout.addMember(submit); raimund@238: raimund@238: return layout; raimund@238: } raimund@238: raimund@238: raimund@238: /** raimund@238: * This method creates a table that contains the location values. raimund@238: */ raimund@238: protected void createLocationTable() { raimund@238: locationTable.setWidth(450); raimund@238: locationTable.setShowRecordComponents(true); raimund@238: locationTable.setShowRecordComponentsByCell(true); raimund@238: locationTable.setHeight(300); raimund@238: raimund@238: ListGridField addLocation = new ListGridField ("", ""); raimund@238: addLocation.setType (ListGridFieldType.ICON); raimund@238: addLocation.setWidth ("30"); raimund@238: addLocation.addRecordClickHandler (new RecordClickHandler () { raimund@238: public void onRecordClick (RecordClickEvent e) { raimund@238: ListGridRecord[] records = locationTable.getSelection(); raimund@238: double[] selected = new double[1]; raimund@238: selected[0] = records[0].getAttributeAsDouble("from"); raimund@238: setLocationValues(selected); raimund@238: } raimund@238: }); raimund@238: addLocation.setCellIcon (IMAGES.markerGreen ().getURL ()); raimund@238: raimund@238: ListGridField ldescr = new ListGridField("description", raimund@238: MESSAGES.description()); raimund@238: ldescr.setType(ListGridFieldType.TEXT); raimund@238: ldescr.setWidth("*"); raimund@238: ListGridField lside = new ListGridField("riverside", raimund@238: MESSAGES.riverside()); raimund@238: lside.setType(ListGridFieldType.TEXT); raimund@238: lside.setWidth(60); raimund@238: ListGridField loc = new ListGridField("from", MESSAGES.location()); raimund@238: loc.setType(ListGridFieldType.TEXT); raimund@238: loc.setWidth(80); raimund@238: locationTable.setFields(addLocation, ldescr, loc, lside); raimund@238: } raimund@238: raimund@238: raimund@238: public Canvas createOld(DataList dataList) { raimund@238: List items = dataList.getAll(); raimund@242: Data dLocation = getData(items, "ld_locations"); raimund@242: DataItem[] loc = dLocation.getItems(); raimund@238: raimund@238: HLayout layout = new HLayout(); raimund@238: layout.setWidth("400px"); raimund@238: raimund@238: Label label = new Label(dataList.getLabel()); raimund@238: label.setWidth("200px"); raimund@238: raimund@238: Canvas back = getBackButton(dataList.getState()); raimund@238: raimund@242: Label selected = new Label(loc[0].getLabel()); raimund@238: selected.setWidth("130px"); raimund@238: raimund@238: layout.addMember(label); raimund@238: layout.addMember(selected); raimund@238: layout.addMember(back); raimund@238: raimund@238: return layout; raimund@238: } raimund@238: raimund@238: raimund@238: /** raimund@238: * This method reads the default values defined in the DataItems of the Data raimund@238: * objects in list. raimund@238: * raimund@238: * @param list The DataList container that stores the Data objects. raimund@238: */ raimund@238: protected void initDefaults(DataList list) { raimund@238: } raimund@238: raimund@238: raimund@238: /** raimund@238: * This method greps the Data with name name from the list and raimund@238: * returns it. raimund@238: * raimund@238: * @param items A list of Data. raimund@238: * @param name The name of the Data that we are searching for. raimund@238: * raimund@238: * @return the Data with the name name. raimund@238: */ raimund@238: protected Data getData(List data, String name) { raimund@238: for (Data d: data) { raimund@238: if (name.equals(d.getLabel())) { raimund@238: return d; raimund@238: } raimund@238: } raimund@238: raimund@238: return null; raimund@238: } raimund@238: raimund@238: raimund@238: protected Canvas createWidget(DataList data) { raimund@238: VLayout layout = new VLayout(); raimund@238: container = new HLayout(); raimund@238: raimund@238: // the initial view will display the location input mode raimund@238: locationPanel = new DoubleArrayPanel( raimund@238: MESSAGES.unitLocation(), raimund@238: getLocationValues(), raimund@238: new BlurHandler(){public void onBlur(BlurEvent be) {}}); raimund@238: container.addMember(locationPanel); raimund@238: raimund@238: layout.addMember(container); raimund@238: raimund@238: container.setMembersMargin(30); raimund@238: raimund@238: container.addMember(locationTable); raimund@238: createInputPanel(); raimund@238: return layout; raimund@238: } raimund@238: raimund@238: raimund@238: /** raimund@238: * This method returns the selected data. raimund@238: * raimund@238: * @return the selected/inserted data. raimund@238: */ raimund@238: public Data[] getData() { raimund@242: saveLocationValues(locationPanel); raimund@238: double[] values = getLocationValues(); raimund@238: Data[] data = new Data[values.length]; raimund@238: DataItem item = new DefaultDataItem(); raimund@238: for (int i = 0; i < values.length; i++) { raimund@238: item = new DefaultDataItem( raimund@238: "ld_locations", raimund@238: "ld_locations", raimund@238: Double.valueOf(values[i]).toString()); raimund@238: data[i] = new DefaultData( raimund@238: "ld_locations", raimund@238: null, raimund@238: null, raimund@238: new DataItem[] {item}); raimund@238: } raimund@238: return data; raimund@238: } raimund@238: raimund@238: raimund@238: raimund@238: raimund@238: /** raimund@238: * Validates and stores all values entered in the location mode. raimund@238: * raimund@238: * @param p The DoubleArrayPanel. raimund@238: */ raimund@238: protected void saveLocationValues(DoubleArrayPanel p) { raimund@238: FormItem[] formItems = p.getFields(); raimund@238: raimund@238: for (FormItem item: formItems) { raimund@238: if (item.getFieldName().equals(DoubleArrayPanel.FIELD_NAME)) { raimund@238: saveLocationValue(p, item); raimund@238: } raimund@238: } raimund@238: } raimund@238: raimund@238: raimund@238: /** raimund@238: * Validates and stores a value entered in the location mode. raimund@238: * raimund@238: * @param p The DoubleArrayPanel. raimund@238: * @param item The item that needs to be validated. raimund@238: */ raimund@238: protected void saveLocationValue(DoubleArrayPanel p, FormItem item) { raimund@238: if (p.validateForm(item)) { raimund@238: setLocationValues(p.getInputValues(item)); raimund@238: } raimund@238: } raimund@238: raimund@238: raimund@238: protected void createInputPanel() { raimund@238: Config config = Config.getInstance(); raimund@238: String url = config.getServerUrl(); raimund@238: String locale = config.getLocale (); raimund@238: String river = ""; raimund@238: raimund@238: ArtifactDescription adescr = artifact.getArtifactDescription(); raimund@238: DataList[] data = adescr.getOldData(); raimund@238: raimund@238: if (data != null && data.length > 0) { raimund@238: for (int i = 0; i < data.length; i++) { raimund@238: DataList dl = data[i]; raimund@238: if (dl.getState().equals("state.winfo.river")) { raimund@238: for (int j = 0; j < dl.size(); j++) { raimund@238: Data d = dl.get(j); raimund@238: DataItem[] di = d.getItems(); raimund@238: if (di != null && di.length == 1) { raimund@238: river = d.getItems()[0].getStringValue(); raimund@238: } raimund@238: } raimund@238: } raimund@238: } raimund@238: } raimund@238: raimund@238: distanceInfoService.getDistanceInfo(url, locale, river, raimund@238: new AsyncCallback() { raimund@238: public void onFailure(Throwable caught) { raimund@238: GWT.log("Could not recieve location informations."); raimund@238: GWT.log(caught.getMessage()); raimund@238: } raimund@238: raimund@238: public void onSuccess(DistanceInfoObject[] di) { raimund@238: int num = di != null ? di.length :0; raimund@238: GWT.log("Recieved " + num + " location informations."); raimund@238: raimund@238: if (num == 0) { raimund@238: return; raimund@238: } raimund@238: tableData = di; raimund@238: updateLocationInfo(di); raimund@238: } raimund@238: } raimund@238: ); raimund@238: } raimund@238: raimund@238: raimund@238: protected void updateLocationInfo(DistanceInfoObject[] di) { raimund@238: int i = 0; raimund@238: for (DistanceInfoObject dio: di) { raimund@242: if (dio.getTo() == null) { raimund@238: locationTable.addData(new DistanceInfoRecord(dio)); raimund@238: } raimund@238: } raimund@238: return; raimund@238: } raimund@238: raimund@238: raimund@238: protected double[] getLocationValues() { raimund@238: return values; raimund@238: } raimund@238: raimund@238: raimund@238: protected void setLocationValues(double[] values) { raimund@238: this.values = values; raimund@238: locationPanel.setValues(values); raimund@238: } raimund@238: } raimund@238: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :