tabular-data-sdmx-export-wi.../src/main/java/org/gcube/portlets/user/sdmxexportwizardtd/client/SDMXTableDetailCard.java

318 lines
10 KiB
Java

/**
*
*/
package org.gcube.portlets.user.sdmxexportwizardtd.client;
import java.util.List;
import org.gcube.portlets.user.sdmxexportwizardtd.client.general.WizardCard;
import org.gcube.portlets.user.sdmxexportwizardtd.client.rpc.SDMXExportWizardServiceAsync;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.Agencies;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.SDMXExportSession;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.TableDetail;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
import com.sencha.gxt.core.client.util.ToggleGroup;
import com.sencha.gxt.data.client.loader.RpcProxy;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoadResultBean;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.FieldSet;
import com.sencha.gxt.widget.core.client.form.Radio;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.info.Info;
/**
*
* @author "Giancarlo Panichi"
* <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class SDMXTableDetailCard extends WizardCard {
protected SDMXExportSession importSession;
protected SDMXTableDetailCard thisCard;
protected static final AgenciesProperties agenciesProperties = GWT.create(AgenciesProperties.class);
protected VerticalLayoutContainer p = new VerticalLayoutContainer();
//private final ComboBox<String> combo2;
protected VerticalPanel tableDetailPanel;
//private static TextField agencyName=null;
protected TextField name;
protected TextArea description;
protected TextField right;
protected ComboBox<Agencies> combo=null;
TableDetail detail=new TableDetail();
public SDMXTableDetailCard(final SDMXExportSession importSession) {
super("SDMX Table Detail", "");
this.importSession = importSession;
thisCard=this;
tableDetailPanel = new VerticalPanel();
tableDetailPanel.setSpacing(4);
tableDetailPanel.setWidth("100%");
tableDetailPanel.setHeight("100%");
FramedPanel form2 = new FramedPanel();
form2.setHeadingText("Details");
//form2.setWidth(350);
FieldSet fieldSet = new FieldSet();
fieldSet.setHeadingText("Information");
fieldSet.setCollapsible(true);
form2.add(fieldSet);
//VerticalLayoutContainer p = new VerticalLayoutContainer();
fieldSet.add(p);
name = new TextField();
name.setAllowBlank(false);
name.setEmptyText("Enter a name...");
name.setValue(importSession.getSelectedCodelist().getName());
p.add(new FieldLabel(name, "Name"), new VerticalLayoutData(1, -1));
description = new TextArea();
description.setAllowBlank(false);
description.setEmptyText("Enter a description...");
description.setValue(importSession.getSelectedCodelist().getDescription());
p.add(new FieldLabel(description, "Description"), new VerticalLayoutData(1, -1));
right = new TextField();
right.setEmptyText("Enter right...");
right.setAllowBlank(false);
p.add(new FieldLabel(right, "Right"), new VerticalLayoutData(1, -1));
ListStore<Agencies> agenciesStore = new ListStore<Agencies>(agenciesProperties.key());
RpcProxy<PagingLoadConfig, PagingLoadResult<Agencies>> proxy = new RpcProxy<PagingLoadConfig, PagingLoadResult<Agencies>>() {
public void load(PagingLoadConfig loadConfig, final AsyncCallback<PagingLoadResult<Agencies>> callback) {
loadData(loadConfig, callback);
}
};
final PagingLoader<PagingLoadConfig, PagingLoadResult<Agencies>> loader = new PagingLoader<PagingLoadConfig, PagingLoadResult<Agencies>>(proxy);
loader.setRemoteSort(false);
loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig, Agencies, PagingLoadResult<Agencies>>(agenciesStore));
//agenciesStore.addAll(TestData.getStates());
combo = new ComboBox<Agencies>(agenciesStore, agenciesProperties.nameLabel());
addHandlersForEventObservation(combo, agenciesProperties.nameLabel());
combo.setLoader(loader);
combo.setEmptyText("Enter Agency...");
combo.setTriggerAction(TriggerAction.ALL);
combo.setAllowBlank(false);
combo.setForceSelection(true);
combo.setVisible(true);
//HorizontalLayoutContainer agenciesSelect= selectAgencies();
p.add(new FieldLabel(combo, "Agencies"), new VerticalLayoutData(1, -1));
/*FieldLabel comboLabel=new FieldLabel(combo, "");
comboLabel.setLabelSeparator("");
p.add(comboLabel, new VerticalLayoutData(1, -1));
agencyName = new TextField();
agencyName.setVisible(false);
agencyName.setEmptyText("Enter Agency...");
FieldLabel agencyNameLabel=new FieldLabel(agencyName, "");
agencyNameLabel.setLabelSeparator("");
p.add(agencyNameLabel, new VerticalLayoutData(1, -1));
*/
tableDetailPanel.add(form2);
setContent(tableDetailPanel);
}
protected void loadData(PagingLoadConfig loadConfig, final AsyncCallback<PagingLoadResult<Agencies>> callback) {
SDMXExportWizardServiceAsync.INSTANCE.getAgencies(new AsyncCallback<List<Agencies>>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(List<Agencies> result) {
Log.trace("loaded "+result.size()+" agencies");
for(int i=0; i<result.size(); i++){
Agencies ag=result.get(i);
//if(ag.getId().compareTo())
}
callback.onSuccess(new PagingLoadResultBean<Agencies>(result, result.size(), 0));
}
});
}
/**
* Helper to add handlers to observe events that occur on each combobox
*/
private <T> void addHandlersForEventObservation(final ComboBox<T> combo, final LabelProvider<T> labelProvider) {
combo.addValueChangeHandler(new ValueChangeHandler<T>() {
@Override
public void onValueChange(ValueChangeEvent<T> event) {
Info.display("Value Changed", "New value: "
+ (event.getValue() == null ? combo.getValue() : labelProvider.getLabel(event.getValue()) + "!"));
}
});
combo.addSelectionHandler(new SelectionHandler<T>() {
@Override
public void onSelection(SelectionEvent<T> event) {
Info.display("Agency Selected", "You selected "
+ (event.getSelectedItem() == null ? combo.getValue() : labelProvider.getLabel(event.getSelectedItem()) + "!"));
}
});
}
@Override
public void setup(){
Command sayNextCard = new Command() {
@Override
public void execute() {
detail.setName(name.getCurrentValue());
detail.setAgency(combo.getSelectedText());
detail.setDescription(description.getCurrentValue());
detail.setRight(right.getCurrentValue());
importSession.setTableDetail(detail);
SDMXOperationInProgressCard sdmxOperationInProgressCard = new SDMXOperationInProgressCard(
importSession);
getWizardWindow()
.addCard(sdmxOperationInProgressCard);
Log.info("NextCard SDMXOperationInProgressCard");
getWizardWindow().nextCard();
}
};
getWizardWindow().setNextButtonCommand(sayNextCard);
Command sayPreviousCard = new Command() {
public void execute() {
try {
getWizardWindow().previousCard();
getWizardWindow().removeCard(thisCard);
Log.info("Remove SDMXTableDetailCard");
} catch (Exception e) {
Log.error("sayPreviousCard :" + e.getLocalizedMessage());
}
}
};
getWizardWindow().setPreviousButtonCommand(sayPreviousCard);
getWizardWindow().setEnableNextButton(true);
}
private HorizontalLayoutContainer selectAgencies(){
HorizontalLayoutContainer documentSelectionPanel = new HorizontalLayoutContainer();
Radio radioAgenciesAlready = new Radio();
radioAgenciesAlready.setBoxLabel("Present");
radioAgenciesAlready.setValue(true);
radioAgenciesAlready.setName("Agencies");
Radio radioAgencyNew = new Radio();
radioAgencyNew.setBoxLabel("New");
radioAgencyNew.setName("AgencyNew");
documentSelectionPanel.add(radioAgenciesAlready);
documentSelectionPanel.add(radioAgencyNew);
// we can set name on radios or use toggle group
ToggleGroup toggle = new ToggleGroup();
toggle.add(radioAgenciesAlready);
toggle.add(radioAgencyNew);
toggle.addValueChangeHandler(new ValueChangeHandler<HasValue<Boolean>>() {
@Override
public void onValueChange(ValueChangeEvent<HasValue<Boolean>> event) {
try {
ToggleGroup group = (ToggleGroup) event.getSource();
Radio radio = (Radio) group.getValue();
Log.info("Document Selected:" + radio.getName());
if (radio.getName().compareTo("AgencyNew") == 0) {
/*agencyName.setVisible(true);
combo.setVisible(false);
*/
} else {
if (radio.getName().compareTo("Agency") == 0) {
/*agencyName.setVisible(false);
combo.setVisible(true);
*/
} else {
}
}
} catch (Exception e) {
Log.error("ToggleGroup: onValueChange " + e.getLocalizedMessage());
}
}
});
return documentSelectionPanel;
}
}