tabular-data-resources-widget/src/main/java/org/gcube/portlets/user/td/resourceswidget/client/ResourcesPanel.java

401 lines
12 KiB
Java

package org.gcube.portlets.user.td.resourceswidget.client;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTIsLockedException;
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTSessionExpiredException;
import org.gcube.portlets.user.td.gwtservice.shared.history.RollBackSession;
import org.gcube.portlets.user.td.gwtservice.shared.tr.TableData;
import org.gcube.portlets.user.td.gwtservice.shared.tr.resources.InternalURITD;
import org.gcube.portlets.user.td.gwtservice.shared.tr.resources.ResourceTD;
import org.gcube.portlets.user.td.gwtservice.shared.tr.resources.ResourceTDDescriptor;
import org.gcube.portlets.user.td.gwtservice.shared.tr.resources.ResourceTDType;
import org.gcube.portlets.user.td.gwtservice.shared.tr.resources.StringResourceTD;
import org.gcube.portlets.user.td.gwtservice.shared.tr.resources.TableResourceTD;
import org.gcube.portlets.user.td.resourceswidget.client.properties.ResourceTDDescriptorProperties;
import org.gcube.portlets.user.td.resourceswidget.client.utils.UtilsGXT3;
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.widgetcommonevent.shared.TRId;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.cell.client.AbstractCell;
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.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.data.client.loader.RpcProxy;
import com.sencha.gxt.data.shared.ListStore;
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.container.MarginData;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
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.RowExpander;
/**
*
* ResourcesPanel shows the resources
*
*
* @author "Giancarlo Panichi" <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class ResourcesPanel extends FramedPanel {
protected String WIDTH = "298px";
protected String HEIGHT = "520px";
protected ResourcesDialog parent;
protected TRId trId;
protected TableData table;
protected String headingTitle;
protected VerticalLayoutContainer vl;
protected EventBus eventBus;
protected ListStore<ResourceTDDescriptor> store;
protected ListLoader<ListLoadConfig, ListLoadResult<ResourceTDDescriptor>> loader;
protected Grid<ResourceTDDescriptor> grid;
protected ResourceTDDescriptor currentTDDescriptor;
protected int currentRowIndex;
protected RollBackSession rollBackSession;
private boolean drawed = false;
public ResourcesPanel(ResourcesDialog parent, TRId trId, EventBus eventBus) {
super();
this.parent = parent;
this.trId = trId;
this.eventBus = eventBus;
forceLayoutOnResize = true;
draw();
}
public ResourcesPanel(TRId trId, EventBus eventBus) {
super();
this.trId = trId;
this.eventBus = eventBus;
forceLayoutOnResize = true;
draw();
}
protected void draw() {
drawed = true;
init();
create();
}
protected void init() {
setWidth(WIDTH);
setHeight(HEIGHT);
setHeaderVisible(false);
setBodyBorder(false);
setResize(true);
}
protected void create() {
ResourceTDDescriptorProperties props = GWT.create(ResourceTDDescriptorProperties.class);
IdentityValueProvider<ResourceTDDescriptor> identityProvider = new IdentityValueProvider<ResourceTDDescriptor>();
RowExpander<ResourceTDDescriptor> expander = new RowExpander<ResourceTDDescriptor>(
identityProvider, new AbstractCell<ResourceTDDescriptor>() {
@Override
public void render(Context context, ResourceTDDescriptor value,
SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>Name:</b>"
+ value.getName() + "</p>");
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>Description:</b> "
+ value.getDescription());
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>Creation Date:</b>"
+ value.getCreationDate() + "</p>");
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>Creator Id:</b>"
+ value.getCreatorId() + "</p>");
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>Type:</b>"
+ value.getResourceType().toString() + "</p>");
ResourceTD resource=value.getResourceTD();
if(resource instanceof InternalURITD){
InternalURITD internalURITD=(InternalURITD)resource;
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>File id:</b>"
+ internalURITD.getFileId() + "</p>");
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>URI:</b>"
+ internalURITD.getUri() + "</p>");
} else {
if(resource instanceof StringResourceTD){
StringResourceTD stringResourceTD=(StringResourceTD)resource;
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>Value:</b>"
+ stringResourceTD.getStringValue() + "</p>");
} else {
if(resource instanceof TableResourceTD){
TableResourceTD tableResourceTD=(TableResourceTD)resource;
sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'><b>Table Id:</b>"
+ tableResourceTD.getTableId() + "</p>");
} else {
}
}
}
}
});
ColumnConfig<ResourceTDDescriptor, String> nameCol = new ColumnConfig<ResourceTDDescriptor, String>(
props.name(), 132, "Name");
nameCol.setCell(new AbstractCell<String>() {
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<span title='" + value + "'>" + value
+ "</span>");
}
});
ColumnConfig<ResourceTDDescriptor,ResourceTDType> typeColumn = new ColumnConfig<ResourceTDDescriptor, ResourceTDType>(
props.resourceType(), 40, "Type");
/*
ActionButtonCellNoFirst button = new ActionButtonCellNoFirst();
button.setIcon(ResourceBundle.INSTANCE.undo());
button.setTitle("Undo");
button.addSelectHandler(new SelectHandler() {
@Override
public void onSelect(SelectEvent event) {
Log.debug("Button Undo Pressed");
Context c = event.getContext();
int rowIndex = c.getIndex();
startSearchRollBackId(rowIndex);
}
});
typeColumn.setCell(button);
*/
List<ColumnConfig<ResourceTDDescriptor, ?>> l = new ArrayList<ColumnConfig<ResourceTDDescriptor, ?>>();
l.add(expander);
l.add(nameCol);
l.add(typeColumn);
ColumnModel<ResourceTDDescriptor> cm = new ColumnModel<ResourceTDDescriptor>(l);
store = new ListStore<ResourceTDDescriptor>(props.id());
RpcProxy<ListLoadConfig, ListLoadResult<ResourceTDDescriptor>> proxy = new RpcProxy<ListLoadConfig, ListLoadResult<ResourceTDDescriptor>>() {
public void load(ListLoadConfig loadConfig,
final AsyncCallback<ListLoadResult<ResourceTDDescriptor>> callback) {
loadData(loadConfig, callback);
}
};
loader = new ListLoader<ListLoadConfig, ListLoadResult<ResourceTDDescriptor>>(
proxy);
loader.setRemoteSort(false);
loader.addLoadHandler(new LoadResultListStoreBinding<ListLoadConfig, ResourceTDDescriptor, ListLoadResult<ResourceTDDescriptor>>(
store) {
});
grid = new Grid<ResourceTDDescriptor>(store, cm) {
@Override
protected void onAfterFirstAttach() {
super.onAfterFirstAttach();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
public void execute() {
loader.load();
}
});
}
};
grid.setLoader(loader);
grid.setSize("200px", "300px");
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(nameCol);
grid.getView().setEmptyText("Empty");
expander.initPlugin(grid);
/*
* VerticalLayoutContainer v = new VerticalLayoutContainer();
* v.setScrollMode(ScrollMode.AUTO); v.add(grid, new
* VerticalLayoutData(1, 1, new Margins(0))); v.forceLayout();
*/
add(grid, new MarginData(0));
onResize();
}
protected void startSearchRollBackId(int rowIndex) {
currentRowIndex = rowIndex;
currentTDDescriptor = store.get(rowIndex - 1);
Log.debug(currentTDDescriptor.toString() + " was clicked.[rowIndex="
+ currentRowIndex + " ]");
callRollBack();
}
protected void callRollBack() {
/*
rollBackSession = new RollBackSession(trId,
currentTDDescriptor.getHistoryId());
TDGWTServiceAsync.INSTANCE.startRollBack(rollBackSession,
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
if (caught instanceof TDGWTIsLockedException) {
Log.error(caught.getLocalizedMessage());
UtilsGXT3.alert("Error Locked",
caught.getLocalizedMessage());
} else {
if (caught instanceof TDGWTIsFinalException) {
Log.error(caught.getLocalizedMessage());
UtilsGXT3.alert("Error Final",
caught.getLocalizedMessage());
} else {
Log.error("Error in rollback: "
+ caught.getLocalizedMessage());
UtilsGXT3
.alert("Error in rollback",
"Error: "
+ caught.getLocalizedMessage());
}
}
}
}
public void onSuccess(String taskId) {
Log.debug("Rollback started");
//openMonitorDialog(taskId);
}
});
*/
}
protected void loadData(ListLoadConfig loadConfig,
final AsyncCallback<ListLoadResult<ResourceTDDescriptor>> callback) {
TDGWTServiceAsync.INSTANCE.getResourcesTD(trId,
new AsyncCallback<ArrayList<ResourceTDDescriptor>>() {
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
if (caught instanceof TDGWTIsLockedException) {
Log.error(caught.getLocalizedMessage());
UtilsGXT3.alert("Error Locked",
caught.getLocalizedMessage());
} else {
Log.error("Error Retrieving Resources: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error retrieving resources",
"Error retrieving resources");
}
}
callback.onFailure(caught);
}
public void onSuccess(ArrayList<ResourceTDDescriptor> result) {
Log.debug("loaded " + result.size());
callback.onSuccess(new ListLoadResultBean<ResourceTDDescriptor>(
result));
}
});
}
public void update() {
retrieveCurrentTR();
loader.load();
forceLayout();
}
protected void retrieveCurrentTR() {
TDGWTServiceAsync.INSTANCE.getCurrentTRId(new AsyncCallback<TRId>() {
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
if (caught instanceof TDGWTIsLockedException) {
Log.error(caught.getLocalizedMessage());
UtilsGXT3.alert("Error Locked",
caught.getLocalizedMessage());
} else {
Log.error("Error retrieving current TRId: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
"Error retrieving current tabular resource id");
}
}
}
public void onSuccess(TRId result) {
Log.debug("retrieved " + result);
trId = result;
if (!drawed) {
draw();
}
}
});
}
protected void close() {
if (parent != null) {
parent.close();
}
}
}