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

382 lines
12 KiB
Java
Raw Normal View History

package org.gcube.portlets.user.td.columnwidget.client.batch;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.user.td.columnwidget.client.custom.ActionButtonCell;
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.batch.Occurences;
import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.safecss.shared.SafeStylesBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.cell.core.client.ButtonCell.IconAlign;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.Style.SelectionMode;
import com.sencha.gxt.core.client.resources.ThemeStyles;
import com.sencha.gxt.core.client.util.Format;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.data.client.loader.RpcProxy;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.event.StoreDataChangeEvent;
import com.sencha.gxt.data.shared.event.StoreDataChangeEvent.StoreDataChangeHandler;
import com.sencha.gxt.data.shared.loader.ListLoadConfig;
import com.sencha.gxt.data.shared.loader.ListLoadResult;
import com.sencha.gxt.data.shared.loader.ListLoadResultBean;
import com.sencha.gxt.data.shared.loader.ListLoader;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
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.BoxLayoutContainer.BoxLayoutData;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer.HBoxLayoutAlign;
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.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.RowNumberer;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;
/**
*
* @author "Giancarlo Panichi" <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class BatchReplacePanel extends FramedPanel {
protected String WIDTH = "560px";
protected String HEIGHT = "520px";
protected EventBus eventBus;
protected TextField label = null;
protected TRId trId;
protected String columnName;
protected ColumnData column;
protected BatchReplaceDialog parent;
protected static final String DON_T_REPLACE = "Don't replace";
private TextButton btnApply;
private TextButton btnClose;
protected ListLoader<ListLoadConfig, ListLoadResult<ReplaceEntry>> loader;
protected Grid<ReplaceEntry> grid;
protected ListStore<ReplaceEntry> store;
protected HTML info;
public BatchReplacePanel(BatchReplaceDialog parent, TRId trId, String columnName, EventBus eventBus) {
this.parent=parent;
Log.debug("BatchReplacePanel:["+trId+", columnName:"+columnName+"]");
setWidth(WIDTH);
setHeight(HEIGHT);
setHeaderVisible(false);
setBodyBorder(false);
this.trId = trId;
this.columnName = columnName;
this.eventBus = eventBus;
Log.debug("Create BatchReplacePanel(): [" + trId.toString()
+ " , columnName:" + columnName + "]");
retrieveColumn();
}
protected void create() {
IdentityValueProvider<ReplaceEntry> identity = new IdentityValueProvider<ReplaceEntry>();
CheckBoxSelectionModel<ReplaceEntry> sm = new CheckBoxSelectionModel<ReplaceEntry>(
identity);
RowNumberer<ReplaceEntry> number = new RowNumberer<ReplaceEntry>(identity);
//number.setFixed(false);
//number.setResizable(false);
number.setWidth(50);
number.setColumnClassSuffix("");
number.setHeader("N.");
SafeStylesBuilder styleBuilder=new SafeStylesBuilder();
styleBuilder.width(50, Unit.PX);
number.setColumnStyle(styleBuilder.toSafeStyles());
ReplaceEntryProperties props = GWT.create(ReplaceEntryProperties.class);
ColumnConfig<ReplaceEntry, String> valueCol = new ColumnConfig<ReplaceEntry, String>(
props.value(), 130, "Values");
ColumnConfig<ReplaceEntry, Integer> numberCol = new ColumnConfig<ReplaceEntry, Integer>(
props.number(), 100,"Occurences");
ColumnConfig<ReplaceEntry, String> replacementValueCol = new ColumnConfig<ReplaceEntry, String>(
props.replacementValue(),130,"Replacement");
/*ColumnConfig<ReplaceEntry, String> replacementIdCol = new ColumnConfig<ReplaceEntry, String>(
props.replacementId());
*/
ColumnConfig<ReplaceEntry, String> changeColumn = new ColumnConfig<ReplaceEntry, String>(
props.value(),24);
ActionButtonCell button = new ActionButtonCell();
button.setIcon(ResourceBundle.INSTANCE.magnifier());
button.setTitle("Change");
button.addSelectHandler(new SelectHandler() {
@Override
public void onSelect(SelectEvent event) {
Context c = event.getContext();
int row = c.getIndex();
ReplaceEntry re = store.get(row);
Log.debug("ReplaceEntry " + re.toString() + " was clicked.");
}
});
changeColumn.setCell(button);
List<ColumnConfig<ReplaceEntry, ?>> l = new ArrayList<ColumnConfig<ReplaceEntry, ?>>();
l.add(number);
l.add(valueCol);
l.add(numberCol);
l.add(replacementValueCol);
//l.add(replacementIdCol);
l.add(changeColumn);
ColumnModel<ReplaceEntry> cm = new ColumnModel<ReplaceEntry>(l);
store = new ListStore<ReplaceEntry>(props.id());
store.addStoreDataChangeHandler(new StoreDataChangeHandler<ReplaceEntry>() {
@Override
public void onDataChange(StoreDataChangeEvent<ReplaceEntry> event) {
updateInfo();
}
});
RpcProxy<ListLoadConfig, ListLoadResult<ReplaceEntry>> proxy = new RpcProxy<ListLoadConfig, ListLoadResult<ReplaceEntry>>() {
public void load(ListLoadConfig loadConfig,
final AsyncCallback<ListLoadResult<ReplaceEntry>> callback) {
loadData(loadConfig, callback);
}
};
loader = new ListLoader<ListLoadConfig, ListLoadResult<ReplaceEntry>>(
proxy);
loader.setRemoteSort(false);
loader.addLoadHandler(new LoadResultListStoreBinding<ListLoadConfig, ReplaceEntry, ListLoadResult<ReplaceEntry>>(
store) {
});
grid = new Grid<ReplaceEntry>(store, cm) {
@Override
protected void onAfterFirstAttach() {
super.onAfterFirstAttach();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
public void execute() {
loader.load();
}
});
}
};
sm.setSelectionMode(SelectionMode.MULTI);
grid.setLoader(loader);
grid.setSelectionModel(sm);
number.initPlugin(grid);
// grid.getView().setAutoExpandColumn(labelCol);
// grid.setHeight(360);
grid.setHeight("418px");
grid.getView().setStripeRows(true);
grid.getView().setColumnLines(true);
grid.getView().setAutoFill(true);
grid.setBorders(false);
grid.setLoadMask(true);
grid.setColumnReordering(true);
grid.setColumnResize(true);
grid.getView().setAutoExpandColumn(valueCol);
info=new HTML("No info");
ToolBar toolBar = new ToolBar();
toolBar.add(info);
toolBar.addStyleName(ThemeStyles.getStyle().borderTop());
toolBar.getElement().getStyle().setProperty("borderBottom", "none");
btnApply = new TextButton("Save");
btnApply.setIcon(ResourceBundle.INSTANCE.save());
btnApply.setIconAlign(IconAlign.RIGHT);
btnApply.setTitle("Apply Filter");
btnApply.addSelectHandler(new SelectHandler() {
public void onSelect(SelectEvent event) {
Log.debug("Pressed Apply");
save();
}
});
btnClose = new TextButton("Close");
btnClose.setIcon(ResourceBundle.INSTANCE.close());
btnClose.setIconAlign(IconAlign.RIGHT);
btnClose.setTitle("Cancel filter");
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(grid, new VerticalLayoutData(-1, -1, new Margins(0)));
v.add(toolBar,new VerticalLayoutData(1, 25, new Margins(0)));
v.add(flowButton, new VerticalLayoutData(-1, 36, new Margins(
5, 2, 5, 2)));
add(v);
}
protected void retrieveColumn(){
TDGWTServiceAsync.INSTANCE.getColumn(trId, columnName,
new AsyncCallback<ColumnData>() {
public void onFailure(Throwable caught) {
Log.error("load column failure:"
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error retrieving column",
"Error retrieving column");
}
public void onSuccess(ColumnData result) {
Log.debug("Column: "+result);
column=result;
create();
}
});
}
protected void loadData(ListLoadConfig loadConfig,
final AsyncCallback<ListLoadResult<ReplaceEntry>> callback) {
TDGWTServiceAsync.INSTANCE.getOccurencesForBatchReplace(column,
new AsyncCallback<ArrayList<Occurences>>() {
public void onFailure(Throwable caught) {
Log.error("load columns failure:"
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error retrieving columns",
"Error retrieving columns");
callback.onFailure(caught);
}
public void onSuccess(ArrayList<Occurences> result) {
Log.trace("loaded " + result.size() + " Occurences");
callback.onSuccess(new ListLoadResultBean<ReplaceEntry>(
getRecord(result)));
}
});
}
protected void updateInfo() {
Log.trace("updating the info bar");
int total = store.size();
int totalErrors = 0;
int assignedErrors = 0;
int assigned = 0;
for (ReplaceEntry record : store.getAll()) {
int errors = record.getNumber();
totalErrors += errors;
if (record.getReplacementValue() != null && record.getReplacementValue().compareTo(DON_T_REPLACE)!=0) {
assigned++;
assignedErrors += errors;
}
}
String text = Format
.substitute("Assigned {0} ({1} occurences) of {2} ({3} occurences)",
String.valueOf(assigned),
String.valueOf(assignedErrors), String.valueOf(total),
String.valueOf(totalErrors));
info.setText(text);
Log.debug(text);
}
/*public ArrayList<ReplaceEntry> getReplaceEntries() {
ArrayList<ReplaceEntry> entries = new ArrayList<ReplaceEntry>();
for (ReplaceEntry record : store.getAll() {
if (record.getAsString(REPLACE_ID_FIELD) != null) {
ReplaceEntry entry = new ReplaceEntry(
record.getAsString(ERROR_VALUE_FIELD),
record.getAsString(REPLACE_VALUE_FIELD),
record.getAsString(REPLACE_ID_FIELD));
entries.add(entry);
}
}
return entries;
}
protected void updateEntries(ArrayList<Occurences> entries) {
store.clear();
ArrayList<ReplaceEntry> records = getRecord(entries);
store.addAll(records);
store.commitChanges();
}*/
protected ArrayList<ReplaceEntry> getRecord(ArrayList<Occurences> entries) {
ArrayList<ReplaceEntry> records = new ArrayList<ReplaceEntry>();
for (Occurences entry : entries)
records.add(getRecord(entry));
return records;
}
protected ReplaceEntry getRecord(Occurences entry) {
ReplaceEntry data = new ReplaceEntry(entry.getValue(), entry.getNumber(),
DON_T_REPLACE, null);
return data;
}
protected void save(){
}
protected void close(){
parent.close();
}
}