PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

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

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