tabular-data-information-wi.../src/main/java/org/gcube/portlets/user/td/informationwidget/client/TabularResourceProperties.java

490 lines
15 KiB
Java

package org.gcube.portlets.user.td.informationwidget.client;
import java.util.ArrayList;
import java.util.HashMap;
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTSessionExpiredException;
import org.gcube.portlets.user.td.gwtservice.shared.share.Contacts;
import org.gcube.portlets.user.td.gwtservice.shared.share.ShareInfo;
import org.gcube.portlets.user.td.gwtservice.shared.tr.TabResource;
import org.gcube.portlets.user.td.gwtservice.shared.tr.TableData;
import org.gcube.portlets.user.td.informationwidget.client.custom.IconButton;
import org.gcube.portlets.user.td.informationwidget.client.resources.ResourceBundle;
import org.gcube.portlets.user.td.informationwidget.client.util.InfoMessageBox;
import org.gcube.portlets.user.td.metadatawidget.client.TRMetadataAccordionPanel;
import org.gcube.portlets.user.td.metadatawidget.client.TableMetadataAccordionPanel;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.RibbonEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.SessionExpiredEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.WidgetRequestEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.RibbonType;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.SessionExpiredType;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.WidgetRequestType;
import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.cell.core.client.ButtonCell.IconAlign;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
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.HBoxLayoutContainer;
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.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
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.CheckBox;
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.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
public class TabularResourceProperties extends FramedPanel {
protected String headingTitle;
protected HashMap<String, String> tabularResourcePropertiesMap;
protected VerticalLayoutContainer vl;
protected EventBus eventBus;
protected TRId trId;
protected FieldSet trFieldSet;
protected FieldSet tableFieldSet;
protected TabResource tabResource;
protected TextField nameField;
protected TextArea descriptionField;
protected TextField agencyField;
protected TextField dateField;
protected TextArea rightField;
protected TextField ownerField;
protected CheckBox finalizedField;
protected TextField typeField;
protected IconButton btnShare;
protected TRMetadataAccordionPanel trMetadataPanel;
protected VerticalLayoutContainer layoutTabularResource;
protected TextField tableTypeNameField;
protected TableMetadataAccordionPanel tableMetadataPanel;
protected VerticalLayoutContainer layoutTable;
protected TextButton saveButton;
protected TextButton validationsButton;
public TabularResourceProperties(String name, EventBus eventBus) {
super();
setId(name);
this.eventBus = eventBus;
forceLayoutOnResize = true;
vl = new VerticalLayoutContainer();
vl.setScrollMode(ScrollMode.AUTO);
vl.setAdjustForScroll(true);
initInformation();
this.add(vl);
}
public void addTabularResource() {
trFieldSet = new FieldSet();
trFieldSet.setHeadingText("Tabular Resource");
trFieldSet.setCollapsible(true);
trFieldSet.setResize(true);
layoutTabularResource = new VerticalLayoutContainer();
trFieldSet.add(layoutTabularResource);
nameField = new TextField();
// nameField.setReadOnly(true);
nameField.setValue("");
layoutTabularResource.add(new FieldLabel(nameField, "Name"),
new VerticalLayoutData(1, -1));
descriptionField = new TextArea();
// descriptionField.setReadOnly(true);
descriptionField.setValue("");
layoutTabularResource.add(new FieldLabel(descriptionField,
"Description"), new VerticalLayoutData(1, -1));
agencyField = new TextField();
// agencyField.setReadOnly(true);
agencyField.setValue("");
layoutTabularResource.add(new FieldLabel(agencyField, "Agency"),
new VerticalLayoutData(1, -1));
dateField = new TextField();
dateField.setReadOnly(true);
dateField.setValue("");
layoutTabularResource.add(new FieldLabel(dateField, "Date"),
new VerticalLayoutData(1, -1));
typeField = new TextField();
typeField.setReadOnly(true);
typeField.setValue("");
layoutTabularResource.add(new FieldLabel(typeField, "Type"),
new VerticalLayoutData(1, -1));
rightField = new TextArea();
// rightField.setReadOnly(true);
rightField.setValue("");
layoutTabularResource.add(new FieldLabel(rightField, "Rights"),
new VerticalLayoutData(1, -1));
ownerField = new TextField();
ownerField.setReadOnly(true);
ownerField.setValue("");
btnShare = new IconButton();
btnShare.setIcon(ResourceBundle.INSTANCE.share());
btnShare.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Log.debug("Clicked btnShare");
eventBus.fireEvent(new RibbonEvent(RibbonType.SHARE));
}
});
btnShare.setVisible(false);
HorizontalPanel hp = new HorizontalPanel();
hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
hp.add(ownerField);
hp.add(btnShare);
FieldLabel ownerLabel = new FieldLabel(hp, "Owner");
layoutTabularResource.add(ownerLabel,
new VerticalLayoutData(1, -1));
finalizedField = new CheckBox();
// finalizedField.setBoxLabel("");
finalizedField.setValue(false);
finalizedField.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
@Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
if (event.getValue()) {
info(" Warning",
"A TabularResource set to final can't be modified anymore!");
}
}
});
layoutTabularResource.add(new FieldLabel(finalizedField, "Final"),
new VerticalLayoutData(1, -1));
// Save Button
saveButton = new TextButton("Save");
saveButton.setIcon(ResourceBundle.INSTANCE.save());
saveButton.setIconAlign(IconAlign.RIGHT);
saveButton.setTitle("Save");
SelectHandler saveHandler = new SelectHandler() {
public void onSelect(SelectEvent event) {
onSave();
}
};
saveButton.addSelectHandler(saveHandler);
// Validation Button
validationsButton = new TextButton("Validations");
validationsButton.setIcon(ResourceBundle.INSTANCE.tableValidation());
validationsButton.setIconAlign(IconAlign.RIGHT);
validationsButton.setTitle("Validations");
SelectHandler validationsHandler = new SelectHandler() {
public void onSelect(SelectEvent event) {
openValidations();
}
};
validationsButton.addSelectHandler(validationsHandler);
HBoxLayoutContainer hBox = new HBoxLayoutContainer();
hBox.add(saveButton, new BoxLayoutData(new Margins(2, 5, 2, 5)));
hBox.add(validationsButton, new BoxLayoutData(new Margins(2, 5, 2, 5)));
layoutTabularResource.add(hBox, new VerticalLayoutData(-1, -1,
new Margins(2)));
vl.add(trFieldSet);
}
protected void updateTabularResource(TabResource tabResource) {
this.tabResource = tabResource;
Log.debug(tabResource.toString());
nameField.setValue(tabResource.getName());
descriptionField.setValue(tabResource.getDescription());
agencyField.setValue(tabResource.getAgency());
dateField.setValue(tabResource.getDate());
typeField.setValue(tabResource.getTrId().getTabularResourceType());
rightField.setValue(tabResource.getRight());
ownerField.setValue(tabResource.getOwner().getLogin());
if (tabResource.isFinalized()) {
finalizedField.setEnabled(false);
finalizedField.setValue(true);
} else {
finalizedField.setEnabled(true);
finalizedField.setValue(false);
}
}
protected void updateShareInfo(ShareInfo result){
ArrayList<Contacts> contacts=result.getContacts();
if(contacts!=null&& contacts.size()>0){
btnShare.setVisible(true);
} else {
btnShare.setVisible(false);
}
}
public void addTable() {
tableFieldSet = new FieldSet();
tableFieldSet.setHeadingText("Table");
tableFieldSet.setCollapsible(true);
tableFieldSet.setResize(true);
layoutTable = new VerticalLayoutContainer();
tableFieldSet.add(layoutTable);
tableTypeNameField = new TextField();
tableTypeNameField.setReadOnly(true);
tableTypeNameField.setValue("");
layoutTable.add(new FieldLabel(tableTypeNameField, "Type"),
new VerticalLayoutData(1, -1));
vl.add(tableFieldSet);
}
protected void openValidations() {
Log.debug("Request Validations Tab");
if (trId != null) {
WidgetRequestEvent e = new WidgetRequestEvent(
WidgetRequestType.VALIDATIONSTASKSPANEL);
e.setTrId(trId);
eventBus.fireEvent(e);
} else {
Log.error("TRId is null");
}
}
protected void updateTable(TableData tableData) {
tableTypeNameField.setValue(tableData.getTypeName());
if (tableMetadataPanel != null) {
tableFieldSet.remove(layoutTable);
layoutTable.remove(tableMetadataPanel);
}
trId = tableData.getTrId();
tableMetadataPanel = new TableMetadataAccordionPanel(
"TableInformation", trId, eventBus);
layoutTable.add(tableMetadataPanel, new VerticalLayoutData(-1, -1));
tableFieldSet.add(layoutTable);
}
public void update() {
TDGWTServiceAsync.INSTANCE
.getTabResourceInformation(new AsyncCallback<TabResource>() {
public void onSuccess(TabResource result) {
Log.info("Retrived TR: " + result.getTrId());
updateTabularResource(result);
getShareInformation(result.getTrId());
}
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
AlertMessageBox d = new AlertMessageBox("Error",
"Error retrienving properties: "
+ caught.getLocalizedMessage());
d.addHideHandler(new HideHandler() {
public void onHide(HideEvent event) {
//
}
});
d.show();
}
}
});
}
protected void getShareInformation(TRId trId){
TDGWTServiceAsync.INSTANCE.getShareInfo(trId,
new AsyncCallback<ShareInfo>() {
public void onSuccess(ShareInfo result) {
Log.debug("Retrived share info:" + result);
updateShareInfo(result);
getLastTable(tabResource.getTrId());
}
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
AlertMessageBox d = new AlertMessageBox("Error",
"Error retrienving Share Informations: "
+ caught.getLocalizedMessage());
d.addHideHandler(new HideHandler() {
public void onHide(HideEvent event) {
//
}
});
d.show();
}
}
});
}
protected void getLastTable(TRId trId) {
TDGWTServiceAsync.INSTANCE.getLastTable(trId,
new AsyncCallback<TableData>() {
public void onSuccess(TableData result) {
Log.debug("Retrived LastTable:" + result);
updateTable(result);
}
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
AlertMessageBox d = new AlertMessageBox("Error",
"Error retrienving Last Table: "
+ caught.getLocalizedMessage());
d.addHideHandler(new HideHandler() {
public void onHide(HideEvent event) {
//
}
});
d.show();
}
}
});
}
public void initInformation() {
addTabularResource();
addTable();
update();
}
protected void onSave() {
if (nameField.getValue() == null || nameField.getValue().isEmpty()) {
alert("Attention", "Fill name field");
return;
}
/*if (descriptionField.getValue() == null
|| descriptionField.getValue().isEmpty()) {
alert("Attention", "Fill description field");
return;
}
if (rightField.getValue() == null || rightField.getValue().isEmpty()) {
alert("Attention", "Fill rights field");
return;
}*/
tabResource.setName(nameField.getValue());
tabResource.setDescription(descriptionField.getValue());
tabResource.setAgency(agencyField.getValue());
tabResource.setRight(rightField.getValue());
tabResource.setFinalized(finalizedField.getValue());
TDGWTServiceAsync.INSTANCE.setTabResourceInformation(tabResource,
new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.debug("Error Setting Tabular Resoruce Properties: "
+ caught.getLocalizedMessage());
alert("Error",
"Error setting tabular resource properties");
}
}
public void onSuccess(Void result) {
Log.debug("Tabular Resource properties are set: "
+ tabResource);
info("Proprerties",
"Tabular Resource properties are set");
if(finalizedField.getValue()){
finalizedField.setEnabled(false);
finalizedField.setValue(true);
}
}
});
}
private void alert(String title, String message) {
final AlertMessageBox d = new AlertMessageBox(title, message);
d.addHideHandler(new HideHandler() {
public void onHide(HideEvent event) {
}
});
d.show();
}
private void info(String title, String message) {
final InfoMessageBox d = new InfoMessageBox(title, message);
d.addHideHandler(new HideHandler() {
public void onHide(HideEvent event) {
}
});
d.show();
}
}