001: package jimm.datavision;
002:
003: import jimm.util.XMLWriter;
004: import java.awt.print.Paper;
005: import java.awt.print.PageFormat;
006: import java.util.*;
007:
008: /**
009: * The class manages lists of paper sizes and instances represents specific
010: * paper sizes and orientations. Instances are immutable.
011: *
012: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
013: */
014: public class PaperFormat extends Paper implements Nameable, Writeable {
015:
016: public static final int PORTRAIT = 0;
017: public static final int LANDSCAPE = 1;
018:
019: protected static final String RESOURCE_FILE_PREFIX = "paper";
020:
021: protected static HashMap[] orientations;
022: protected static TreeSet names;
023: protected static PaperFormat defaultPaper;
024:
025: protected String name; // Immutable
026: protected int orientation; // PORTRAIT or LANDSCAPE; immutable
027: protected PageFormat pageFormat;
028: protected String latexPaperSizeString;
029:
030: static {
031: orientations = new HashMap[2];
032: orientations[PORTRAIT] = new HashMap();
033: orientations[LANDSCAPE] = new HashMap();
034: ArrayList nameList = new ArrayList();
035:
036: ResourceBundle bundle = ResourceBundle.getBundle(
037: RESOURCE_FILE_PREFIX, Locale.getDefault());
038: try {
039: int numPaperSizes = Integer.parseInt(bundle
040: .getString("num_paper_sizes"));
041: for (int i = 0; i < numPaperSizes; ++i) {
042: String vals = bundle.getString("paper" + i);
043: int pos1 = vals.indexOf(',');
044: int pos2 = vals.indexOf(',', pos1 + 1);
045: int pos3 = vals.indexOf(',', pos2 + 1);
046: int pos4 = vals.indexOf(',', pos3 + 1);
047: int pos5 = vals.indexOf(',', pos4 + 1);
048: if (pos1 == -1 || pos2 == -1 || pos3 == -1
049: || pos4 == -1)
050: continue; // pos5 is optional
051:
052: double w = Double.parseDouble(vals.substring(pos1 + 1,
053: pos2));
054: double h = Double.parseDouble(vals.substring(pos2 + 1,
055: pos3));
056: double hMargin = Double.parseDouble(vals.substring(
057: pos3 + 1, pos4));
058: double vMargin = 0;
059: String latexPaperSizeString = null;
060: if (pos5 == -1)
061: vMargin = Double.parseDouble(vals
062: .substring(pos4 + 1));
063: else {
064: vMargin = Double.parseDouble(vals.substring(
065: pos4 + 1, pos5));
066: latexPaperSizeString = vals.substring(pos5 + 1);
067: }
068:
069: String name = vals.substring(0, pos1);
070: nameList.add(name);
071: PaperFormat p = addPaper(PORTRAIT, name, w, h, hMargin,
072: vMargin, latexPaperSizeString);
073: addPaper(LANDSCAPE, name, h, w, vMargin, hMargin,
074: latexPaperSizeString);
075: if (i == 0) // Zero'th one is the default
076: defaultPaper = p;
077: }
078: } catch (Exception e) { // Number format exceptions, mostly
079: ErrorHandler.error(e);
080: } finally {
081: // Copy list of names to a sorted set
082: names = new TreeSet(nameList);
083: }
084: }
085:
086: private static PaperFormat addPaper(int orientation, String name,
087: double w, double h, double hMargin, double vMargin,
088: String latexPaperSizeString) {
089: PaperFormat p = new PaperFormat(orientation, name, w, h,
090: hMargin, vMargin, latexPaperSizeString);
091: orientations[orientation].put(name, p);
092: return p;
093: }
094:
095: public static PaperFormat get(int orientation, String name) {
096: return (PaperFormat) orientations[orientation].get(name);
097: }
098:
099: public static PaperFormat getDefault() {
100: return defaultPaper;
101: }
102:
103: public static Iterator names() {
104: return names.iterator();
105: }
106:
107: PaperFormat(int orientation, String name, double w, double h,
108: double hMargin, double vMargin, String latexPaperSizeString) {
109: this .orientation = orientation;
110: this .name = name;
111: this .latexPaperSizeString = latexPaperSizeString;
112: setSize(w, h);
113: setImageableArea(hMargin, vMargin, w - hMargin / 2.0, h
114: - vMargin / 2.0);
115: }
116:
117: public int getOrientation() {
118: return orientation;
119: }
120:
121: public String getName() {
122: return name;
123: }
124:
125: /** A paper format's name is immutable. */
126: public void setName(String name) {
127: }
128:
129: public String getLaTeXPaperSizeString() {
130: return latexPaperSizeString;
131: }
132:
133: /**
134: * Returns a <code>java.awt.print.PageFormat</code> that describes
135: * our orientation, size, and margins. Used by print jobs.
136: *
137: * @return a page format
138: * @see jimm.datavision.layout.swing.SwingLE#printReport
139: */
140: public PageFormat getPageFormat() {
141: if (pageFormat == null) {
142: pageFormat = new PageFormat();
143: if (orientation == PORTRAIT) {
144: pageFormat.setOrientation(PageFormat.PORTRAIT);
145: pageFormat.setPaper(this );
146: } else {
147: pageFormat.setOrientation(PageFormat.LANDSCAPE);
148: // Apparently the paper object given to the page format needs
149: // to have the x, w, width, and height values be the same as
150: // the portrait values, not rotated.
151: pageFormat.setPaper(get(PORTRAIT, getName()));
152: }
153: }
154: return pageFormat;
155: }
156:
157: public void writeXML(XMLWriter out) {
158: out.startElement("paper");
159: out.attr("name", name);
160: out.attr("orientation", orientation == PORTRAIT ? "portrait"
161: : "landscape");
162: out.endElement();
163: }
164:
165: }
|