KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > OperatingSystem


1 /*
2  * OperatingSystem.java - OS detection
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002, 2005 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit;
24
25 import java.awt.GraphicsConfiguration JavaDoc;
26 import java.awt.GraphicsDevice JavaDoc;
27 import java.awt.GraphicsEnvironment JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.Toolkit JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import java.io.File JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Vector JavaDoc;
34 import org.gjt.sp.util.Log;
35
36 /**
37  * Operating system detection routines.
38  * @author Slava Pestov
39  * @version $Id: OperatingSystem.java 5354 2006-03-03 16:18:06Z ezust $
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 JavaDoc 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         
54         if (isMacOS())
55         {
56             x = 0;
57             y = 22;
58             w = screenX;
59             h = screenY - y - 4;//shadow size
60
}
61         else if (isWindows())
62         {
63             x = -4;
64             y = -4;
65             w = screenX - 2*x;
66             h = screenY - 2*y;
67         }
68         else
69         {
70             x = 0;
71             y = 0;
72             w = screenX;
73             h = screenY;
74         }
75         
76         return new Rectangle JavaDoc(x,y,w,h);
77     } //}}}
78

79     //{{{ getScreenBounds() method
80
/**
81      * Returns the bounds of the (virtual) screen that the window should be in
82      * @param window The bounds of the window to get the screen for
83      */

84     public static final Rectangle JavaDoc getScreenBounds(Rectangle JavaDoc window)
85     {
86         GraphicsDevice JavaDoc[] gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
87         Vector JavaDoc intersects = new Vector JavaDoc();
88
89         // Get available screens
90
// O(n^3), this is nasty, but since we aren't dealling with
91
// many items it should be fine
92
for (int i=0; i < gd.length; i++)
93         {
94             GraphicsConfiguration JavaDoc gc = gd[i]
95                 .getDefaultConfiguration();
96             // Don't add duplicates
97
if (window.intersects(gc.getBounds()))
98             {
99                 for (Enumeration JavaDoc e = intersects.elements(); e.hasMoreElements();)
100                 {
101                     GraphicsConfiguration JavaDoc gcc = (GraphicsConfiguration JavaDoc)e.nextElement();
102                     if (gcc.getBounds().equals(gc.getBounds()))
103                         break;
104                 }
105                 intersects.add(gc);
106             }
107         }
108         
109         GraphicsConfiguration JavaDoc choice = null;
110         if (intersects.size() > 0)
111         {
112             // Pick screen with largest intersection
113
for (Enumeration JavaDoc e = intersects.elements(); e.hasMoreElements();)
114             {
115                 GraphicsConfiguration JavaDoc gcc = (GraphicsConfiguration JavaDoc)e.nextElement();
116                 if (choice == null)
117                     choice = gcc;
118                 else
119                 {
120                     Rectangle JavaDoc int1 = choice.getBounds().intersection(window);
121                     Rectangle JavaDoc int2 = gcc.getBounds().intersection(window);
122                     int area1 = int1.width * int1.height;
123                     int area2 = int2.width * int2.height;
124                     if (area2 > area1)
125                         choice = gcc;
126                 }
127             }
128         }
129         else
130             choice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
131         
132         // Make adjustments for some OS's
133
int screenX = choice.getBounds().x;
134         int screenY = choice.getBounds().y;
135         int screenW = choice.getBounds().width;
136         int screenH = choice.getBounds().height;
137         int x, y, w, h;
138         
139         if (isMacOS())
140         {
141             x = screenX;
142             y = screenY + 22;
143             w = screenW;
144             h = screenH - y - 4;//shadow size
145
}
146         else
147         {
148             x = screenX;
149             y = screenY;
150             w = screenW;
151             h = screenH;
152         }
153         
154         // Yay, we're finally there
155
return new Rectangle JavaDoc(x,y,w,h);
156     } //}}}
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

167     //{{{ isWindows() method
168
/**
169      * Returns if we're running Windows 95/98/ME/NT/2000/XP.
170      */

171     public static final boolean isWindows()
172     {
173         return os == WINDOWS_9x || os == WINDOWS_NT;
174     } //}}}
175

176     //{{{ isWindows9x() method
177
/**
178      * Returns if we're running Windows 95/98/ME.
179      */

180     public static final boolean isWindows9x()
181     {
182         return os == WINDOWS_9x;
183     } //}}}
184

185     //{{{ isWindowsNT() method
186
/**
187      * Returns if we're running Windows NT/2000/XP.
188      */

189     public static final boolean isWindowsNT()
190     {
191         return os == WINDOWS_NT;
192     } //}}}
193

194     //{{{ isOS2() method
195
/**
196      * Returns if we're running OS/2.
197      */

198     public static final boolean isOS2()
199     {
200         return os == OS2;
201     } //}}}
202

203     //{{{ isUnix() method
204
/**
205      * Returns if we're running Unix (this includes MacOS X).
206      */

207     public static final boolean isUnix()
208     {
209         return os == UNIX || os == MAC_OS_X;
210     } //}}}
211

212     //{{{ isMacOS() method
213
/**
214      * Returns if we're running MacOS X.
215      */

216     public static final boolean isMacOS()
217     {
218         return os == MAC_OS_X;
219     } //}}}
220

221     //{{{ isX11() method
222
/**
223      * Returns if this OS is likely to be using X11 as the graphics
224      * system.
225      * @since jEdit 4.2pre3
226      */

227     public static boolean isX11()
228     {
229         return os == UNIX;
230     } //}}}
231

232     //{{{ isVMS() method
233
/**
234      * Returns if we're running VMS.
235      */

236     public static final boolean isVMS()
237     {
238         return os == VMS;
239     } //}}}
240

241     //{{{ isMacOSLF() method
242
/**
243     * Returns if we're running MacOS X and using the native look and feel.
244     */

245     public static final boolean isMacOSLF()
246     {
247         return (isMacOS() && UIManager.getLookAndFeel().isNativeLookAndFeel());
248     } //}}}
249

250     //{{{ hasScreenMenuBar() method
251
/**
252      * Returns whether the screen menu bar on Mac OS X is in use.
253      * @since jEdit 4.2pre1
254     */

255     public static final boolean hasScreenMenuBar()
256     {
257         if(!isMacOS())
258             return false;
259         else if(hasScreenMenuBar == -1)
260         {
261             String JavaDoc result = System.getProperty("apple.laf.useScreenMenuBar");
262             if (result == null)
263                 result = System.getProperty("com.apple.macos.useScreenMenuBar");
264             hasScreenMenuBar = ("true".equals(result)) ? 1 : 0;
265         }
266
267         return (hasScreenMenuBar == 1);
268     } //}}}
269

270     //{{{ isJava14() method
271
/**
272      * Returns if Java 2 version 1.4, or Java 2 version 1.5 is in use.
273      */

274     public static final boolean hasJava14()
275     {
276         // jEdit 4.3 requires Java 1.4 or later. However, this method exists
277
// for two reasons. Compatibility with plugins for jEdit 4.2, and
278
// in case somebody wants to borrow this class for their app.
279
return java14;
280     } //}}}
281

282     //{{{ isJava15() method
283
/**
284      * Returns if Java 2 version 1.5 is in use.
285      */

286     public static final boolean hasJava15()
287     {
288         return java15;
289     } //}}}
290

291     //{{{ isCaseInsensitiveFS() method
292
/**
293      * @since jEdit 4.3pre2
294      */

295     public static boolean isCaseInsensitiveFS()
296     {
297         return isDOSDerived() || isMacOS();
298     } //}}}
299

300     //{{{ Private members
301
private static final int UNIX = 0x31337;
302     private static final int WINDOWS_9x = 0x640;
303     private static final int WINDOWS_NT = 0x666;
304     private static final int OS2 = 0xDEAD;
305     private static final int MAC_OS_X = 0xABC;
306     private static final int VMS = 0xDEAD2;
307     private static final int UNKNOWN = 0xBAD;
308
309     private static int os;
310     private static boolean java14;
311     private static boolean java15;
312     private static int hasScreenMenuBar = -1;
313
314     //{{{ Class initializer
315
static
316     {
317         if(System.getProperty("mrj.version") != null)
318         {
319             os = MAC_OS_X;
320         }
321         else
322         {
323             String JavaDoc osName = System.getProperty("os.name");
324             if(osName.indexOf("Windows 9") != -1
325                 || osName.indexOf("Windows M") != -1)
326             {
327                 os = WINDOWS_9x;
328             }
329             else if(osName.indexOf("Windows") != -1)
330             {
331                 os = WINDOWS_NT;
332             }
333             else if(osName.indexOf("OS/2") != -1)
334             {
335                 os = OS2;
336             }
337             else if(osName.indexOf("VMS") != -1)
338             {
339                 os = VMS;
340             }
341             else if(File.separatorChar == '/')
342             {
343                 os = UNIX;
344             }
345             else
346             {
347                 os = UNKNOWN;
348                 Log.log(Log.WARNING,OperatingSystem.class,
349                     "Unknown operating system: " + osName);
350             }
351         }
352
353         // for debugging, make jEdit think its using a different
354
// version of Java than it really is.
355
String JavaDoc javaVersion = System.getProperty("jedit.force.java.version");
356         if(javaVersion == null || javaVersion.equals(""))
357             javaVersion = System.getProperty("java.version");
358         java14 = (javaVersion.compareTo("1.4") >= 0);
359         java15 = (javaVersion.compareTo("1.5") >= 0);
360     } //}}}
361

362     //}}}
363
}
364
Popular Tags