changeset 1142:66b8b266a5dd

Fix for GTKThemeUtils review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-June/007071.html reviewed-by: vanaltj
author Mario Torre <neugens.limasoftware@gmail.com>
date Mon, 24 Jun 2013 13:50:44 +0200
parents 2e71b9784316
children 9683c9b7bd61
files laf-utils/src/main/java/com/redhat/thermostat/internal/utils/laf/ThemeManager.java laf-utils/src/main/java/com/redhat/thermostat/internal/utils/laf/gtk/GTKThemeUtils.java laf-utils/src/main/native/GTKThemeUtils.c
diffstat 3 files changed, 67 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/laf-utils/src/main/java/com/redhat/thermostat/internal/utils/laf/ThemeManager.java	Mon Jun 10 17:00:25 2013 -0400
+++ b/laf-utils/src/main/java/com/redhat/thermostat/internal/utils/laf/ThemeManager.java	Mon Jun 24 13:50:44 2013 +0200
@@ -41,6 +41,7 @@
 
 import javax.swing.JPopupMenu;
 import javax.swing.SwingConstants;
+import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
 import javax.swing.plaf.nimbus.NimbusLookAndFeel;
@@ -71,8 +72,25 @@
         return set;
     }
 
+    /**
+     * Sets the Look and Feel for Thermostat based on user preferences.
+     * 
+     * <br /><br />
+     * 
+     * If the default theme is used, we try to match if possible the native
+     * theme main colours.
+     * 
+     * <br /><br />
+     * 
+     * This method must be called in the EDT.
+     */
     public void setLAF() {
         
+        if (!SwingUtilities.isEventDispatchThread()) {
+            throw new IllegalStateException("This method expect to be called " +
+            		                    "from the Event Dispatching Thread");
+        }
+        
         boolean tryGTKColors = false;
         
         // check if the user has other preferences...
--- a/laf-utils/src/main/java/com/redhat/thermostat/internal/utils/laf/gtk/GTKThemeUtils.java	Mon Jun 10 17:00:25 2013 -0400
+++ b/laf-utils/src/main/java/com/redhat/thermostat/internal/utils/laf/gtk/GTKThemeUtils.java	Mon Jun 24 13:50:44 2013 +0200
@@ -40,20 +40,23 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 
-import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 
 import com.redhat.thermostat.shared.config.NativeLibraryResolver;
 
 public class GTKThemeUtils {
 
+    private static boolean nativeLoaded;
     private static boolean initialized;
     static {
         try {
             String lib = NativeLibraryResolver.getAbsoluteLibraryPath("GTKThemeUtils");
             System.load(lib);
-            initialized = init();
-        } catch (UnsatisfiedLinkError ignore) {}
+            nativeLoaded = true;
+        
+        } catch (UnsatisfiedLinkError ignore) {
+            nativeLoaded = false;
+        }
     }
     
     native private static boolean init();
@@ -83,7 +86,7 @@
         rederiveColorMethod.setAccessible(true);
         rederiveColorMethod.invoke(color);
     }
-        
+
     private Color deriveColor(String colorID, Color defaultColor, float bOffset) {
 
         Color result = defaultColor;
@@ -108,52 +111,52 @@
     
     public void setNimbusColours() {
 
-        if (!initialized) {
+        if (!nativeLoaded) {
+            return;
+        }
+        
+        if (!initialized && !init()) {
             return;
         }
         
-        SwingUtilities.invokeLater(new Runnable() {
-            @Override
-            public void run() {
+        initialized = true;
+        
+        // if we at least have the bg colour we can try the rest,
+        // otherwise, just skip everything and use nimbus defaults
+        if (hasColor("bg_color")) {
+            
+            // Those numbers are some kind of magic, they represent the
+            // value, or brightness, in the HSV encoding of the colour.
+            // The idea is to derive a darker version of the
+            // base colour because nimbus will use a brighter version
+            // for most components. The version used by nimbus does not
+            // exactly match because nimbus use many multi-gradient
+            // paints.
+            float brightnessOffset = -.300f;
+            
+            Color nimbusBase = deriveColor("bg_color", UIManager.getDefaults().getColor("nimbusBase"), brightnessOffset);
+            Color control = UIManager.getDefaults().getColor("control");
+            int bgColor = getColor("bg_color");
+            control = new Color(bgColor);
+            
+            Color info = control;
+            
+            UIManager.put("nimbusBase", nimbusBase);
+            
+            UIManager.put("control", control);
+            UIManager.put("info", info);
+            
+            Color nimbusFocus = UIManager.getDefaults().getColor("nimbusFocus");
+            if (hasColor("selected_bg_color")) {
+                int fgColor = getColor("selected_bg_color");
+                nimbusFocus = new Color(fgColor);
                 
-                // if we at least have the bg colour we can try the rest,
-                // otherwise, just skip everything and use nimbus defaults
-                if (hasColor("bg_color")) {
-
-                    // Those numbers are some kind of magic, they represent the
-                    // value, or brightness, in the HSV encoding of the colour.
-                    // The idea is to derive a darker version of the
-                    // base colour because nimbus will use a brighter version
-                    // for most components. The version used by nimbus does not
-                    // exactly match because nimbus use many multi-gradient
-                    // paints.
-                    float brightnessOffset = -.300f;
-                    
-                    Color nimbusBase = deriveColor("bg_color", UIManager.getDefaults().getColor("nimbusBase"), brightnessOffset);
-                    Color control = UIManager.getDefaults().getColor("control");
-                    int bgColor = getColor("bg_color");
-                    control = new Color(bgColor);
-                    
-                    Color info = control;
-                    
-                    UIManager.put("nimbusBase", nimbusBase);
-                    
-                    UIManager.put("control", control);
-                    UIManager.put("info", info);
-
-                    Color nimbusFocus = UIManager.getDefaults().getColor("nimbusFocus");
-                    if (hasColor("selected_bg_color")) {
-                        int fgColor = getColor("selected_bg_color");
-                        nimbusFocus = new Color(fgColor);
-                        
-                        UIManager.put("nimbusFocus", nimbusFocus);
-                        UIManager.put("nimbusSelectionBackground", nimbusFocus);
-                        UIManager.put("nimbusSelection", nimbusFocus);
-                        UIManager.put("menu", nimbusFocus);
-                        UIManager.put("Menu.background", nimbusFocus);
-                    }
-                }
+                UIManager.put("nimbusFocus", nimbusFocus);
+                UIManager.put("nimbusSelectionBackground", nimbusFocus);
+                UIManager.put("nimbusSelection", nimbusFocus);
+                UIManager.put("menu", nimbusFocus);
+                UIManager.put("Menu.background", nimbusFocus);
             }
-        });
+        }
     }
 }
--- a/laf-utils/src/main/native/GTKThemeUtils.c	Mon Jun 10 17:00:25 2013 -0400
+++ b/laf-utils/src/main/native/GTKThemeUtils.c	Mon Jun 24 13:50:44 2013 +0200
@@ -57,18 +57,11 @@
     handler = XSetErrorHandler(NULL);
     io_handler = XSetIOErrorHandler(NULL);
 
-    g_thread_init(NULL);
-    gdk_threads_init();
-
-    gdk_threads_enter();
-
     gboolean result = gtk_init_check(NULL, NULL);
 
     XSetErrorHandler(handler);
     XSetIOErrorHandler(io_handler);
 
-    gdk_threads_leave();
-
     return (result == TRUE) ? JNI_TRUE : JNI_FALSE;
 }
 
@@ -76,8 +69,6 @@
 Java_com_redhat_thermostat_internal_utils_laf_gtk_GTKThemeUtils_hasColor
     (JNIEnv *env, jclass GTKThemeUtils, jstring jColourID)
 {
-    gdk_threads_enter();
-
     const char *colourID = (*env)->GetStringUTFChars(env, jColourID, NULL);
     gboolean result = FALSE;
 
@@ -102,8 +93,6 @@
 bailString:
     (*env)->ReleaseStringUTFChars(env, jColourID, colourID);
 
-    gdk_threads_leave();
-
     return (result == TRUE) ? JNI_TRUE : JNI_FALSE;
 }
 
@@ -111,9 +100,6 @@
 Java_com_redhat_thermostat_internal_utils_laf_gtk_GTKThemeUtils_getColor
     (JNIEnv *env, jclass GTKThemeUtils, jstring jColourID)
 {
-
-    gdk_threads_enter();
-
     const char *colourID = (*env)->GetStringUTFChars(env, jColourID, NULL);
 
     GtkWidget *dummy = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@@ -153,7 +139,5 @@
 bailString:
     (*env)->ReleaseStringUTFChars(env, jColourID, colourID);
 
-    gdk_threads_leave();
-
     return pixel;
 }