InkAPIBrowser.java

00001 package edu.stanford.hci.r3.tools.develop.inkapibrowser;
00002 
00003 import java.awt.Desktop;
00004 import java.io.File;
00005 import java.io.IOException;
00006 import java.lang.reflect.InvocationTargetException;
00007 import java.lang.reflect.Method;
00008 import java.util.ArrayList;
00009 import java.util.HashMap;
00010 import java.util.List;
00011 
00012 import javax.swing.filechooser.FileSystemView;
00013 
00014 import edu.stanford.hci.r3.PaperToolkit;
00015 import edu.stanford.hci.r3.flash.FlashCommunicationServer;
00016 import edu.stanford.hci.r3.flash.FlashListener;
00017 import edu.stanford.hci.r3.pen.batch.PenSynch;
00018 import edu.stanford.hci.r3.pen.batch.PenSynchManager;
00019 import edu.stanford.hci.r3.pen.ink.Ink;
00020 import edu.stanford.hci.r3.pen.ink.InkUtils;
00021 import edu.stanford.hci.r3.util.ArrayUtils;
00022 import edu.stanford.hci.r3.util.DebugUtils;
00023 import edu.stanford.hci.r3.util.files.FileUtils;
00024 
00044 public class InkAPIBrowser {
00045 
00046         public static void main(String[] args) {
00047                 new InkAPIBrowser();
00048         }
00049 
00050         private int currentSynchedFileIndex = -1;
00051         private FlashCommunicationServer flash;
00052         private List<File> synchedFiles;
00053         private List<Method> exposedMethods;
00054         private HashMap<String, Method> methodsHashMap = new HashMap<String, Method>();
00055         private Ink mostRecentInkObject;
00056 
00060         public InkAPIBrowser() {
00061                 PenSynchManager penSynchManager = new PenSynchManager();
00062                 synchedFiles = penSynchManager.getFiles();
00063                 // DebugUtils.println(synchedFiles);
00064 
00065                 // use Flash as our GUI
00066                 startFlashAPIBrowser();
00067         }
00068 
00072         private File getCurrentFile() {
00073                 if (currentSynchedFileIndex < 0) {
00074                         // if we never advanced...
00075                         currentSynchedFileIndex = 0;
00076                 }
00077                 return synchedFiles.get(currentSynchedFileIndex);
00078         }
00079 
00083         private void nextFile() {
00084                 currentSynchedFileIndex++;
00085                 if (currentSynchedFileIndex == synchedFiles.size()) {
00086                         currentSynchedFileIndex = 0;
00087                 }
00088                 DebugUtils.println(getCurrentFile().getName());
00089         }
00090 
00094         private void prevFile() {
00095                 currentSynchedFileIndex--;
00096                 if (currentSynchedFileIndex < 0) {
00097                         currentSynchedFileIndex = synchedFiles.size() - 1;
00098                 }
00099                 DebugUtils.println(getCurrentFile().getName());
00100         }
00101 
00105         protected void saveInkFromCurrentFileToDiskAndDisplayIt() {
00106                 PenSynch penSynch = new PenSynch(getCurrentFile());
00107                 List<Ink> importedInk = penSynch.getImportedInk();
00108                 int countOfInk = 0;
00109                 List<File> renderedImages = new ArrayList<File>();
00110                 for (Ink ink : importedInk) {
00111                         DebugUtils.println(ink.getName());
00112                         ink.setName(ink.getName() + countOfInk);
00113                         File file = ink.renderToJPEGFile();
00114                         renderedImages.add(file);
00115                         // DebugUtils.println("Sending ink from: " + ink.getSourcePageAddress());
00116                         flash.sendMessage("Rendered to file: " + file.getAbsolutePath());
00117                         countOfInk++;
00118                 }
00119 
00120                 String html = FileUtils.readFileIntoStringBuffer(
00121                                 PaperToolkit.getResourceFile("/templates/Preview.html")).toString();
00122                 StringBuilder sb = new StringBuilder();
00123                 for (File f : renderedImages) {
00124                         sb.append("<img src=\"file:///C|/Documents and Settings/Ron Yeh/Desktop/" + f.getName()
00125                                         + "\"/>");
00126                 }
00127                 html = html.replace("__IMAGES__", sb.toString());
00128 
00129                 File homeDir = FileSystemView.getFileSystemView().getHomeDirectory();
00130                 File destFile = new File(homeDir, "Preview.html");
00131                 FileUtils.writeStringToFile(html, destFile);
00132                 try {
00133                         Desktop.getDesktop().browse(destFile.toURI());
00134                 } catch (IOException e) {
00135                         e.printStackTrace();
00136                 }
00137         }
00138 
00142         private void sendInkFromCurrentFile() {
00143                 PenSynch penSynch = new PenSynch(getCurrentFile());
00144                 List<Ink> importedInk = penSynch.getImportedInk();
00145                 for (Ink ink : importedInk) {
00146                         DebugUtils.println("Sending ink from: " + ink.getSourcePageAddress());
00147                         flash.sendMessage(ink.getAsXML(false));
00148 
00149                         mostRecentInkObject = ink;
00150                 }
00151         }
00152 
00156         private void sendListOfExposedMethods() {
00157                 StringBuilder sb = new StringBuilder();
00158                 sb.append("<methods>");
00159 
00160                 // add methods to a hashtable from name --> method object
00161 
00162                 // enumerate InkUtils
00163                 exposedMethods = InkUtils.getExposedMethods();
00164                 for (Method m : exposedMethods) {
00165                         sb.append("<method name='" + m.getName() + "' className='"
00166                                         + m.getDeclaringClass().getSimpleName() + "'/>");
00167 
00168                         // TODO: Will have to fix the lowercase problem =)
00169                         methodsHashMap.put(m.getName().toLowerCase(), m);
00170                 }
00171                 sb.append("</methods>");
00172                 flash.sendMessage(sb.toString());
00173         }
00174 
00175         public void showFlashView() {
00176                 // start the Flash GUI
00177                 File r3RootPath = PaperToolkit.getToolkitRootPath();
00178                 final File apiBrowserTML = new File(r3RootPath, "flash/bin/APIBrowserDefault.html");
00179                 flash.openFlashGUI(apiBrowserTML);
00180                 flash.removeAllFlashClientListeners(); // HACK: for now..., so we always have one flash
00181                 // listener
00182                 flash.addFlashClientListener(new FlashListener() {
00183                         @Override
00184                         public void messageReceived(String command) {
00185                                 if (command.equals("apibrowserclient connected")) {
00186                                         DebugUtils.println("Flash Client Connected!");
00187                                         sendListOfExposedMethods();
00188                                 } else if (command.equals("next")) {
00189                                         nextFile();
00190                                         sendInkFromCurrentFile();
00191                                 } else if (command.equals("prev")) {
00192                                         prevFile();
00193                                         sendInkFromCurrentFile();
00194                                 } else if (command.equals("saveimage")) {
00195                                         saveInkFromCurrentFileToDiskAndDisplayIt();
00196                                 } else if (command.startsWith("callmethods")) {
00197                                         String listOfCommands = command.substring(command.indexOf("[") + 1, command
00198                                                         .indexOf("]"));
00199                                         String[] commands = listOfCommands.split(",");
00200                                         callTheseMethods(commands);
00201                                 } else {
00202                                         DebugUtils.println("Unhandled command: " + command);
00203                                 }
00204                         }
00205 
00206                 });
00207         }
00208 
00212         private void callTheseMethods(String[] commands) {
00213                 ArrayUtils.printArray(commands);
00214 
00215                 Ink ink = mostRecentInkObject;
00216                 if (ink == null) {
00217                         return;
00218                 }
00219 
00220                 Ink inkResult = new Ink();
00221                 for (String methodName : commands) {
00222                         Method method = methodsHashMap.get(methodName);
00223                         try {
00224                                 inkResult = (Ink) method.invoke(null, ink);
00225                         } catch (IllegalArgumentException e) {
00226                                 e.printStackTrace();
00227                         } catch (IllegalAccessException e) {
00228                                 e.printStackTrace();
00229                         } catch (InvocationTargetException e) {
00230                                 e.printStackTrace();
00231                         }
00232                 }
00233 
00234                 DebugUtils.println("Original Ink: " + ink.getNumStrokes());
00235                 DebugUtils.println("New Ink: " + inkResult.getNumStrokes());
00236                 
00237                 // send this ink back to the Flash GUI, to highlight in red!
00238                 flash.sendMessage("<highlight>" + inkResult.getAsXML(false) + "</highlight>");
00239         }
00240 
00244         private void startFlashAPIBrowser() {
00245                 // Start the local messaging server
00246                 flash = new FlashCommunicationServer();
00247                 showFlashView();
00248         }
00249 }

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