View Javadoc

1   /***********************************************************************************************************************
2   Copyright (c) 2003, International Barcode Consortium
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without modification,
6   are permitted provided that the following conditions are met:
7   
8       * Redistributions of source code must retain the above copyright notice, this list of
9         conditions and the following disclaimer.
10      * Redistributions in binary form must reproduce the above copyright notice, this list of
11        conditions and the following disclaimer in the documentation and/or other materials
12        provided with the distribution.
13      * Neither the name of the International Barcode Consortium nor the names of any contributors may be used to endorse
14        or promote products derived from this software without specific prior written permission.
15  
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  POSSIBILITY OF SUCH DAMAGE.
25  ***********************************************************************************************************************/
26  
27  package net.sourceforge.barbecue.env;
28  
29  /**
30   * Factory class for getting hold of the current operating environment.
31   *
32   * @author <a href="mailto:opensource@ianbourke.com">Ian Bourke</a>
33   */
34  public class EnvironmentFactory {
35  
36  	private static Environment env;
37  	private static Environment defaultEnvironment;
38  
39  	///CLOVER:OFF
40  	/** Cannot construct directly */
41  	protected EnvironmentFactory() {
42  	}
43  	///CLOVER:ON
44  
45  	/**
46  	 * Returns the current operating environment.
47  	 * @return The current environment
48  	 */
49  	public static Environment getEnvironment() {
50  		if (env == null) {
51  			determineCurrentEnvironment();
52  		}
53  		return env;
54  	}
55  
56  	/**
57  	 * Forces the factory to assume headless mode, regardless of whether
58  	 * this is actually true or not.
59  	 */
60  	public static void setHeadlessMode() {
61  		env = new HeadlessEnvironment();
62  	}
63  
64      /**
65  	 * Forces the factory to use the environment that does not access the AWT.
66  	 */
67  	public static void setNonAWTMode() {
68  		env = new NonAWTEnvironment(HeadlessEnvironment.DEFAULT_RESOLUTION);
69  	}
70  
71  	/**
72  	 * Forces the factory to use the environment that does not access the AWT.
73  	 * @param resolution The desired (or current) screen/output resolution
74  	 */
75  	public static void setNonAWTMode(int resolution) {
76  		env = new NonAWTEnvironment(resolution);
77  	}
78  
79  	/**
80  	 * Sets the factory to use the default (discovered) environment.
81  	 */
82  	public static void setDefaultMode() {
83  		determineCurrentEnvironment();
84  	}
85  
86  	/**
87  	 * Sets the default environment for the factory.
88  	 * Use this to set your own environment implementation.
89  	 * @param newEnv The new default environment
90  	 */
91  	public static void setDefaultEnvironment(Environment newEnv) {
92  		env = null;
93  		defaultEnvironment = newEnv;
94  	}
95  
96  	private static void determineCurrentEnvironment() {
97  		Environment current;
98  		if (defaultEnvironment != null) {
99  			current = defaultEnvironment;
100 		} else {
101 			current = new DefaultEnvironment();
102 		}
103 		try {
104 			// Try to get the res, this will fail in headless mode
105 			current.getResolution();
106 		} catch (UnsupportedOperationException e) {
107 			current = new HeadlessEnvironment();
108 		} catch (InternalError e) {
109 			current = new HeadlessEnvironment();
110 		}
111 		env = current;
112 	}
113 }