PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/gui/src/main/java/com/jakeapp/gui/swing/helpers/Platform.java

https://github.com/JohannesBuchner/Jake
Java | 208 lines | 105 code | 33 blank | 70 comment | 13 complexity | a9624c533741d08cf24f63c410266493 MD5 | raw file
Possible License(s): AGPL-1.0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.jakeapp.gui.swing.helpers;
  6. import com.jakeapp.gui.swing.JakeMainApp;
  7. import com.jakeapp.gui.swing.helpers.pftools.AbstractPfTools;
  8. import com.jakeapp.gui.swing.helpers.pftools.MacPfTools;
  9. import com.jakeapp.gui.swing.helpers.pftools.NullPfTools;
  10. import com.jakeapp.gui.swing.helpers.pftools.WinPfTools;
  11. import com.jakeapp.gui.swing.helpers.styler.MacStyler;
  12. import com.jakeapp.gui.swing.helpers.styler.NullStyler;
  13. import com.jakeapp.gui.swing.helpers.styler.Styler;
  14. import com.jakeapp.gui.swing.helpers.styler.WinStyler;
  15. import org.apache.log4j.Logger;
  16. import javax.swing.*;
  17. import java.awt.*;
  18. import java.awt.image.BufferedImage;
  19. /**
  20. * @author studpete
  21. */
  22. public class Platform {
  23. private static final Logger log = Logger.getLogger(Platform.class);
  24. /**
  25. * Tests if a dialog should be shown as sheet.
  26. * Only enabled on mac.
  27. *
  28. * @return
  29. */
  30. public static boolean isSetShowAsSheet() {
  31. return isMac();
  32. }
  33. enum OperatingSystem {
  34. Windows, Mac, Linux, Other
  35. }
  36. private static OperatingSystem os;
  37. private static Styler styler;
  38. private static AbstractPfTools pftools;
  39. // static initializeJakeMainHelper code
  40. static {
  41. if (System.getProperty("os.name").startsWith("Mac OS")) {
  42. os = OperatingSystem.Mac;
  43. } else if (System.getProperty("os.name").startsWith("Windows")) {
  44. os = OperatingSystem.Windows;
  45. } else if (System.getProperty("os.name").startsWith("Lin")) {
  46. os = OperatingSystem.Linux;
  47. } else {
  48. os = OperatingSystem.Other;
  49. }
  50. if (isMac()) {
  51. styler = new MacStyler();
  52. pftools = new MacPfTools();
  53. } else if (isWin()) {
  54. styler = new WinStyler();
  55. pftools = new WinPfTools();
  56. } else {
  57. styler = new NullStyler();
  58. pftools = new NullPfTools();
  59. }
  60. log.info("Jake's Platform: " + System.getProperty("os.name") + " @ " + System
  61. .getProperty("java.version") + ", using " + styler.getClass()
  62. .getSimpleName() + " with LAF " + UIManager.getLookAndFeel().getName());
  63. }
  64. /**
  65. * Get's the version of Java currently running.
  66. *
  67. * @return the version of Java that is running.
  68. */
  69. public static String getJavaVersion() {
  70. return System.getProperty("java.version");
  71. }
  72. /**
  73. * True if this JVM is running on a Mac.
  74. *
  75. * @return true if this JVM is running on a Mac.
  76. */
  77. public static boolean isMac() {
  78. return os == OperatingSystem.Mac;
  79. }
  80. // FIXME: we don't support tiger anyway
  81. public static boolean isMacTiger() {
  82. return System.getProperty("os.version").startsWith("10.4");
  83. }
  84. public static boolean isMacLeopard() {
  85. return System.getProperty("os.version").startsWith("10.5");
  86. }
  87. /**
  88. * True if this JVM is running on Windows.
  89. *
  90. * @return true if this JVM is running on Windows.
  91. */
  92. public static boolean isWin() {
  93. return os == OperatingSystem.Windows;
  94. }
  95. /**
  96. * True if this JVM is running on Linux.
  97. *
  98. * @return true if this JVM is running on Linux.
  99. */
  100. public static boolean isLin() {
  101. return os == OperatingSystem.Linux;
  102. }
  103. /**
  104. * Returns the pf dependant styler.
  105. *
  106. * @return
  107. */
  108. public static Styler getStyler() {
  109. return styler;
  110. }
  111. /**
  112. * Returns the pf dependant toolkit
  113. *
  114. * @return
  115. */
  116. public static AbstractPfTools getToolkit() {
  117. return pftools;
  118. }
  119. /**
  120. * Overrides AWT's default guess of what to use as our windows' WM_CLASS.
  121. * <p/>
  122. * AWT's XToolkit guesses a WM_CLASS for us based on the bottom-most class name in the stack trace of the thread that causes its construction.
  123. * For those of our application that launch from e.util.Launcher, that means they all get the WM_CLASS "e-util-Launcher".
  124. * Even those that don't, get a fully-qualified name such as "e-tools-FatBits" or "terminator-Terminator".
  125. * These names aren't usually very important unless you're doing some ugly application-specific hacking in your window manager.
  126. * Sadly, though, they show through in certain cases:
  127. * 1. When space gets too tight for GNOME's panel to have an icon for each window, it starts collapsing them by application, and uses WM_CLASS as the application name.
  128. * 2.If you use the GNOME/the Java Desktop System's Alt-PrtScr screenshot tool, its default filename is "Screenshot-<WM_CLASS>".
  129. * There are probably more examples, but these are enough to warrant a solution.
  130. * Given that we know what our application calls itself, we can use reflection to override AWT's default guess.
  131. */
  132. public static void fixWmClass() {
  133. try {
  134. Toolkit xToolkit = Toolkit.getDefaultToolkit();
  135. java.lang.reflect.Field awtAppClassNameField =
  136. xToolkit.getClass().getDeclaredField("awtAppClassName");
  137. awtAppClassNameField.setAccessible(true);
  138. awtAppClassNameField.set(xToolkit, AppUtilities.getAppName());
  139. } catch (Throwable th) {
  140. log.warn("Failed to fix WM_CLASS for " + AppUtilities
  141. .getAppName() + " windows.", th);
  142. }
  143. }
  144. /**
  145. * Currently not used.
  146. * Event counter feature - like in mail.app.
  147. * Should be possible on windows too.
  148. * (tray icon change?)
  149. *
  150. * @param num
  151. */
  152. public static void setEventCounter(int num) {
  153. log.debug("set event count to: " + num);
  154. ImageIcon ico = ImageLoader.get(Platform.class, "/icons/jakeapp-large.png");
  155. BufferedImage newIcon = new BufferedImage(ico.getIconWidth(),
  156. ico.getIconHeight(),
  157. BufferedImage.TYPE_INT_ARGB);
  158. Graphics2D graphics = (Graphics2D) newIcon.getGraphics();
  159. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  160. RenderingHints.VALUE_ANTIALIAS_ON);
  161. graphics.setColor(Color.decode("#E40000"));
  162. graphics.drawImage(ico.getImage(), 0, 0, null);
  163. graphics.fillOval(ico.getIconWidth() - 40, 0, 35, 35);
  164. graphics.setColor(Color.WHITE);
  165. graphics.setFont(new Font("Helvetica", Font.BOLD, 23));
  166. graphics.drawString(Integer.toString(num), ico.getIconWidth() - 28, 25);
  167. graphics.dispose();
  168. // TODO: find a way to set app icon
  169. // may need jni interface!
  170. //setApplicationIconImage(newIcon);
  171. }
  172. // prevent construction
  173. private Platform() {
  174. }
  175. }