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