43 44 45 /** 46 * Implements Theme Support for Windows XP. 47 * 48 * @author Sergey Salishev 49 * @author Bino George 50 * @author Igor Kushnirskiy 51 */ 52 public final class ThemeReader { 53 54 private static final Map<String, Long> widgetToTheme = new HashMap<>(); 55 56 // lock for the cache 57 // reading should be done with readLock 58 // writing with writeLock 59 private static final ReadWriteLock readWriteLock = 60 new ReentrantReadWriteLock(); 61 private static final Lock readLock = readWriteLock.readLock(); 62 private static final Lock writeLock = readWriteLock.writeLock(); 63 64 static void flush() { 65 writeLock.lock(); 66 try { 67 // Close old themes. 68 for (Long value : widgetToTheme.values()) { 69 closeTheme(value.longValue()); 70 } 71 widgetToTheme.clear(); 72 } finally { 73 writeLock.unlock(); 74 } 75 } 76 77 public static native boolean isThemed(); 78 79 // this should be called only with writeLock held 80 private static Long getThemeImpl(String widget) { 81 Long theme = widgetToTheme.get(widget); 82 if (theme == null) { 83 int i = widget.indexOf("::"); 84 if (i > 0) { 85 // We're using the syntax "subAppName::controlName" here, as used by msstyles. 86 // See documentation for SetWindowTheme on MSDN. 87 setWindowTheme(widget.substring(0, i)); 88 theme = openTheme(widget.substring(i+2)); 89 setWindowTheme(null); 90 } else { 91 theme = openTheme(widget); 92 } 93 widgetToTheme.put(widget, theme); 94 } 95 return theme; 96 } 97 98 // returns theme value 99 // this method should be invoked with readLock locked 100 private static Long getTheme(String widget) { 101 // mostly copied from the javadoc for ReentrantReadWriteLock 102 Long theme = widgetToTheme.get(widget); 103 if (theme == null) { 104 readLock.unlock(); 105 writeLock.lock(); 106 try { 107 theme = getThemeImpl(widget); 108 } finally { 109 readLock.lock(); 110 writeLock.unlock();// Unlock write, still hold read 111 } 112 } 113 return theme; 114 } 115 116 private static native void paintBackground(int[] buffer, long theme, 117 int part, int state, int x, 118 int y, int w, int h, int stride); 119 120 public static void paintBackground(int[] buffer, String widget, | 43 44 45 /** 46 * Implements Theme Support for Windows XP. 47 * 48 * @author Sergey Salishev 49 * @author Bino George 50 * @author Igor Kushnirskiy 51 */ 52 public final class ThemeReader { 53 54 private static final Map<String, Long> widgetToTheme = new HashMap<>(); 55 56 // lock for the cache 57 // reading should be done with readLock 58 // writing with writeLock 59 private static final ReadWriteLock readWriteLock = 60 new ReentrantReadWriteLock(); 61 private static final Lock readLock = readWriteLock.readLock(); 62 private static final Lock writeLock = readWriteLock.writeLock(); 63 private static volatile boolean valid = false; 64 65 static volatile boolean xpStyleEnabled; 66 67 static void flush() { 68 // Could be called on Toolkit thread, so do not try to acquire locks 69 // to avoid deadlock with theme initialization 70 valid = false; 71 } 72 73 public static native boolean isThemed(); 74 75 public static boolean isXPStyleEnabled() { 76 return xpStyleEnabled; 77 } 78 79 // this should be called only with writeLock held 80 private static Long getThemeImpl(String widget) { 81 Long theme = widgetToTheme.get(widget); 82 if (theme == null) { 83 int i = widget.indexOf("::"); 84 if (i > 0) { 85 // We're using the syntax "subAppName::controlName" here, as used by msstyles. 86 // See documentation for SetWindowTheme on MSDN. 87 setWindowTheme(widget.substring(0, i)); 88 theme = openTheme(widget.substring(i+2)); 89 setWindowTheme(null); 90 } else { 91 theme = openTheme(widget); 92 } 93 widgetToTheme.put(widget, theme); 94 } 95 return theme; 96 } 97 98 // returns theme value 99 // this method should be invoked with readLock locked 100 private static Long getTheme(String widget) { 101 if (!valid) { 102 readLock.unlock(); 103 writeLock.lock(); 104 try { 105 if (!valid) { 106 // Close old themes. 107 for (Long value : widgetToTheme.values()) { 108 closeTheme(value); 109 } 110 widgetToTheme.clear(); 111 valid = true; 112 } 113 } finally { 114 readLock.lock(); 115 writeLock.unlock(); 116 } 117 } 118 119 // mostly copied from the javadoc for ReentrantReadWriteLock 120 Long theme = widgetToTheme.get(widget); 121 if (theme == null) { 122 readLock.unlock(); 123 writeLock.lock(); 124 try { 125 theme = getThemeImpl(widget); 126 } finally { 127 readLock.lock(); 128 writeLock.unlock();// Unlock write, still hold read 129 } 130 } 131 return theme; 132 } 133 134 private static native void paintBackground(int[] buffer, long theme, 135 int part, int state, int x, 136 int y, int w, int h, int stride); 137 138 public static void paintBackground(int[] buffer, String widget, |