create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / PaperSize.java
1 package net.sf.openrocket.gui.print;
2
3 import java.io.BufferedReader;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.Locale;
7
8 import net.sf.openrocket.logging.LogHelper;
9 import net.sf.openrocket.startup.Application;
10
11 import com.itextpdf.text.PageSize;
12 import com.itextpdf.text.Rectangle;
13
14 public enum PaperSize {
15         A3("A3", PageSize.A3),
16         A4("A4", PageSize.A4),
17         A5("A5", PageSize.A5),
18         LETTER("Letter", PageSize.LETTER),
19         LEGAL("Legal", PageSize.LEGAL);
20         
21         private final String name;
22         private final Rectangle size;
23         
24         private PaperSize(String name, Rectangle size) {
25                 this.name = name;
26                 this.size = size;
27         }
28         
29         public Rectangle getSize() {
30                 return size;
31         }
32         
33         @Override
34         public String toString() {
35                 return name;
36         }
37         
38         
39         
40         //////////////////////////
41         
42         private static final LogHelper log = Application.getLogger();
43         private static PaperSize defaultSize = null;
44         
45         /**
46          * Return the default paper size for the current system.
47          * @return      the default paper size
48          */
49         public static PaperSize getDefault() {
50                 if (defaultSize == null) {
51                         
52                         // Test environment variable "PAPERSIZE" (Unix)
53                         defaultSize = getDefaultFromEnvironmentVariable();
54                         if (defaultSize != null) {
55                                 log.info("Selecting default paper size from PAPERSIZE environment variable: " + defaultSize);
56                                 return defaultSize;
57                         }
58                         
59                         // Test /etc/papersize (Unix)
60                         defaultSize = getDefaultFromEtcPapersize();
61                         if (defaultSize != null) {
62                                 log.info("Selecting default paper size from /etc/papersize: " + defaultSize);
63                                 return defaultSize;
64                         }
65                         
66                         // Test user.country
67                         defaultSize = getDefaultForCountry(System.getProperty("user.country"));
68                         if (defaultSize != null) {
69                                 log.info("Selecting default paper size based on user.country: " + defaultSize);
70                                 return defaultSize;
71                         }
72                         
73                         // Test locale country
74                         defaultSize = getDefaultForCountry(Locale.getDefault().getCountry());
75                         if (defaultSize != null) {
76                                 log.info("Selecting default paper size based on locale country: " + defaultSize);
77                                 return defaultSize;
78                         }
79                         
80                         // Fallback to A4
81                         defaultSize = A4;
82                         log.info("Selecting default paper size fallback: " + defaultSize);
83                 }
84                 
85                 return defaultSize;
86         }
87         
88         
89         /**
90          * Attempt to read the default paper size from the "PAPERSIZE" environment variable.
91          * 
92          * @return      the default paper size if successful, or <code>null</code> if unable to read/parse file.
93          */
94         private static PaperSize getDefaultFromEnvironmentVariable() {
95                 String str = System.getenv("PAPERSIZE");
96                 return getSizeFromString(str);
97         }
98         
99         /**
100          * Attempt to read the default paper size from the file defined by the environment variable
101          * PAPERCONF or from /etc/papersize.
102          * 
103          * @return      the default paper size if successful, or <code>null</code> if unable to read/parse file.
104          */
105         private static PaperSize getDefaultFromEtcPapersize() {
106                 
107                 // Find file to read
108                 String file = System.getenv("PAPERCONF");
109                 if (file == null) {
110                         file = "/etc/papersize";
111                 }
112                 
113                 // Attempt to read the file
114                 BufferedReader in = null;
115                 try {
116                         
117                         String str;
118                         in = new BufferedReader(new FileReader(file));
119                         while ((str = in.readLine()) != null) {
120                                 if (str.matches("^\\s*(#.*|$)")) {
121                                         continue;
122                                 }
123                                 break;
124                         }
125                         
126                         return getSizeFromString(str);
127                         
128                 } catch (IOException e) {
129                         
130                         // Could not read file
131                         return null;
132                         
133                 } finally {
134                         if (in != null) {
135                                 try {
136                                         in.close();
137                                 } catch (IOException e) {
138                                 }
139                         }
140                 }
141         }
142         
143         
144         /**
145          * Get a paper size based on a string.  The string is trimmed and case-insensitively 
146          * compared to the base names of the paper sizes.
147          * 
148          * @param size  the size string (may be null)
149          * @return              the corresponding paper size, or null if unknown
150          */
151         static PaperSize getSizeFromString(String size) {
152                 if (size == null) {
153                         return null;
154                 }
155                 
156                 size = size.trim();
157                 for (PaperSize p : PaperSize.values()) {
158                         if (p.name.equalsIgnoreCase(size)) {
159                                 return p;
160                         }
161                 }
162                 return null;
163         }
164         
165         
166         /**
167          * Get default paper size for a specific country.  This method falls back to A4 for
168          * any country not known to use Letter.
169          * 
170          * @param country       the 2-char country code (may be null)
171          * @return                      the paper size, or <code>null</code> if country is not a country code
172          */
173         static PaperSize getDefaultForCountry(String country) {
174                 /*
175                  * List is based on info from http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/territory_language_information.html
176                  * OpenOffice.org agrees with this:  http://wiki.services.openoffice.org/wiki/DefaultPaperSize#Summary
177                  */
178                 final String[] letterCountries = { "BZ", "CA", "CL", "CO", "CR", "SV", "GT", "MX", "NI", "PA", "PH", "PR", "US", "VE" };
179                 
180                 if (country == null || !country.matches("^[a-zA-Z][a-zA-Z]$")) {
181                         return null;
182                 }
183                 
184                 country = country.toUpperCase(Locale.ENGLISH);
185                 for (String c : letterCountries) {
186                         if (c.equals(country)) {
187                                 return LETTER;
188                         }
189                 }
190                 return A4;
191         }
192         
193 }