PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 347 lines | 218 code | 29 blank | 100 comment | 57 complexity | 4e1d4dcc8890df62e5e3fdce149943e4 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 4908 2003-11-02 21:16:38Z 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 if (isWindows())
  140. {
  141. x = screenX - 4;
  142. y = screenY - 4;
  143. w = screenW - 2*x;
  144. h = screenH - 2*y;
  145. }
  146. else
  147. {
  148. x = screenX;
  149. y = screenY;
  150. w = screenW;
  151. h = screenH;
  152. }
  153. // Yay, we're finally there
  154. return new Rectangle(x,y,w,h);
  155. } //}}}
  156. //{{{ isDOSDerived() method
  157. /**
  158. * Returns if we're running Windows 95/98/ME/NT/2000/XP, or OS/2.
  159. */
  160. public static final boolean isDOSDerived()
  161. {
  162. return isWindows() || isOS2();
  163. } //}}}
  164. //{{{ isWindows() method
  165. /**
  166. * Returns if we're running Windows 95/98/ME/NT/2000/XP.
  167. */
  168. public static final boolean isWindows()
  169. {
  170. return os == WINDOWS_9x || os == WINDOWS_NT;
  171. } //}}}
  172. //{{{ isWindows9x() method
  173. /**
  174. * Returns if we're running Windows 95/98/ME.
  175. */
  176. public static final boolean isWindows9x()
  177. {
  178. return os == WINDOWS_9x;
  179. } //}}}
  180. //{{{ isWindowsNT() method
  181. /**
  182. * Returns if we're running Windows NT/2000/XP.
  183. */
  184. public static final boolean isWindowsNT()
  185. {
  186. return os == WINDOWS_NT;
  187. } //}}}
  188. //{{{ isOS2() method
  189. /**
  190. * Returns if we're running OS/2.
  191. */
  192. public static final boolean isOS2()
  193. {
  194. return os == OS2;
  195. } //}}}
  196. //{{{ isUnix() method
  197. /**
  198. * Returns if we're running Unix (this includes MacOS X).
  199. */
  200. public static final boolean isUnix()
  201. {
  202. return os == UNIX || os == MAC_OS_X;
  203. } //}}}
  204. //{{{ isMacOS() method
  205. /**
  206. * Returns if we're running MacOS X.
  207. */
  208. public static final boolean isMacOS()
  209. {
  210. return os == MAC_OS_X;
  211. } //}}}
  212. //{{{ isX11() method
  213. /**
  214. * Returns if this OS is likely to be using X11 as the graphics
  215. * system.
  216. * @since jEdit 4.2pre3
  217. */
  218. public static boolean isX11()
  219. {
  220. return os == UNIX;
  221. } //}}}
  222. //{{{ isVMS() method
  223. /**
  224. * Returns if we're running VMS.
  225. */
  226. public static final boolean isVMS()
  227. {
  228. return os == VMS;
  229. } //}}}
  230. //{{{ isMacOSLF() method
  231. /**
  232. * Returns if we're running MacOS X and using the native look and feel.
  233. */
  234. public static final boolean isMacOSLF()
  235. {
  236. return (isMacOS() && UIManager.getLookAndFeel().isNativeLookAndFeel());
  237. } //}}}
  238. //{{{ hasScreenMenuBar
  239. /**
  240. * Returns whether the screen menu bar on Mac OS X is in use.
  241. * @since jEdit 4.2pre1
  242. */
  243. public static final boolean hasScreenMenuBar()
  244. {
  245. if(!isMacOS())
  246. return false;
  247. else if(hasScreenMenuBar == -1)
  248. {
  249. String result = System.getProperty("apple.laf.useScreenMenuBar");
  250. if (result == null)
  251. result = System.getProperty("com.apple.macos.useScreenMenuBar");
  252. hasScreenMenuBar = ("true".equals(result)) ? 1 : 0;
  253. }
  254. return (hasScreenMenuBar == 1);
  255. } //}}}
  256. //{{{ isJava14() method
  257. /**
  258. * Returns if Java 2 version 1.4 is in use.
  259. */
  260. public static final boolean hasJava14()
  261. {
  262. return java14;
  263. } //}}}
  264. //{{{ Private members
  265. private static final int UNIX = 0x31337;
  266. private static final int WINDOWS_9x = 0x640;
  267. private static final int WINDOWS_NT = 0x666;
  268. private static final int OS2 = 0xDEAD;
  269. private static final int MAC_OS_X = 0xABC;
  270. private static final int VMS = 0xDEAD2;
  271. private static final int UNKNOWN = 0xBAD;
  272. private static int os;
  273. private static boolean java14;
  274. private static int hasScreenMenuBar = -1;
  275. //{{{ Class initializer
  276. static
  277. {
  278. if(System.getProperty("mrj.version") != null)
  279. {
  280. os = MAC_OS_X;
  281. }
  282. else
  283. {
  284. String osName = System.getProperty("os.name");
  285. if(osName.indexOf("Windows 9") != -1
  286. || osName.indexOf("Windows M") != -1)
  287. {
  288. os = WINDOWS_9x;
  289. }
  290. else if(osName.indexOf("Windows") != -1)
  291. {
  292. os = WINDOWS_NT;
  293. }
  294. else if(osName.indexOf("OS/2") != -1)
  295. {
  296. os = OS2;
  297. }
  298. else if(osName.indexOf("VMS") != -1)
  299. {
  300. os = VMS;
  301. }
  302. else if(File.separatorChar == '/')
  303. {
  304. os = UNIX;
  305. }
  306. else
  307. {
  308. os = UNKNOWN;
  309. Log.log(Log.WARNING,OperatingSystem.class,
  310. "Unknown operating system: " + osName);
  311. }
  312. }
  313. if(System.getProperty("java.version").compareTo("1.4") >= 0
  314. && System.getProperty("jedit.nojava14") == null)
  315. java14 = true;
  316. } //}}}
  317. //}}}
  318. }