1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Oleg V. Khaschansky
19 * @version $Revision$
20 */
21
22package java.awt;
23
24import java.awt.image.BufferedImage;
25import java.util.Locale;
26
27import org.apache.harmony.awt.ContextStorage;
28import org.apache.harmony.awt.gl.CommonGraphics2DFactory;
29
30/**
31 * The GraphicsEnvironment class defines a collection of GraphicsDevice objects
32 * and Font objects which are available for Java application on current
33 * platform.
34 *
35 * @since Android 1.0
36 */
37public abstract class GraphicsEnvironment {
38
39    /**
40     * Constructor could not be used directly and should be obtained in extended
41     * classes.
42     */
43    protected GraphicsEnvironment() {
44    }
45
46    /**
47     * Gets the local GraphicsEnvironment.
48     *
49     * @return the local GraphicsEnvironment.
50     */
51    public static GraphicsEnvironment getLocalGraphicsEnvironment() {
52        synchronized (ContextStorage.getContextLock()) {
53            if (ContextStorage.getGraphicsEnvironment() == null) {
54                if (isHeadless()) {
55                    ContextStorage.setGraphicsEnvironment(new HeadlessGraphicsEnvironment());
56                } else {
57                    CommonGraphics2DFactory g2df = (CommonGraphics2DFactory)Toolkit
58                            .getDefaultToolkit().getGraphicsFactory();
59
60                    ContextStorage.setGraphicsEnvironment(g2df
61                            .createGraphicsEnvironment(ContextStorage.getWindowFactory()));
62                }
63            }
64
65            return ContextStorage.getGraphicsEnvironment();
66        }
67    }
68
69    /**
70     * Returns whether or not a display, keyboard, and mouse are supported in
71     * this graphics environment.
72     *
73     * @return true, if HeadlessException will be thrown from areas of the
74     *         graphics environment that are dependent on a display, keyboard,
75     *         or mouse, false otherwise.
76     */
77    public boolean isHeadlessInstance() {
78        return false;
79    }
80
81    /**
82     * Checks whether or not a display, keyboard, and mouse are supported in
83     * this environment.
84     *
85     * @return true, if a HeadlessException is thrown from areas of the Toolkit
86     *         and GraphicsEnvironment that are dependent on a display,
87     *         keyboard, or mouse, false otherwise.
88     */
89    public static boolean isHeadless() {
90        return "true".equals(System.getProperty("java.awt.headless"));
91    }
92
93    /**
94     * Gets the maximum bounds of system centered windows.
95     *
96     * @return the maximum bounds of system centered windows.
97     * @throws HeadlessException
98     *             if isHeadless() method returns true.
99     */
100    public Rectangle getMaximumWindowBounds() throws HeadlessException {
101        return getDefaultScreenDevice().getDefaultConfiguration().getBounds();
102    }
103
104    /**
105     * Gets the Point which should defines the center of system window.
106     *
107     * @return the Point where the system window should be centered.
108     * @throws HeadlessException
109     *             if isHeadless() method returns true.
110     */
111    public Point getCenterPoint() throws HeadlessException {
112        Rectangle mwb = getMaximumWindowBounds();
113        return new Point(mwb.width >> 1, mwb.height >> 1);
114    }
115
116    /**
117     * Indicates that the primary font should be used. Primary font is specified
118     * by initial system locale or default encoding).
119     */
120    public void preferLocaleFonts() {
121        // Note: API specification says following:
122        // "The actual change in font rendering behavior resulting
123        // from a call to this method is implementation dependent;
124        // it may have no effect at all." So, doing nothing is an
125        // acceptable behavior for this method.
126
127        // For now FontManager uses 1.4 font.properties scheme for font mapping,
128        // so
129        // this method doesn't make any sense. The implementation of this method
130        // which will influence font mapping is postponed until
131        // 1.5 mapping scheme not implemented.
132
133        // todo - Implement non-default behavior with 1.5 font mapping scheme
134    }
135
136    /**
137     * Indicates that a proportional preference of the font should be used.
138     */
139    public void preferProportionalFonts() {
140        // Note: API specification says following:
141        // "The actual change in font rendering behavior resulting
142        // from a call to this method is implementation dependent;
143        // it may have no effect at all." So, doing nothing is an
144        // acceptable behavior for this method.
145
146        // For now FontManager uses 1.4 font.properties scheme for font mapping,
147        // so
148        // this method doesn't make any sense. The implementation of this method
149        // which will influence font mapping is postponed until
150        // 1.5 mapping scheme not implemented.
151
152        // todo - Implement non-default behavior with 1.5 font mapping scheme
153    }
154
155    /**
156     * Creates the Graphics2D object for rendering to the specified
157     * BufferedImage.
158     *
159     * @param bufferedImage
160     *            the BufferedImage object.
161     * @return the Graphics2D object which allows to render to the specified
162     *         BufferedImage.
163     */
164    public abstract Graphics2D createGraphics(BufferedImage bufferedImage);
165
166    /**
167     * Gets the array of all available fonts instances in this
168     * GraphicsEnviroments.
169     *
170     * @return the array of all available fonts instances in this
171     *         GraphicsEnviroments.
172     */
173    public abstract Font[] getAllFonts();
174
175    /**
176     * Gets the array of all available font family names.
177     *
178     * @return the array of all available font family names.
179     */
180    public abstract String[] getAvailableFontFamilyNames();
181
182    /**
183     * Gets the array of all available font family names for the specified
184     * locale.
185     *
186     * @param locale
187     *            the Locale object which represents geographical region. The
188     *            default locale is used if locale is null.
189     * @return the array of available font family names for the specified
190     *         locale.
191     */
192    public abstract String[] getAvailableFontFamilyNames(Locale locale);
193
194    /**
195     * Gets the default screen device as GraphicDevice object.
196     *
197     * @return the GraphicDevice object which represents default screen device.
198     * @throws HeadlessException
199     *             if isHeadless() returns true.
200     */
201    public abstract GraphicsDevice getDefaultScreenDevice() throws HeadlessException;
202
203    /**
204     * Gets an array of all available screen devices.
205     *
206     * @return the array of GraphicsDevice objects which represents all
207     *         available screen devices.
208     * @throws HeadlessException
209     *             if isHeadless() returns true.
210     */
211    public abstract GraphicsDevice[] getScreenDevices() throws HeadlessException;
212}
213