1 package jimm.datavision.layout; 2 import jimm.datavision.*; 3 import jimm.datavision.field.Field; 4 import jimm.datavision.field.ImageField; 5 import java.io.PrintWriter ; 6 import java.util.*; 7 8 18 public abstract class SortedLayoutEngine extends LayoutEngine { 19 20 protected HashMap sectionFields; 21 protected Comparator comp; 22 23 26 public SortedLayoutEngine() { 27 this(null); 28 } 29 30 35 public SortedLayoutEngine(PrintWriter out) { 36 super(out); 37 sectionFields = new HashMap(); 38 39 comp = new Comparator() { 41 public int compare(Object o1, Object o2) { 42 double y1 = ((Field)o1).getBounds().y; 43 double y2 = ((Field)o2).getBounds().y; 44 if (y1 == y2) { 45 double x1 = ((Field)o1).getBounds().x; 46 double x2 = ((Field)o2).getBounds().x; 47 return (x1 < x2) ? -1 : ((x1 > x2) ? 1 : 0); 48 } 49 else 50 return (y1 < y2) ? -1 : ((y1 > y2) ? 1 : 0); 51 } 52 }; 53 } 54 55 62 protected void doOutputSection(Section sect) { 63 Object [] fields = (Object [])sectionFields.get(sect); 64 if (fields == null) 65 fields = buildSectionFields(sect); 66 67 if (fields != null) { 68 for (int i = 0; i < fields.length; ++i) { 70 Field f = (Field)fields[i]; 71 if (f.isVisible()) { 72 if (f instanceof ImageField) 73 outputImage((ImageField)f); 74 else 75 outputField(f); 76 } 77 } 78 } 79 for (Iterator iter = sect.lines(); iter.hasNext(); ) { 81 Line l = (Line)iter.next(); 82 if (l.isVisible()) outputLine(l); 83 } 84 } 85 86 94 protected Object [] buildSectionFields(Section sect) { 95 int numFields = sect.numFields(); 96 if (numFields == 0) 97 return null; 98 99 Object [] fields = sect.fieldsSortedBy(comp); 100 sectionFields.put(sect, fields); 101 102 return fields; 103 } 104 105 } 106 | Popular Tags |