PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/OperatingSystem.java

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