2
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
package sun.print;
|
|
28 |
|
|
29 |
import javax.print.attribute.EnumSyntax;
|
|
30 |
import javax.print.attribute.standard.Media;
|
|
31 |
import javax.print.attribute.standard.MediaSize;
|
|
32 |
import javax.print.attribute.standard.MediaSizeName;
|
|
33 |
import java.util.ArrayList;
|
|
34 |
|
|
35 |
class CustomMediaSizeName extends MediaSizeName {
|
|
36 |
private static ArrayList customStringTable = new ArrayList();
|
|
37 |
private static ArrayList customEnumTable = new ArrayList();
|
|
38 |
private String choiceName;
|
|
39 |
private MediaSizeName mediaName;
|
|
40 |
|
|
41 |
private CustomMediaSizeName(int x) {
|
|
42 |
super(x);
|
|
43 |
|
|
44 |
}
|
|
45 |
|
|
46 |
private synchronized static int nextValue(String name) {
|
|
47 |
customStringTable.add(name);
|
|
48 |
|
|
49 |
return (customStringTable.size()-1);
|
|
50 |
}
|
|
51 |
|
|
52 |
public CustomMediaSizeName(String name) {
|
|
53 |
super(nextValue(name));
|
|
54 |
customEnumTable.add(this);
|
|
55 |
choiceName = null;
|
|
56 |
mediaName = null;
|
|
57 |
}
|
|
58 |
|
|
59 |
public CustomMediaSizeName(String name, String choice,
|
|
60 |
float width, float length) {
|
|
61 |
super(nextValue(name));
|
|
62 |
choiceName = choice;
|
|
63 |
customEnumTable.add(this);
|
|
64 |
mediaName = null;
|
|
65 |
try {
|
|
66 |
mediaName = MediaSize.findMedia(width, length,
|
|
67 |
MediaSize.INCH);
|
|
68 |
} catch (IllegalArgumentException iae) {
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Version ID for serialized form.
|
|
74 |
*/
|
|
75 |
private static final long serialVersionUID = 7412807582228043717L;
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Returns the command string for this media.
|
|
79 |
*/
|
|
80 |
public String getChoiceName() {
|
|
81 |
return choiceName;
|
|
82 |
}
|
|
83 |
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Returns matching standard MediaSizeName.
|
|
87 |
*/
|
|
88 |
public MediaSizeName getStandardMedia() {
|
|
89 |
return mediaName;
|
|
90 |
}
|
|
91 |
|
|
92 |
|
|
93 |
// moved from RasterPrinterJob
|
|
94 |
/**
|
|
95 |
* Returns closest matching MediaSizeName among given array of Media
|
|
96 |
*/
|
|
97 |
public static MediaSizeName findMedia(Media[] media, float x, float y,
|
|
98 |
int units) {
|
|
99 |
|
|
100 |
|
|
101 |
if (x <= 0.0f || y <= 0.0f || units < 1) {
|
|
102 |
throw new IllegalArgumentException("args must be +ve values");
|
|
103 |
}
|
|
104 |
|
|
105 |
if (media == null || media.length == 0) {
|
|
106 |
throw new IllegalArgumentException("args must have valid array of media");
|
|
107 |
}
|
|
108 |
|
|
109 |
int size =0;
|
|
110 |
MediaSizeName[] msn = new MediaSizeName[media.length];
|
|
111 |
for (int i=0; i<media.length; i++) {
|
|
112 |
if (media[i] instanceof MediaSizeName) {
|
|
113 |
msn[size++] = (MediaSizeName)media[i];
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
if (size == 0) {
|
|
118 |
return null;
|
|
119 |
}
|
|
120 |
|
|
121 |
int match = 0;
|
|
122 |
|
|
123 |
double ls = x * x + y * y;
|
|
124 |
double tmp_ls;
|
|
125 |
float []dim;
|
|
126 |
float diffx = x;
|
|
127 |
float diffy = y;
|
|
128 |
|
|
129 |
for (int i=0; i < size ; i++) {
|
|
130 |
MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]);
|
|
131 |
if (mediaSize == null) {
|
|
132 |
continue;
|
|
133 |
}
|
|
134 |
dim = mediaSize.getSize(units);
|
|
135 |
if (x == dim[0] && y == dim[1]) {
|
|
136 |
match = i;
|
|
137 |
break;
|
|
138 |
} else {
|
|
139 |
diffx = x - dim[0];
|
|
140 |
diffy = y - dim[1];
|
|
141 |
tmp_ls = diffx * diffx + diffy * diffy;
|
|
142 |
if (tmp_ls < ls) {
|
|
143 |
ls = tmp_ls;
|
|
144 |
match = i;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
return msn[match];
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Returns the string table for super class MediaSizeName.
|
|
154 |
*/
|
|
155 |
public Media[] getSuperEnumTable() {
|
|
156 |
return (Media[])super.getEnumValueTable();
|
|
157 |
}
|
|
158 |
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Returns the string table for class CustomMediaSizeName.
|
|
162 |
*/
|
|
163 |
protected String[] getStringTable() {
|
|
164 |
String[] nameTable = new String[customStringTable.size()];
|
|
165 |
return (String[])customStringTable.toArray(nameTable);
|
|
166 |
}
|
|
167 |
|
|
168 |
/**
|
|
169 |
* Returns the enumeration value table for class CustomMediaSizeName.
|
|
170 |
*/
|
|
171 |
protected EnumSyntax[] getEnumValueTable() {
|
|
172 |
MediaSizeName[] enumTable = new MediaSizeName[customEnumTable.size()];
|
|
173 |
return (MediaSizeName[])customEnumTable.toArray(enumTable);
|
|
174 |
}
|
|
175 |
|
|
176 |
}
|