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.font.TextAttribute;
  55: import java.awt.peer.DesktopPeer;
  56: import java.awt.peer.RobotPeer;
  57: import java.io.IOException;
  58: import java.io.InputStream;
  59: import java.lang.reflect.Constructor;
  60: import java.lang.reflect.InvocationTargetException;
  61: import java.security.AccessController;
  62: import java.util.Map;
  63: 
  64: import javax.imageio.spi.IIORegistry;
  65: 
  66: /**
  67:  * An abstract superclass for Classpath toolkits.
  68:  *
  69:  * <p>There exist some parts of AWT and Java2D that are specific to
  70:  * the underlying platform, but for which the {@link Toolkit} class
  71:  * does not provide suitable abstractions. Examples include some
  72:  * methods of {@link Font} or {@link GraphicsEnvironment}. Those
  73:  * methods use ClasspathToolkit as a central place for obtaining
  74:  * platform-specific functionality.
  75:  *
  76:  * <p>In addition, ClasspathToolkit implements some abstract methods
  77:  * of {@link java.awt.Toolkit} that are not really platform-specific,
  78:  * such as the maintenance of a cache of loaded images.
  79:  *
  80:  * <p><b>Thread Safety:</b> The methods of this class may safely be
  81:  * called without external synchronization. This also hold for any
  82:  * inherited {@link Toolkit} methods. Subclasses are responsible for
  83:  * the necessary synchronization.
  84:  *
  85:  * @author Sascha Brawer (brawer@dandelis.ch)
  86:  */
  87: public abstract class ClasspathToolkit
  88:   extends Toolkit
  89: {
  90:   /**
  91:    * Returns a shared instance of the local, platform-specific
  92:    * graphics environment.
  93:    *
  94:    * <p>This method is specific to GNU Classpath. It gets called by
  95:    * the Classpath implementation of {@link
  96:    * GraphicsEnvironment.getLocalGraphcisEnvironment()}.
  97:    */
  98:   public abstract GraphicsEnvironment getLocalGraphicsEnvironment();
  99: 
 100:   /**
 101:    * Acquires an appropriate {@link ClasspathFontPeer}, for use in
 102:    * classpath's implementation of {@link java.awt.Font}.
 103:    *
 104:    * @param name The logical name of the font. This may be either a face
 105:    * name or a logical font name, or may even be null. A default
 106:    * implementation of name decoding is provided in 
 107:    * {@link ClasspathFontPeer}, but may be overridden in other toolkits.
 108:    *
 109:    * @param attrs Any extra {@link java.awt.font.TextAttribute} attributes
 110:    * this font peer should have, such as size, weight, family name, or
 111:    * transformation.
 112:    */
 113:   public abstract ClasspathFontPeer getClasspathFontPeer (String name,
 114:                                                           Map<?,?> attrs); 
 115: 
 116:   /** 
 117:    * Creates a {@link Font}, in a platform-specific manner.
 118:    * 
 119:    * The default implementation simply constructs a {@link Font}, but some
 120:    * toolkits may wish to override this, to return {@link Font} subclasses 
 121:    * which implement {@link java.awt.font.OpenType} or
 122:    * {@link java.awt.font.MultipleMaster}.
 123:    */
 124:   public Font getFont (String name, Map attrs) 
 125:   {
 126:     Font f = null;
 127: 
 128:     // Circumvent the package-privateness of the
 129:     // java.awt.Font.Font(String,Map) constructor.
 130:     try
 131:       {
 132:         Constructor fontConstructor = Font.class.getDeclaredConstructor
 133:                                       (new Class[] { String.class, Map.class });
 134:         AccessController.doPrivileged(new SetAccessibleAction(fontConstructor));
 135:         f = (Font) fontConstructor.newInstance(new Object[] { name, attrs });
 136:       }
 137:     catch (IllegalAccessException e)
 138:       {
 139:         throw new AssertionError(e);
 140:       }
 141:     catch (NoSuchMethodException e)
 142:       {
 143:         throw new AssertionError(e);
 144:       }
 145:     catch (InstantiationException e)
 146:       {
 147:         throw new AssertionError(e);
 148:       }
 149:     catch (InvocationTargetException e)
 150:       {
 151:         throw new AssertionError(e);
 152:       }
 153:     return f;
 154:   }
 155: 
 156:   /**
 157:    * Creates a font, reading the glyph definitions from a stream.
 158:    *
 159:    * <p>This method provides the platform-specific implementation for
 160:    * the static factory method {@link Font#createFont(int,
 161:    * java.io.InputStream)}.
 162:    *
 163:    * @param format the format of the font data, such as {@link
 164:    * Font#TRUETYPE_FONT}. An implementation may ignore this argument
 165:    * if it is able to automatically recognize the font format from the
 166:    * provided data.
 167:    *
 168:    * @param stream an input stream from where the font data is read
 169:    * in. The stream will be advanced to the position after the font
 170:    * data, but not closed.
 171:    *
 172:    * @throws IllegalArgumentException if <code>format</code> is
 173:    * not supported.
 174:    * 
 175:    * @throws FontFormatException if <code>stream</code> does not
 176:    * contain data in the expected format, or if required tables are
 177:    * missing from a font.
 178:    *
 179:    * @throws IOException if a problem occurs while reading in the
 180:    * contents of <code>stream</code>.
 181:    */
 182:   public abstract Font createFont(int format, InputStream stream);
 183: 
 184:   /**
 185:    * Creates a RobotPeer on a given GraphicsDevice.
 186:    */
 187:   public abstract RobotPeer createRobot (GraphicsDevice screen)
 188:     throws AWTException;
 189: 
 190:   /**
 191:    * Creates an embedded window peer, and associates it with an
 192:    * EmbeddedWindow object.
 193:    *
 194:    * @param w The embedded window with which to associate a peer.
 195:    */
 196:   public abstract EmbeddedWindowPeer createEmbeddedWindow (EmbeddedWindow w);
 197: 
 198:   /**
 199:    * Used to register ImageIO SPIs provided by the toolkit.
 200:    *
 201:    * Our default implementation does nothing.
 202:    */
 203:    public void registerImageIOSpis(IIORegistry reg)
 204:    {
 205:    }
 206: 
 207:    /**
 208:     * Returns the number of mouse buttons.
 209:     * (used by java.awt.MouseInfo).
 210:     *
 211:     * This dummy implementation returns -1 (no mouse).
 212:     * toolkit implementors should overload this method if possible.
 213:     * @since 1.5
 214:     */
 215:    public int getMouseNumberOfButtons() 
 216:    {
 217:      return -1;
 218:    }
 219:    
 220:    /* (non-Javadoc)
 221:     * @see java.awt.Toolkit#createDesktopPeer(java.awt.Desktop)
 222:     */
 223:    protected DesktopPeer createDesktopPeer(Desktop target)
 224:      throws HeadlessException
 225:    {
 226:      if (GraphicsEnvironment.isHeadless())
 227:        throw new HeadlessException();
 228:      
 229:      return ClasspathDesktopPeer.getDesktop();
 230:    }
 231: 
 232: }