jdk/src/share/classes/sun/print/CustomMediaSizeName.java
author prr
Mon, 14 Jul 2014 11:11:13 -0700
changeset 25779 86b8de4f7d09
parent 25140 80e863984492
permissions -rw-r--r--
8048583: CustomMediaSizeName class matching to standard media is too loose Reviewed-by: bae, jgodinez
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.print.attribute.EnumSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.print.attribute.standard.Media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.print.attribute.standard.MediaSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.print.attribute.standard.MediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private String choiceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private MediaSizeName mediaName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private CustomMediaSizeName(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private synchronized static int nextValue(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
      customStringTable.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
      return (customStringTable.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public CustomMediaSizeName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super(nextValue(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        customEnumTable.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        choiceName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        mediaName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public CustomMediaSizeName(String name, String choice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                               float width, float length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super(nextValue(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        choiceName = choice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        customEnumTable.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        mediaName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            mediaName = MediaSize.findMedia(width, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                            MediaSize.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Version ID for serialized form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final long serialVersionUID = 7412807582228043717L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Returns the command string for this media.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public String getChoiceName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return choiceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Returns matching standard MediaSizeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public MediaSizeName getStandardMedia() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return mediaName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // moved from RasterPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Returns closest matching MediaSizeName among given array of Media
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static MediaSizeName findMedia(Media[] media, float x, float y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                          int units) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (x <= 0.0f || y <= 0.0f || units < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new IllegalArgumentException("args must be +ve values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (media == null || media.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IllegalArgumentException("args must have valid array of media");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int size =0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        MediaSizeName[] msn = new MediaSizeName[media.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        for (int i=0; i<media.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (media[i] instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                msn[size++] = (MediaSizeName)media[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int match = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        double ls = x * x + y * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        double tmp_ls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        float []dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        float diffx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        float diffy = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (int i=0; i < size ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (mediaSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            dim = mediaSize.getSize(units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (x == dim[0] && y == dim[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                match = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                diffx = x - dim[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                diffy = y - dim[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                tmp_ls = diffx * diffx + diffy * diffy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (tmp_ls < ls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    ls = tmp_ls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    match = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return msn[match];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns the string table for super class MediaSizeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public  Media[] getSuperEnumTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return (Media[])super.getEnumValueTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Returns the string table for class CustomMediaSizeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected String[] getStringTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns the enumeration value table for class CustomMediaSizeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected EnumSyntax[] getEnumValueTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}