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

232 lines
6.3 KiB
Java

package org.gcube.portlets.user.workspace.client.view.windows;
import org.gcube.portlets.user.workspace.client.AppControllerExplorer;
import org.gcube.portlets.user.workspace.client.event.SessionExpiredEvent;
import org.gcube.portlets.user.workspace.client.model.InfoContactModel;
import org.gcube.portlets.user.workspace.client.resources.Resources;
import org.gcube.portlets.user.workspace.shared.PublicLink;
import org.gcube.portlets.user.workspace.shared.SessionExpiredException;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
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.Label;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* The Class DialogGetLink.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 13, 2016
*/
public class DialogGetLink extends Dialog {
private TextField<String> txtCompleteURL;
private TextField<String> txtShortURL;
private int widht = 450;
private int height = 210;
private VerticalPanel vp = new VerticalPanel();
/**
* The Enum Link_Type.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 13, 2016
*/
public enum Link_Type {PUBLIC_LINK, FOLDER_LINK};
/**
* Instantiates a new dialog get link.
*
* @param headingTxt the heading txt
* @param itemId the item id
* @param type the type
*/
public DialogGetLink(String headingTxt, final String itemId, Link_Type type) {
setButtonAlign(HorizontalAlignment.CENTER);
setIcon(Resources.getIconPublicLink());
vp.setHorizontalAlign(HorizontalAlignment.CENTER);
vp.setVerticalAlign(VerticalAlignment.MIDDLE);
vp.getElement().getStyle().setPadding(1, Unit.PX);
setHeading(headingTxt);
setModal(true);
setBodyStyle("padding: 3px; background: none");
setWidth(widht);
setHeight(height);
setResizable(false);
setButtons(Dialog.CLOSE);
setScrollMode(Scroll.AUTOY);
// label.setText(msgTitle);
// label.setStyleName("myWebDavStyle");
VerticalPanel vp1 = new VerticalPanel();
vp1.setStyleAttribute("margin-top", "8px");
txtCompleteURL = new TextField<String>();
txtCompleteURL.setStyleAttribute("margin-top", "1px");
txtCompleteURL.setWidth(widht-20);
txtCompleteURL.setReadOnly(true);
// txtCompleteURL.mask("Getting Link...");
vp1.add(new Label("Link"));
vp1.add(txtCompleteURL);
VerticalPanel vp2 = new VerticalPanel();
vp2.setStyleAttribute("margin-top", "8px");
txtShortURL = new TextField<String>();
txtShortURL.setStyleAttribute("margin-top", "1px");
txtShortURL.setWidth(widht-20);
// txtShortURL.mask("Getting Link...");
vp2.add(new Label("Short Link"));
vp2.add(txtShortURL);
switch (type) {
case PUBLIC_LINK:
vp.mask("Getting Public Link...");
if(itemId!=null && !itemId.isEmpty()){
AppControllerExplorer.rpcWorkspaceService.getPublicLinkForFolderItemId(itemId, true, new AsyncCallback<PublicLink>() {
@Override
public void onSuccess(PublicLink publicLink) {
vp.unmask();
txtCompleteURL.setValue(publicLink.getCompleteURL());
txtShortURL.setValue(publicLink.getShortURL());
selectTxt();
}
@Override
public void onFailure(Throwable caught) {
vp.unmask();
if(caught instanceof SessionExpiredException){
GWT.log("Session expired");
AppControllerExplorer.getEventBus().fireEvent(new SessionExpiredEvent());
return;
}
new MessageBoxAlert("Error", caught.getMessage(), null);
}
});
}else{
txtCompleteURL.unmask();
new MessageBoxAlert("Error", "The item identifier is null", null);
}
break;
case FOLDER_LINK:
vp.mask("Getting Folder Link... checking permissions");
if(itemId!=null && !itemId.isEmpty()){
AppControllerExplorer.rpcWorkspaceService.getOwnerByItemId(itemId, new AsyncCallback<InfoContactModel>() {
@Override
public void onFailure(Throwable caught) {
vp.unmask();
if(caught instanceof SessionExpiredException){
GWT.log("Session expired");
AppControllerExplorer.getEventBus().fireEvent(new SessionExpiredEvent());
return;
}
new MessageBoxAlert("Error", caught.getMessage(), null);
}
@Override
public void onSuccess(InfoContactModel result) {
if(result.getLogin().compareToIgnoreCase(AppControllerExplorer.myLogin)==0){
vp.mask("Getting Folder Link... permissions granted");
allowAccessToFolderLink(itemId);
}else
new MessageBoxAlert("Permission Denied", "You have not permission to get Folder Link, you must be owner or administrator to the folder", null);
}
});
}
break;
default:
break;
}
this.getButtonById(Dialog.CLOSE).addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
hide();
}
});
// vp.add(label);
vp.add(vp1);
vp.add(vp2);
setFocusWidget(txtCompleteURL);
add(vp);
}
/**
* Allow access to folder link.
*
* @param folderId the folder id
*/
private void allowAccessToFolderLink(String folderId){
AppControllerExplorer.rpcWorkspaceService.getFolderLinkForFolderItemId(folderId, true, new AsyncCallback<PublicLink>() {
@Override
public void onSuccess(PublicLink publicLink) {
vp.unmask();
txtCompleteURL.setValue(publicLink.getCompleteURL());
txtShortURL.setValue(publicLink.getShortURL());
selectTxt();
}
@Override
public void onFailure(Throwable caught) {
vp.unmask();
if(caught instanceof SessionExpiredException){
GWT.log("Session expired");
AppControllerExplorer.getEventBus().fireEvent(new SessionExpiredEvent());
return;
}
new MessageBoxAlert("Error", caught.getMessage(), null);
}
});
}
/**
* Gets the txt value.
*
* @return the txt value
*/
public String getTxtValue() {
return txtCompleteURL.getValue();
}
/**
* Select txt.
*/
public void selectTxt(){
if(txtCompleteURL.getValue()!=null)
txtCompleteURL.select(0, txtCompleteURL.getValue().length());
}
}