Sheet.java

00001 package edu.stanford.hci.r3.paper;
00002 
00003 import java.io.File;
00004 import java.util.ArrayList;
00005 import java.util.HashMap;
00006 import java.util.HashSet;
00007 import java.util.List;
00008 import java.util.Map;
00009 import java.util.Set;
00010 
00011 import edu.stanford.hci.r3.Application;
00012 import edu.stanford.hci.r3.PaperToolkit;
00013 import edu.stanford.hci.r3.pattern.coordinates.PatternLocationToSheetLocationMapping;
00014 import edu.stanford.hci.r3.render.SheetRenderer;
00015 import edu.stanford.hci.r3.tools.design.acrobat.RegionConfiguration;
00016 import edu.stanford.hci.r3.units.Inches;
00017 import edu.stanford.hci.r3.units.Size;
00018 import edu.stanford.hci.r3.units.Units;
00019 import edu.stanford.hci.r3.units.coordinates.Coordinates;
00020 
00049 public class Sheet {
00050 
00051         private static final String INDENT = "   ";
00052 
00058         private Set<File> configurationPaths = new HashSet<File>();
00059 
00063         private String name = "A Sheet";
00064 
00070         private PatternLocationToSheetLocationMapping patternLocationToSheetLocationMapping;
00071 
00075         private Map<String, Region> regionNameToRegionObject = new HashMap<String, Region>();
00076 
00081         private List<Region> regions = new ArrayList<Region>();
00082 
00086         private Map<Region, Coordinates> regionsAndRelativeLocations = new HashMap<Region, Coordinates>();
00087 
00091         private Size size = new Size();
00092 
00093         private Application parentApp;
00094 
00098         public Sheet() {
00099                 this(new Inches(8.5), new Inches(11.0));
00100         }
00101 
00108         public Sheet(double widthInches, double heightInches) {
00109                 this(new Inches(widthInches), new Inches(heightInches));
00110         }
00111 
00118         public Sheet(Units w, Units h) {
00119                 setSize(w, h);
00120         }
00121 
00127         public void addConfigurationPath(File configPath) {
00128                 // register the fact that we loaded a configuration file from this directory
00129                 configurationPaths.add(configPath);
00130                 // DebugUtils.println("Configuration Paths: " + configurationPaths);
00131         }
00132 
00137         public void addRegion(Region r) {
00138                 regions.add(r);
00139                 regionNameToRegionObject.put(r.getName(), r);
00140                 r.setParentSheet(this);
00141         }
00142 
00150         public void addRegion(Region r, Units xOffset, Units yOffset) {
00151                 addRegion(r);
00152                 setRegionOffset(r, xOffset, yOffset);
00153         }
00154 
00162         public void addRegions(File regionConfigurationFile) {
00163                 final RegionConfiguration rc = (RegionConfiguration) PaperToolkit.fromXML(regionConfigurationFile);
00164 
00165                 // make sure the size of the sheet matches this object...
00166                 // otherwise, raise a warning flag
00167                 if (!rc.getHeight().equals(getHeight()) || !rc.getWidth().equals(getWidth())) {
00168                         System.err.println("Sheet::addRegions(regionConfigurationFile): Warning! "
00169                                         + "This region configuration file was made for a sheet of a different size. "
00170                                         + "RegionConfiguration: [" + rc.getWidth() + "," + rc.getHeight() + "] versus "
00171                                         + "This Sheet: [" + getWidth() + "," + getHeight() + "]");
00172                         System.err.println("We will proceed.... but you have been warned. =)");
00173                 }
00174 
00175                 final List<Region> theRegions = rc.getRegions();
00176                 for (Region r : theRegions) {
00177                         addRegion(r);
00178                 }
00179 
00180                 addConfigurationPath(regionConfigurationFile.getParentFile());
00181         }
00182 
00187         public boolean containsRegion(Region r) {
00188                 return regions.contains(r);
00189         }
00190 
00194         public Set<File> getConfigurationPaths() {
00195                 return configurationPaths;
00196         }
00197 
00201         public Units getHeight() {
00202                 return size.getHeight();
00203         }
00204 
00208         public String getName() {
00209                 return name;
00210         }
00211 
00215         public PatternLocationToSheetLocationMapping getPatternLocationToSheetLocationMapping() {
00216                 if (patternLocationToSheetLocationMapping == null) {
00217                         // in case no one has set this object, we will ensure that it is not null...
00218                         setPatternLocationToSheetLocationMapping(new PatternLocationToSheetLocationMapping(this));
00219                 }
00220                 return patternLocationToSheetLocationMapping;
00221         }
00222 
00230         public PatternLocationToSheetLocationMapping getPatternLocationToSheetLocationMapping(File patternInfoFile) {
00231                 setPatternLocationToSheetLocationMapping(new PatternLocationToSheetLocationMapping(this,
00232                                 patternInfoFile));
00233                 return patternLocationToSheetLocationMapping;
00234         }
00235 
00241         public Region getRegion(String regionName) {
00242                 return regionNameToRegionObject.get(regionName);
00243         }
00244 
00248         public List<String> getRegionNames() {
00249                 final List<String> names = new ArrayList<String>();
00250                 for (Region r : regions) {
00251                         names.add(r.getName());
00252                 }
00253                 return names;
00254         }
00255 
00261         public Coordinates getRegionOffset(Region r) {
00262                 final Coordinates coordinates = regionsAndRelativeLocations.get(r);
00263                 if (coordinates == null) {
00264                         return new Coordinates(new Inches(0), new Inches(0));
00265                 }
00266                 return coordinates;
00267         }
00268 
00272         public List<Region> getRegions() {
00273                 return regions;
00274         }
00275 
00279         public SheetRenderer getRenderer() {
00280                 return new SheetRenderer(this);
00281         }
00282 
00286         public Size getSize() {
00287                 return size.clone();
00288         }
00289 
00293         public Units getWidth() {
00294                 return size.getWidth();
00295         }
00296 
00300         public void setName(String n) {
00301                 name = n;
00302         }
00303 
00311         public void setPatternLocationToSheetLocationMapping(PatternLocationToSheetLocationMapping mapping) {
00312                 patternLocationToSheetLocationMapping = mapping;
00313         }
00314 
00320         public void setRegionOffset(Region r, Units xOffset, Units yOffset) {
00321                 regionsAndRelativeLocations.put(r, new Coordinates(xOffset, yOffset));
00322         }
00323 
00328         protected void setSize(Units width, Units height) {
00329                 size.setSize(width, height);
00330         }
00331 
00335         public String toDetailedString() {
00336                 final StringBuilder sb = new StringBuilder();
00337 
00338                 // the indent
00339                 String i = INDENT;
00340 
00341                 sb.append("Sheet {\n");
00342                 sb.append(i + "Size: " + getWidth() + " x " + getHeight() + "\n");
00343 
00344                 sb.append(i + "Regions: " + regions.size() + " {\n");
00345 
00346                 // indent twice
00347                 i = INDENT + INDENT;
00348 
00349                 for (Region r : regions) {
00350                         sb.append(i + r.toString() + "\n");
00351                 }
00352 
00353                 i = INDENT;
00354 
00355                 sb.append(i + "}\n");
00356                 sb.append("}\n");
00357                 return sb.toString();
00358         }
00359 
00363         public String toString() {
00364                 return "Sheet { name: [" + name + "] size: [" + getWidth() + " x " + getHeight() + "] numRegions: ["
00365                                 + regions.size() + "]}";
00366         }
00367 
00371         public String toXML() {
00372                 return PaperToolkit.toXML(this);
00373         }
00374 
00375         public void setParentApplication(Application application) {
00376                 parentApp = application;
00377         }
00378         public Application getParentApplication() {
00379                 return parentApp;
00380         }
00381 }

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