00001 package edu.stanford.hci.r3.tools.debug;
00002
00003 import java.io.File;
00004 import java.util.List;
00005
00006 import edu.stanford.hci.r3.Application;
00007 import edu.stanford.hci.r3.PaperToolkit;
00008 import edu.stanford.hci.r3.events.EventHandler;
00009 import edu.stanford.hci.r3.flash.FlashCommunicationServer;
00010 import edu.stanford.hci.r3.flash.FlashListener;
00011 import edu.stanford.hci.r3.paper.Region;
00012 import edu.stanford.hci.r3.paper.Sheet;
00013 import edu.stanford.hci.r3.util.DebugUtils;
00014
00027 public class DebuggingEnvironment {
00028
00029 public static String escapeLiteral(String s) {
00030 return s.replace("&", "&").replace("<", "<").replace(">", ">");
00031 }
00032
00033 private Application app;
00034 private DebugPCanvas canvas;
00035 private FlashCommunicationServer flash;
00036
00037 private DebugFrame frame;
00038
00042 public DebuggingEnvironment(Application paperApp) {
00043 app = paperApp;
00044
00045 DebugUtils.println("Starting to debug " + app);
00046
00047
00048 startFlashDebugger();
00049 }
00050
00054 private void sendAppLayout() {
00055 List<Sheet> sheets = app.getSheets();
00056 DebugUtils.println("Number of Sheets: " + sheets.size());
00057
00058 StringBuilder msg = new StringBuilder();
00059 msg.append("<app>");
00060 for (Sheet s : sheets) {
00061 msg.append("<sheet w=\"" + s.getWidth().getValueInInches() + "\" h=\""
00062 + s.getHeight().getValueInInches() + "\">");
00063 for (Region r : s.getRegions()) {
00064 double rX = r.getOriginX().getValueInInches();
00065 double rY = r.getOriginY().getValueInInches();
00066 double rWidth = r.getWidth().getValueInInches();
00067 double rHeight = r.getHeight().getValueInInches();
00068 msg.append("<region name='" + r.getName() + "'x=\"" + rX + "\" y=\"" + rY + "\" w=\""
00069 + rWidth + "\" h=\"" + rHeight + "\">");
00070
00071 List<EventHandler> eventHandlers = r.getEventHandlers();
00072 for (EventHandler eh : eventHandlers) {
00073
00074
00075 String eventType = eh.getClass().getSuperclass().getSimpleName();
00076 String container = eh.getClass().toString();
00077 container = container.substring(0, container.indexOf("$"));
00078
00079 msg.append("<eventhandler name='" + eventType + "' location='" + container + "'/>");
00080 }
00081 msg.append("</region>");
00082 }
00083 msg.append("</sheet>");
00084 }
00085 msg.append("</app>");
00086 flash.sendMessage(msg.toString());
00087 }
00088
00092 private void sendApplicationLayout() {
00093 flash.sendMessage("<loadingapplication/>");
00094 sendAppLayout();
00095 }
00096
00097 public void showFlashView() {
00098
00099 File r3RootPath = PaperToolkit.getToolkitRootPath();
00100 final File eventVizHTML = new File(r3RootPath, "flash/bin/EventViz.html");
00101 flash.openFlashGUI(eventVizHTML);
00102 flash.removeAllFlashClientListeners();
00103 flash.addFlashClientListener(new FlashListener() {
00104 @Override
00105 public void messageReceived(String command) {
00106 if (command.equals("eventvizclient connected")) {
00107 DebugUtils.println("Flash Client Connected!");
00108 sendApplicationLayout();
00109 } else if (command.equals("load most recent pattern mappings")) {
00110 app.getHostToolkit().loadMostRecentPatternMappings();
00111 }
00112 }
00113 });
00114 }
00115
00119 private void startFlashDebugger() {
00120
00121 flash = new FlashCommunicationServer();
00122 showFlashView();
00123 }
00124
00130 @Deprecated
00131 private void startPiccoloDebugView(Application paperApp) {
00132
00133 frame = new DebugFrame(paperApp.getName());
00134 canvas = frame.getCanvas();
00135
00136
00137 canvas.addVisualComponents(paperApp);
00138 }
00139
00144 public void visualize(String msg, Region r, String code) {
00145 String message = "<showMe msg='" + msg + "' regionName='" + r.getName() + "'";
00146 if (code != null)
00147 message += "><code>" + escapeLiteral(code) + "</code></showMe>";
00148 else
00149 message += "/>";
00150 flash.sendMessage(message);
00151 }
00152
00153 }