PageRenderTime 55ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/before-fast-scroll/org/gjt/sp/jedit/OperatingSystem.java

#
Java | 354 lines | 218 code | 30 blank | 106 comment | 55 complexity | c7316060dc47bc784436fc14e5dfeae9 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. * :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. package org.gjt.sp.jedit;
  26. import java.awt.GraphicsConfiguration;
  27. import java.awt.GraphicsDevice;
  28. import java.awt.GraphicsEnvironment;
  29. import java.awt.Rectangle;
  30. import java.awt.Toolkit;
  31. import javax.swing.UIManager;
  32. import java.io.File;
  33. import java.util.Enumeration;
  34. import java.util.Vector;
  35. import org.gjt.sp.util.Log;
  36. /**
  37. * Operating system detection routines.
  38. * @author Slava Pestov
  39. * @version $Id: OperatingSystem.java 5166 2005-01-09 00:33:06Z spestov $
  40. * @since jEdit 4.0pre4
  41. */
  42. public class OperatingSystem
  43. {
  44. //{{{ getScreenBounds() method
  45. /**
  46. * Returns the bounds of the default screen.
  47. */
  48. public static final Rectangle getScreenBounds()
  49. {
  50. int screenX = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
  51. int screenY = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
  52. int x, y, w, h;
  53. if (isMacOS())
  54. {
  55. x = 0;
  56. y = 22;
  57. w = screenX;
  58. h = screenY - y - 4;//shadow size
  59. }
  60. else if (isWindows())
  61. {
  62. x = -4;
  63. y = -4;
  64. w = screenX - 2*x;
  65. h = screenY - 2*y;
  66. }
  67. else
  68. {
  69. x = 0;
  70. y = 0;
  71. w = screenX;
  72. h = screenY;
  73. }
  74. return new Rectangle(x,y,w,h);
  75. } //}}}
  76. //{{{ getScreenBounds() method
  77. /**
  78. * Returns the bounds of the (virtual) screen that the window should be in
  79. * @param window The bounds of the window to get the screen for
  80. */
  81. public static final Rectangle getScreenBounds(Rectangle window)
  82. {
  83. GraphicsDevice[] gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  84. Vector intersects = new Vector();
  85. // Get available screens
  86. // O(n^3), this is nasty, but since we aren't dealling with
  87. // many items it should be fine
  88. for (int i=0; i < gd.length; i++)
  89. {
  90. GraphicsConfiguration gc = gd[i]
  91. .getDefaultConfiguration();
  92. // Don't add duplicates
  93. if (window.intersects(gc.getBounds()))
  94. {
  95. for (Enumeration e = intersects.elements(); e.hasMoreElements();)
  96. {
  97. GraphicsConfiguration gcc = (GraphicsConfiguration)e.nextElement();
  98. if (gcc.getBounds().equals(gc.getBounds()))
  99. break;
  100. }
  101. intersects.add(gc);
  102. }
  103. }
  104. GraphicsConfiguration choice = null;
  105. if (intersects.size() > 0)
  106. {
  107. // Pick screen with largest intersection
  108. for (Enumeration e = intersects.elements(); e.hasMoreElements();)
  109. {
  110. GraphicsConfiguration gcc = (GraphicsConfiguration)e.nextElement();
  111. if (choice == null)
  112. choice = gcc;
  113. else
  114. {
  115. Rectangle int1 = choice.getBounds().intersection(window);
  116. Rectangle int2 = gcc.getBounds().intersection(window);
  117. int area1 = int1.width * int1.height;
  118. int area2 = int2.width * int2.height;
  119. if (area2 > area1)
  120. choice = gcc;
  121. }
  122. }
  123. }
  124. else
  125. choice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
  126. // Make adjustments for some OS's
  127. int screenX = (int)choice.getBounds().x;
  128. int screenY = (int)choice.getBounds().y;
  129. int screenW = (int)choice.getBounds().width;
  130. int screenH = (int)choice.getBounds().height;
  131. int x, y, w, h;
  132. if (isMacOS())
  133. {
  134. x = screenX;
  135. y = screenY + 22;
  136. w = screenW;
  137. h = screenH - y - 4;//shadow size
  138. }
  139. else
  140. {
  141. x = screenX;
  142. y = screenY;
  143. w = screenW;
  144. h = screenH;
  145. }
  146. // Yay, we're finally there
  147. return new Rectangle(x,y,w,h);
  148. } //}}}
  149. //{{{ isDOSDerived() method
  150. /**
  151. * Returns if we're running Windows 95/98/ME/NT/2000/XP, or OS/2.
  152. */
  153. public static final boolean isDOSDerived()
  154. {
  155. return isWindows() || isOS2();
  156. } //}}}
  157. //{{{ isWindows() method
  158. /**
  159. * Returns if we're running Windows 95/98/ME/NT/2000/XP.
  160. */
  161. public static final boolean isWindows()
  162. {
  163. return os == WINDOWS_9x || os == WINDOWS_NT;
  164. } //}}}
  165. //{{{ isWindows9x() method
  166. /**
  167. * Returns if we're running Windows 95/98/ME.
  168. */
  169. public static final boolean isWindows9x()
  170. {
  171. return os == WINDOWS_9x;
  172. } //}}}
  173. //{{{ isWindowsNT() method
  174. /**
  175. * Returns if we're running Windows NT/2000/XP.
  176. */
  177. public static final boolean isWindowsNT()
  178. {
  179. return os == WINDOWS_NT;
  180. } //}}}
  181. //{{{ isOS2() method
  182. /**
  183. * Returns if we're running OS/2.
  184. */
  185. public static final boolean isOS2()
  186. {
  187. return os == OS2;
  188. } //}}}
  189. //{{{ isUnix() method
  190. /**
  191. * Returns if we're running Unix (this includes MacOS X).
  192. */
  193. public static final boolean isUnix()
  194. {
  195. return os == UNIX || os == MAC_OS_X;
  196. } //}}}
  197. //{{{ isMacOS() method
  198. /**
  199. * Returns if we're running MacOS X.
  200. */
  201. public static final boolean isMacOS()
  202. {
  203. return os == MAC_OS_X;
  204. } //}}}
  205. //{{{ isX11() method
  206. /**
  207. * Returns if this OS is likely to be using X11 as the graphics
  208. * system.
  209. * @since jEdit 4.2pre3
  210. */
  211. public static boolean isX11()
  212. {
  213. return os == UNIX;
  214. } //}}}
  215. //{{{ isVMS() method
  216. /**
  217. * Returns if we're running VMS.
  218. */
  219. public static final boolean isVMS()
  220. {
  221. return os == VMS;
  222. } //}}}
  223. //{{{ isMacOSLF() method
  224. /**
  225. * Returns if we're running MacOS X and using the native look and feel.
  226. */
  227. public static final boolean isMacOSLF()
  228. {
  229. return (isMacOS() && UIManager.getLookAndFeel().isNativeLookAndFeel());
  230. } //}}}
  231. //{{{ hasScreenMenuBar
  232. /**
  233. * Returns whether the screen menu bar on Mac OS X is in use.
  234. * @since jEdit 4.2pre1
  235. */
  236. public static final boolean hasScreenMenuBar()
  237. {
  238. if(!isMacOS())
  239. return false;
  240. else if(hasScreenMenuBar == -1)
  241. {
  242. String result = System.getProperty("apple.laf.useScreenMenuBar");
  243. if (result == null)
  244. result = System.getProperty("com.apple.macos.useScreenMenuBar");
  245. hasScreenMenuBar = ("true".equals(result)) ? 1 : 0;
  246. }
  247. return (hasScreenMenuBar == 1);
  248. } //}}}
  249. //{{{ isJava14() method
  250. /**
  251. * Returns if Java 2 version 1.4, or Java 2 version 1.5 is in use.
  252. */
  253. public static final boolean hasJava14()
  254. {
  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. //{{{ Private members
  266. private static final int UNIX = 0x31337;
  267. private static final int WINDOWS_9x = 0x640;
  268. private static final int WINDOWS_NT = 0x666;
  269. private static final int OS2 = 0xDEAD;
  270. private static final int MAC_OS_X = 0xABC;
  271. private static final int VMS = 0xDEAD2;
  272. private static final int UNKNOWN = 0xBAD;
  273. private static int os;
  274. private static boolean java14;
  275. private static boolean java15;
  276. private static int hasScreenMenuBar = -1;
  277. //{{{ Class initializer
  278. static
  279. {
  280. if(System.getProperty("mrj.version") != null)
  281. {
  282. os = MAC_OS_X;
  283. }
  284. else
  285. {
  286. String osName = System.getProperty("os.name");
  287. if(osName.indexOf("Windows 9") != -1
  288. || osName.indexOf("Windows M") != -1)
  289. {
  290. os = WINDOWS_9x;
  291. }
  292. else if(osName.indexOf("Windows") != -1)
  293. {
  294. os = WINDOWS_NT;
  295. }
  296. else if(osName.indexOf("OS/2") != -1)
  297. {
  298. os = OS2;
  299. }
  300. else if(osName.indexOf("VMS") != -1)
  301. {
  302. os = VMS;
  303. }
  304. else if(File.separatorChar == '/')
  305. {
  306. os = UNIX;
  307. }
  308. else
  309. {
  310. os = UNKNOWN;
  311. Log.log(Log.WARNING,OperatingSystem.class,
  312. "Unknown operating system: " + osName);
  313. }
  314. }
  315. // for debugging, make jEdit think its using a different
  316. // version of Java than it really is.
  317. String javaVersion = System.getProperty("jedit.force.java.version");
  318. if(javaVersion == null || javaVersion.equals(""))
  319. javaVersion = System.getProperty("java.version");
  320. java14 = (javaVersion.compareTo("1.4") >= 0);
  321. java15 = (javaVersion.compareTo("1.5") >= 0);
  322. } //}}}
  323. //}}}
  324. }