Source for gnu.java.awt.ClasspathToolkit

   1: /* ClasspathToolkit.java -- Abstract superclass for Classpath toolkits.
   2:    Copyright (C) 2003, 2004, 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.awt;
  40: 
  41: import gnu.java.awt.peer.ClasspathDesktopPeer;
  42: import gnu.java.awt.peer.ClasspathFontPeer;
  43: import gnu.java.awt.peer.EmbeddedWindowPeer;
  44: import gnu.java.security.action.SetAccessibleAction;
  45: 
  46: import java.awt.AWTException;
  47: import java.awt.Desktop;
  48: import java.awt.Font;
  49: import java.awt.FontFormatException;
  50: import java.awt.GraphicsDevice;
  51: import java.awt.GraphicsEnvironment;
  52: import java.awt.HeadlessException;
  53: import java.awt.Toolkit;
  54: import java.awt.peer.DesktopPeer;
  55: import java.awt.peer.RobotPeer;
  56: import java.io.IOException;
  57: import java.io.InputStream;
  58: import java.lang.reflect.Constructor;
  59: import java.lang.reflect.InvocationTargetException;
  60: import java.security.AccessController;
  61: import java.util.Map;
  62: 
  63: import javax.imageio.spi.IIORegistry;
  64: 
  65: /**
  66:  * An abstract superclass for Classpath toolkits.
  67:  *
  68:  * <p>There exist some parts of AWT and Java2D that are specific to
  69:  * the underlying platform, but for which the {@link Toolkit} class
  70:  * does not provide suitable abstractions. Examples include some
  71:  * methods of {@link Font} or {@link GraphicsEnvironment}. Those
  72:  * methods use ClasspathToolkit as a central place for obtaining
  73:  * platform-specific functionality.
  74:  *
  75:  * <p>In addition, ClasspathToolkit implements some abstract methods
  76:  * of {@link java.awt.Toolkit} that are not really platform-specific,
  77:  * such as the maintenance of a cache of loaded images.
  78:  *
  79:  * <p><b>Thread Safety:</b> The methods of this class may safely be
  80:  * called without external synchronization. This also hold for any
  81:  * inherited {@link Toolkit} methods. Subclasses are responsible for
  82:  * the necessary synchronization.
  83:  *
  84:  * @author Sascha Brawer (brawer@dandelis.ch)
  85:  */
  86: public abstract class ClasspathToolkit
  87:   extends Toolkit
  88: {
  89:   /**
  90:    * Returns a shared instance of the local, platform-specific
  91:    * graphics environment.
  92:    *
  93:    * <p>This method is specific to GNU Classpath. It gets called by
  94:    * the Classpath implementation of {@link
  95:    * GraphicsEnvironment.getLocalGraphcisEnvironment()}.
  96:    */
  97:   public abstract GraphicsEnvironment getLocalGraphicsEnvironment();
  98: 
  99:   /**
 100:    * Acquires an appropriate {@link ClasspathFontPeer}, for use in
 101:    * classpath's implementation of {@link java.awt.Font}.
 102:    *
 103:    * @param name The logical name of the font. This may be either a face
 104:    * name or a logical font name, or may even be null. A default
 105:    * implementation of name decoding is provided in
 106:    * {@link ClasspathFontPeer}, but may be overridden in other toolkits.
 107:    *
 108:    * @param attrs Any extra {@link java.awt.font.TextAttribute} attributes
 109:    * this font peer should have, such as size, weight, family name, or
 110:    * transformation.
 111:    */
 112:   public abstract ClasspathFontPeer getClasspathFontPeer (String name,
 113:                                                           Map<?,?> attrs);
 114: 
 115:   /**
 116:    * Creates a {@link Font}, in a platform-specific manner.
 117:    *
 118:    * The default implementation simply constructs a {@link Font}, but some
 119:    * toolkits may wish to override this, to return {@link Font} subclasses
 120:    * which implement {@link java.awt.font.OpenType} or
 121:    * {@link java.awt.font.MultipleMaster}.
 122:    */
 123:   public Font getFont (String name, Map attrs)
 124:   {
 125:     Font f = null;
 126: 
 127:     // Circumvent the package-privateness of the
 128:     // java.awt.Font.Font(String,Map) constructor.
 129:     try
 130:       {
 131:         Constructor fontConstructor = Font.class.getDeclaredConstructor
 132:                                       (new Class[] { String.class, Map.class });
 133:         AccessController.doPrivileged(new SetAccessibleAction(fontConstructor));
 134:         f = (Font) fontConstructor.newInstance(new Object[] { name, attrs });
 135:       }
 136:     catch (IllegalAccessException e)
 137:       {
 138:         throw new AssertionError(e);
 139:       }
 140:     catch (NoSuchMethodException e)
 141:       {
 142:         throw new AssertionError(e);
 143:       }
 144:     catch (InstantiationException e)
 145:       {
 146:         throw new AssertionError(e);
 147:       }
 148:     catch (InvocationTargetException e)
 149:       {
 150:         throw new AssertionError(e);
 151:       }
 152:     return f;
 153:   }
 154: 
 155:   /**
 156:    * Creates a font, reading the glyph definitions from a stream.
 157:    *
 158:    * <p>This method provides the platform-specific implementation for
 159:    * the static factory method {@link Font#createFont(int,
 160:    * java.io.InputStream)}.
 161:    *
 162:    * @param format the format of the font data, such as {@link
 163:    * Font#TRUETYPE_FONT}. An implementation may ignore this argument
 164:    * if it is able to automatically recognize the font format from the
 165:    * provided data.
 166:    *
 167:    * @param stream an input stream from where the font data is read
 168:    * in. The stream will be advanced to the position after the font
 169:    * data, but not closed.
 170:    *
 171:    * @throws IllegalArgumentException if <code>format</code> is
 172:    * not supported.
 173:    *
 174:    * @throws FontFormatException if <code>stream</code> does not
 175:    * contain data in the expected format, or if required tables are
 176:    * missing from a font.
 177:    *
 178:    * @throws IOException if a problem occurs while reading in the
 179:    * contents of <code>stream</code>.
 180:    */
 181:   public abstract Font createFont(int format, InputStream stream);
 182: 
 183:   /**
 184:    * Creates a RobotPeer on a given GraphicsDevice.
 185:    */
 186:   public abstract RobotPeer createRobot (GraphicsDevice screen)
 187:     throws AWTException;
 188: 
 189:   /**
 190:    * Creates an embedded window peer, and associates it with an
 191:    * EmbeddedWindow object.
 192:    *
 193:    * @param w The embedded window with which to associate a peer.
 194:    */
 195:   public abstract EmbeddedWindowPeer createEmbeddedWindow (EmbeddedWindow w);
 196: 
 197:   /**
 198:    * Used to register ImageIO SPIs provided by the toolkit.
 199:    *
 200:    * Our default implementation does nothing.
 201:    */
 202:    public void registerImageIOSpis(IIORegistry reg)
 203:    {
 204:    }
 205: 
 206:    /**
 207:     * Returns the number of mouse buttons.
 208:     * (used by java.awt.MouseInfo).
 209:     *
 210:     * This dummy implementation returns -1 (no mouse).
 211:     * toolkit implementors should overload this method if possible.
 212:     * @since 1.5
 213:     */
 214:    public int getMouseNumberOfButtons()
 215:    {
 216:      return -1;
 217:    }
 218: 
 219:    /* (non-Javadoc)
 220:     * @see java.awt.Toolkit#createDesktopPeer(java.awt.Desktop)
 221:     */
 222:    protected DesktopPeer createDesktopPeer(Desktop target)
 223:      throws HeadlessException
 224:    {
 225:      if (GraphicsEnvironment.isHeadless())
 226:        throw new HeadlessException();
 227: 
 228:      return ClasspathDesktopPeer.getDesktop();
 229:    }
 230: 
 231: }