PageRenderTime 87ms CodeModel.GetById 60ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/OperatingSystem.java

#
Java | 361 lines | 225 code | 30 blank | 106 comment | 57 complexity | 27bb53a79e5b6fbd9c597a34591b264a MD5 | raw file
  1/*
  2 * :tabSize=8:indentSize=8:noTabs=false:
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * OperatingSystem.java - OS detection
  6 * :tabSize=8:indentSize=8:noTabs=false:
  7 * :folding=explicit:collapseFolds=1:
  8 *
  9 * Copyright (C) 2002, 2003 Slava Pestov
 10 *
 11 * This program is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU General Public License
 13 * as published by the Free Software Foundation; either version 2
 14 * of the License, or any later version.
 15 *
 16 * This program is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 * GNU General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program; if not, write to the Free Software
 23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 24 */
 25
 26package org.gjt.sp.jedit;
 27
 28import java.awt.GraphicsConfiguration;
 29import java.awt.GraphicsDevice;
 30import java.awt.GraphicsEnvironment;
 31import java.awt.Rectangle;
 32import java.awt.Toolkit;
 33import javax.swing.UIManager;
 34import java.io.File;
 35import java.util.Enumeration;
 36import java.util.Vector;
 37import org.gjt.sp.util.Log;
 38
 39/**
 40 * Operating system detection routines.
 41 * @author Slava Pestov
 42 * @version $Id: OperatingSystem.java 4939 2003-12-25 00:09:42Z spestov $
 43 * @since jEdit 4.0pre4
 44 */
 45public class OperatingSystem
 46{
 47	//{{{ getScreenBounds() method
 48	/**
 49	 * Returns the bounds of the default screen.
 50	 */
 51	public static final Rectangle getScreenBounds()
 52	{
 53		int screenX = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
 54		int screenY = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
 55		int x, y, w, h;
 56		
 57		if (isMacOS())
 58		{
 59			x = 0;
 60			y = 22;
 61			w = screenX;
 62			h = screenY - y - 4;//shadow size
 63		}
 64		else if (isWindows())
 65		{
 66			x = -4;
 67			y = -4;
 68			w = screenX - 2*x;
 69			h = screenY - 2*y;
 70		}
 71		else
 72		{
 73			x = 0;
 74			y = 0;
 75			w = screenX;
 76			h = screenY;
 77		}
 78		
 79		return new Rectangle(x,y,w,h);
 80	} //}}}
 81
 82	//{{{ getScreenBounds() method
 83	/**
 84	 * Returns the bounds of the (virtual) screen that the window should be in
 85	 * @param window The bounds of the window to get the screen for
 86	 */
 87	public static final Rectangle getScreenBounds(Rectangle window)
 88	{
 89		GraphicsDevice[] gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
 90		Vector intersects = new Vector();
 91
 92		// Get available screens
 93		// O(n^3), this is nasty, but since we aren't dealling with
 94		// many items it should be fine
 95		for (int i=0; i < gd.length; i++)
 96		{
 97			GraphicsConfiguration gc = gd[i]
 98				.getDefaultConfiguration();
 99			// Don't add duplicates
100			if (window.intersects(gc.getBounds()))
101			{
102				for (Enumeration e = intersects.elements(); e.hasMoreElements();)
103				{
104					GraphicsConfiguration gcc = (GraphicsConfiguration)e.nextElement();
105					if (gcc.getBounds().equals(gc.getBounds()))
106						break;
107				}
108				intersects.add(gc);
109			}
110		}
111		
112		GraphicsConfiguration choice = null;
113		if (intersects.size() > 0)
114		{
115			// Pick screen with largest intersection
116			for (Enumeration e = intersects.elements(); e.hasMoreElements();)
117			{
118				GraphicsConfiguration gcc = (GraphicsConfiguration)e.nextElement();
119				if (choice == null)
120					choice = gcc;
121				else
122				{
123					Rectangle int1 = choice.getBounds().intersection(window);
124					Rectangle int2 = gcc.getBounds().intersection(window);
125					int area1 = int1.width * int1.height;
126					int area2 = int2.width * int2.height;
127					if (area2 > area1)
128						choice = gcc;
129				}
130			}
131		}
132		else
133			choice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
134		
135		// Make adjustments for some OS's
136		int screenX = (int)choice.getBounds().x;
137		int screenY = (int)choice.getBounds().y;
138		int screenW = (int)choice.getBounds().width;
139		int screenH = (int)choice.getBounds().height;
140		int x, y, w, h;
141		
142		if (isMacOS())
143		{
144			x = screenX;
145			y = screenY + 22;
146			w = screenW;
147			h = screenH - y - 4;//shadow size
148		}
149		else if (isWindows())
150		{
151			x = screenX - 4;
152			y = screenY - 4;
153			w = screenW - 2*x;
154			h = screenH - 2*y;
155		}
156		else
157		{
158			x = screenX;
159			y = screenY;
160			w = screenW;
161			h = screenH;
162		}
163		
164		// Yay, we're finally there
165		return new Rectangle(x,y,w,h);
166	} //}}}
167
168	//{{{ isDOSDerived() method
169	/**
170	 * Returns if we're running Windows 95/98/ME/NT/2000/XP, or OS/2.
171	 */
172	public static final boolean isDOSDerived()
173	{
174		return isWindows() || isOS2();
175	} //}}}
176
177	//{{{ isWindows() method
178	/**
179	 * Returns if we're running Windows 95/98/ME/NT/2000/XP.
180	 */
181	public static final boolean isWindows()
182	{
183		return os == WINDOWS_9x || os == WINDOWS_NT;
184	} //}}}
185
186	//{{{ isWindows9x() method
187	/**
188	 * Returns if we're running Windows 95/98/ME.
189	 */
190	public static final boolean isWindows9x()
191	{
192		return os == WINDOWS_9x;
193	} //}}}
194
195	//{{{ isWindowsNT() method
196	/**
197	 * Returns if we're running Windows NT/2000/XP.
198	 */
199	public static final boolean isWindowsNT()
200	{
201		return os == WINDOWS_NT;
202	} //}}}
203
204	//{{{ isOS2() method
205	/**
206	 * Returns if we're running OS/2.
207	 */
208	public static final boolean isOS2()
209	{
210		return os == OS2;
211	} //}}}
212
213	//{{{ isUnix() method
214	/**
215	 * Returns if we're running Unix (this includes MacOS X).
216	 */
217	public static final boolean isUnix()
218	{
219		return os == UNIX || os == MAC_OS_X;
220	} //}}}
221
222	//{{{ isMacOS() method
223	/**
224	 * Returns if we're running MacOS X.
225	 */
226	public static final boolean isMacOS()
227	{
228		return os == MAC_OS_X;
229	} //}}}
230
231	//{{{ isX11() method
232	/**
233	 * Returns if this OS is likely to be using X11 as the graphics
234	 * system.
235	 * @since jEdit 4.2pre3
236	 */
237	public static boolean isX11()
238	{
239		return os == UNIX;
240	} //}}}
241
242	//{{{ isVMS() method
243	/**
244	 * Returns if we're running VMS.
245	 */
246	public static final boolean isVMS()
247	{
248		return os == VMS;
249	} //}}}
250
251	//{{{ isMacOSLF() method
252	/**
253	* Returns if we're running MacOS X and using the native look and feel.
254	*/
255	public static final boolean isMacOSLF()
256	{
257		return (isMacOS() && UIManager.getLookAndFeel().isNativeLookAndFeel());
258	} //}}}
259
260	//{{{ hasScreenMenuBar
261	/**
262	 * Returns whether the screen menu bar on Mac OS X is in use.
263	 * @since jEdit 4.2pre1
264	*/
265	public static final boolean hasScreenMenuBar()
266	{
267		if(!isMacOS())
268			return false;
269		else if(hasScreenMenuBar == -1)
270		{
271			String result = System.getProperty("apple.laf.useScreenMenuBar");
272			if (result == null)
273				result = System.getProperty("com.apple.macos.useScreenMenuBar");
274			hasScreenMenuBar = ("true".equals(result)) ? 1 : 0;
275		}
276
277		return (hasScreenMenuBar == 1);
278	} //}}}
279
280	//{{{ isJava14() method
281	/**
282	 * Returns if Java 2 version 1.4, or Java 2 version 1.5 is in use.
283	 */
284	public static final boolean hasJava14()
285	{
286		return java14;
287	} //}}}
288
289	//{{{ isJava15() method
290	/**
291	 * Returns if Java 2 version 1.5 is in use.
292	 */
293	public static final boolean hasJava15()
294	{
295		return java15;
296	} //}}}
297
298	//{{{ Private members
299	private static final int UNIX = 0x31337;
300	private static final int WINDOWS_9x = 0x640;
301	private static final int WINDOWS_NT = 0x666;
302	private static final int OS2 = 0xDEAD;
303	private static final int MAC_OS_X = 0xABC;
304	private static final int VMS = 0xDEAD2;
305	private static final int UNKNOWN = 0xBAD;
306
307	private static int os;
308	private static boolean java14;
309	private static boolean java15;
310	private static int hasScreenMenuBar = -1;
311
312	//{{{ Class initializer
313	static
314	{
315		if(System.getProperty("mrj.version") != null)
316		{
317			os = MAC_OS_X;
318		}
319		else
320		{
321			String osName = System.getProperty("os.name");
322			if(osName.indexOf("Windows 9") != -1
323				|| osName.indexOf("Windows M") != -1)
324			{
325				os = WINDOWS_9x;
326			}
327			else if(osName.indexOf("Windows") != -1)
328			{
329				os = WINDOWS_NT;
330			}
331			else if(osName.indexOf("OS/2") != -1)
332			{
333				os = OS2;
334			}
335			else if(osName.indexOf("VMS") != -1)
336			{
337				os = VMS;
338			}
339			else if(File.separatorChar == '/')
340			{
341				os = UNIX;
342			}
343			else
344			{
345				os = UNKNOWN;
346				Log.log(Log.WARNING,OperatingSystem.class,
347					"Unknown operating system: " + osName);
348			}
349		}
350
351		// for debugging, make jEdit think its using a different
352		// version of Java than it really is.
353		String javaVersion = System.getProperty("jedit.force.java.version");
354		if(javaVersion == null || javaVersion.equals(""))
355			javaVersion = System.getProperty("java.version");
356		java14 = (javaVersion.compareTo("1.4") >= 0);
357		java15 = (javaVersion.compareTo("1.5") >= 0);
358	} //}}}
359
360	//}}}
361}