mediasize.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 884 行 · 第 1/2 页

JAVA
884
字号
/* MediaSize.java --    Copyright (C) 2005, 2006  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.print.attribute.standard;import java.util.ArrayList;import javax.print.attribute.Attribute;import javax.print.attribute.Size2DSyntax;/** * The <code>MediaSize</code> printing attribute class specifies the size * of a printing media. The size is defined in portrait orientation with  * x at the bottom edge and y at the left edge. * <p> * There are several media sizes predefined through the nested classes. Further * sizes may be provided by the application. <code>MediaSize</code> is not used * as a printing attribute currently. It may be used to get the actual sizes  * for a named media or to find a suitable <code>MediaSizeName</code> instance * by querying with the needed sizes. * </p>  * <p> * <b>IPP Compatibility:</b> MediaSize is not an IPP 1.1 attribute. * </p> * @see javax.print.attribute.standard.MediaSizeName *  * @author Michael Koch (konqueror@gmx.de) * @author Wolfgang Baer (WBaer@gmx.de) */public class MediaSize extends Size2DSyntax  implements Attribute{  private static final long serialVersionUID = -1967958664615414771L;  private static ArrayList mediaCache;    static    {      mediaCache = new ArrayList();            // We call one instance of every container class to make sure it gets      // loaded during class initialization and therefore all other static      // fields of this container class also.            // This is needed to put all MediaSize instance into the mediaCache      // for use by the static methods in this class.            MediaSize tmp = MediaSize.ISO.A0;      tmp = MediaSize.JIS.B0;      tmp = MediaSize.Engineering.A;      tmp = MediaSize.NA.LEGAL;      tmp = MediaSize.Other.EXECUTIVE;    }    private MediaSizeName mediaName;    /**   * Creates a <code>MediaSize</code> object. The created object will be added    * to an internal cache used in the static methods of this class for lookup    * of available <code>MediaSize</code> instances.   *   * @param x the size in x direction   * @param y the size in y direction   * @param units the units to use for the sizes   *   * @exception IllegalArgumentException if x or y &lt; 0 or units &lt; 1   *    * @see #findMedia(float, float, int)   * @see #getMediaSizeForName(MediaSizeName)   */  public MediaSize(float x, float y, int units)  {    super(x, y, units);    mediaCache.add(this);  }    /**   * Creates a <code>MediaSize</code> object associated with the given   * media name. The created object will be added to an internal cache used    * in the static methods of this class for lookup of available    * <code>MediaSize</code> instances.   *   * @param x the size in x direction   * @param y the size in y direction   * @param units the units to use for the sizes   * @param media the media name to associate   *   * @exception IllegalArgumentException if x or y &lt; 0 or units &lt; 1   *    * @see #findMedia(float, float, int)   * @see #getMediaSizeForName(MediaSizeName)   */  public MediaSize(float x, float y, int units, MediaSizeName media)  {    super(x, y, units);    mediaName = media;    mediaCache.add(this);  }    /**   * Creates a <code>MediaSize</code> object. The created object will be added    * to an internal cache used in the static methods of this class for lookup    * of available <code>MediaSize</code> instances.   *   * @param x the size in x direction   * @param y the size in y direction   * @param units the units to use for the sizes   *   * @exception IllegalArgumentException if x or y &lt; 0 or units &lt; 1   *    * @see #findMedia(float, float, int)   * @see #getMediaSizeForName(MediaSizeName)   */  public MediaSize(int x, int y, int units)  {    super(x, y, units);    mediaCache.add(this);  }    /**   * Creates a <code>MediaSize</code> object associated with the given   * media name. The created object will be added to an internal cache used    * in the static methods of this class for lookup of available    * <code>MediaSize</code> instances.   *   * @param x the size in x direction   * @param y the size in y direction   * @param units the units to use for the sizes   * @param media the media name to associate   *   * @exception IllegalArgumentException if x or y &lt; 0 or units &lt; 1   *    * @see #findMedia(float, float, int)   * @see #getMediaSizeForName(MediaSizeName)   */  public MediaSize(int x, int y, int units, MediaSizeName media)  {    super(x, y, units);    mediaName = media;    mediaCache.add(this);  }    /**   * Returns category of this class.   *   * @return The class <code>MediaSize</code> itself.   */  public final Class getCategory()  {    return MediaSize.class;  }      /**   * Searches for a MediaSize object with the given dimensions.   * If none is found with exact dimensions, the closest match is used.   * Afterwards the MediaSizeName of the found MediaSize object is    * returned - which might be null if none is specified.   *    * @param x the dimension for x   * @param y the dimension for y   * @param units the units to be used for comparison   * @return the corresponding MediaSizeName object, or null   */  public static MediaSizeName findMedia(float x, float y, int units)  {    if (x <= 0.0f || y <= 0.0f)      throw new IllegalArgumentException(        "x and/or y may not be less or equal 0");    if (units < 1)      throw new IllegalArgumentException("units may not be less then 1");    MediaSize bestMatch = null;    int bestDistance = Integer.MAX_VALUE;    int xMicro = (int) x * units;    int yMicro = (int) y * units;    for (int i = 0; i < mediaCache.size(); i++)      {        MediaSize size = (MediaSize) mediaCache.get(i);        int dist = (Math.abs(size.getXMicrometers() - xMicro)                     + Math.abs(size.getYMicrometers() - yMicro));        if (dist < bestDistance)          {            bestMatch = size;            bestDistance = dist;          }      }    return bestMatch.getMediaSizeName();  }    /**   * Returns the associated <code>MediaSize</code> instance for the    * given named media <code>MediaSizeName</code> instance.   *    * @param media the named media to search for.   * @return The corresponding <code>MediaSize</code> instance or    * <code>null</code> if none found.   */  public static MediaSize getMediaSizeForName(MediaSizeName media)  {    for (int i = 0; i < mediaCache.size(); i++)      {	MediaSize size = (MediaSize) mediaCache.get(i);		if (size.getMediaSizeName().equals(media))	  return size;      }    return null;  }    /**   * Tests if the given object is equal to this object.   *   * @param obj the object to test   *   * @return <code>true</code> if both objects are equal,    * <code>false</code> otherwise.   */  public boolean equals(Object obj)  {    if (!(obj instanceof MediaSize))      return false;    MediaSize tmp = (MediaSize) obj;    return (tmp.getXMicrometers() == this.getXMicrometers()            && tmp.getYMicrometers() == this.getYMicrometers());  }    /**   * Returns the media name of this size.   *    * @return The media name.   */  public MediaSizeName getMediaSizeName()  {    return mediaName;  }  /**   * Returns the name of this attribute.   *   * @return The name "media-size".   */  public final String getName()  {    return "media-size";  }  /**   * Container class for predefined ISO media sizes.   *    * @author Sven de Marothy (sven@physto.se)   */  public static final class ISO   {    private ISO()    {      // prevent instantiation    }        /**     * ISO A0 paper, 841 mm x 1189 mm.     */    public static final MediaSize A0 = new MediaSize(841, 1189, 					       MediaSize.MM, 					       MediaSizeName.ISO_A0);    /**     * ISO A1 paper, 594 mm x 841 mm     */    public static final MediaSize A1 = new MediaSize(594, 841, MediaSize.MM, 					       MediaSizeName.ISO_A1);    /**     * ISO A2 paper, 420 mm x 594 mm     */    public static final MediaSize A2 = new MediaSize(420, 594, MediaSize.MM, MediaSizeName.ISO_A2);    /**     * ISO A3 paper, 297 mm x 420 mm     */    public static final MediaSize A3 = new MediaSize(297, 420, MediaSize.MM, MediaSizeName.ISO_A3);    /**     * ISO A4 paper, 210 mm x 297 mm     */    public static final MediaSize A4 = new MediaSize(210, 297, MediaSize.MM, MediaSizeName.ISO_A4);    /**     * ISO A5 paper, 148 mm x 210 mm     */    public static final MediaSize A5 = new MediaSize(148, 210, MediaSize.MM, MediaSizeName.ISO_A5);    /**     * ISO A6 paper, 105 mm x 148 mm     */    public static final MediaSize A6 = new MediaSize(105, 148, MediaSize.MM, MediaSizeName.ISO_A6);    /**     * ISO A7 paper, 74 mm x 105 mm     */    public static final MediaSize A7 = new MediaSize(74, 105, MediaSize.MM, MediaSizeName.ISO_A7);    /**     * ISO A8 paper, 52 mm x 74 mm     */    public static final MediaSize A8 = new MediaSize(52, 74, MediaSize.MM, MediaSizeName.ISO_A8);    /**     * ISO A9 paper, 37 mm x 52 mm     */    public static final MediaSize A9 = new MediaSize(37, 52, MediaSize.MM, MediaSizeName.ISO_A9);    /**     * ISO A10 paper, 26 mm x 37 mm     */    public static final MediaSize A10 = new MediaSize(26, 37, MediaSize.MM, MediaSizeName.ISO_A10);    /**     * ISO B0 paper, 1000 mm x 1414 mm     */    public static final MediaSize B0 = new MediaSize(1000, 1414, MediaSize.MM, MediaSizeName.ISO_B0);    /**     * ISO B1 paper, 707 mm x 1000 mm     */    public static final MediaSize B1 = new MediaSize(707, 1000, MediaSize.MM, MediaSizeName.ISO_B1);    /**     * ISO B2 paper, 500 mm x 707 mm     */    public static final MediaSize B2 = new MediaSize(500, 707, MediaSize.MM, MediaSizeName.ISO_B2);    /**     * ISO B3 paper, 353 mm x 500 mm     */    public static final MediaSize B3 = new MediaSize(353, 500, MediaSize.MM, MediaSizeName.ISO_B3);    /**     * ISO B4 paper, 250 mm x 353 mm     */    public static final MediaSize B4 = new MediaSize(250, 353, MediaSize.MM, MediaSizeName.ISO_B4);    /**     * ISO B5 paper, 176 mm x 250 mm     */    public static final MediaSize B5 = new MediaSize(176, 250, MediaSize.MM, MediaSizeName.ISO_B5);    /**     * ISO B6 paper, 125 mm x 176 mm     */    public static final MediaSize B6 = new MediaSize(125, 176, MediaSize.MM, MediaSizeName.ISO_B6);    /**     * ISO B7 paper, 88 mm x 125 mm     */    public static final MediaSize B7 = new MediaSize(88, 125, MediaSize.MM, MediaSizeName.ISO_B7);    /**     * ISO B8 paper, 62 mm x 88 mm     */    public static final MediaSize B8 = new MediaSize(62, 88, MediaSize.MM, MediaSizeName.ISO_B8);    /**     * ISO B9 paper, 44 mm x 62 mm     */    public static final MediaSize B9 = new MediaSize(44, 62, MediaSize.MM, MediaSizeName.ISO_B9);    /**     * ISO B10 paper, 31 mm x 44 mm     */    public static final MediaSize B10 = new MediaSize(31, 44, MediaSize.MM, MediaSizeName.ISO_B10);        /**     * ISO C3 envelope, 324 mm x 458 mm     */    public static final MediaSize C3 = new MediaSize(324, 458, MediaSize.MM, MediaSizeName.ISO_C3);    /**     * ISO C4 envelope, 229 mm x 324 mm     */    public static final MediaSize C4 = new MediaSize(229, 324, MediaSize.MM, MediaSizeName.ISO_C4);    /**     * ISO C5 envelope, 162 mm x 229 mm     */    public static final MediaSize C5 = new MediaSize(162, 229, MediaSize.MM, MediaSizeName.ISO_C5);    /**     * ISO C6 envelope, 114 mm x 162 mm     */    public static final MediaSize C6 = new MediaSize(114, 162, MediaSize.MM, MediaSizeName.ISO_C6);    /**     * ISO ISO Designated Long paper, 324 mm x 458 mm     */    public static final MediaSize DESIGNATED_LONG =       new MediaSize(324, 458, MediaSize.MM, MediaSizeName.ISO_DESIGNATED_LONG);  }   /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?