Merge from the pain train
[official-gcc.git] / libjava / gnu / java / awt / ClasspathToolkit.java
blob72302e11c3046a6f88568aa0db8d98d77548430d
1 /* ClasspathToolkit.java -- Abstract superclass for Classpath toolkits.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package gnu.java.awt;
41 import gnu.java.awt.peer.ClasspathFontPeer;
42 import gnu.java.awt.peer.ClasspathTextLayoutPeer;
44 import java.awt.AWTException;
45 import java.awt.Dimension;
46 import java.awt.DisplayMode;
47 import java.awt.EventQueue;
48 import java.awt.Font;
49 import java.awt.FontMetrics;
50 import java.awt.GraphicsDevice;
51 import java.awt.GraphicsEnvironment;
52 import java.awt.Image;
53 import java.awt.Toolkit;
54 import java.awt.font.FontRenderContext;
55 import java.awt.image.ColorModel;
56 import java.awt.image.ImageProducer;
57 import java.awt.peer.RobotPeer;
58 import java.io.File;
59 import java.io.InputStream;
60 import java.net.MalformedURLException;
61 import java.net.URL;
62 import java.text.AttributedString;
63 import java.util.HashMap;
64 import java.util.Map;
66 import javax.imageio.spi.IIORegistry;
68 /**
69 * An abstract superclass for Classpath toolkits.
71 * <p>There exist some parts of AWT and Java2D that are specific to
72 * the underlying platform, but for which the {@link Toolkit} class
73 * does not provide suitable abstractions. Examples include some
74 * methods of {@link Font} or {@link GraphicsEnvironment}. Those
75 * methods use ClasspathToolkit as a central place for obtaining
76 * platform-specific functionality.
78 * <p>In addition, ClasspathToolkit implements some abstract methods
79 * of {@link java.awt.Toolkit} that are not really platform-specific,
80 * such as the maintenance of a cache of loaded images.
82 * <p><b>Thread Safety:</b> The methods of this class may safely be
83 * called without external synchronization. This also hold for any
84 * inherited {@link Toolkit} methods. Subclasses are responsible for
85 * the necessary synchronization.
87 * @author Sascha Brawer (brawer@dandelis.ch)
89 public abstract class ClasspathToolkit
90 extends Toolkit
92 /**
93 * A map from URLs to previously loaded images, used by {@link
94 * #getImage(java.net.URL)}. For images that were loaded via a path
95 * to an image file, the map contains a key with a file URL.
97 private HashMap imageCache;
101 * Returns a shared instance of the local, platform-specific
102 * graphics environment.
104 * <p>This method is specific to GNU Classpath. It gets called by
105 * the Classpath implementation of {@link
106 * GraphicsEnvironment.getLocalGraphcisEnvironment()}.
108 public abstract GraphicsEnvironment getLocalGraphicsEnvironment();
112 * Determines the current size of the default, primary screen.
114 * @throws HeadlessException if the local graphics environment is
115 * headless, which means that no screen is attached and no user
116 * interaction is allowed.
118 public Dimension getScreenSize()
120 DisplayMode mode;
122 // getDefaultScreenDevice throws HeadlessException if the
123 // local graphics environment is headless.
124 mode = GraphicsEnvironment.getLocalGraphicsEnvironment()
125 .getDefaultScreenDevice().getDisplayMode();
127 return new Dimension(mode.getWidth(), mode.getHeight());
132 * Determines the current color model of the default, primary
133 * screen.
135 * @see GraphicsEnvironment#getDefaultScreenDevice()
136 * @see java.awt.GraphicsDevice#getDefaultConfiguration()
137 * @see java.awt.GraphicsConfiguration#getColorModel()
139 * @throws HeadlessException if the local graphics environment is
140 * headless, which means that no screen is attached and no user
141 * interaction is allowed.
143 public ColorModel getColorModel()
145 // getDefaultScreenDevice throws HeadlessException if the
146 // local graphics environment is headless.
147 return GraphicsEnvironment.getLocalGraphicsEnvironment()
148 .getDefaultScreenDevice().getDefaultConfiguration()
149 .getColorModel();
153 * Retrieves the metrics for rendering a font on the screen.
155 * @param font the font whose metrics are requested.
157 public FontMetrics getFontMetrics(Font font)
159 return ((ClasspathFontPeer) font.getPeer ()).getFontMetrics (font);
164 * Acquires an appropriate {@link ClasspathFontPeer}, for use in
165 * classpath's implementation of {@link java.awt.Font}.
167 * @param name The logical name of the font. This may be either a face
168 * name or a logical font name, or may even be null. A default
169 * implementation of name decoding is provided in
170 * {@link ClasspathFontPeer}, but may be overridden in other toolkits.
172 * @param attrs Any extra {@link java.awt.font.TextAttribute} attributes
173 * this font peer should have, such as size, weight, family name, or
174 * transformation.
176 public abstract ClasspathFontPeer getClasspathFontPeer (String name, Map attrs);
178 public abstract ClasspathTextLayoutPeer
179 getClasspathTextLayoutPeer (AttributedString str, FontRenderContext frc);
182 /**
183 * Creates a {@link Font}, in a platform-specific manner.
185 * The default implementation simply constructs a {@link Font}, but some
186 * toolkits may wish to override this, to return {@link Font} subclasses which
187 * implement {@link java.awt.font.OpenType} or
188 * {@link java.awt.font.MultipleMaster}.
190 public Font getFont (String name, Map attrs)
192 return new Font (name, attrs);
197 * Creates a font, reading the glyph definitions from a stream.
199 * <p>This method provides the platform-specific implementation for
200 * the static factory method {@link Font#createFont(int,
201 * java.io.InputStream)}.
203 * @param format the format of the font data, such as {@link
204 * Font#TRUETYPE_FONT}. An implementation may ignore this argument
205 * if it is able to automatically recognize the font format from the
206 * provided data.
208 * @param stream an input stream from where the font data is read
209 * in. The stream will be advanced to the position after the font
210 * data, but not closed.
212 * @throws IllegalArgumentException if <code>format</code> is
213 * not supported.
215 * @throws FontFormatException if <code>stream</code> does not
216 * contain data in the expected format, or if required tables are
217 * missing from a font.
219 * @throws IOException if a problem occurs while reading in the
220 * contents of <code>stream</code>.
222 public abstract Font createFont(int format, InputStream stream);
226 * Returns an image from the specified file, which must be in a
227 * recognized format. The set of recognized image formats may vary
228 * from toolkit to toolkit.
230 * <p>This method maintains a cache for images. If an image has been
231 * loaded from the same path before, the cached copy will be
232 * returned. The implementation may hold cached copies for an
233 * indefinite time, which can consume substantial resources with
234 * large images. Users are therefore advised to use {@link
235 * #createImage(java.lang.String)} instead.
237 * <p>The default implementation creates a file URL for the
238 * specified path and invokes {@link #getImage(URL)}.
240 * @param path A path to the image file.
242 * @return IllegalArgumentException if <code>path</code> does not
243 * designate a valid path.
245 public Image getImage(String path)
249 return getImage(new File(path).toURL());
251 catch (MalformedURLException muex)
253 throw (IllegalArgumentException) new IllegalArgumentException(path)
254 .initCause(muex);
260 * Loads an image from the specified URL. The image data must be in
261 * a recognized format. The set of recognized image formats may vary
262 * from toolkit to toolkit.
264 * <p>This method maintains a cache for images. If an image has been
265 * loaded from the same URL before, the cached copy will be
266 * returned. The implementation may hold cached copies for an
267 * indefinite time, which can consume substantial resources with
268 * large images. Users are therefore advised to use {@link
269 * #createImage(java.net.URL)} instead.
271 * @param url the URL from where the image is read.
273 public Image getImage(URL url)
275 Image result;
277 synchronized (this)
279 // Many applications never call getImage. Therefore, we lazily
280 // create the image cache when it is actually needed.
281 if (imageCache == null)
282 imageCache = new HashMap();
283 else
285 result = (Image) imageCache.get(url);
286 if (result != null)
287 return result;
290 // The createImage(URL) method, which is specified by
291 // java.awt.Toolkit, is not implemented by this abstract class
292 // because it is platform-dependent. Once Classpath has support
293 // for the javax.imageio package, it might be worth considering
294 // that toolkits provide native stream readers. Then, the class
295 // ClasspathToolkit could provide a general implementation that
296 // delegates the image format parsing to javax.imageio.
297 result = createImage(url);
299 // It is not clear whether it would be a good idea to use weak
300 // references here. The advantage would be reduced memory
301 // consumption, since loaded images would not be kept
302 // forever. But on VMs that frequently perform garbage
303 // collection (which includes VMs with a parallel or incremental
304 // collector), the image might frequently need to be re-loaded,
305 // possibly over a slow network connection.
306 imageCache.put(url, result);
308 return result;
314 * Returns an image from the specified file, which must be in a
315 * recognized format. The set of recognized image formats may vary
316 * from toolkit to toolkit.
318 * <p>A new image is created every time this method gets called,
319 * even if the same path has been passed before.
321 * <p>The default implementation creates a file URL for the
322 * specified path and invokes {@link #createImage(URL)}.
324 * @param path A path to the file to be read in.
326 public Image createImage(String path)
330 // The abstract method createImage(URL) is defined by
331 // java.awt.Toolkit, but intentionally not implemented by
332 // ClasspathToolkit because it is platform specific.
333 return createImage(new File(path).toURL());
335 catch (MalformedURLException muex)
337 throw (IllegalArgumentException) new IllegalArgumentException(path)
338 .initCause(muex);
343 * Creates an ImageProducer from the specified URL. The image is assumed
344 * to be in a recognised format. If the toolkit does not implement the
345 * image format or the image format is not recognised, null is returned.
346 * This default implementation is overriden by the Toolkit implementations.
348 * @param url URL to read image data from.
350 public ImageProducer createImageProducer(URL url)
352 return null;
355 public abstract RobotPeer createRobot (GraphicsDevice screen)
356 throws AWTException;
358 /**
359 * Used to register ImageIO SPIs provided by the toolkit.
362 public void registerImageIOSpis(IIORegistry reg)
366 public abstract boolean nativeQueueEmpty();
367 public abstract void wakeNativeQueue();
368 public abstract void iterateNativeQueue(EventQueue locked, boolean block);