teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5861: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5861: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.client.ui; ingo@30: ingo@30: import com.google.gwt.core.client.GWT; bjoern@3518: import com.google.gwt.user.client.rpc.AsyncCallback; ingo@30: ingo@83: import com.smartgwt.client.types.VerticalAlignment; bjoern@3518: import com.smartgwt.client.util.SC; ingo@30: import com.smartgwt.client.widgets.Canvas; ingo@30: import com.smartgwt.client.widgets.Label; ingo@30: import com.smartgwt.client.widgets.form.DynamicForm; ingo@30: import com.smartgwt.client.widgets.form.fields.RadioGroupItem; ingo@30: import com.smartgwt.client.widgets.layout.HLayout; ingo@30: import com.smartgwt.client.widgets.layout.VLayout; ingo@30: teichmann@5835: import org.dive4elements.river.client.client.Config; teichmann@5835: import org.dive4elements.river.client.client.FLYSConstants; teichmann@5835: import org.dive4elements.river.client.client.services.ModuleService; teichmann@5835: import org.dive4elements.river.client.client.services.ModuleServiceAsync; 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; teichmann@5835: import org.dive4elements.river.client.shared.model.Module; ingo@30: christian@4131: import java.util.LinkedHashMap; ingo@30: ingo@30: /** ingo@30: * The ModuleSelection combines the river selection and the module selection in ingo@30: * one widget. It will display a vertical splitted widget - the upper part will ingo@30: * render checkboxes for each module, the lower one will display a combobox at ingo@30: * the left and a map panel on the right to choose the river. ingo@30: * ingo@30: * @author Ingo Weinzierl ingo@30: */ ingo@30: public class ModuleSelection extends MapSelection { ingo@30: christian@4131: private static final long serialVersionUID = -5634831815175543328L; christian@4131: ingo@30: /** The message class that provides i18n strings.*/ ingo@211: protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); ingo@30: ingo@30: /** The module checkboxes.*/ ingo@30: protected RadioGroupItem radio; ingo@30: bjoern@3518: /** */ bjoern@3518: protected Module[] modules; bjoern@3518: bjoern@3518: /** The ModuleService used to retrieve the available modules of a user.*/ bjoern@3518: protected ModuleServiceAsync moduleService = GWT.create(ModuleService.class); bjoern@3518: ingo@30: ingo@30: /** ingo@30: * The default constructor. ingo@30: */ ingo@30: public ModuleSelection() { bjoern@3518: readModules(); ingo@30: } ingo@30: ingo@30: ingo@30: /** ingo@30: * This method returns a widget that renders the checkboxes for each module ingo@30: * and the MapSelection that lets the user choose the river. ingo@30: * ingo@30: * @param data The provided rivers. ingo@30: * ingo@30: * @return the module selection combined with the river selection. ingo@30: */ christian@4131: @Override ingo@51: public Canvas create(DataList data) { bjoern@3518: GWT.log("ModuleSelection - create()"); ingo@30: VLayout newLayout = new VLayout(); ingo@30: newLayout.setMembersMargin(10); raimund@282: newLayout.setAlign(VerticalAlignment.TOP); ingo@30: Canvas moduleSelection = createWidget(); ingo@30: raimund@282: moduleSelection.setHeight(100); raimund@282: newLayout.setHeight(70); ingo@30: newLayout.addMember(moduleSelection); ingo@30: ingo@30: return newLayout; ingo@30: } ingo@30: bjoern@3518: private void readModules() { bjoern@3518: Config config = Config.getInstance(); bjoern@3518: String locale = config.getLocale(); bjoern@3518: bjoern@3518: moduleService.list(locale, new AsyncCallback() { bjoern@3518: @Override bjoern@3518: public void onFailure(Throwable caught) { bjoern@3518: GWT.log("Could not recieve a list of modules."); bjoern@3518: SC.warn(MSG.getString(caught.getMessage())); bjoern@3518: } bjoern@3518: bjoern@3518: @Override bjoern@3518: public void onSuccess(Module[] newmodules) { bjoern@3518: GWT.log("Retrieved " + newmodules.length + " modules."); bjoern@3518: modules = newmodules; bjoern@3518: setModules(); bjoern@3518: } bjoern@3518: }); bjoern@3518: } bjoern@3518: bjoern@3518: private void setModules() { christian@4131: LinkedHashMap values = new LinkedHashMap(); bjoern@3530: bjoern@3518: if (this.modules!= null) { bjoern@3518: for(Module module : this.modules) { bjoern@3518: values.put(module.getName(), module.getLocalizedName()); bjoern@3530: if (module.isSelected()) { bjoern@3530: GWT.log("Module " + module.getName() + " is selected."); bjoern@3530: if (radio != null) { bjoern@3530: radio.setDefaultValue(module.getName()); bjoern@3530: GWT.log("Setting " + module.getName() + " as selected."); bjoern@3530: } bjoern@3530: } bjoern@3518: } bjoern@3518: } bjoern@3518: if (radio != null) { bjoern@3518: radio.setValueMap(values); bjoern@3518: } bjoern@3518: } ingo@30: ingo@30: /** ingo@30: * Creates a widget that displays a checkbox for each module. ingo@30: * ingo@30: * @return a widget with checkboxes. ingo@30: */ ingo@30: protected Canvas createWidget() { ingo@30: HLayout layout = new HLayout(); ingo@30: ingo@30: Label label = new Label(MESSAGES.module_selection()); ingo@30: DynamicForm form = new DynamicForm(); ingo@30: ingo@30: radio = new RadioGroupItem("plugin"); ingo@30: ingo@30: label.setWidth(50); raimund@282: label.setHeight(25); ingo@30: ingo@45: ingo@30: radio.setShowTitle(false); ingo@86: radio.setVertical(true); ingo@45: bjoern@3518: setModules(); bjoern@3518: ingo@30: form.setFields(radio); ingo@30: ingo@30: layout.addMember(label); ingo@30: layout.addMember(form); ingo@30: ingo@30: return layout; ingo@30: } ingo@30: ingo@30: ingo@30: /** ingo@30: * This method prepares the data of two widgets - the module selection and ingo@30: * the river selection. The returning field will contain the Data that ingo@30: * represents the module selection at first position, the second position ingo@30: * stores the Data object that represents the river selection. ingo@30: * ingo@30: * @return the Data that was chosen in this widget. ingo@30: */ christian@4131: @Override ingo@30: protected Data[] getData() { ingo@30: ingo@30: String module = radio.getValueAsString(); ingo@30: ingo@30: DataItem[] items = new DefaultDataItem[1]; ingo@30: items[0] = new DefaultDataItem(module, module, module); ingo@30: ingo@51: Data data = new DefaultData("module", null, null, items); ingo@30: raimund@282: return new Data[] {data}; ingo@30: } ingo@30: } ingo@30: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :