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@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;
rrenkert@5928: import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
rrenkert@5928: import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
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;
rrenkert@5928: import java.util.List;
rrenkert@5928: import java.util.Map;
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.*/
rrenkert@6133: protected static 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.*/
tom@8856: protected ModuleServiceAsync moduleService = GWT.create(
tom@8856: ModuleService.class);
bjoern@3518:
andre@8528: private Map > modulesRiverMap;
rrenkert@6119: protected Map rivers;
ingo@30:
ingo@30: /**
ingo@30: * The default constructor.
ingo@30: */
ingo@30: public ModuleSelection() {
rrenkert@5928: rivers = null;
andre@8528: modulesRiverMap = new LinkedHashMap >();
rrenkert@5928:
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()");
rrenkert@6133: createCallback();
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:
andre@8528: private void checkRivers(String selected) {
andre@8528: if (selected == null) {
andre@8528: selected = getSelectedModule();
andre@8528: }
tom@8856: if (rivers != null
tom@8856: && !rivers.isEmpty()
tom@8856: && modules != null
tom@8856: && selected != null
tom@8856: ) {
andre@8528: List allowedRivers = modulesRiverMap.get(selected);
andre@8528: if ( allowedRivers == null ) {
andre@8528: GWT.log("No configured rivers for module: " + selected);
andre@8528: }
andre@8528: for (Map.Entry s: rivers.entrySet()) {
andre@8528: if ( allowedRivers == null ) {
andre@8528: s.getValue().hide();
andre@8528: continue;
andre@8528: }
andre@8528: if (!allowedRivers.contains(s.getKey())) {
andre@8528: s.getValue().hide();
andre@8528: } else {
andre@8528: s.getValue().show();
andre@8528: }
andre@8528: }
andre@8528: }
andre@8528: }
andre@8528:
bjoern@3518: private void setModules() {
tom@8856: LinkedHashMap values =
tom@8856: 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());
rrenkert@7763: }
rrenkert@7763: }
andre@8528: modulesRiverMap.put(module.getName(), module.getRivers());
bjoern@3518: }
bjoern@3518: }
bjoern@3518: if (radio != null) {
bjoern@3518: radio.setValueMap(values);
bjoern@3518: }
andre@8528: checkRivers(null);
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");
rrenkert@5928: radio.addChangeHandler(new ChangeHandler() {
rrenkert@5928: @Override
rrenkert@5928: public void onChange(ChangeEvent event) {
andre@8528: checkRivers((String)event.getValue());
rrenkert@5928: }
rrenkert@5928: });
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: }
rrenkert@5928:
rrenkert@6119: public void setRivers(Map rivers) {
rrenkert@5928: this.rivers = rivers;
rrenkert@5928: }
rrenkert@6133:
rrenkert@6133: private native void createCallback() /*-{
rrenkert@6133: $wnd.getModule = @org.dive4elements.river.client.client.ui.ModuleSelection::getSelectedModule();
rrenkert@6133: }-*/;
rrenkert@6133:
rrenkert@6133: private static String getSelectedModule() {
andre@8528: if (radio == null) {
andre@8528: return null;
andre@8528: }
rrenkert@6133: return radio.getValueAsString();
rrenkert@6133: }
ingo@30: }
ingo@30: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :