tabular-data-column-widget/src/main/java/org/gcube/portlets/user/td/columnwidget/client/replace/ReplacePanel.java

201 lines
6.1 KiB
Java
Raw Normal View History

package org.gcube.portlets.user.td.columnwidget.client.replace;
import org.gcube.portlets.user.td.columnwidget.client.progress.ReplaceColumnProgressDialog;
import org.gcube.portlets.user.td.columnwidget.client.resources.ResourceBundle;
import org.gcube.portlets.user.td.columnwidget.client.utils.UtilsGXT3;
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData;
import org.gcube.portlets.user.td.gwtservice.shared.tr.column.ReplaceColumnSession;
import org.gcube.portlets.user.td.widgetcommonevent.shared.CellData;
import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.cell.core.client.ButtonCell.IconAlign;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer.HBoxLayoutAlign;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.TextField;
/**
*
* @author "Giancarlo Panichi" <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class ReplacePanel extends FramedPanel {
protected String WIDTH = "500px";
protected String HEIGHT = "150px";
protected EventBus eventBus;
protected TRId trId;
protected CellData cellData;
protected ReplaceDialog parent;
protected ColumnData column;
protected ReplaceColumnSession replaceColumnSession;
private TextField value;
private TextField replaceValue;
private TextButton btnApply;
private TextButton btnClose;
public ReplacePanel(ReplaceDialog parent, TRId trId, CellData cellData,
EventBus eventBus) {
this.parent = parent;
this.cellData = cellData;
this.trId = trId;
this.eventBus = eventBus;
Log.debug("ReplacePanel:[" + trId + ", CellData:" + cellData + "]");
initPanel();
retrieveColumn();
}
protected void initPanel() {
setWidth(WIDTH);
setHeight(HEIGHT);
setHeaderVisible(false);
setBodyBorder(false);
}
protected void retrieveColumn() {
TDGWTServiceAsync.INSTANCE.getColumn(trId, cellData.getColumnName(),
new AsyncCallback<ColumnData>() {
public void onFailure(Throwable caught) {
Log.error("Error retrieving column: "
+ caught.getMessage());
UtilsGXT3.alert("Error retrieving column",
caught.getMessage());
}
public void onSuccess(ColumnData result) {
Log.debug("Retrived column: " + result);
if (result.isViewColumn()) {
UtilsGXT3
.info("View Column",
"You can not replace value on view column for now");
close();
} else {
column = result;
create();
}
}
});
}
protected void create() {
value = new TextField();
value.setValue(cellData.getValue());
value.setReadOnly(true);
replaceValue = new TextField();
btnApply = new TextButton("Replace");
btnApply.setIcon(ResourceBundle.INSTANCE.replace());
btnApply.setIconAlign(IconAlign.RIGHT);
btnApply.setTitle("Replace Value");
btnApply.addSelectHandler(new SelectHandler() {
public void onSelect(SelectEvent event) {
Log.debug("Pressed Apply");
replaceValue();
}
});
btnClose = new TextButton("Close");
btnClose.setIcon(ResourceBundle.INSTANCE.close());
btnClose.setIconAlign(IconAlign.RIGHT);
btnClose.setTitle("Close");
btnClose.addSelectHandler(new SelectHandler() {
public void onSelect(SelectEvent event) {
Log.debug("Pressed Close");
close();
}
});
HBoxLayoutContainer flowButton = new HBoxLayoutContainer();
flowButton.setHBoxLayoutAlign(HBoxLayoutAlign.MIDDLE);
flowButton.setPack(BoxLayoutPack.CENTER);
flowButton.add(btnApply, new BoxLayoutData(new Margins(2, 4, 2, 4)));
flowButton.add(btnClose, new BoxLayoutData(new Margins(2, 4, 2, 4)));
VerticalLayoutContainer v = new VerticalLayoutContainer();
v.add(new FieldLabel(value, "Value"), new VerticalLayoutData(1, -1));
v.add(new FieldLabel(replaceValue, "Replace"), new VerticalLayoutData(
1, -1));
v.add(flowButton, new VerticalLayoutData(-1, 36,
new Margins(5, 2, 5, 2)));
add(v);
}
protected void replaceValue() {
String rValue = replaceValue.getCurrentValue();
if (rValue == null || rValue.isEmpty()) {
UtilsGXT3.alert("Attention", "Insert a valid replace value");
} else {
callReplaceValue(rValue);
}
}
protected void callReplaceValue(String rValue) {
replaceColumnSession = new ReplaceColumnSession(
value.getCurrentValue(), rValue, trId, column,
cellData.getRowId());
Log.debug(replaceColumnSession.toString());
TDGWTServiceAsync.INSTANCE.startReplaceColumn(replaceColumnSession,
new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
Log.debug("Submitted replace column value");
callReplaceColumnProgressDialog();
}
@Override
public void onFailure(Throwable caught) {
Log.error("Error submitting replace column value: "
+ caught.getMessage() + " " + caught.getCause());
caught.printStackTrace();
UtilsGXT3.alert(
"Error submitting replace column value",
caught.getMessage());
}
});
}
protected void callReplaceColumnProgressDialog() {
ReplaceColumnProgressDialog dialog = new ReplaceColumnProgressDialog(
parent, eventBus);
dialog.show();
}
protected void close() {
parent.close();
}
}