00001 package edu.stanford.hci.r3; 00002 00003 import java.io.File; 00004 import java.util.ArrayList; 00005 import java.util.Collection; 00006 import java.util.List; 00007 00008 import javax.swing.filechooser.FileSystemView; 00009 00010 import edu.stanford.hci.r3.devices.Device; 00011 import edu.stanford.hci.r3.paper.Sheet; 00012 import edu.stanford.hci.r3.pattern.coordinates.PatternLocationToSheetLocationMapping; 00013 import edu.stanford.hci.r3.pen.PenInput; 00014 import edu.stanford.hci.r3.pen.batch.BatchedEventHandler; 00015 import edu.stanford.hci.r3.render.SheetRenderer; 00016 import edu.stanford.hci.r3.tools.debug.DebuggingEnvironment; 00017 import edu.stanford.hci.r3.util.DebugUtils; 00018 00042 public class Application { 00043 00048 private List<BatchedEventHandler> batchEventHandlers = new ArrayList<BatchedEventHandler>(); 00049 00050 private DebuggingEnvironment debuggingEnvironment; 00051 00056 private List<Device> devices = new ArrayList<Device>(); 00057 00061 private PaperToolkit host = null; 00062 00066 private boolean isRunning = false; 00067 00072 private String name; 00073 00079 private List<PenInput> penInputDevices = new ArrayList<PenInput>(); 00080 00087 private List<Sheet> sheets = new ArrayList<Sheet>(); 00088 00092 private boolean userChoosesPDFDestination = false; 00093 00097 public Application(String theName) { 00098 name = theName; 00099 } 00100 00104 public void addBatchEventHandler(BatchedEventHandler beh) { 00105 batchEventHandlers.add(beh); 00106 } 00107 00111 public void addDevice(Device dev) { 00112 devices.add(dev); 00113 } 00114 00120 public void addPenInput(PenInput penInputDevice) { 00121 penInputDevices.add(penInputDevice); 00122 } 00123 00135 public void addSheet(Sheet sheet) { 00136 // ensure that a mapping object is created 00137 final PatternLocationToSheetLocationMapping mapping = sheet 00138 .getPatternLocationToSheetLocationMapping(); 00139 addSheetObjectToInternalList(sheet); 00140 registerMappingWithEventEngineDuringRuntime(mapping); 00141 00142 sheet.setParentApplication(this); 00143 } 00144 00152 public void addSheet(Sheet sheet, File patternInfoFile) { 00153 // ensure that a mapping object is created from this file 00154 PatternLocationToSheetLocationMapping mapping = sheet 00155 .getPatternLocationToSheetLocationMapping(patternInfoFile); 00156 addSheetObjectToInternalList(sheet); 00157 registerMappingWithEventEngineDuringRuntime(mapping); 00158 } 00159 00167 public void addSheet(Sheet sheet, PatternLocationToSheetLocationMapping patternToSheetMapping) { 00168 sheet.setPatternLocationToSheetLocationMapping(patternToSheetMapping); 00169 addSheetObjectToInternalList(sheet); 00170 registerMappingWithEventEngineDuringRuntime(patternToSheetMapping); 00171 } 00172 00179 private void addSheetObjectToInternalList(Sheet sheet) { 00180 if (sheets.contains(sheet)) { 00181 DebugUtils.println("Already added this sheet: " + sheet); 00182 } else { 00183 DebugUtils.println("Adding Sheet: " + sheet); 00184 sheets.add(sheet); 00185 } 00186 } 00187 00196 public List<BatchedEventHandler> getBatchEventHandlers() { 00197 return batchEventHandlers; 00198 } 00199 00200 public DebuggingEnvironment getDebuggingEnvironment() { 00201 return debuggingEnvironment; 00202 } 00203 00207 public PaperToolkit getHostToolkit() { 00208 return host; 00209 } 00210 00214 public String getName() { 00215 return name; 00216 } 00217 00224 public Collection<PatternLocationToSheetLocationMapping> getPatternMaps() { 00225 Collection<PatternLocationToSheetLocationMapping> map = new ArrayList<PatternLocationToSheetLocationMapping>(); 00226 for (Sheet s : getSheets()) { 00227 map.add(s.getPatternLocationToSheetLocationMapping()); 00228 } 00229 return map; 00230 } 00231 00235 public List<PenInput> getPenInputDevices() { 00236 return penInputDevices; 00237 } 00238 00242 public List<Sheet> getSheets() { 00243 return sheets; 00244 } 00245 00250 protected void initializeBeforeStarting() { 00251 // do nothing, unless it is overridden. 00252 } 00253 00257 public boolean isRunning() { 00258 return isRunning; 00259 } 00260 00264 public boolean isUserChoosingDestinationForPDF() { 00265 return userChoosesPDFDestination; 00266 } 00267 00274 private void registerMappingWithEventEngineDuringRuntime(PatternLocationToSheetLocationMapping mapping) { 00275 if (isRunning) { 00276 // tell the already-running event engine to be aware of this new pattern mapping! 00277 getHostToolkit().getEventEngine().registerPatternMapForEventHandling(mapping); 00278 } 00279 } 00280 00284 public void removeAllSheets() { 00285 sheets.clear(); 00286 } 00287 00291 public void removeBatchEventHandler(BatchedEventHandler beh) { 00292 batchEventHandlers.remove(beh); 00293 } 00294 00301 public void removeSheet(Sheet sheet) { 00302 if (sheets.contains(sheet)) { 00303 sheets.remove(sheet); 00304 } 00305 00306 // unregister this sheet's mapping information 00307 // so that the EventEngine won't dispatch events to this sheet. 00308 if (isRunning) { 00309 getHostToolkit().getEventEngine().unregisterPatternMapForEventHandling( 00310 sheet.getPatternLocationToSheetLocationMapping()); 00311 } 00312 } 00313 00318 public void renderToPDF() { 00319 renderToPDF(FileSystemView.getFileSystemView().getHomeDirectory(), getName()); 00320 } 00321 00339 public void renderToPDF(File parentDirectory, String fileNameWithoutExtension) { 00340 if (sheets.size() == 1) { 00341 DebugUtils.println("Rendering PDF..."); 00342 final Sheet sheet = sheets.get(0); 00343 final File destPDFFile = new File(parentDirectory, fileNameWithoutExtension + ".pdf"); 00344 System.out.println("Rendering: " + destPDFFile.getAbsolutePath()); 00345 final SheetRenderer renderer = sheet.getRenderer(); 00346 renderer.renderToPDF(destPDFFile); 00347 } else { 00348 DebugUtils.println("Rendering PDFs..."); 00349 for (int i = 0; i < sheets.size(); i++) { 00350 final Sheet sheet = sheets.get(i); 00351 final File destPDFFile = new File(parentDirectory, fileNameWithoutExtension + "_Sheet_" + i 00352 + ".pdf"); 00353 System.out.println("Rendering: " + destPDFFile.getAbsolutePath()); 00354 final SheetRenderer renderer = sheet.getRenderer(); 00355 renderer.renderToPDF(destPDFFile); 00356 } 00357 } 00358 00359 } 00360 00361 public void setDebuggingEnvironment(DebuggingEnvironment debugEnvironment) { 00362 debuggingEnvironment = debugEnvironment; 00363 } 00364 00372 public void setHostToolkit(PaperToolkit toolkit) { 00373 host = toolkit; 00374 if (host != null) { 00375 setRunning(true); 00376 } else { 00377 setRunning(false); 00378 } 00379 } 00380 00387 private void setRunning(boolean flag) { 00388 isRunning = flag; 00389 } 00390 00394 public void setUserChoosesPDFDestinationFlag(boolean flag) { 00395 userChoosesPDFDestination = flag; 00396 } 00397 00401 public String toString() { 00402 return name + " Application"; 00403 } 00404 }