workspace-tree-widget/src/main/java/org/gcube/portlets/user/workspace/client/view/sharing/DialogShareFolder.java

425 lines
12 KiB
Java

package org.gcube.portlets.user.workspace.client.view.sharing;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.user.workspace.client.ConstantsExplorer;
import org.gcube.portlets.user.workspace.client.model.FileModel;
import org.gcube.portlets.user.workspace.client.model.InfoContactModel;
import org.gcube.portlets.user.workspace.client.resources.Resources;
import org.gcube.portlets.user.workspace.client.view.sharing.multisuggest.MultiDragContact;
import org.gcube.portlets.user.workspace.client.view.sharing.multisuggest.MultiValuePanel;
import org.gcube.portlets.user.workspace.client.view.windows.MessageBoxAlert;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
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.LayoutContainer;
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.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
*
*/
public class DialogShareFolder extends Dialog {
private int widthDialog = 530;
private int heightTextArea = 100;
private TextField<String> txtName;
private TextArea textAreaDescription = new TextArea();
private FileModel parentFolder = null;
private UserStore userStore = new UserStore();
private MultiValuePanel suggestPanel = new MultiValuePanel(userStore);
private Button buttonMultiDrag = new Button("Choose Contacts");
private TextField<String> txtOwner;
private List<InfoContactModel> listAlreadyShared = new ArrayList<InfoContactModel>(){
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Compare Login
*/
@Override
public boolean contains(Object o) {
if(o==null)
return false;
InfoContactModel contact = (InfoContactModel) o;
for (int i = 0; i < listAlreadyShared.size(); i++){
if (contact.getName().compareTo(listAlreadyShared.get(i).getName())==0)
return true;
}
return false;
};
};
/**
* Use to modify a shared folder or share an existing folder
* @param folder
* @param type
*/
public DialogShareFolder(String folderParentName, FileModel folder, HandlerManager eventBus) {
initLayout(folderParentName);
this.parentFolder = folder;
this.setIcon(Resources.getIconShareFolder());
setWidth(widthDialog);
setButtons(Dialog.OKCANCEL);
setHeading("Share folder: "+folder.getName());
txtName = new TextField<String>();
txtName.setAllowBlank(false);
txtName.setValue(folder.getName());
txtName.setReadOnly(true);
txtName.setAutoValidate(true);
txtName.setFieldLabel("Folder Name");
txtOwner = new TextField<String>();
txtOwner.setAllowBlank(true);
txtOwner.setValue("");
txtOwner.setReadOnly(true);
txtOwner.setAutoValidate(false);
txtOwner.setFieldLabel("Owner");
textAreaDescription.setFieldLabel(ConstantsExplorer.DIALOG_DESCRIPTION);
textAreaDescription.setHeight(heightTextArea);
textAreaDescription.setWidth(380);
//TODO REMOVE
// System.out.println("folder description: "+folder.getDescription());
textAreaDescription.setValue(folder.getDescription());
// textAreaDescription.setReadOnly(true);
final LayoutContainer lc = new LayoutContainer();
lc.setStyleAttribute("margin-top", "10px");
lc.setStyleAttribute("margin-bottom", "10px");
lc.setSize(480, 50);
FlexTable flexTable = new FlexTable();
flexTable.setStyleName("userssuggest");
Label labelShareWith = new Label("Share with users");
flexTable.setWidget(0, 0, labelShareWith);
flexTable.setWidget(0, 1, suggestPanel);
buttonMultiDrag.setStyleName("wizardButton");
// buttonMultiDrag.setIcon(Resources.getIconUsers());
flexTable.setWidget(1, 0, new Label("Add more"));
flexTable.setWidget(1, 1, buttonMultiDrag);
Label labelDescription = new Label("Description");
flexTable.setWidget(2, 0, labelDescription);
flexTable.setWidget(2, 1, textAreaDescription);
if(!folder.isShared()){ //SETTABLE PERMISSION ONLY IF FOLDER IS NOT ALREADY SHARED
Label labelProperty = new Label("Property");
Anchor anchorPermission = new Anchor("Set Permissions");
flexTable.setWidget(3, 0, labelProperty);
flexTable.setWidget(3, 1, anchorPermission);
final DialogPermission permission = new DialogPermission(folderParentName, eventBus);
anchorPermission.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
permission.show();
}
});
// flexTable.setWidget(3, 1, permission);
}
lc.add(flexTable);
lc.mask();
userStore.getListSharedUserByFolderId(folder.getIdentifier(), new AsyncCallback<List<InfoContactModel>>() {
@Override
public void onSuccess(List<InfoContactModel> result) {
for (InfoContactModel infoContactModel : result) {
listAlreadyShared.add(infoContactModel);
suggestPanel.addRecipient(infoContactModel.getName(),false);
}
lc.unmask();
}
@Override
public void onFailure(Throwable caught) {
lc.unmask();
}
});
userStore.getOwner(folder.getIdentifier(), new AsyncCallback<InfoContactModel>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(InfoContactModel result) {
txtOwner.setValue(result.getName());
}
});
setFocusWidget(suggestPanel.getBox());
add(txtName);
add(txtOwner);
add(lc);
addListners();
this.show();
}
public FileModel getParentFolder() {
return parentFolder;
}
public void initLayout(String folderParentName){
FormLayout layout = new FormLayout();
layout.setLabelWidth(90);
layout.setDefaultWidth(380);
setLayout(layout);
setModal(true);
setScrollMode(Scroll.AUTOY);
setBodyStyle("padding: 9px; background: none");
setWidth(widthDialog);
setHeight(380);
setResizable(true);
setButtonAlign(HorizontalAlignment.CENTER);
setWidth(widthDialog);
setButtons(Dialog.OKCANCEL);
}
/**
* Use to create a new shared folder
*/
public DialogShareFolder(String folderParentName, HandlerManager eventBus) {
initLayout(folderParentName);
this.setIcon(Resources.getIconSharedFolder());
setHeading("Create a new shared folder in: "+folderParentName);
setWidth(widthDialog);
setButtons(Dialog.OKCANCEL);
txtName = new TextField<String>();
txtName.setAllowBlank(false);
txtName.setAutoValidate(true);
txtName.setFieldLabel("Folder Name");
txtName.getMessages().setRegexText(ConstantsExplorer.REGEX_FOLDER_NAME+": .<>\\|?/*%$ or contains / or \\");
txtName.setRegex("^[^.<>\\|?/*%$]+[^\\/]*$");
textAreaDescription.setFieldLabel(ConstantsExplorer.DIALOG_DESCRIPTION);
textAreaDescription.setHeight(heightTextArea);
textAreaDescription.setWidth(380);
LayoutContainer lc = new LayoutContainer();
lc.setStyleAttribute("margin-top", "10px");
lc.setStyleAttribute("margin-bottom", "10px");
lc.setSize(480, 50);
FlexTable flexTable = new FlexTable();
flexTable.setStyleName("userssuggest");
Label lableShareWith = new Label("Share with users");
flexTable.setWidget(0, 0, lableShareWith);
flexTable.setWidget(0, 1, suggestPanel);
buttonMultiDrag.setStyleName("wizardButton");
// buttonMultiDrag.setIcon(Resources.getIconUsers());
flexTable.setWidget(1, 0, new Label("Add more"));
flexTable.setWidget(1, 1, buttonMultiDrag);
Label labelDescription = new Label("Description");
flexTable.setWidget(2, 0, labelDescription);
flexTable.setWidget(2, 1, textAreaDescription);
lc.add(flexTable);
setFocusWidget(txtName);
add(txtName);
add(lc);
addListners();
this.show();
}
public List<InfoContactModel> getSharedListUsers() {
// printSelectedUser();
return suggestPanel.getSelectedUser();
}
public void addListners(){
this.getButtonById(Dialog.CANCEL).addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
hide();
}
});
this.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
if(isValidForm(false))
hide();
}
});
buttonMultiDrag.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
final MultiDragContact multiDrag = new MultiDragContact();
multiDrag.show();
List<InfoContactModel> exclusiveContacts = userStore.getExclusiveContactsFromAllContact(suggestPanel.getSelectedUser());
multiDrag.addSourceContacts(exclusiveContacts);
for (InfoContactModel infoContactModel : suggestPanel.getSelectedUser()) {
if(!listAlreadyShared.contains(infoContactModel))
multiDrag.addTargetContact(infoContactModel);
}
multiDrag.addAlreadySharedContacts(suggestPanel.getSelectedUser());
// multiDrag.addTargetContacts(suggestPanel.getSelectedUser());
multiDrag.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
initSuggestContacts();
for (InfoContactModel infoContactModel : multiDrag.getTargetListContact()) {
suggestPanel.addRecipient(infoContactModel.getName(),true);
}
// for (InfoContactModel infoContactModel : multiDrag.getTargetListContact()) {
//
// if(!listAlreadySharedContains(infoContactModel))
// suggestPanel.addRecipient(infoContactModel.getName(),true);
// }
suggestPanel.boxSetFocus();
// printSelectedUser();
}
});
}
});
}
private boolean listAlreadySharedContains(InfoContactModel contact){
if(contact==null)
return false;
for (InfoContactModel ct : listAlreadyShared) {
if(ct.getLogin().compareTo(contact.getLogin())==0){
return true;
}
}
return false;
}
//DEBUG
private void printSelectedUser(){
System.out.println("SELETECTED USERS: ");
for (InfoContactModel contact : suggestPanel.getSelectedUser())
System.out.println(contact);
}
private void initSuggestContacts(){
suggestPanel.resetItemSelected();
for (InfoContactModel contact : listAlreadyShared) {
suggestPanel.addRecipient(contact.getName(), false);
}
}
public String getName() {
return txtName.getValue();
}
public String getDescription() {
if(textAreaDescription.getValue()==null)
return "";
return textAreaDescription.getValue();
}
/**
*
* @param displayAlert
* @return
*/
public boolean isValidForm(boolean displayAlert){
if(!txtName.isValid()){
if(displayAlert)
new MessageBoxAlert("Attention", "Name must not be empty", null);
txtName.focus();
return false;
}
if(getSharedListUsers().size()==0){
if(displayAlert)
new MessageBoxAlert("Attention", "You must pick at least one user with which share the folder", null);
suggestPanel.boxSetFocus();
return false;
}
return true;
}
}