ckan-content-moderator-widget/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/ui/CkanFramePanel.java

139 lines
3.6 KiB
Java

/**
*
*/
package org.gcube.portlets.widgets.ckancontentmoderator.client.ui;
import org.gcube.portlets.widgets.ckancontentmoderator.client.events.IFrameInstanciedEvent;
import org.gcube.portlets.widgets.ckancontentmoderator.client.resources.ContentModeratorWidgetResources;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.NamedFrame;
// TODO: Auto-generated Javadoc
/**
* The Class CkanFramePanel.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Jun 9, 2016
*/
public class CkanFramePanel extends FlowPanel{
private NamedFrame frame;
//private static final String FRAME_NAME = "ckan-content-frame";
private HandlerManager eventBus;
private Image loading = new Image(ContentModeratorWidgetResources.ICONS.loading());
private String messageToSend;
private String iFrameRandomName;
/**
* Instantiates a new ckan frame panel.
*
* @param eventBus the event bus
*/
public CkanFramePanel(HandlerManager eventBus) {
this.eventBus = eventBus;
addLoading();
}
/**
* Post message.
*
* @param msg the msg (as json)
* @param frameName the frame name
*/
protected native void sendMessage(String msg, String frameName) /*-{
console.log("Sending message " + msg);
// $wnd.parent.postMessage(msg,'*');
//var f = $wnd.frames[frameName];
//console.log(f);
//f.contentWindow.postMessage(msg, "*");
$wnd.frames[frameName].postMessage(msg,"*")
console.log("Message sent");
}-*/;
/**
* Prints the.
*
* @param msg the msg
*/
private static native void print(String msg)/*-{
console.log(msg);
}-*/;
/**
* Instance frame.
*
* @param datasetURL the dataset URL
* @param messageToSend the message to send
* @return the frame
*/
public Frame instanceFrame(String datasetURL, final String messageToSend) {
GWT.log("Instancing new IFRAME with uri: "+datasetURL);
try{
if(frame != null)
remove(frame);
}catch(Exception e){
print("Error " + e);
}
addLoading();
String urlEncoded = URL.encode(datasetURL);
GWT.log("Encoded url for instanciating frame is " + urlEncoded);
iFrameRandomName = Random.nextInt() + "dataset-iframe"+Random.nextInt();
frame = new NamedFrame(iFrameRandomName);
frame.setUrl(urlEncoded);
frame.getElement().setId(iFrameRandomName);
frame.setWidth("100%");
frame.setHeight("500px");
// frame.setHeight("100%");
// frame.getElement().getStyle().setOverflow(Overflow.HIDDEN);
// frame.getElement().setAttribute("scrolling", "no");
frame.getElement().getStyle().setBorderWidth(0, Unit.PX);
frame.addLoadHandler(new LoadHandler() {
@Override
public void onLoad(LoadEvent arg0) {
CkanFramePanel.this.remove(loading);
if(messageToSend!=null)
sendMessage(messageToSend, iFrameRandomName);
}
});
add(frame);
frame.setVisible(true);
eventBus.fireEvent(new IFrameInstanciedEvent(frame));
return frame;
}
/**
* Gets the frame.
*
* @return the frame
*/
public Frame getFrame() {
return frame;
}
/**
* add loading image.
*/
private void addLoading(){
this.add(loading);
loading.getElement().getStyle().setProperty("margin", "auto");
loading.getElement().getStyle().setDisplay(Display.BLOCK);
}
}