author | prr |
Mon, 14 Jul 2014 11:11:13 -0700 | |
changeset 25779 | 86b8de4f7d09 |
parent 25140 | 80e863984492 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 { |
|
25140
80e863984492
8042870: Fix raw and unchecked warnings in sun.print
darcy
parents:
5506
diff
changeset
|
36 |
private static ArrayList<String> customStringTable = new ArrayList<>(); |
80e863984492
8042870: Fix raw and unchecked warnings in sun.print
darcy
parents:
5506
diff
changeset
|
37 |
private static ArrayList<MediaSizeName> customEnumTable = new ArrayList<>(); |
2 | 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 |
} |
|
25779
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
70 |
// The public API method finds a closest match even if it not |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
71 |
// all that close. Here we want to be sure its *really* close. |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
72 |
if (mediaName != null) { |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
73 |
MediaSize sz = MediaSize.getMediaSizeForName(mediaName); |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
74 |
if (sz == null) { |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
75 |
mediaName = null; |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
76 |
} else { |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
77 |
float w = sz.getX(MediaSize.INCH); |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
78 |
float h = sz.getY(MediaSize.INCH); |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
79 |
float dw = Math.abs(w - width); |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
80 |
float dh = Math.abs(h - length); |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
81 |
if (dw > 0.1 || dh > 0.1) { |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
82 |
mediaName = null; |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
83 |
} |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
84 |
} |
86b8de4f7d09
8048583: CustomMediaSizeName class matching to standard media is too loose
prr
parents:
25140
diff
changeset
|
85 |
} |
2 | 86 |
} |
87 |
||
88 |
/** |
|
89 |
* Version ID for serialized form. |
|
90 |
*/ |
|
91 |
private static final long serialVersionUID = 7412807582228043717L; |
|
92 |
||
93 |
/** |
|
94 |
* Returns the command string for this media. |
|
95 |
*/ |
|
96 |
public String getChoiceName() { |
|
97 |
return choiceName; |
|
98 |
} |
|
99 |
||
100 |
||
101 |
/** |
|
102 |
* Returns matching standard MediaSizeName. |
|
103 |
*/ |
|
104 |
public MediaSizeName getStandardMedia() { |
|
105 |
return mediaName; |
|
106 |
} |
|
107 |
||
108 |
||
109 |
// moved from RasterPrinterJob |
|
110 |
/** |
|
111 |
* Returns closest matching MediaSizeName among given array of Media |
|
112 |
*/ |
|
113 |
public static MediaSizeName findMedia(Media[] media, float x, float y, |
|
114 |
int units) { |
|
115 |
||
116 |
||
117 |
if (x <= 0.0f || y <= 0.0f || units < 1) { |
|
118 |
throw new IllegalArgumentException("args must be +ve values"); |
|
119 |
} |
|
120 |
||
121 |
if (media == null || media.length == 0) { |
|
122 |
throw new IllegalArgumentException("args must have valid array of media"); |
|
123 |
} |
|
124 |
||
125 |
int size =0; |
|
126 |
MediaSizeName[] msn = new MediaSizeName[media.length]; |
|
127 |
for (int i=0; i<media.length; i++) { |
|
128 |
if (media[i] instanceof MediaSizeName) { |
|
129 |
msn[size++] = (MediaSizeName)media[i]; |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
if (size == 0) { |
|
134 |
return null; |
|
135 |
} |
|
136 |
||
137 |
int match = 0; |
|
138 |
||
139 |
double ls = x * x + y * y; |
|
140 |
double tmp_ls; |
|
141 |
float []dim; |
|
142 |
float diffx = x; |
|
143 |
float diffy = y; |
|
144 |
||
145 |
for (int i=0; i < size ; i++) { |
|
146 |
MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]); |
|
147 |
if (mediaSize == null) { |
|
148 |
continue; |
|
149 |
} |
|
150 |
dim = mediaSize.getSize(units); |
|
151 |
if (x == dim[0] && y == dim[1]) { |
|
152 |
match = i; |
|
153 |
break; |
|
154 |
} else { |
|
155 |
diffx = x - dim[0]; |
|
156 |
diffy = y - dim[1]; |
|
157 |
tmp_ls = diffx * diffx + diffy * diffy; |
|
158 |
if (tmp_ls < ls) { |
|
159 |
ls = tmp_ls; |
|
160 |
match = i; |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
return msn[match]; |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Returns the string table for super class MediaSizeName. |
|
170 |
*/ |
|
171 |
public Media[] getSuperEnumTable() { |
|
172 |
return (Media[])super.getEnumValueTable(); |
|
173 |
} |
|
174 |
||
175 |
||
176 |
/** |
|
177 |
* Returns the string table for class CustomMediaSizeName. |
|
178 |
*/ |
|
179 |
protected String[] getStringTable() { |
|
180 |
String[] nameTable = new String[customStringTable.size()]; |
|
25140
80e863984492
8042870: Fix raw and unchecked warnings in sun.print
darcy
parents:
5506
diff
changeset
|
181 |
return customStringTable.toArray(nameTable); |
2 | 182 |
} |
183 |
||
184 |
/** |
|
185 |
* Returns the enumeration value table for class CustomMediaSizeName. |
|
186 |
*/ |
|
187 |
protected EnumSyntax[] getEnumValueTable() { |
|
188 |
MediaSizeName[] enumTable = new MediaSizeName[customEnumTable.size()]; |
|
25140
80e863984492
8042870: Fix raw and unchecked warnings in sun.print
darcy
parents:
5506
diff
changeset
|
189 |
return customEnumTable.toArray(enumTable); |
2 | 190 |
} |
191 |
||
192 |
} |