EventHandler.java

00001 package edu.stanford.hci.r3.events;
00002 
00003 import java.io.File;
00004 import java.io.IOException;
00005 import java.util.ArrayList;
00006 import java.util.List;
00007 
00008 import edu.stanford.hci.r3.Application;
00009 import edu.stanford.hci.r3.paper.Region;
00010 import edu.stanford.hci.r3.paper.Sheet;
00011 import edu.stanford.hci.r3.tools.debug.DebuggingEnvironment;
00012 import edu.stanford.hci.r3.tools.debug.Utils;
00013 import edu.stanford.hci.r3.util.DebugUtils;
00014 
00053 public abstract class EventHandler {
00054 
00059         protected List<Region> parentRegions = new ArrayList<Region>();
00060 
00061         public void addParentRegion(Region r) {
00062                 parentRegions.add(r);
00063         }
00064 
00065         public List<Region> getParentRegions() {
00066                 return parentRegions;
00067         }
00068 
00073         public abstract void handleEvent(PenEvent event);
00074 
00075         public void showMe(String message) {
00076                 
00077                 for (Region r : parentRegions) {
00078                         // DebugUtils.println(r);
00079                         
00080                         Sheet s = r.getParentSheet();
00081                         Application a = s.getParentApplication();
00082                         DebugUtils.println("Hosted in: " + a.getName());
00083                         
00084                         Thread currThread = Thread.currentThread();
00085                         StackTraceElement[] trace = currThread.getStackTrace();
00086                         String code = null;
00087                         for (StackTraceElement ste : trace) {
00088                                 String method = ste.getMethodName();
00089                                 String className = ste.getClassName(); 
00090                                 if (method.equals("showMe") || className.startsWith("java."))
00091                                         continue;
00092                                 String filename = "src/"+className.replace('.', '/');
00093                                 int innerclass = filename.indexOf('$');
00094                                 if (innerclass>=0)
00095                                         filename = filename.substring(0,innerclass);
00096                                 filename += ".java";
00097                                 try {
00098                                         int line = ste.getLineNumber()-1;
00099                                         List<String> lines = Utils.getLines(new File(filename));
00100                                         code = filename+":"+ste.getLineNumber()+"\n";
00101                                         for (int i=Math.max(0, line-3); 
00102                                                          i<Math.min(line+4, lines.size()-1); 
00103                                                          i++)
00104                                                 code += (i+1)+": "+lines.get(i)+"\n";
00105                                         
00106                                 } catch (IOException ex) {
00107                                         ex.printStackTrace();
00108                                 }
00109                                 break;
00110                         }
00111                         
00112                         DebuggingEnvironment d = a.getDebuggingEnvironment();
00113                         if (d != null) {
00114                                 d.visualize(message, r, code);
00115                         }
00116                 }
00117         }
00118 
00122         public abstract String toString();
00123 
00124 }

Generated on Sat Apr 14 18:21:34 2007 for R3 Paper Toolkit by  doxygen 1.4.7