RegionGroup.java

00001 package edu.stanford.hci.r3.paper.layout;
00002 
00003 import java.awt.geom.Rectangle2D;
00004 import java.util.ArrayList;
00005 import java.util.HashMap;
00006 import java.util.List;
00007 import java.util.Map;
00008 
00009 import edu.stanford.hci.r3.paper.Region;
00010 import edu.stanford.hci.r3.units.Inches;
00011 import edu.stanford.hci.r3.units.Units;
00012 import edu.stanford.hci.r3.units.coordinates.Coordinates;
00013 
00030 public class RegionGroup {
00031 
00032         private static final Inches MY_UNITS = new Inches();
00033 
00034         private double maxXInches = Double.MIN_VALUE;
00035 
00036         private double maxYInches = Double.MIN_VALUE;
00037 
00038         private double minXInches = Double.MAX_VALUE;
00039 
00040         private double minYInches = Double.MAX_VALUE;
00041 
00042         private String name;
00043 
00044         private List<Region> regions = new ArrayList<Region>();
00045 
00049         private Map<Region, Coordinates> regionsAndRelativeLocations = new HashMap<Region, Coordinates>();
00050 
00051         private Units xOffset;
00052 
00053         private Units yOffset;
00054 
00058         public RegionGroup(String rgName, Units xOrigin, Units yOrigin) {
00059                 name = rgName;
00060                 xOffset = xOrigin;
00061                 yOffset = yOrigin.getUnitsObjectOfSameLengthIn(xOffset);
00062         }
00063 
00069         public void addRegion(Region childRegion, Coordinates relativeCoord) {
00070                 regionsAndRelativeLocations.put(childRegion, relativeCoord);
00071                 regions.add(childRegion);
00072 
00073                 // update the coordinates by taking the union of the current bounds with the
00074                 // bounds of the child (translated by relativeCoords)
00075                 final Rectangle2D childBounds = childRegion.getShape().getBounds2D();
00076                 final Units childUnits = childRegion.getUnits();
00077 
00078                 // change the child's bounds into bounds that can be interpreted in OUR units
00079                 final double c = childUnits.getConversionTo(MY_UNITS);
00080                 // DebugUtils.println("Conversion is: " + c);
00081                 final Rectangle2D childBoundsInOurUnits = new Rectangle2D.Double( //
00082                                 childBounds.getX() * c, childBounds.getY() * c, //
00083                                 childBounds.getWidth() * childRegion.getScaleX() * c, //
00084                                 childBounds.getHeight() * childRegion.getScaleY() * c);
00085 
00086                 // the origin of the child RELATIVE TO THIS GROUP childOrigin + childOffset
00087                 final double childXTranslated = childBoundsInOurUnits.getX()
00088                                 + relativeCoord.getX().getValueIn(MY_UNITS);
00089                 final double childYTranslated = childBoundsInOurUnits.getY()
00090                                 + relativeCoord.getY().getValueIn(MY_UNITS);
00091 
00092                 final Rectangle2D childBoundsTranslated = new Rectangle2D.Double( //
00093                                 childXTranslated, childYTranslated, //
00094                                 childBoundsInOurUnits.getWidth(), //
00095                                 childBoundsInOurUnits.getHeight());
00096 
00097                 // take the union our bounds...
00098                 maxXInches = Math.max(maxXInches, childXTranslated + childBoundsTranslated.getWidth());
00099                 maxYInches = Math.max(maxYInches, childYTranslated + childBoundsTranslated.getHeight());
00100                 minXInches = Math.min(minXInches, childXTranslated);
00101                 minYInches = Math.min(minYInches, childYTranslated);
00102         }
00103 
00104         public Units getHeight() {
00105                 return new Inches(maxYInches);
00106         }
00107 
00111         public String getName() {
00112                 return name;
00113         }
00114 
00119         public Coordinates getRegionOffset(Region child) {
00120                 return regionsAndRelativeLocations.get(child);
00121         }
00122 
00126         public List<Region> getRegions() {
00127                 return regions;
00128         }
00129 
00133         public Units getWidth() {
00134                 return new Inches(maxXInches);
00135         }
00136 
00140         public double getXOffsetInInches() {
00141                 return xOffset.getValueInInches();
00142         }
00143 
00147         public double getYOffsetInInches() {
00148                 return yOffset.getValueInInches();
00149         }
00150 }

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