Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/print/CustomMediaSizeName.java
38829 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package sun.print;2728import javax.print.attribute.EnumSyntax;29import javax.print.attribute.standard.Media;30import javax.print.attribute.standard.MediaSize;31import javax.print.attribute.standard.MediaSizeName;32import java.util.ArrayList;3334class CustomMediaSizeName extends MediaSizeName {35private static ArrayList customStringTable = new ArrayList();36private static ArrayList customEnumTable = new ArrayList();37private String choiceName;38private MediaSizeName mediaName;3940private CustomMediaSizeName(int x) {41super(x);4243}4445private synchronized static int nextValue(String name) {46customStringTable.add(name);4748return (customStringTable.size()-1);49}5051public CustomMediaSizeName(String name) {52super(nextValue(name));53customEnumTable.add(this);54choiceName = null;55mediaName = null;56}5758public CustomMediaSizeName(String name, String choice,59float width, float length) {60super(nextValue(name));61choiceName = choice;62customEnumTable.add(this);63mediaName = null;64try {65mediaName = MediaSize.findMedia(width, length,66MediaSize.INCH);67} catch (IllegalArgumentException iae) {68}69// The public API method finds a closest match even if it not70// all that close. Here we want to be sure its *really* close.71if (mediaName != null) {72MediaSize sz = MediaSize.getMediaSizeForName(mediaName);73if (sz == null) {74mediaName = null;75} else {76float w = sz.getX(MediaSize.INCH);77float h = sz.getY(MediaSize.INCH);78float dw = Math.abs(w - width);79float dh = Math.abs(h - length);80if (dw > 0.1 || dh > 0.1) {81mediaName = null;82}83}84}85}8687/**88* Version ID for serialized form.89*/90private static final long serialVersionUID = 7412807582228043717L;9192/**93* Returns the command string for this media.94*/95public String getChoiceName() {96return choiceName;97}9899100/**101* Returns matching standard MediaSizeName.102*/103public MediaSizeName getStandardMedia() {104return mediaName;105}106107108// moved from RasterPrinterJob109/**110* Returns closest matching MediaSizeName among given array of Media111*/112public static MediaSizeName findMedia(Media[] media, float x, float y,113int units) {114115116if (x <= 0.0f || y <= 0.0f || units < 1) {117throw new IllegalArgumentException("args must be +ve values");118}119120if (media == null || media.length == 0) {121throw new IllegalArgumentException("args must have valid array of media");122}123124int size =0;125MediaSizeName[] msn = new MediaSizeName[media.length];126for (int i=0; i<media.length; i++) {127if (media[i] instanceof MediaSizeName) {128msn[size++] = (MediaSizeName)media[i];129}130}131132if (size == 0) {133return null;134}135136int match = 0;137138double ls = x * x + y * y;139double tmp_ls;140float []dim;141float diffx = x;142float diffy = y;143144for (int i=0; i < size ; i++) {145MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]);146if (mediaSize == null) {147continue;148}149dim = mediaSize.getSize(units);150if (x == dim[0] && y == dim[1]) {151match = i;152break;153} else {154diffx = x - dim[0];155diffy = y - dim[1];156tmp_ls = diffx * diffx + diffy * diffy;157if (tmp_ls < ls) {158ls = tmp_ls;159match = i;160}161}162}163164return msn[match];165}166167/**168* Returns the string table for super class MediaSizeName.169*/170public Media[] getSuperEnumTable() {171return (Media[])super.getEnumValueTable();172}173174175/**176* Returns the string table for class CustomMediaSizeName.177*/178protected String[] getStringTable() {179String[] nameTable = new String[customStringTable.size()];180return (String[])customStringTable.toArray(nameTable);181}182183/**184* Returns the enumeration value table for class CustomMediaSizeName.185*/186protected EnumSyntax[] getEnumValueTable() {187MediaSizeName[] enumTable = new MediaSizeName[customEnumTable.size()];188return (MediaSizeName[])customEnumTable.toArray(enumTable);189}190191}192193194