KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > PaperFormat


1 package jimm.datavision;
2 import jimm.util.XMLWriter;
3 import java.awt.print.Paper JavaDoc;
4 import java.awt.print.PageFormat JavaDoc;
5 import java.util.*;
6
7 /**
8  * The class manages lists of paper sizes and instances represents specific
9  * paper sizes and orientations. Instances are immutable.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 public class PaperFormat extends Paper JavaDoc implements Nameable, Writeable {
14
15 public static final int PORTRAIT = 0;
16 public static final int LANDSCAPE = 1;
17
18 protected static final String JavaDoc RESOURCE_FILE_PREFIX = "paper";
19
20 protected static HashMap[] orientations;
21 protected static TreeSet names;
22 protected static PaperFormat defaultPaper;
23
24 protected String JavaDoc name; // Immutable
25
protected int orientation; // PORTRAIT or LANDSCAPE; immutable
26
protected PageFormat JavaDoc pageFormat;
27 protected String JavaDoc 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 JavaDoc 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; // pos5 is optional
48

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 JavaDoc 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 JavaDoc 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) // Zero'th one is the default
68
defaultPaper = p;
69     }
70     }
71     catch (Exception JavaDoc e) { // Number format exceptions, mostly
72
ErrorHandler.error(e);
73     }
74     finally {
75     // Copy list of names to a sorted set
76
names = new TreeSet(nameList);
77     }
78 }
79
80 private static PaperFormat addPaper(int orientation, String JavaDoc name,
81                     double w, double h,
82                     double hMargin, double vMargin,
83                     String JavaDoc 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 JavaDoc 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 JavaDoc name, double w, double h, double hMargin,
105         double vMargin, String JavaDoc 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 JavaDoc getName() { return name; }
117
118 /** A paper format's name is immutable. */
119 public void setName(String JavaDoc name) { }
120
121 public String JavaDoc getLaTeXPaperSizeString() { return latexPaperSizeString; }
122
123 /**
124  * Returns a <code>java.awt.print.PageFormat</code> that describes
125  * our orientation, size, and margins. Used by print jobs.
126  *
127  * @return a page format
128  * @see jimm.datavision.layout.swing.SwingLE#printReport
129  */

130 public PageFormat JavaDoc getPageFormat() {
131     if (pageFormat == null) {
132     pageFormat = new PageFormat JavaDoc();
133     if (orientation == PORTRAIT) {
134         pageFormat.setOrientation(PageFormat.PORTRAIT);
135         pageFormat.setPaper(this);
136     }
137     else {
138         pageFormat.setOrientation(PageFormat.LANDSCAPE);
139         // Apparently the paper object given to the page format needs
140
// to have the x, w, width, and height values be the same as
141
// the portrait values, not rotated.
142
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