workspace-tree-widget/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogGetInfo.java

253 lines
7.0 KiB
Java
Raw Normal View History

package org.gcube.portlets.user.workspace.client.view.windows;
import java.util.Date;
import java.util.List;
import org.gcube.portlets.user.workspace.client.AppControllerExplorer;
import org.gcube.portlets.user.workspace.client.ConstantsExplorer;
import org.gcube.portlets.user.workspace.client.model.FileGridModel;
import org.gcube.portlets.user.workspace.client.model.FileModel;
import org.gcube.portlets.user.workspace.client.model.InfoContactModel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
*
*/
public class DialogGetInfo extends Dialog {
private int widthDialog = 450;
private int heightTextArea = 50;
private TextField<String> txtName = new TextField<String>();
private TextField<String> txtType = new TextField<String>();
private TextField<String> txtCategory = new TextField<String>();
private TextField<String> txtOwner = new TextField<String>();
private TextField<String> txtCreated = new TextField<String>();
private TextField<String> txtSize = new TextField<String>();
private TextField<String> txtLocation = new TextField<String>();
private TextField<String> txtShared = new TextField<String>();
private TextArea textAreaSharedWith = new TextArea();
private final NumberFormat number = ConstantsExplorer.numberFormatterKB;
public DialogGetInfo(FileModel fileModel) {
FormLayout layout = new FormLayout();
layout.setLabelWidth(90);
layout.setDefaultWidth(300);
setLayout(layout);
setIcon(fileModel.getAbstractPrototypeIcon());
setHeading(fileModel.getName() + " Properties");
setButtonAlign(HorizontalAlignment.RIGHT);
setModal(true);
// setBodyBorder(true);
setBodyStyle("padding: 9px; background: none");
setWidth(widthDialog);
setResizable(false);
setButtons(Dialog.OK);
txtName = new TextField<String>();
txtName.setFieldLabel("Name");
txtName.setReadOnly(true);
textFieldSetValue(txtName,fileModel.getName());
add(txtName);
txtType = new TextField<String>();
txtType.setFieldLabel("Type");
txtType.setReadOnly(true);
textFieldSetValue(txtType,fileModel.getType());
add(txtType);
txtCategory = new TextField<String>();
txtCategory.setFieldLabel("Category");
txtCategory.setReadOnly(true);
textFieldSetValue(txtCategory,fileModel.getShortcutCategory());
add(txtCategory);
txtOwner = new TextField<String>();
txtOwner.setFieldLabel("Owner");
txtOwner.setReadOnly(true);
loadOwner(fileModel.getIdentifier());
add(txtOwner);
txtCreated = new TextField<String>();
txtCreated.setFieldLabel("Created");
txtCreated.setReadOnly(true);
if(fileModel instanceof FileGridModel)
textFieldSetValue(txtCreated, ((FileGridModel) fileModel).getCreationDate().toString());
else
loadCreationDate(fileModel.getIdentifier());
add(txtCreated);
txtSize = new TextField<String>();
txtSize.setFieldLabel("Size");
txtSize.setReadOnly(true);
if(fileModel instanceof FileGridModel)
textFieldSetValue(txtSize,getFormattedSize(((FileGridModel) fileModel).getSize()));
else
loadSize(fileModel.getIdentifier());
add(txtSize);
txtShared = new TextField<String>();
txtShared.setFieldLabel("Shared");
txtShared.setReadOnly(true);
textFieldSetValue(txtShared,fileModel.isShared()+"");
add(txtShared);
if(fileModel.isShared()){
textAreaSharedWith.setFieldLabel("Shared with");
textAreaSharedWith.setHeight(heightTextArea);
textAreaSharedWith.setReadOnly(true);
getListSharedContacts(fileModel.getIdentifier());
add(textAreaSharedWith);
}
this.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
hide();
}
});
this.show();
}
private void textFieldSetValue(TextField<String> field, String value){
if(value==null || value.isEmpty())
field.setValue("unknown");
else
field.setValue(value);
}
private void loadOwner(final String itemId){
txtOwner.mask();
// System.out.println("owner by id "+itemId);
AppControllerExplorer.rpcWorkspaceService.getOwnerByItemId(itemId, new AsyncCallback<InfoContactModel>() {
@Override
public void onFailure(Throwable caught) {
GWT.log("an error occured in get Owner by Id "+itemId + " "+caught.getMessage());
txtOwner.unmask();
}
@Override
public void onSuccess(InfoContactModel result) {
textFieldSetValue(txtOwner,result.getName());
txtOwner.unmask();
}
});
}
private void loadSize(final String itemId){
txtSize.mask();
AppControllerExplorer.rpcWorkspaceService.loadSizeByItemId(itemId, new AsyncCallback<Long>() {
@Override
public void onFailure(Throwable caught) {
GWT.log("an error occured in load creation date by Id "+itemId + " "+caught.getMessage());
txtSize.unmask();
}
@Override
public void onSuccess(Long result) {
textFieldSetValue(txtSize,getFormattedSize(result));
txtSize.unmask();
}
});
}
private void loadCreationDate(final String itemId){
txtCreated.mask();
AppControllerExplorer.rpcWorkspaceService.getItemCreationDateById(itemId, new AsyncCallback<Date>() {
@Override
public void onFailure(Throwable caught) {
GWT.log("an error occured in load creation date by Id "+itemId + " "+caught.getMessage());
txtCreated.unmask();
}
@Override
public void onSuccess(Date result) {
if(result!=null)
textFieldSetValue(txtCreated,result.toString());
txtCreated.unmask();
}
});
}
private void getListSharedContacts(String sharedId){
textAreaSharedWith.mask();
AppControllerExplorer.rpcWorkspaceService.getListUserSharedBySharedItem(sharedId, new AsyncCallback<List<InfoContactModel>>() {
@Override
public void onFailure(Throwable caught) {
textAreaSharedWith.unmask();
}
@Override
public void onSuccess(List<InfoContactModel> result) {
String users = "";
for (int i = 0; i < result.size()-1; i++) {
users+= result.get(i).getName() + ", ";
}
if(result.size()>1)
users += result.get(result.size()-1).getName();
textAreaSharedWith.setValue(users);
textAreaSharedWith.unmask();
}
});
}
private String getFormattedSize(long value){
if(value>0){
double kb = value/1024;
if(kb<1)
kb=1;
return number.format(kb);
}else
return "";
}
}