PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/before-screen-line-refactoring/org/gjt/sp/jedit/OperatingSystem.java

#
Java | 361 lines | 225 code | 30 blank | 106 comment | 57 complexity | 27bb53a79e5b6fbd9c597a34591b264a 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 4939 2003-12-25 00:09:42Z 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, or Java 2 version 1.5 is in use.
  259. */
  260. public static final boolean hasJava14()
  261. {
  262. return java14;
  263. } //}}}
  264. //{{{ isJava15() method
  265. /**
  266. * Returns if Java 2 version 1.5 is in use.
  267. */
  268. public static final boolean hasJava15()
  269. {
  270. return java15;
  271. } //}}}
  272. //{{{ Private members
  273. private static final int UNIX = 0x31337;
  274. private static final int WINDOWS_9x = 0x640;
  275. private static final int WINDOWS_NT = 0x666;
  276. private static final int OS2 = 0xDEAD;
  277. private static final int MAC_OS_X = 0xABC;
  278. private static final int VMS = 0xDEAD2;
  279. private static final int UNKNOWN = 0xBAD;
  280. private static int os;
  281. private static boolean java14;
  282. private static boolean java15;
  283. private static int hasScreenMenuBar = -1;
  284. //{{{ Class initializer
  285. static
  286. {
  287. if(System.getProperty("mrj.version") != null)
  288. {
  289. os = MAC_OS_X;
  290. }
  291. else
  292. {
  293. String osName = System.getProperty("os.name");
  294. if(osName.indexOf("Windows 9") != -1
  295. || osName.indexOf("Windows M") != -1)
  296. {
  297. os = WINDOWS_9x;
  298. }
  299. else if(osName.indexOf("Windows") != -1)
  300. {
  301. os = WINDOWS_NT;
  302. }
  303. else if(osName.indexOf("OS/2") != -1)
  304. {
  305. os = OS2;
  306. }
  307. else if(osName.indexOf("VMS") != -1)
  308. {
  309. os = VMS;
  310. }
  311. else if(File.separatorChar == '/')
  312. {
  313. os = UNIX;
  314. }
  315. else
  316. {
  317. os = UNKNOWN;
  318. Log.log(Log.WARNING,OperatingSystem.class,
  319. "Unknown operating system: " + osName);
  320. }
  321. }
  322. // for debugging, make jEdit think its using a different
  323. // version of Java than it really is.
  324. String javaVersion = System.getProperty("jedit.force.java.version");
  325. if(javaVersion == null || javaVersion.equals(""))
  326. javaVersion = System.getProperty("java.version");
  327. java14 = (javaVersion.compareTo("1.4") >= 0);
  328. java15 = (javaVersion.compareTo("1.5") >= 0);
  329. } //}}}
  330. //}}}
  331. }