PageRenderTime 119ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 338 lines | 216 code | 28 blank | 94 comment | 57 complexity | e13f85866c274d99e3c5dfceef86ac37 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 4698 2003-05-10 03:13:54Z 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. //{{{ isVMS() method
  215. /**
  216. * Returns if we're running VMS.
  217. */
  218. public static final boolean isVMS()
  219. {
  220. return os == VMS;
  221. } //}}}
  222. //{{{ isMacOSLF() method
  223. /**
  224. * Returns if we're running MacOS X and using the native look and feel.
  225. */
  226. public static final boolean isMacOSLF()
  227. {
  228. return (isMacOS() && UIManager.getLookAndFeel().isNativeLookAndFeel());
  229. } //}}}
  230. //{{{ hasScreenMenuBar
  231. /**
  232. * Returns whether the screen menu bar on Mac OS X is in use.
  233. * @since jEdit 4.2pre1
  234. */
  235. public static final boolean hasScreenMenuBar()
  236. {
  237. if(!isMacOS())
  238. return false;
  239. else if(hasScreenMenuBar == -1)
  240. {
  241. String result = System.getProperty("apple.laf.useScreenMenuBar");
  242. if (result == null)
  243. result = System.getProperty("com.apple.macos.useScreenMenuBar");
  244. hasScreenMenuBar = ("true".equals(result)) ? 1 : 0;
  245. }
  246. return (hasScreenMenuBar == 1);
  247. } //}}}
  248. //{{{ isJava14() method
  249. /**
  250. * Returns if Java 2 version 1.4 is in use.
  251. */
  252. public static final boolean hasJava14()
  253. {
  254. return java14;
  255. } //}}}
  256. //{{{ Private members
  257. private static final int UNIX = 0x31337;
  258. private static final int WINDOWS_9x = 0x640;
  259. private static final int WINDOWS_NT = 0x666;
  260. private static final int OS2 = 0xDEAD;
  261. private static final int MAC_OS_X = 0xABC;
  262. private static final int VMS = 0xDEAD2;
  263. private static final int UNKNOWN = 0xBAD;
  264. private static int os;
  265. private static boolean java14;
  266. private static int hasScreenMenuBar = -1;
  267. //{{{ Class initializer
  268. static
  269. {
  270. if(System.getProperty("mrj.version") != null)
  271. {
  272. os = MAC_OS_X;
  273. }
  274. else
  275. {
  276. String osName = System.getProperty("os.name");
  277. if(osName.indexOf("Windows 9") != -1
  278. || osName.indexOf("Windows M") != -1)
  279. {
  280. os = WINDOWS_9x;
  281. }
  282. else if(osName.indexOf("Windows") != -1)
  283. {
  284. os = WINDOWS_NT;
  285. }
  286. else if(osName.indexOf("OS/2") != -1)
  287. {
  288. os = OS2;
  289. }
  290. else if(osName.indexOf("VMS") != -1)
  291. {
  292. os = VMS;
  293. }
  294. else if(File.separatorChar == '/')
  295. {
  296. os = UNIX;
  297. }
  298. else
  299. {
  300. os = UNKNOWN;
  301. Log.log(Log.WARNING,OperatingSystem.class,
  302. "Unknown operating system: " + osName);
  303. }
  304. }
  305. if(System.getProperty("java.version").compareTo("1.4") >= 0
  306. && System.getProperty("jedit.nojava14") == null)
  307. java14 = true;
  308. } //}}}
  309. //}}}
  310. }