PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

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

#
Java | 349 lines | 220 code | 29 blank | 100 comment | 58 complexity | 3252dc5f67e8a828eab4ddc3837eda29 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 4770 2003-06-08 22:49:09Z 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].getConfigurations();
 98L2:			for (int j=0; j < gc.length; j++)
 99			{
100				// Don't add duplicates
101				if (window.intersects(gc[j].getBounds()))
102				{
103					for (Enumeration e = intersects.elements(); e.hasMoreElements();)
104					{
105						GraphicsConfiguration gcc = (GraphicsConfiguration)e.nextElement();
106						if (gcc.getBounds().equals(gc[j].getBounds()))
107							continue L2;
108					}
109					intersects.add(gc[j]);
110				}
111			}
112		}
113		
114		GraphicsConfiguration choice = null;
115		if (intersects.size() > 0)
116		{
117			// Pick screen with largest intersection
118			for (Enumeration e = intersects.elements(); e.hasMoreElements();)
119			{
120				GraphicsConfiguration gcc = (GraphicsConfiguration)e.nextElement();
121				if (choice == null)
122					choice = gcc;
123				else
124				{
125					Rectangle int1 = choice.getBounds().intersection(window);
126					Rectangle int2 = gcc.getBounds().intersection(window);
127					int area1 = int1.width * int1.height;
128					int area2 = int2.width * int2.height;
129					if (area2 > area1)
130						choice = gcc;
131				}
132			}
133		}
134		else
135			choice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
136		
137		// Make adjustments for some OS's
138		int screenX = (int)choice.getBounds().x;
139		int screenY = (int)choice.getBounds().y;
140		int screenW = (int)choice.getBounds().width;
141		int screenH = (int)choice.getBounds().height;
142		int x, y, w, h;
143		
144		if (isMacOS())
145		{
146			x = screenX;
147			y = screenY + 22;
148			w = screenW;
149			h = screenH - y - 4;//shadow size
150		}
151		else if (isWindows())
152		{
153			x = screenX - 4;
154			y = screenY - 4;
155			w = screenW - 2*x;
156			h = screenH - 2*y;
157		}
158		else
159		{
160			x = screenX;
161			y = screenY;
162			w = screenW;
163			h = screenH;
164		}
165		
166		// Yay, we're finally there
167		return new Rectangle(x,y,w,h);
168	} //}}}
169
170	//{{{ isDOSDerived() method
171	/**
172	 * Returns if we're running Windows 95/98/ME/NT/2000/XP, or OS/2.
173	 */
174	public static final boolean isDOSDerived()
175	{
176		return isWindows() || isOS2();
177	} //}}}
178
179	//{{{ isWindows() method
180	/**
181	 * Returns if we're running Windows 95/98/ME/NT/2000/XP.
182	 */
183	public static final boolean isWindows()
184	{
185		return os == WINDOWS_9x || os == WINDOWS_NT;
186	} //}}}
187
188	//{{{ isWindows9x() method
189	/**
190	 * Returns if we're running Windows 95/98/ME.
191	 */
192	public static final boolean isWindows9x()
193	{
194		return os == WINDOWS_9x;
195	} //}}}
196
197	//{{{ isWindowsNT() method
198	/**
199	 * Returns if we're running Windows NT/2000/XP.
200	 */
201	public static final boolean isWindowsNT()
202	{
203		return os == WINDOWS_NT;
204	} //}}}
205
206	//{{{ isOS2() method
207	/**
208	 * Returns if we're running OS/2.
209	 */
210	public static final boolean isOS2()
211	{
212		return os == OS2;
213	} //}}}
214
215	//{{{ isUnix() method
216	/**
217	 * Returns if we're running Unix (this includes MacOS X).
218	 */
219	public static final boolean isUnix()
220	{
221		return os == UNIX || os == MAC_OS_X;
222	} //}}}
223
224	//{{{ isMacOS() method
225	/**
226	 * Returns if we're running MacOS X.
227	 */
228	public static final boolean isMacOS()
229	{
230		return os == MAC_OS_X;
231	} //}}}
232
233	//{{{ isX11() method
234	/**
235	 * Returns if this OS is likely to be using X11 as the graphics
236	 * system.
237	 * @since jEdit 4.2pre3
238	 */
239	public static boolean isX11()
240	{
241		return os == UNIX;
242	} //}}}
243
244	//{{{ isVMS() method
245	/**
246	 * Returns if we're running VMS.
247	 */
248	public static final boolean isVMS()
249	{
250		return os == VMS;
251	} //}}}
252
253	//{{{ isMacOSLF() method
254	/**
255	* Returns if we're running MacOS X and using the native look and feel.
256	*/
257	public static final boolean isMacOSLF()
258	{
259		return (isMacOS() && UIManager.getLookAndFeel().isNativeLookAndFeel());
260	} //}}}
261
262	//{{{ hasScreenMenuBar
263	/**
264	 * Returns whether the screen menu bar on Mac OS X is in use.
265	 * @since jEdit 4.2pre1
266	*/
267	public static final boolean hasScreenMenuBar()
268	{
269		if(!isMacOS())
270			return false;
271		else if(hasScreenMenuBar == -1)
272		{
273			String result = System.getProperty("apple.laf.useScreenMenuBar");
274			if (result == null)
275				result = System.getProperty("com.apple.macos.useScreenMenuBar");
276			hasScreenMenuBar = ("true".equals(result)) ? 1 : 0;
277		}
278
279		return (hasScreenMenuBar == 1);
280	} //}}}
281
282	//{{{ isJava14() method
283	/**
284	 * Returns if Java 2 version 1.4 is in use.
285	 */
286	public static final boolean hasJava14()
287	{
288		return java14;
289	} //}}}
290
291	//{{{ Private members
292	private static final int UNIX = 0x31337;
293	private static final int WINDOWS_9x = 0x640;
294	private static final int WINDOWS_NT = 0x666;
295	private static final int OS2 = 0xDEAD;
296	private static final int MAC_OS_X = 0xABC;
297	private static final int VMS = 0xDEAD2;
298	private static final int UNKNOWN = 0xBAD;
299
300	private static int os;
301	private static boolean java14;
302	private static int hasScreenMenuBar = -1;
303
304	//{{{ Class initializer
305	static
306	{
307		if(System.getProperty("mrj.version") != null)
308		{
309			os = MAC_OS_X;
310		}
311		else
312		{
313			String osName = System.getProperty("os.name");
314			if(osName.indexOf("Windows 9") != -1
315				|| osName.indexOf("Windows M") != -1)
316			{
317				os = WINDOWS_9x;
318			}
319			else if(osName.indexOf("Windows") != -1)
320			{
321				os = WINDOWS_NT;
322			}
323			else if(osName.indexOf("OS/2") != -1)
324			{
325				os = OS2;
326			}
327			else if(osName.indexOf("VMS") != -1)
328			{
329				os = VMS;
330			}
331			else if(File.separatorChar == '/')
332			{
333				os = UNIX;
334			}
335			else
336			{
337				os = UNKNOWN;
338				Log.log(Log.WARNING,OperatingSystem.class,
339					"Unknown operating system: " + osName);
340			}
341		}
342
343		if(System.getProperty("java.version").compareTo("1.4") >= 0
344			&& System.getProperty("jedit.nojava14") == null)
345			java14 = true;
346	} //}}}
347
348	//}}}
349}