< prev index next >

src/java.desktop/share/classes/sun/print/CustomMediaSizeName.java

Print this page

 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.print;
 27 
 28 import java.io.Serial;
 29 import java.util.ArrayList;



 30 
 31 import javax.print.attribute.EnumSyntax;
 32 import javax.print.attribute.standard.Media;
 33 import javax.print.attribute.standard.MediaSize;
 34 import javax.print.attribute.standard.MediaSizeName;

 35 
 36 class CustomMediaSizeName extends MediaSizeName {
 37     private static ArrayList<String> customStringTable = new ArrayList<>();
 38     private static ArrayList<MediaSizeName> customEnumTable = new ArrayList<>();

 39     private String choiceName;
 40     private MediaSizeName mediaName;
 41 
 42     private CustomMediaSizeName(int x) {
 43         super(x);
 44 
 45     }
 46 
 47     private static synchronized int nextValue(String name) {
 48       customStringTable.add(name);
 49 
 50       return (customStringTable.size()-1);
 51     }
 52 
 53     public CustomMediaSizeName(String name) {
 54         super(nextValue(name));
 55         customEnumTable.add(this);
 56         choiceName = null;
 57         mediaName = null;
 58     }

174         return (Media[])super.getEnumValueTable();
175     }
176 
177 
178     /**
179      * Returns the string table for class CustomMediaSizeName.
180      */
181     protected String[] getStringTable() {
182       String[] nameTable = new String[customStringTable.size()];
183       return customStringTable.toArray(nameTable);
184     }
185 
186     /**
187      * Returns the enumeration value table for class CustomMediaSizeName.
188      */
189     protected EnumSyntax[] getEnumValueTable() {
190       MediaSizeName[] enumTable = new MediaSizeName[customEnumTable.size()];
191       return customEnumTable.toArray(enumTable);
192     }
193 



















































194 }

 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.print;
 27 
 28 import java.io.Serial;
 29 import java.util.ArrayList;
 30 import java.util.Map;
 31 import java.util.HashMap;
 32 import java.util.Objects;
 33 
 34 import javax.print.attribute.EnumSyntax;
 35 import javax.print.attribute.standard.Media;
 36 import javax.print.attribute.standard.MediaSize;
 37 import javax.print.attribute.standard.MediaSizeName;
 38 import javax.print.attribute.Size2DSyntax;
 39 
 40 class CustomMediaSizeName extends MediaSizeName {
 41     private static ArrayList<String> customStringTable = new ArrayList<>();
 42     private static ArrayList<MediaSizeName> customEnumTable = new ArrayList<>();
 43     private static Map<SizeNameChoiceItem, CustomMediaSizeName> customMap = new HashMap<>();
 44     private String choiceName;
 45     private MediaSizeName mediaName;
 46 
 47     private CustomMediaSizeName(int x) {
 48         super(x);
 49 
 50     }
 51 
 52     private static synchronized int nextValue(String name) {
 53       customStringTable.add(name);
 54 
 55       return (customStringTable.size()-1);
 56     }
 57 
 58     public CustomMediaSizeName(String name) {
 59         super(nextValue(name));
 60         customEnumTable.add(this);
 61         choiceName = null;
 62         mediaName = null;
 63     }

179         return (Media[])super.getEnumValueTable();
180     }
181 
182 
183     /**
184      * Returns the string table for class CustomMediaSizeName.
185      */
186     protected String[] getStringTable() {
187       String[] nameTable = new String[customStringTable.size()];
188       return customStringTable.toArray(nameTable);
189     }
190 
191     /**
192      * Returns the enumeration value table for class CustomMediaSizeName.
193      */
194     protected EnumSyntax[] getEnumValueTable() {
195       MediaSizeName[] enumTable = new MediaSizeName[customEnumTable.size()];
196       return customEnumTable.toArray(enumTable);
197     }
198 
199     public static CustomMediaSizeName create(String name, String choice,
200                                              float width, float length) {
201         SizeNameChoiceItem key = new SizeNameChoiceItem(name, choice, width, length);
202         CustomMediaSizeName value = customMap.get(key);
203         if (value == null) {
204             value = new CustomMediaSizeName(name, choice, width, length);
205             customMap.put(key, value);
206 
207             // add this new custom media size name to MediaSize array
208             if ((width > 0.0) && (length > 0.0)) {
209                 try {
210                     new MediaSize(width, length, Size2DSyntax.INCH, value);
211                 } catch (IllegalArgumentException e) {
212                         /* PDF printer in Linux for Ledger paper causes
213                         "IllegalArgumentException: X dimension > Y dimension".
214                         We rotate based on IPP spec. */
215                     new MediaSize(length, width, Size2DSyntax.INCH, value);
216                 }
217             }
218         }
219         return value;
220     }
221 
222     private static class SizeNameChoiceItem {
223 
224         private final String name;
225         private final String choice;
226         private final float width;
227         private final float length;
228 
229         public SizeNameChoiceItem(String name, String choice, float width, float length) {
230             this.name = name;
231             this.choice = choice;
232             this.width = width;
233             this.length = length;
234         }
235 
236         public boolean equals(Object object) {
237             if (this == object) return true;
238             if (object == null || getClass() != object.getClass()) return false;
239             SizeNameChoiceItem that = (SizeNameChoiceItem) object;
240             return Objects.equals(this.name, that.name)
241                     && Objects.equals(this.choice, that.choice) &&
242                     Float.compare(this.width, that.width) == 0 &&
243                     Float.compare(this.length, that.length) == 0;
244         }
245 
246         public int hashCode() {
247             return Objects.hash(name, choice, width, length);
248         }
249     }
250 }
< prev index next >