SunGraphicsEnvironment.java revision 3358
0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d;
0N/A
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Font;
0N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.GraphicsConfiguration;
0N/Aimport java.awt.GraphicsDevice;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Toolkit;
0N/Aimport java.awt.font.TextAttribute;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.awt.peer.ComponentPeer;
658N/Aimport java.io.BufferedReader;
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FilenameFilter;
0N/Aimport java.io.InputStreamReader;
0N/Aimport java.io.IOException;
0N/Aimport java.text.AttributedCharacterIterator;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Locale;
0N/Aimport java.util.Map;
0N/Aimport java.util.NoSuchElementException;
0N/Aimport java.util.Set;
0N/Aimport java.util.StringTokenizer;
0N/Aimport java.util.TreeMap;
0N/Aimport java.util.Vector;
0N/Aimport java.util.concurrent.ConcurrentHashMap;
0N/Aimport sun.awt.AppContext;
0N/Aimport sun.awt.DisplayChangedListener;
0N/Aimport sun.awt.FontConfiguration;
0N/Aimport sun.awt.SunDisplayChanger;
0N/Aimport sun.font.CompositeFontDescriptor;
0N/Aimport sun.font.Font2D;
0N/Aimport sun.font.FontManager;
0N/Aimport sun.font.FontManagerFactory;
0N/Aimport sun.font.FontManagerForSGE;
0N/Aimport sun.font.NativeFont;
0N/A
0N/A/**
0N/A * This is an implementation of a GraphicsEnvironment object for the
0N/A * default local GraphicsEnvironment.
0N/A *
0N/A * @see GraphicsDevice
0N/A * @see GraphicsConfiguration
0N/A */
0N/Apublic abstract class SunGraphicsEnvironment extends GraphicsEnvironment
0N/A implements DisplayChangedListener {
0N/A
0N/A public static boolean isOpenSolaris;
0N/A private static Font defaultFont;
0N/A
0N/A public SunGraphicsEnvironment() {
0N/A java.security.AccessController.doPrivileged(
0N/A new java.security.PrivilegedAction() {
0N/A public Object run() {
0N/A String version = System.getProperty("os.version", "0.0");
0N/A try {
0N/A float ver = Float.parseFloat(version);
0N/A if (ver > 5.10f) {
0N/A File f = new File("/etc/release");
0N/A FileInputStream fis = new FileInputStream(f);
0N/A InputStreamReader isr
0N/A = new InputStreamReader(fis, "ISO-8859-1");
0N/A BufferedReader br = new BufferedReader(isr);
0N/A String line = br.readLine();
0N/A if (line.indexOf("OpenSolaris") >= 0) {
0N/A isOpenSolaris = true;
0N/A }
0N/A fis.close();
}
} catch (Exception e) {
}
/* Establish the default font to be used by SG2D etc */
defaultFont = new Font(Font.DIALOG, Font.PLAIN, 12);
return null;
}
});
}
protected GraphicsDevice[] screens;
/**
* Returns an array of all of the screen devices.
*/
public synchronized GraphicsDevice[] getScreenDevices() {
GraphicsDevice[] ret = screens;
if (ret == null) {
int num = getNumScreens();
ret = new GraphicsDevice[num];
for (int i = 0; i < num; i++) {
ret[i] = makeScreenDevice(i);
}
screens = ret;
}
return ret;
}
/**
* Returns the number of screen devices of this graphics environment.
*
* @return the number of screen devices of this graphics environment
*/
protected abstract int getNumScreens();
/**
* Create and return the screen device with the specified number. The
* device with number <code>0</code> will be the default device (returned
* by {@link #getDefaultScreenDevice()}.
*
* @param screennum the number of the screen to create
*
* @return the created screen device
*/
protected abstract GraphicsDevice makeScreenDevice(int screennum);
/**
* Returns the default screen graphics device.
*/
public GraphicsDevice getDefaultScreenDevice() {
return getScreenDevices()[0];
}
/**
* Returns a Graphics2D object for rendering into the
* given BufferedImage.
* @throws NullPointerException if BufferedImage argument is null
*/
public Graphics2D createGraphics(BufferedImage img) {
if (img == null) {
throw new NullPointerException("BufferedImage cannot be null");
}
SurfaceData sd = SurfaceData.getPrimarySurfaceData(img);
return new SunGraphics2D(sd, Color.white, Color.black, defaultFont);
}
public static FontManagerForSGE getFontManagerForSGE() {
FontManager fm = FontManagerFactory.getInstance();
return (FontManagerForSGE) fm;
}
/**
* Returns all fonts available in this environment.
*/
public Font[] getAllFonts() {
FontManagerForSGE fm = getFontManagerForSGE();
Font[] installedFonts = fm.getAllInstalledFonts();
Font[] created = fm.getCreatedFonts();
if (created == null || created.length == 0) {
return installedFonts;
} else {
int newlen = installedFonts.length + created.length;
Font [] fonts = java.util.Arrays.copyOf(installedFonts, newlen);
System.arraycopy(created, 0, fonts,
installedFonts.length, created.length);
return fonts;
}
}
public String[] getAvailableFontFamilyNames(Locale requestedLocale) {
FontManagerForSGE fm = getFontManagerForSGE();
String[] installed = fm.getInstalledFontFamilyNames(requestedLocale);
/* Use a new TreeMap as used in getInstalledFontFamilyNames
* and insert all the keys in lower case, so that the sort order
* is the same as the installed families. This preserves historical
* behaviour and inserts new families in the right place.
* It would have been marginally more efficient to directly obtain
* the tree map and just insert new entries, but not so much as
* to justify the extra internal interface.
*/
TreeMap<String, String> map = fm.getCreatedFontFamilyNames();
if (map == null || map.size() == 0) {
return installed;
} else {
for (int i=0; i<installed.length; i++) {
map.put(installed[i].toLowerCase(requestedLocale),
installed[i]);
}
String[] retval = new String[map.size()];
Object [] keyNames = map.keySet().toArray();
for (int i=0; i < keyNames.length; i++) {
retval[i] = (String)map.get(keyNames[i]);
}
return retval;
}
}
public String[] getAvailableFontFamilyNames() {
return getAvailableFontFamilyNames(Locale.getDefault());
}
/**
* Return the bounds of a GraphicsDevice, less its screen insets.
* See also java.awt.GraphicsEnvironment.getUsableBounds();
*/
public static Rectangle getUsableBounds(GraphicsDevice gd) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
Rectangle usableBounds = gc.getBounds();
usableBounds.x += insets.left;
usableBounds.y += insets.top;
usableBounds.width -= (insets.left + insets.right);
usableBounds.height -= (insets.top + insets.bottom);
return usableBounds;
}
/**
* From the DisplayChangedListener interface; called
* when the display mode has been changed.
*/
public void displayChanged() {
// notify screens in device array to do display update stuff
for (GraphicsDevice gd : getScreenDevices()) {
if (gd instanceof DisplayChangedListener) {
((DisplayChangedListener) gd).displayChanged();
}
}
// notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and
// SurfaceDataProxies) about the display change event
displayChanger.notifyListeners();
}
/**
* Part of the DisplayChangedListener interface:
* propagate this event to listeners
*/
public void paletteChanged() {
displayChanger.notifyPaletteChanged();
}
/**
* Returns true when the display is local, false for remote displays.
*
* @return true when the display is local, false for remote displays
*/
public abstract boolean isDisplayLocal();
/*
* ----DISPLAY CHANGE SUPPORT----
*/
protected SunDisplayChanger displayChanger = new SunDisplayChanger();
/**
* Add a DisplayChangeListener to be notified when the display settings
* are changed.
*/
public void addDisplayChangedListener(DisplayChangedListener client) {
displayChanger.add(client);
}
/**
* Remove a DisplayChangeListener from Win32GraphicsEnvironment
*/
public void removeDisplayChangedListener(DisplayChangedListener client) {
displayChanger.remove(client);
}
/*
* ----END DISPLAY CHANGE SUPPORT----
*/
/**
* Returns true if FlipBufferStrategy with COPIED buffer contents
* is preferred for this peer's GraphicsConfiguration over
* BlitBufferStrategy, false otherwise.
*
* The reason FlipBS could be preferred is that in some configurations
* an accelerated copy to the screen is supported (like Direct3D 9)
*
* @return true if flip strategy should be used, false otherwise
*/
public boolean isFlipStrategyPreferred(ComponentPeer peer) {
return false;
}
}