jdk/src/share/classes/sun/print/CustomMediaSizeName.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static ArrayList customStringTable = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static ArrayList customEnumTable = new ArrayList();
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Version ID for serialized form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final long serialVersionUID = 7412807582228043717L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns the command string for this media.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public String getChoiceName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return choiceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns matching standard MediaSizeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public MediaSizeName getStandardMedia() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return mediaName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // moved from RasterPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Returns closest matching MediaSizeName among given array of Media
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static MediaSizeName findMedia(Media[] media, float x, float y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                          int units) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (x <= 0.0f || y <= 0.0f || units < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IllegalArgumentException("args must be +ve values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (media == null || media.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new IllegalArgumentException("args must have valid array of media");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int size =0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        MediaSizeName[] msn = new MediaSizeName[media.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i=0; i<media.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (media[i] instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                msn[size++] = (MediaSizeName)media[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
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 (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int match = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        double ls = x * x + y * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        double tmp_ls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        float []dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        float diffx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        float diffy = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int i=0; i < size ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (mediaSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            dim = mediaSize.getSize(units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (x == dim[0] && y == dim[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                match = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                diffx = x - dim[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                diffy = y - dim[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                tmp_ls = diffx * diffx + diffy * diffy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (tmp_ls < ls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    ls = tmp_ls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    match = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return msn[match];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns the string table for super class MediaSizeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public  Media[] getSuperEnumTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return (Media[])super.getEnumValueTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Returns the string table for class CustomMediaSizeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected String[] getStringTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      String[] nameTable = new String[customStringTable.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
      return (String[])customStringTable.toArray(nameTable);
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 enumeration value table for class CustomMediaSizeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected EnumSyntax[] getEnumValueTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
      MediaSizeName[] enumTable = new MediaSizeName[customEnumTable.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
      return (MediaSizeName[])customEnumTable.toArray(enumTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}