1 package jimm.datavision; 2 import jimm.util.XMLWriter; 3 import java.awt.print.Paper ; 4 import java.awt.print.PageFormat ; 5 import java.util.*; 6 7 13 public class PaperFormat extends Paper implements Nameable, Writeable { 14 15 public static final int PORTRAIT = 0; 16 public static final int LANDSCAPE = 1; 17 18 protected static final String RESOURCE_FILE_PREFIX = "paper"; 19 20 protected static HashMap[] orientations; 21 protected static TreeSet names; 22 protected static PaperFormat defaultPaper; 23 24 protected String name; protected int orientation; protected PageFormat pageFormat; 27 protected String latexPaperSizeString; 28 29 static { 30 orientations = new HashMap[2]; 31 orientations[PORTRAIT] = new HashMap(); 32 orientations[LANDSCAPE] = new HashMap(); 33 ArrayList nameList = new ArrayList(); 34 35 ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_FILE_PREFIX, 36 Locale.getDefault()); 37 try { 38 int numPaperSizes = Integer.parseInt(bundle.getString("num_paper_sizes")); 39 for (int i = 0; i < numPaperSizes; ++i) { 40 String vals = bundle.getString("paper" + i); 41 int pos1 = vals.indexOf(','); 42 int pos2 = vals.indexOf(',', pos1 + 1); 43 int pos3 = vals.indexOf(',', pos2 + 1); 44 int pos4 = vals.indexOf(',', pos3 + 1); 45 int pos5 = vals.indexOf(',', pos4 + 1); 46 if (pos1 == -1 || pos2 == -1 || pos3 == -1 || pos4 == -1) 47 continue; 49 double w = Double.parseDouble(vals.substring(pos1+1, pos2)); 50 double h = Double.parseDouble(vals.substring(pos2+1, pos3)); 51 double hMargin = Double.parseDouble(vals.substring(pos3+1, pos4)); 52 double vMargin = 0; 53 String latexPaperSizeString = null; 54 if (pos5 == -1) 55 vMargin = Double.parseDouble(vals.substring(pos4+1)); 56 else { 57 vMargin = Double.parseDouble(vals.substring(pos4+1, pos5)); 58 latexPaperSizeString = vals.substring(pos5+1); 59 } 60 61 String name = vals.substring(0, pos1); 62 nameList.add(name); 63 PaperFormat p = addPaper(PORTRAIT, name, w, h, hMargin, vMargin, 64 latexPaperSizeString); 65 addPaper(LANDSCAPE, name, h, w, vMargin, hMargin, 66 latexPaperSizeString); 67 if (i == 0) defaultPaper = p; 69 } 70 } 71 catch (Exception e) { ErrorHandler.error(e); 73 } 74 finally { 75 names = new TreeSet(nameList); 77 } 78 } 79 80 private static PaperFormat addPaper(int orientation, String name, 81 double w, double h, 82 double hMargin, double vMargin, 83 String latexPaperSizeString) 84 { 85 PaperFormat p = new PaperFormat(orientation, name, w, h, hMargin, 86 vMargin, latexPaperSizeString); 87 orientations[orientation].put(name, p); 88 return p; 89 } 90 91 92 public static PaperFormat get(int orientation, String name) { 93 return (PaperFormat)orientations[orientation].get(name); 94 } 95 96 public static PaperFormat getDefault() { 97 return defaultPaper; 98 } 99 100 public static Iterator names() { 101 return names.iterator(); 102 } 103 104 PaperFormat(int orientation, String name, double w, double h, double hMargin, 105 double vMargin, String latexPaperSizeString) 106 { 107 this.orientation = orientation; 108 this.name = name; 109 this.latexPaperSizeString = latexPaperSizeString; 110 setSize(w, h); 111 setImageableArea(hMargin, vMargin, w - hMargin / 2.0, h - vMargin / 2.0); 112 } 113 114 public int getOrientation() { return orientation; } 115 116 public String getName() { return name; } 117 118 119 public void setName(String name) { } 120 121 public String getLaTeXPaperSizeString() { return latexPaperSizeString; } 122 123 130 public PageFormat getPageFormat() { 131 if (pageFormat == null) { 132 pageFormat = new PageFormat (); 133 if (orientation == PORTRAIT) { 134 pageFormat.setOrientation(PageFormat.PORTRAIT); 135 pageFormat.setPaper(this); 136 } 137 else { 138 pageFormat.setOrientation(PageFormat.LANDSCAPE); 139 pageFormat.setPaper(get(PORTRAIT, getName())); 143 } 144 } 145 return pageFormat; 146 } 147 148 public void writeXML(XMLWriter out) { 149 out.startElement("paper"); 150 out.attr("name", name); 151 out.attr("orientation", 152 orientation == PORTRAIT ? "portrait" : "landscape"); 153 out.endElement(); 154 } 155 156 } 157 | Popular Tags |