001: /*
002: * @(#)SystemInfo.java
003: *
004: * Copyright 2002 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.utils;
007:
008: import java.util.Locale;
009:
010: /**
011: * A utility class can detect OS system information.
012: */
013: final public class SystemInfo {
014:
015: /**
016: * Variable for whether or not we're on Windows.
017: */
018: private static boolean _isWindows = false;
019:
020: /**
021: * Variable for whether or not we're on Windows NT or 2000.
022: */
023: private static boolean _isWindowsNTor2000 = false;
024:
025: /**
026: * Variable for whether or not we're on Windows XP.
027: */
028: private static boolean _isWindowsXP = false;
029:
030: /**
031: * Variable for whether or not we're on Windows 2003.
032: */
033: private static boolean _isWindows2003 = false;
034:
035: /**
036: * Flag which indicates that the Win98/Win2k/WinME features
037: * should be disabled.
038: */
039: private static boolean _isClassicWindows = false;
040:
041: /**
042: * Variable for whether or not we're on Windows 95.
043: */
044: private static boolean _isWindows95 = false;
045:
046: /**
047: * Variable for whether or not we're on Windows 98.
048: */
049: private static boolean _isWindows98 = false;
050:
051: /**
052: * Variable for whether or not the operating system allows the
053: * application to be reduced to the system tray.
054: */
055: private static boolean _supportsTray = false;
056:
057: /**
058: * Variable for whether or not we're on Mac 9.1 or below.
059: */
060: private static boolean _isMacClassic = false;
061:
062: /**
063: * Variable for whether or not we're on MacOSX.
064: */
065: private static boolean _isMacOSX = false;
066:
067: /**
068: * Variable for whether or not we're on Linux.
069: */
070: private static boolean _isLinux = false;
071:
072: /**
073: * Variable for whether or not we're on Solaris.
074: */
075: private static boolean _isSolaris = false;
076:
077: /**
078: * Make sure the constructor can never be called.
079: */
080: private SystemInfo() {
081: }
082:
083: /**
084: * Initialize the settings statically.
085: */
086: static {
087: // get the operating system
088: String os = SecurityUtils.getProperty("os.name", "Windows XP");
089:
090: // set the operating system variables
091: _isWindows = os.indexOf("Windows") != -1;
092: try {
093: String osVersion = SecurityUtils.getProperty("os.version",
094: "5.0");
095: Float version = Float.valueOf(osVersion);
096: _isClassicWindows = version <= 4.0;
097: } catch (NumberFormatException ex) {
098: _isClassicWindows = false;
099: }
100: if (os.indexOf("Windows XP") != -1
101: || os.indexOf("Windows NT") != -1
102: || os.indexOf("Windows 2000") != -1) {
103: _isWindowsNTor2000 = true;
104: }
105: if (os.indexOf("Windows XP") != -1) {
106: _isWindowsXP = true;
107: }
108: if (os.indexOf("Windows 2003") != -1) {
109: _isWindows2003 = true;
110: _isWindowsXP = true;
111: }
112: if (os.indexOf("Windows 95") != -1) {
113: _isWindows95 = true;
114: }
115: if (os.indexOf("Windows 98") != -1) {
116: _isWindows98 = true;
117: }
118: if (_isWindows)
119: _supportsTray = true;
120: _isSolaris = (os.indexOf("Solaris") != -1)
121: || (os.indexOf("SunOS") != -1);
122: _isLinux = os.indexOf("Linux") != -1;
123: if (os.startsWith("Mac OS")) {
124: if (os.endsWith("X")) {
125: _isMacOSX = true;
126: } else {
127: _isMacClassic = true;
128: }
129: }
130: }
131:
132: /**
133: * Returns the version of java we're using.
134: *
135: * @return the java verison.
136: */
137: public static String getJavaVersion() {
138: return SecurityUtils.getProperty("java.version", "1.4.2");
139: }
140:
141: /**
142: * Returns the vendor for java we're using.
143: *
144: * @return the java vendor.
145: */
146: public static String getJavaVendor() {
147: return SecurityUtils.getProperty("java.vendor", "");
148: }
149:
150: /**
151: * Returns the verion of the java classwe're using.
152: *
153: * @return the java class verison.
154: */
155: public static String getJavaClassVerion() {
156: return SecurityUtils.getProperty("java.class.version", "");
157: }
158:
159: /**
160: * Returns the operating system.
161: *
162: * @return the os name.
163: */
164: public static String getOS() {
165: return SecurityUtils.getProperty("os.name", "Windows XP");
166: }
167:
168: /**
169: * Returns the operating system version.
170: *
171: * @return the os version.
172: */
173: public static String getOSVersion() {
174: return SecurityUtils.getProperty("os.version", "");
175: }
176:
177: /**
178: * Returns the operating system architecture.
179: *
180: * @return the os architecture.
181: */
182: public static String getOSArchitecture() {
183: return SecurityUtils.getProperty("os.arch", "");
184: }
185:
186: /**
187: * Returns the user's home directory.
188: *
189: * @return the user home .
190: */
191: public static String getCurrentDirectory() {
192: return SecurityUtils.getProperty("user.dir", "");
193: }
194:
195: /**
196: * Returns true if this is Windows NT or Windows 2000 and
197: * hence can support a system tray feature.
198: *
199: * @return true of system tray is supported.
200: */
201: public static boolean supportsTray() {
202: return _supportsTray;
203: }
204:
205: /**
206: * Set supportTray to false in case dll is missing.
207: *
208: * @param support true or false.
209: */
210: public static void setSupportsTray(boolean support) {
211: _supportsTray = support;
212: }
213:
214: /**
215: * Returns whether or not the os is some version of Windows.
216: *
217: * @return <tt>true</tt> if the application is running on some Windows
218: * version, <tt>false</tt> otherwise.
219: */
220: public static boolean isWindows() {
221: return _isWindows;
222: }
223:
224: /**
225: * Gets the state of the flag which indicates if the old Windows
226: * look and feel should be rendered. This flag is used by the
227: * component UI delegates as a hint to determine which style the component
228: * should be rendered.
229: *
230: * @return true if Windows 95 and Windows NT 4 look and feel should
231: * be rendered.
232: */
233: public static boolean isClassicWindows() {
234: return _isClassicWindows;
235: }
236:
237: /**
238: * Returns whether or not the os is some version of Windows NT.
239: *
240: * @return <tt>true</tt> if the application is running on Windows NT
241: * or 2000, <tt>false</tt> otherwise.
242: */
243: public static boolean isWindowsNTor2000() {
244: return _isWindowsNTor2000;
245: }
246:
247: /**
248: * Returns whether or not the os is some version of Windows XP.
249: *
250: * @return <tt>true</tt> if the application is running on Windows XP,
251: * <tt>false</tt> otherwise.
252: */
253: public static boolean isWindowsXP() {
254: return _isWindowsXP;
255: }
256:
257: /**
258: * Returns whether or not the os is some version of Windows 95.
259: *
260: * @return <tt>true</tt> if the application is running on Windows XP,
261: * <tt>false</tt> otherwise.
262: */
263: public static boolean isWindows95() {
264: return _isWindows95;
265: }
266:
267: /**
268: * Returns whether or not the os is some version of Windows 98.
269: *
270: * @return <tt>true</tt> if the application is running on Windows XP,
271: * <tt>false</tt> otherwise.
272: */
273: public static boolean isWindows98() {
274: return _isWindows98;
275: }
276:
277: /**
278: * Returns whether or not the os is some version of Windows 2003.
279: *
280: * @return <tt>true</tt> if the application is running on Windows 2003,
281: * <tt>false</tt> otherwise.
282: */
283: public static boolean isWindows2003() {
284: return _isWindows2003;
285: }
286:
287: /**
288: * Returns whether or not the os is Mac 9.1 or earlier.
289: *
290: * @return <tt>true</tt> if the application is running on a Mac version
291: * prior to OSX, <tt>false</tt> otherwise.
292: */
293: public static boolean isMacClassic() {
294: return _isMacClassic;
295: }
296:
297: /**
298: * Returns whether or not the os is Mac OSX.
299: *
300: * @return <tt>true</tt> if the application is running on Mac OSX,
301: * <tt>false</tt> otherwise.
302: */
303: public static boolean isMacOSX() {
304: return _isMacOSX;
305: }
306:
307: /**
308: * Returns whether or not the os is any Mac os.
309: *
310: * @return <tt>true</tt> if the application is running on Mac OSX
311: * or any previous mac version, <tt>false</tt> otherwise.
312: */
313: public static boolean isAnyMac() {
314: return _isMacClassic || _isMacOSX;
315: }
316:
317: /**
318: * Returns whether or not the os is Solaris.
319: *
320: * @return <tt>true</tt> if the application is running on Solaris,
321: * <tt>false</tt> otherwise.
322: */
323: public static boolean isSolaris() {
324: return _isSolaris;
325: }
326:
327: /**
328: * Returns whether or not the os is Linux.
329: *
330: * @return <tt>true</tt> if the application is running on Linux,
331: * <tt>false</tt> otherwise.
332: */
333: public static boolean isLinux() {
334: return _isLinux;
335: }
336:
337: /**
338: * Returns whether or not the os is some version of
339: * Unix, defined here as only Solaris or Linux.
340: *
341: * @return <tt>true</tt> if the application is running on a type of UNIX such as Linux or Solaris,
342: * <tt>false</tt> otherwise.
343: */
344: public static boolean isUnix() {
345: return _isLinux || _isSolaris;
346: }
347:
348: /**
349: * Returns whether or no the JDK version is 1.3 and above.
350: *
351: * @return <tt>true</tt> if the application is running on JDK 1.3 and above,
352: * <tt>false</tt> otherwise.
353: */
354: public static boolean isJdk13Above() {
355: String s = getJavaVersion();
356: String sub = s.substring(0, 3);
357: try {
358: double version = Double.parseDouble(sub);
359: return version >= 1.3;
360: } catch (NumberFormatException e) {
361: // ignore
362: }
363: return false;
364: }
365:
366: /**
367: * Returns whether or no the JDK version is 1.4.2 and above.
368: *
369: * @return <tt>true</tt> if the application is running on JDK 1.4.2 and above,
370: * <tt>false</tt> otherwise.
371: */
372: public static boolean isJdk142Above() {
373: String s = getJavaVersion();
374: String sub = s.substring(0, 5);
375:
376: return sub.compareTo("1.4.2") >= 0;
377:
378: }
379:
380: /**
381: * Returns whether or no the JDK version is 1.4 and above.
382: *
383: * @return <tt>true</tt> if the application is running on JDK 1.4 and above,
384: * <tt>false</tt> otherwise.
385: */
386: public static boolean isJdk14Above() {
387: String s = getJavaVersion();
388: String sub = s.substring(0, 3);
389: try {
390: double version = Double.parseDouble(sub);
391: return version >= 1.4;
392: } catch (NumberFormatException e) {
393: // ignore
394: }
395: return false;
396: }
397:
398: /**
399: * Returns whether or no the JDK version is 1.5 and above.
400: *
401: * @return <tt>true</tt> if the application is running on JDK 1.5 and above,
402: * <tt>false</tt> otherwise.
403: */
404: public static boolean isJdk15Above() {
405: String s = getJavaVersion();
406: String sub = s.substring(0, 3);
407: try {
408: double version = Double.parseDouble(sub);
409: return version >= 1.5;
410: } catch (NumberFormatException e) {
411: // ignore
412: }
413: return false;
414: }
415:
416: /**
417: * Returns whether or no the JDK version is 6 and above.
418: *
419: * @return <tt>true</tt> if the application is running on JDK 6 and above,
420: * <tt>false</tt> otherwise.
421: * @deprecated use {@link #isJdk6Above()}. Nothing is wrong with this method. It's just
422: * Sun starts to call JDK1.6 as JDK6 now.
423: */
424: public static boolean isJdk16Above() {
425: return isJdk6Above();
426: }
427:
428: /**
429: * Returns whether or no the JDK version is 6 and above.
430: *
431: * @return <tt>true</tt> if the application is running on JDK 6 and above,
432: * <tt>false</tt> otherwise.
433: */
434: public static boolean isJdk6Above() {
435: String s = getJavaVersion();
436: String sub = s.substring(0, 3);
437: try {
438: double version = Double.parseDouble(sub);
439: return version >= 1.6;
440: } catch (NumberFormatException e) {
441: // ignore
442: }
443: return false;
444: }
445:
446: /**
447: * Returns whether or no the JDK version is 1.7 and above.
448: *
449: * @return <tt>true</tt> if the application is running on JDK 1.7 and above,
450: * <tt>false</tt> otherwise.
451: */
452: public static boolean isJdk7Above() {
453: String s = getJavaVersion();
454: String sub = s.substring(0, 3);
455: try {
456: double version = Double.parseDouble(sub);
457: return version >= 1.7;
458: } catch (NumberFormatException e) {
459: // ignore
460: }
461: return false;
462: }
463:
464: /**
465: * Returns whether the default locale is one of the three language - Chinese, Japanese or Korean - also known as CJK.
466: *
467: * @return true if the default locale is in CJK.
468: */
469: public static boolean isCJKLocale() {
470: return isCJKLocale(Locale.getDefault());
471: }
472:
473: /**
474: * Returns whether the locale is one of the three language - Chinese, Japanese or Korean - also known as CJK.
475: *
476: * @param locale the locale to be checked.
477: * @return true if the default locale is in CJK.
478: */
479: public static boolean isCJKLocale(Locale locale) {
480: return locale.equals(Locale.CHINA)
481: || locale.equals(Locale.CHINESE)
482: || locale.equals(new Locale("zh", "HK"))
483: || locale.equals(Locale.TAIWAN)
484: || locale.equals(Locale.JAPAN)
485: || locale.equals(Locale.JAPANESE)
486: || locale.equals(Locale.KOREA)
487: || locale.equals(Locale.KOREAN);
488: }
489:
490: }
|