Skip to content

Instantly share code, notes, and snippets.

@ibaca
Created May 30, 2016 20:54
Show Gist options
  • Save ibaca/d8c14e19bf8f96bbe1b4b7f1291b6b24 to your computer and use it in GitHub Desktop.
Save ibaca/d8c14e19bf8f96bbe1b4b7f1291b6b24 to your computer and use it in GitHub Desktop.
package dtachoviewer.client;
import static com.google.gwt.safehtml.shared.SafeHtmlUtils.htmlEscape;
import static elemental.events.Event.CHANGE;
import static elemental.events.Event.COPY;
import static elemental.events.Event.DRAGLEAVE;
import static elemental.events.Event.DRAGOVER;
import static elemental.events.Event.DROP;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.typedarrays.shared.ArrayBuffer;
import com.google.gwt.typedarrays.shared.Int8Array;
import com.google.gwt.typedarrays.shared.TypedArrays;
import com.intendia.dtacho.DDDQuery.DDDDataSource;
import com.intendia.dtacho.DDDQuery.dumper.Content;
import com.intendia.dtacho.DDDQuery.dumper.ContentVisitor;
import com.intendia.dtacho.DDDQuery.dumper.Element;
import com.intendia.dtacho.DDDQuery.dumper.Text;
import com.intendia.dtacho.DDDQuery.internalData.DTCODataClass;
import elemental.client.Browser;
import elemental.dom.Clipboard;
import elemental.dom.Document;
import elemental.events.EventListener;
import elemental.html.DivElement;
import elemental.html.File;
import elemental.html.FileList;
import elemental.html.FileReader;
import elemental.html.InputElement;
import elemental.html.OutputElement;
import elemental.html.Window;
// http://www.html5rocks.com/en/tutorials/file/dndfiles/
public class DtachoViewer implements EntryPoint {
private Window wnd;
private OutputElement list;
private OutputElement ddd;
@Override public void onModuleLoad() {
wnd = Browser.getWindow();
Document doc = Browser.getDocument();
if (def(wnd, "File") && def(wnd, "FileReader") && def(wnd, "FileList") && def(wnd, "Blob")) {
// Great success! All the File APIs are supported.
} else {
wnd.alert("The File APIs are not fully supported in this browser.");
}
DivElement dropHere = doc.createDivElement();
dropHere.setClassName("drop-here");
InputElement file = doc.createInputElement();
file.setType("file"); file.setId("files"); file.setName("files[]"); file.setMultiple(true);
dropHere.appendChild(doc.createTextNode("Drop files here or use "));
dropHere.appendChild(file);
dropHere.appendChild(list = doc.createOutputElement());
doc.getBody().appendChild(dropHere);
doc.getBody().appendChild(ddd = doc.createOutputElement());
file.addEventListener(CHANGE, evt -> show(((InputElement) evt.getTarget()).getFiles()), false);
dropHere.addEventListener(DRAGOVER, evt -> {
evt.stopPropagation();
evt.preventDefault();
dropHere.getClassList().add("dragover");
js(evt, "dataTransfer", Clipboard.class).setDropEffect(COPY);
}, false);
dropHere.addEventListener(DRAGLEAVE, evt -> dropHere.getClassList().remove("dragover"), false);
dropHere.addEventListener(DROP, evt -> {
evt.stopPropagation();
evt.preventDefault();
dropHere.getClassList().remove("dragover");
list.setInnerHTML("");
ddd.setInnerHTML("");
FileList files = js(evt, "dataTransfer", Clipboard.class).getFiles();
wnd.getConsole().log("setting files to file");
file.setFiles(files);
show(files);
}, false);
}
private void show(FileList files) {
String out = "";
for (int i = 0; i < files.length(); i++) {
File f = files.item(i);
final String name = htmlEscape(f.getName());
final String type = htmlEscape(f.getType());
out += "<li><strong>" + name + "</strong> (" + type + ") - " + f.getSize() + " bytes, "
+ "last modified: " + f.getLastModifiedDate() + "</li>";
FileReader reader = wnd.newFileReader();
final EventListener eventListener = loaded -> {
wnd.getConsole().log(loaded);
final Object result = reader.getResult();
wnd.getConsole().log(result);
ArrayBuffer buf = (ArrayBuffer) result;
final Int8Array int8Array = TypedArrays.createInt8Array(buf);
final int length = int8Array.length();
byte[] byteArray = new byte[length];
for (int j = 0; j < length; j++) {
byteArray[j] = int8Array.get(j);
}
final DDDDataSource ds = new DDDDataSource();
final DTCODataClass dtco = ds.processFile(f.getName(), byteArray);
Element element = dtco.generateXMLElement();
ddd.setInnerHTML(element.accept(new ContentVisitor<String>() {
public String element(Element node) {
switch (node.name) {
case "activityChangeInfo":
case "vuDetailedSpeedBlock" : {
String s = node.name + " ";
for (Content c : node.content)
s += c.accept(new ContentVisitor<String>() {
public String element(Element node) {
String s = "";
for (Content c : node.content) s += c.accept(this) + " ";
return s;
}
public String text(Text node) { return node.text; }
});
return s;
}
default: {
String s = "<label>" + node.name + "</label><ul>";
for (Content c : node.content) s += "<li>" + c.accept(this);
return s + "</ul>";
}
}
}
public String text(Text node) {
return node.text;
}
}));
};
reader.setOnload(eventListener);
reader.readAsArrayBuffer(f);
}
list.setInnerHTML("<ul>" + out + "</ul>");
}
static <T> T js(Object jso, String property, Class<T> as) { return js(jso, property); }
static boolean def(Object jso, String property) { return js(jso, property) != null; }
static native <T> T js(Object jso, String property) /*-{
return property.split(".").reduce(function (acc, x) {
return acc && acc[x];
}, jso);
}-*/;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment