1 5 package com.sun.java.swing.plaf.windows; 6 7 import java.awt.*; 8 import java.beans.*; 9 import java.lang.ref.*; 10 import javax.swing.*; 11 import javax.swing.plaf.*; 12 13 22 public class DesktopProperty implements UIDefaults.ActiveValue { 25 28 private static boolean updatePending; 29 30 33 private static ReferenceQueue queue; 34 35 36 39 private WeakPCL pcl; 40 43 private String key; 44 47 private Object value; 48 51 private Object fallback; 52 53 56 private Toolkit toolkit; 57 58 59 static { 60 queue = new ReferenceQueue(); 61 } 62 63 67 static void flushUnreferencedProperties() { 68 WeakPCL pcl; 69 70 while ((pcl = (WeakPCL)queue.poll()) != null) { 71 pcl.dispose(); 72 } 73 } 74 75 76 79 private static synchronized void setUpdatePending(boolean update) { 80 updatePending = update; 81 } 82 83 86 private static synchronized boolean isUpdatePending() { 87 return updatePending; 88 } 89 90 93 private static void updateAllUIs() { 94 Class uiClass = UIManager.getLookAndFeel().getClass(); 97 if (uiClass.getPackage().equals(DesktopProperty.class.getPackage())) { 98 XPStyle.invalidateStyle(); 99 } 100 Frame appFrames[] = Frame.getFrames(); 101 for (int j=0; j < appFrames.length; j++) { 102 updateWindowUI(appFrames[j]); 103 } 104 } 105 106 109 private static void updateWindowUI(Window window) { 110 SwingUtilities.updateComponentTreeUI(window); 111 Window ownedWins[] = window.getOwnedWindows(); 112 for (int i=0; i < ownedWins.length; i++) { 113 updateWindowUI(ownedWins[i]); 114 } 115 } 116 117 118 126 public DesktopProperty(String key, Object fallback, Toolkit toolkit) { 127 this.key = key; 128 this.fallback = fallback; 129 this.toolkit = toolkit; 130 flushUnreferencedProperties(); 140 } 141 142 146 public Object createValue(UIDefaults table) { 147 if (value == null) { 148 value = configureValue(getValueFromDesktop()); 149 if (value == null) { 150 value = configureValue(getDefaultValue()); 151 } 152 } 153 return value; 154 } 155 156 159 protected Object getValueFromDesktop() { 160 if (this.toolkit == null) { 161 this.toolkit = Toolkit.getDefaultToolkit(); 162 } 163 Object value = toolkit.getDesktopProperty(getKey()); 164 pcl = new WeakPCL(this, toolkit, getKey(), UIManager.getLookAndFeel()); 165 toolkit.addPropertyChangeListener(getKey(), pcl); 166 return value; 167 } 168 169 172 protected Object getDefaultValue() { 173 return fallback; 174 } 175 176 180 public void invalidate() { 181 if (pcl != null) { 182 toolkit.removePropertyChangeListener(getKey(), pcl); 183 toolkit = null; 184 pcl = null; 185 value = null; 186 } 187 } 188 189 196 protected void updateUI() { 197 if (!isUpdatePending()) { 198 setUpdatePending(true); 199 Runnable uiUpdater = new Runnable () { 200 public void run() { 201 updateAllUIs(); 202 setUpdatePending(false); 203 } 204 }; 205 SwingUtilities.invokeLater(uiUpdater); 206 } 207 } 208 209 213 protected Object configureValue(Object value) { 214 if (value != null) { 215 if (value instanceof Color) { 216 return new ColorUIResource((Color)value); 217 } 218 else if (value instanceof Font) { 219 return new FontUIResource((Font)value); 220 } 221 else if (value instanceof UIDefaults.LazyValue) { 222 value = ((UIDefaults.LazyValue)value).createValue(null); 223 } 224 else if (value instanceof UIDefaults.ActiveValue) { 225 value = ((UIDefaults.ActiveValue)value).createValue(null); 226 } 227 } 228 return value; 229 } 230 231 234 protected String getKey() { 235 return key; 236 } 237 238 239 240 245 private static class WeakPCL extends WeakReference 246 implements PropertyChangeListener { 247 private Toolkit kit; 248 private String key; 249 private LookAndFeel laf; 250 251 WeakPCL(Object target, Toolkit kit, String key, LookAndFeel laf) { 252 super(target, queue); 253 this.kit = kit; 254 this.key = key; 255 this.laf = laf; 256 } 257 258 public void propertyChange(PropertyChangeEvent pce) { 259 DesktopProperty property = (DesktopProperty)get(); 260 261 if (property == null || laf != UIManager.getLookAndFeel()) { 262 dispose(); 265 } 266 else { 267 property.invalidate(); 268 property.updateUI(); 269 } 270 } 271 272 void dispose() { 273 kit.removePropertyChangeListener(key, this); 274 } 275 } 276 } 277 | Popular Tags |