Skip to content

Instantly share code, notes, and snippets.

@akjava
Created June 4, 2013 11:19
Show Gist options
  • Save akjava/5705224 to your computer and use it in GitHub Desktop.
Save akjava/5705224 to your computer and use it in GitHub Desktop.
GWT paste receive test.
package com.akjava.gwt.servletmaker.client;
import com.akjava.gwt.lib.client.LogUtils;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.DataTransfer;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class ServletMaker implements EntryPoint {
private TextArea output;
/**
* This is the entry point method.
*/
public void onModuleLoad() {
VerticalPanel root=new VerticalPanel();
RootPanel.get().add(root);
PasteValueReceiveArea test=new PasteValueReceiveArea();
test.setText("Focus & Paste Here");
root.add(test);
test.setSize("400px", "100px");
test.setFocus(true);
test.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
output.setText(event.getValue());
}
});
output = new TextArea();
output.setSize("400px","400px");
root.add(output);
}
/**
* only work on Chrome(web-kit)
* to catch new value add addValueChangeHandler
*
* addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
GWT.log("changed:"+event.getValue());
}
});
* ref
* https://groups.google.com/forum/?fromgroups#!topic/google-web-toolkit/CaNSdwfSK-A
* @author aki
*
*/
public class PasteValueReceiveArea extends TextArea{
public PasteValueReceiveArea(){
super();
sinkEvents(Event.ONPASTE);
setReadOnly(true);
addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
GWT.log("changed:"+event.getValue());
}
});
}
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
switch (event.getTypeInt()) {
case Event.ONPASTE: {
LogUtils.log(event);
GWT.log("paste");
ValueChangeEvent.fire(this, getPastedText(event));
break;
}
}
}
}
public static native String getPastedText(Event event)
/*-{
var text = "";
if (event.clipboardData) // WebKit/Chrome/Safari
{
try
{
text = event.clipboardData.getData("Text");
return text;
}
catch (e)
{
// Hmm, that didn't work.
}
}
return text;
}-*/;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment