1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 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 
  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<String> customStringTable = new ArrayList<>();
  37     private static ArrayList<MediaSizeName> 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 static synchronized 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         // The public API method finds a closest match even if it not
  71         // all that close. Here we want to be sure its *really* close.
  72         if (mediaName != null) {
  73             MediaSize sz = MediaSize.getMediaSizeForName(mediaName);
  74             if (sz == null) {
  75                 mediaName = null;
  76             } else {
  77                 float w = sz.getX(MediaSize.INCH);
  78                 float h = sz.getY(MediaSize.INCH);
  79                 float dw = Math.abs(w - width);
  80                 float dh = Math.abs(h - length);
  81                 if (dw > 0.1 || dh > 0.1) {
  82                     mediaName = null;
  83                 }
  84             }
  85         }
  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()];
 181       return customStringTable.toArray(nameTable);
 182     }
 183 
 184     /**
 185      * Returns the enumeration value table for class CustomMediaSizeName.
 186      */
 187     protected EnumSyntax[] getEnumValueTable() {
 188       MediaSizeName[] enumTable = new MediaSizeName[customEnumTable.size()];
 189       return customEnumTable.toArray(enumTable);
 190     }
 191 
 192 }