package org.gcube.portlets.user.reportgenerator.client.dialog; import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter; import org.gcube.portlets.user.reportgenerator.client.components.FancyFileUpload; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ChangeListener; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.RadioButton; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; /** * ImageUploaderDialog class is the Dialog for uploading images * * @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it * @version October 2008 (0.2) */ public class ImageUploaderDialog extends DialogBox { private int width; private int height; private Presenter presenter; private String currTemplateName = ""; private HorizontalPanel topLabel = new HorizontalPanel(); private EventListener listener = new EventListener(); private RadioButton local = new RadioButton(""); private RadioButton web = new RadioButton(""); /** * the container panel */ private VerticalPanel mainLayout = new VerticalPanel(); private VerticalPanel dialogPanel = new VerticalPanel(); /** * Creates the dialog * @param presenter my controller */ public ImageUploaderDialog(Presenter presenter) { // Create a dialog box and set the caption text this.width = 400; this.height = 200; this.presenter = presenter; setText("Insert Image"); local.setHTML(" From this computer"); topLabel.add(local); local.setChecked(true); web.setHTML(" From the web (URL)"); topLabel.add(web); web.addClickListener(listener); local.addClickListener(listener); currTemplateName = presenter.getModel().getTemplateName(); dialogPanel.add(getFromLocalPanel()); dialogPanel.setPixelSize(this.width, this.height); mainLayout.add(topLabel); mainLayout.add(dialogPanel); setWidget(mainLayout); } private VerticalPanel getFromLocalPanel() { VerticalPanel toReturn = new VerticalPanel(); FancyFileUpload uploader = new FancyFileUpload(this, currTemplateName); HTML theLabel = new HTML("Browse your computer for the image file to upload:"); toReturn.add(theLabel); toReturn.add(uploader); toReturn.setSpacing(4); toReturn.setStyleName("uploadDialog"); toReturn.setPixelSize(this.width, this.height); return toReturn; } private VerticalPanel getFromURLPanel() { VerticalPanel toReturn = new VerticalPanel(); toReturn.setSpacing(5); HTML theLabel = new HTML("Enter image web address:"); final TextBox urlTextbox = new TextBox(); urlTextbox.setWidth("90%"); final HTML previewBox = new HTML("

Image preview will be displayed here.

*Remember: Using others' images on the web without their permission may be bad manners, or worse, copyright infringement.

", true); previewBox.setStyleName("imagePreviewBox"); toReturn.add(theLabel); toReturn.add(urlTextbox); toReturn.add(previewBox); urlTextbox.addChangeListener(new ChangeListener() { public void onChange(Widget sender) { previewBox.setHTML(""); previewBox.removeStyleName("imagePreviewBox"); } }); HorizontalPanel buttonsPanel = new HorizontalPanel(); buttonsPanel.add(new Button("Insert", new ClickListener() { public void onClick(Widget sender) { hide(); presenter.getCurrentSelected().getExtendedFormatter().insertImage(urlTextbox.getText()); presenter.getModel().storeInSession(); } })); buttonsPanel.add(new Button("Cancel", new ClickListener() { public void onClick(Widget sender) { hide(); } })); buttonsPanel.setSpacing(5); toReturn.add(buttonsPanel); return toReturn; } /** * * @param imageName . */ public void insertImage(String imageName) { String imgURL = getImageURL(imageName, "CURRENT_OPEN"); presenter.getCurrentSelected().getExtendedFormatter().insertImage(imgURL); presenter.getModel().storeInSession(); } /** * use an inner EventListener class to avoid exposing event methods on the dialog class itself. */ private class EventListener implements ClickListener { public void onClick(Widget sender) { if (sender == web && web.isChecked()) { dialogPanel.clear(); dialogPanel.add(getFromURLPanel()); } else if (sender == local && local.isChecked()) { dialogPanel.clear(); dialogPanel.add(getFromLocalPanel()); } } } /** * return a URL which is lookable for on the web * @param imageName . * @param templateName . * @return . */ public String getImageURL(String imageName, String templateName) { String currentUser = presenter.getCurrentUser(); String currentScope = presenter.getCurrentScope(); /** * Images will be stored under webapps/usersArea... * GWT.getModuleBaseURL() returns * e.g. http://dlib28.isti.cnr.it/templatecreator/html/ * need to get just http://dlib28.isti.cnr.it/ */ //remove "/html/" and get e.g. http://dlib28.isti.cnr.it/templatecreator String host = GWT.getModuleBaseURL().substring(0, GWT.getModuleBaseURL().length()-6); //loog for last slash int lastSlash = host.lastIndexOf("/"); //get what i need : e.g. http://dlib28.isti.cnr.it/ or host = "http://localhost:8080/"; host = host.substring(0, lastSlash +1 ); //host = "http://localhost:8080/"; String imgURL = host + "usersArea/" + currentScope + "/templates/" + currentUser + "/" + "CURRENT_OPEN" + "/images/" + imageName; return imgURL; } }