Skip to content

Instantly share code, notes, and snippets.

@lenards
Created July 12, 2010 16:34
Show Gist options
  • Select an option

  • Save lenards/472690 to your computer and use it in GitHub Desktop.

Select an option

Save lenards/472690 to your computer and use it in GitHub Desktop.
package org.iplantc.de.client.views.dialogs;
import java.util.List;
import org.iplantc.de.client.ErrorHandler;
import org.iplantc.de.client.I18N;
import org.iplantc.de.client.events.UploadCompleteHandler;
import org.iplantc.de.client.models.UserInfo;
import org.iplantc.de.client.services.FolderServices;
import org.iplantc.de.client.utils.JsonUtil;
import org.iplantc.de.client.utils.NotifyInfo;
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.util.Params;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
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.FitLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
public class URLImportDialog extends Dialog
{
private FormPanel form;
private UploadCompleteHandler handler;
private Status importStatus;
private String parentFolderId;
public URLImportDialog(String parentFolderId, UploadCompleteHandler handler)
{
super();
initDialog();
form = new FormPanel();
form.setHideLabels(true);
form.setHeaderVisible(false);
form.setAutoHeight(true);
form.setFieldWidth(340);
this.handler = handler;
this.parentFolderId = parentFolderId;
addUrlField();
addDescField();
addButtons();
setHeading(I18N.DISPLAY.urlImport());
add(form);
}
private void initDialog()
{
setButtonAlign(HorizontalAlignment.RIGHT);
setLayout(new FitLayout());
setResizable(false);
setModal(true);
setHideOnButtonClick(false);
}
private void addButtons()
{
importStatus = new Status();
getHeader().addTool(importStatus);
setButtons(Dialog.OKCANCEL);
final Button submitBtn = getOkButton();
submitBtn.setText(I18N.DISPLAY.importLabel());
Button cancelBtn = getButtonById(Dialog.CANCEL);
cancelBtn.addSelectionListener(new SelectionListener<ButtonEvent>()
{
@Override
public void componentSelected(ButtonEvent ce)
{
hide();
}
});
submitBtn.addSelectionListener(new SelectionListener<ButtonEvent>()
{
@Override
public void componentSelected(ButtonEvent ce)
{
submitForm();
}
});
}
/**
* Retrieve the OK button for the dialog.
*
* If a more generic retrieval is needed, use the inherited getButtonById() method.
*
* @return a reference to the OK button for the dialog.
*/
public Button getOkButton()
{
return getButtonById(Dialog.OK);
}
private void addDescField()
{
form.add(new HTML(I18N.DISPLAY.descriptionPrompt()));
TextArea description = new TextArea();
description.setMaxLength(255);
description.setName(I18N.DISPLAY.description());
description.setId(I18N.DISPLAY.description());
form.add(description);
}
private void addUrlField()
{
form.add(new HTML(I18N.DISPLAY.urlPrompt()));
final TextField<String> url = new TextField<String>();
url.setAllowBlank(false);
url.setName(I18N.DISPLAY.url());
url.setId(I18N.DISPLAY.url());
form.add(url);
}
private void setButtonEnable(boolean state)
{
getOkButton().setEnabled(state);
}
/**
* submit url and the description to the service
*/
private void submitForm()
{
if(form.isValid())
{
importStatus.setBusy(I18N.DISPLAY.fileImportStatus());
UserInfo info = UserInfo.getInstance();
List<Field<?>> fields = form.getFields();
String url = null;
String description = null;
for(Field<?> f : fields)
{
if(f.getId().equals(I18N.DISPLAY.url()))
{
url = (String)f.getValue();
}
else if(f.getId().equals(I18N.DISPLAY.description()))
{
description = (String)f.getValue();
}
}
setButtonEnable(false);
FolderServices.importFile(info.getWorkspaceId(), parentFolderId, url, JsonUtil.escapeNewLine(((description != null) ? description : "")),
new AsyncCallback<String>()
{
@Override
public void onSuccess(String response)
{
handler.onCompletion(response);
importStatus.clearStatus(null);
}
@Override
public void onFailure(Throwable arg0)
{
handler.onCompletion(arg0.toString());
importStatus.clearStatus(null);
}
});
NotifyInfo.display(I18N.DISPLAY.information(),I18N.DISPLAY.importRequestSubmit(),new Params(url));
hide();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment