335 lines
10 KiB
Java
335 lines
10 KiB
Java
package org.gcube.portlets.user.td.csvexportwidget.client;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import org.gcube.portlets.user.td.csvexportwidget.client.grid.ColumnDataGridPanel;
|
|
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
|
|
import org.gcube.portlets.user.td.gwtservice.shared.csv.AvailableCharsetList;
|
|
import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVExportSession;
|
|
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTSessionExpiredException;
|
|
import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData;
|
|
import org.gcube.portlets.user.td.widgetcommonevent.client.event.SessionExpiredEvent;
|
|
import org.gcube.portlets.user.td.widgetcommonevent.client.type.SessionExpiredType;
|
|
import org.gcube.portlets.user.td.wizardwidget.client.WizardCard;
|
|
|
|
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.HorizontalPanel;
|
|
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
|
|
import com.sencha.gxt.core.client.util.Padding;
|
|
import com.sencha.gxt.core.client.util.ToggleGroup;
|
|
import com.sencha.gxt.data.shared.StringLabelProvider;
|
|
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
|
|
import com.sencha.gxt.widget.core.client.container.MarginData;
|
|
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
|
|
import com.sencha.gxt.widget.core.client.event.HideEvent;
|
|
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
|
|
import com.sencha.gxt.widget.core.client.form.FieldLabel;
|
|
import com.sencha.gxt.widget.core.client.form.FormPanel;
|
|
import com.sencha.gxt.widget.core.client.form.Radio;
|
|
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;
|
|
import com.sencha.gxt.widget.core.client.form.TextField;
|
|
import com.sencha.gxt.widget.core.client.tips.ToolTip;
|
|
import com.sencha.gxt.widget.core.client.tips.ToolTipConfig;
|
|
|
|
/**
|
|
*
|
|
* @author "Giancarlo Panichi" <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class CSVExportConfigCard extends WizardCard {
|
|
|
|
private static final int LABEL_WIDTH = 128;
|
|
private static final int LABEL_PAD_WIDTH = 2;
|
|
private static final String DEFAULT_DELIMETER = ",";
|
|
protected CSVExportSession exportSession;
|
|
|
|
private SimpleComboBox<String> encodings;
|
|
private TextField customDelimiterField;
|
|
private Radio otherDelimiter;
|
|
private Radio commaDelimiter;
|
|
private Radio spaceDelimiter;
|
|
private Radio tabDelimiter;
|
|
private Radio semicoloDelimiter;
|
|
|
|
private Radio viewColumnExportTrue;
|
|
private Radio viewColumnExportFalse;
|
|
|
|
protected ColumnDataGridPanel csvColumnGridPanel;
|
|
|
|
public CSVExportConfigCard(final CSVExportSession exportSession) {
|
|
super("CSV configuration", "");
|
|
|
|
if (exportSession == null) {
|
|
Log.error("CSVExportSession is null");
|
|
}
|
|
this.exportSession = exportSession;
|
|
|
|
FormPanel panel = createPanel();
|
|
setCenterWidget(panel, new MarginData(0));
|
|
|
|
}
|
|
|
|
protected FormPanel createPanel() {
|
|
FormPanel panel = new FormPanel();
|
|
panel.setLabelWidth(90);
|
|
panel.getElement().setPadding(new Padding(5));
|
|
|
|
VerticalLayoutContainer content = new VerticalLayoutContainer();
|
|
panel.add(content);
|
|
|
|
encodings = new SimpleComboBox<String>(
|
|
new StringLabelProvider<String>());
|
|
encodings.setToolTip("The CSV file encoding");
|
|
encodings.setTabIndex(0);
|
|
encodings.setEditable(false);
|
|
encodings.setForceSelection(true);
|
|
encodings.setTriggerAction(TriggerAction.ALL);
|
|
encodings.addSelectionHandler(new SelectionHandler<String>() {
|
|
|
|
public void onSelection(SelectionEvent<String> event) {
|
|
|
|
}
|
|
});
|
|
|
|
encodings.focus();
|
|
|
|
TDGWTServiceAsync.INSTANCE
|
|
.getAvailableCharset(new AsyncCallback<AvailableCharsetList>() {
|
|
|
|
public void onSuccess(AvailableCharsetList result) {
|
|
GWT.log("CharsetInfo: "
|
|
+ result.getCharsetList().size()
|
|
+ " charset, default: "
|
|
+ result.getDefaultCharset());
|
|
|
|
for (String charset : result.getCharsetList())
|
|
encodings.add(charset);
|
|
|
|
encodings.setValue(result.getDefaultCharset());
|
|
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public void onFailure(Throwable caught) {
|
|
if (caught instanceof TDGWTSessionExpiredException) {
|
|
getEventBus()
|
|
.fireEvent(
|
|
new SessionExpiredEvent(
|
|
SessionExpiredType.EXPIREDONSERVER));
|
|
} else {
|
|
|
|
Log.error("Error loading charset list", caught);
|
|
showErrorAndHide("Error loading charset list",
|
|
"Error loading charset list:",caught.getLocalizedMessage() , caught);
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
FieldLabel fieldEncoding=new FieldLabel(encodings, "File encoding");
|
|
fieldEncoding.setLabelWidth(LABEL_WIDTH);
|
|
fieldEncoding.setLabelPad(LABEL_PAD_WIDTH);
|
|
content.add(fieldEncoding);
|
|
|
|
// Delimiter
|
|
commaDelimiter = new Radio();
|
|
commaDelimiter.setBoxLabel("Comma");
|
|
commaDelimiter.setValue(true);
|
|
|
|
spaceDelimiter = new Radio();
|
|
spaceDelimiter.setBoxLabel("Space");
|
|
|
|
tabDelimiter = new Radio();
|
|
tabDelimiter.setBoxLabel("Tab");
|
|
|
|
semicoloDelimiter = new Radio();
|
|
semicoloDelimiter.setBoxLabel("Semicolon");
|
|
|
|
otherDelimiter = new Radio();
|
|
otherDelimiter.setBoxLabel("Other delimiter");
|
|
|
|
customDelimiterField = new TextField();
|
|
customDelimiterField.setEnabled(false);
|
|
customDelimiterField.setValue(DEFAULT_DELIMETER);
|
|
customDelimiterField.setAllowBlank(false);
|
|
customDelimiterField.setWidth(20);
|
|
|
|
ToggleGroup delimitersGroup = new ToggleGroup();
|
|
delimitersGroup.add(commaDelimiter);
|
|
delimitersGroup.add(spaceDelimiter);
|
|
delimitersGroup.add(tabDelimiter);
|
|
delimitersGroup.add(semicoloDelimiter);
|
|
delimitersGroup.add(otherDelimiter);
|
|
|
|
delimitersGroup
|
|
.addValueChangeHandler(new ValueChangeHandler<HasValue<Boolean>>() {
|
|
|
|
public void onValueChange(
|
|
ValueChangeEvent<HasValue<Boolean>> event) {
|
|
|
|
customDelimiterField.setEnabled(otherDelimiter
|
|
.getValue());
|
|
|
|
if (!otherDelimiter.getValue())
|
|
customDelimiterField.clearInvalid();
|
|
else
|
|
customDelimiterField.validate();
|
|
|
|
if (otherDelimiter.getValue()
|
|
&& !customDelimiterField.isValid())
|
|
return;
|
|
}
|
|
});
|
|
|
|
HorizontalPanel delimitersPanel = new HorizontalPanel();
|
|
delimitersPanel.add(commaDelimiter);
|
|
delimitersPanel.add(spaceDelimiter);
|
|
delimitersPanel.add(tabDelimiter);
|
|
delimitersPanel.add(semicoloDelimiter);
|
|
delimitersPanel.add(otherDelimiter);
|
|
delimitersPanel.add(customDelimiterField);
|
|
|
|
new ToolTip(delimitersPanel, new ToolTipConfig(
|
|
"The delimiter use to delimit the CSV fields"));
|
|
FieldLabel fieldDelimeter=new FieldLabel(delimitersPanel, "Delimiter");
|
|
fieldDelimeter.setLabelWidth(LABEL_WIDTH);
|
|
fieldDelimeter.setLabelPad(LABEL_PAD_WIDTH);
|
|
content.add(fieldDelimeter);
|
|
|
|
// Export View Column
|
|
viewColumnExportTrue = new Radio();
|
|
viewColumnExportTrue.setBoxLabel("True");
|
|
viewColumnExportTrue.setValue(true);
|
|
|
|
viewColumnExportFalse = new Radio();
|
|
viewColumnExportFalse.setBoxLabel("False");
|
|
|
|
ToggleGroup exportViewColumnGroup = new ToggleGroup();
|
|
exportViewColumnGroup.add(viewColumnExportTrue);
|
|
exportViewColumnGroup.add(viewColumnExportFalse);
|
|
|
|
HorizontalPanel viewColumnExportPanel = new HorizontalPanel();
|
|
viewColumnExportPanel.add(viewColumnExportTrue);
|
|
viewColumnExportPanel.add(viewColumnExportFalse);
|
|
|
|
new ToolTip(viewColumnExportPanel, new ToolTipConfig(
|
|
"Export View Columns Too"));
|
|
FieldLabel fieldViewColumns=new FieldLabel(viewColumnExportPanel, "Export View Columns");
|
|
fieldViewColumns.setLabelWidth(LABEL_WIDTH);
|
|
fieldViewColumns.setLabelPad(LABEL_PAD_WIDTH);
|
|
content.add(fieldViewColumns);
|
|
|
|
// Column Selection Grid
|
|
csvColumnGridPanel = new ColumnDataGridPanel(this);
|
|
|
|
csvColumnGridPanel
|
|
.addSelectionHandler(new SelectionHandler<ColumnData>() {
|
|
|
|
public void onSelection(SelectionEvent<ColumnData> event) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
content.add(csvColumnGridPanel);
|
|
|
|
return panel;
|
|
}
|
|
|
|
protected char getSelectedDelimiter() {
|
|
if (otherDelimiter.getValue())
|
|
return customDelimiterField.getValue().charAt(0);
|
|
if (commaDelimiter.getValue())
|
|
return ',';
|
|
if (spaceDelimiter.getValue())
|
|
return ' ';
|
|
if (tabDelimiter.getValue())
|
|
return '\t';
|
|
if (semicoloDelimiter.getValue())
|
|
return ';';
|
|
return DEFAULT_DELIMETER.charAt(0);
|
|
}
|
|
|
|
protected boolean getExportViewColumns() {
|
|
if(viewColumnExportTrue.getValue()){
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setup() {
|
|
Log.debug("CSVExportConfigCard Setup");
|
|
Command sayNextCard = new Command() {
|
|
|
|
public void execute() {
|
|
Log.debug("CSVExportConfigCard Call sayNextCard");
|
|
checkData();
|
|
}
|
|
|
|
};
|
|
|
|
getWizardWindow().setNextButtonCommand(sayNextCard);
|
|
setEnableBackButton(false);
|
|
setEnableNextButton(true);
|
|
}
|
|
|
|
protected void checkData() {
|
|
getWizardWindow().setEnableNextButton(false);
|
|
getWizardWindow().setEnableBackButton(false);
|
|
AlertMessageBox d;
|
|
HideHandler hideHandler = new HideHandler() {
|
|
|
|
public void onHide(HideEvent event) {
|
|
getWizardWindow().setEnableNextButton(true);
|
|
getWizardWindow().setEnableBackButton(false);
|
|
|
|
}
|
|
};
|
|
|
|
ArrayList<ColumnData> columns = csvColumnGridPanel.getSelectedItems();
|
|
if (columns.size() == 0) {
|
|
d = new AlertMessageBox("Attention", "No columns selected");
|
|
d.addHideHandler(hideHandler);
|
|
d.setModal(false);
|
|
d.show();
|
|
} else {
|
|
exportSession.setColumns(columns);
|
|
exportSession.setEncoding(encodings.getCurrentValue());
|
|
exportSession.setSeparator(String.valueOf(getSelectedDelimiter()));
|
|
exportSession.setExportViewColumns(getExportViewColumns());
|
|
goNext();
|
|
}
|
|
|
|
}
|
|
|
|
protected void goNext() {
|
|
try {
|
|
DestinationSelectionCard destCard = new DestinationSelectionCard(
|
|
exportSession);
|
|
getWizardWindow().addCard(destCard);
|
|
getWizardWindow().nextCard();
|
|
} catch (Exception e) {
|
|
Log.error("sayNextCard :" + e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void dispose() {
|
|
|
|
}
|
|
|
|
}
|