001: /*
002: * OperatingSystem.java - OS detection
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2002, 2005 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit;
024:
025: import java.awt.GraphicsConfiguration;
026: import java.awt.GraphicsDevice;
027: import java.awt.GraphicsEnvironment;
028: import java.awt.Rectangle;
029: import java.awt.Toolkit;
030: import javax.swing.UIManager;
031: import java.io.File;
032: import java.util.Enumeration;
033: import java.util.Vector;
034: import org.gjt.sp.util.Log;
035:
036: /**
037: * Operating system detection routines.
038: * @author Slava Pestov
039: * @version $Id: OperatingSystem.java 5354 2006-03-03 16:18:06Z ezust $
040: * @since jEdit 4.0pre4
041: */
042: public class OperatingSystem {
043: //{{{ getScreenBounds() method
044: /**
045: * Returns the bounds of the default screen.
046: */
047: public static final Rectangle getScreenBounds() {
048: int screenX = (int) Toolkit.getDefaultToolkit().getScreenSize()
049: .getWidth();
050: int screenY = (int) Toolkit.getDefaultToolkit().getScreenSize()
051: .getHeight();
052: int x, y, w, h;
053:
054: if (isMacOS()) {
055: x = 0;
056: y = 22;
057: w = screenX;
058: h = screenY - y - 4;//shadow size
059: } else if (isWindows()) {
060: x = -4;
061: y = -4;
062: w = screenX - 2 * x;
063: h = screenY - 2 * y;
064: } else {
065: x = 0;
066: y = 0;
067: w = screenX;
068: h = screenY;
069: }
070:
071: return new Rectangle(x, y, w, h);
072: } //}}}
073:
074: //{{{ getScreenBounds() method
075: /**
076: * Returns the bounds of the (virtual) screen that the window should be in
077: * @param window The bounds of the window to get the screen for
078: */
079: public static final Rectangle getScreenBounds(Rectangle window) {
080: GraphicsDevice[] gd = GraphicsEnvironment
081: .getLocalGraphicsEnvironment().getScreenDevices();
082: Vector intersects = new Vector();
083:
084: // Get available screens
085: // O(n^3), this is nasty, but since we aren't dealling with
086: // many items it should be fine
087: for (int i = 0; i < gd.length; i++) {
088: GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
089: // Don't add duplicates
090: if (window.intersects(gc.getBounds())) {
091: for (Enumeration e = intersects.elements(); e
092: .hasMoreElements();) {
093: GraphicsConfiguration gcc = (GraphicsConfiguration) e
094: .nextElement();
095: if (gcc.getBounds().equals(gc.getBounds()))
096: break;
097: }
098: intersects.add(gc);
099: }
100: }
101:
102: GraphicsConfiguration choice = null;
103: if (intersects.size() > 0) {
104: // Pick screen with largest intersection
105: for (Enumeration e = intersects.elements(); e
106: .hasMoreElements();) {
107: GraphicsConfiguration gcc = (GraphicsConfiguration) e
108: .nextElement();
109: if (choice == null)
110: choice = gcc;
111: else {
112: Rectangle int1 = choice.getBounds().intersection(
113: window);
114: Rectangle int2 = gcc.getBounds().intersection(
115: window);
116: int area1 = int1.width * int1.height;
117: int area2 = int2.width * int2.height;
118: if (area2 > area1)
119: choice = gcc;
120: }
121: }
122: } else
123: choice = GraphicsEnvironment.getLocalGraphicsEnvironment()
124: .getDefaultScreenDevice().getDefaultConfiguration();
125:
126: // Make adjustments for some OS's
127: int screenX = choice.getBounds().x;
128: int screenY = choice.getBounds().y;
129: int screenW = choice.getBounds().width;
130: int screenH = choice.getBounds().height;
131: int x, y, w, h;
132:
133: if (isMacOS()) {
134: x = screenX;
135: y = screenY + 22;
136: w = screenW;
137: h = screenH - y - 4;//shadow size
138: } else {
139: x = screenX;
140: y = screenY;
141: w = screenW;
142: h = screenH;
143: }
144:
145: // Yay, we're finally there
146: return new Rectangle(x, y, w, h);
147: } //}}}
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: return isWindows() || isOS2();
155: } //}}}
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: return os == WINDOWS_9x || os == WINDOWS_NT;
163: } //}}}
164:
165: //{{{ isWindows9x() method
166: /**
167: * Returns if we're running Windows 95/98/ME.
168: */
169: public static final boolean isWindows9x() {
170: return os == WINDOWS_9x;
171: } //}}}
172:
173: //{{{ isWindowsNT() method
174: /**
175: * Returns if we're running Windows NT/2000/XP.
176: */
177: public static final boolean isWindowsNT() {
178: return os == WINDOWS_NT;
179: } //}}}
180:
181: //{{{ isOS2() method
182: /**
183: * Returns if we're running OS/2.
184: */
185: public static final boolean isOS2() {
186: return os == OS2;
187: } //}}}
188:
189: //{{{ isUnix() method
190: /**
191: * Returns if we're running Unix (this includes MacOS X).
192: */
193: public static final boolean isUnix() {
194: return os == UNIX || os == MAC_OS_X;
195: } //}}}
196:
197: //{{{ isMacOS() method
198: /**
199: * Returns if we're running MacOS X.
200: */
201: public static final boolean isMacOS() {
202: return os == MAC_OS_X;
203: } //}}}
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: return os == UNIX;
213: } //}}}
214:
215: //{{{ isVMS() method
216: /**
217: * Returns if we're running VMS.
218: */
219: public static final boolean isVMS() {
220: return os == VMS;
221: } //}}}
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: return (isMacOS() && UIManager.getLookAndFeel()
229: .isNativeLookAndFeel());
230: } //}}}
231:
232: //{{{ hasScreenMenuBar() method
233: /**
234: * Returns whether the screen menu bar on Mac OS X is in use.
235: * @since jEdit 4.2pre1
236: */
237: public static final boolean hasScreenMenuBar() {
238: if (!isMacOS())
239: return false;
240: else if (hasScreenMenuBar == -1) {
241: String result = System
242: .getProperty("apple.laf.useScreenMenuBar");
243: if (result == null)
244: result = System
245: .getProperty("com.apple.macos.useScreenMenuBar");
246: hasScreenMenuBar = ("true".equals(result)) ? 1 : 0;
247: }
248:
249: return (hasScreenMenuBar == 1);
250: } //}}}
251:
252: //{{{ isJava14() method
253: /**
254: * Returns if Java 2 version 1.4, or Java 2 version 1.5 is in use.
255: */
256: public static final boolean hasJava14() {
257: // jEdit 4.3 requires Java 1.4 or later. However, this method exists
258: // for two reasons. Compatibility with plugins for jEdit 4.2, and
259: // in case somebody wants to borrow this class for their app.
260: return java14;
261: } //}}}
262:
263: //{{{ isJava15() method
264: /**
265: * Returns if Java 2 version 1.5 is in use.
266: */
267: public static final boolean hasJava15() {
268: return java15;
269: } //}}}
270:
271: //{{{ isCaseInsensitiveFS() method
272: /**
273: * @since jEdit 4.3pre2
274: */
275: public static boolean isCaseInsensitiveFS() {
276: return isDOSDerived() || isMacOS();
277: } //}}}
278:
279: //{{{ Private members
280: private static final int UNIX = 0x31337;
281: private static final int WINDOWS_9x = 0x640;
282: private static final int WINDOWS_NT = 0x666;
283: private static final int OS2 = 0xDEAD;
284: private static final int MAC_OS_X = 0xABC;
285: private static final int VMS = 0xDEAD2;
286: private static final int UNKNOWN = 0xBAD;
287:
288: private static int os;
289: private static boolean java14;
290: private static boolean java15;
291: private static int hasScreenMenuBar = -1;
292:
293: //{{{ Class initializer
294: static {
295: if (System.getProperty("mrj.version") != null) {
296: os = MAC_OS_X;
297: } else {
298: String osName = System.getProperty("os.name");
299: if (osName.indexOf("Windows 9") != -1
300: || osName.indexOf("Windows M") != -1) {
301: os = WINDOWS_9x;
302: } else if (osName.indexOf("Windows") != -1) {
303: os = WINDOWS_NT;
304: } else if (osName.indexOf("OS/2") != -1) {
305: os = OS2;
306: } else if (osName.indexOf("VMS") != -1) {
307: os = VMS;
308: } else if (File.separatorChar == '/') {
309: os = UNIX;
310: } else {
311: os = UNKNOWN;
312: Log.log(Log.WARNING, OperatingSystem.class,
313: "Unknown operating system: " + osName);
314: }
315: }
316:
317: // for debugging, make jEdit think its using a different
318: // version of Java than it really is.
319: String javaVersion = System
320: .getProperty("jedit.force.java.version");
321: if (javaVersion == null || javaVersion.equals(""))
322: javaVersion = System.getProperty("java.version");
323: java14 = (javaVersion.compareTo("1.4") >= 0);
324: java15 = (javaVersion.compareTo("1.5") >= 0);
325: } //}}}
326:
327: //}}}
328: }
|