1 25 26 package com.sun.java.swing; 27 28 import sun.awt.EventQueueDelegate; 29 import sun.awt.AppContext; 30 import java.util.Map ; 31 import java.util.concurrent.Callable ; 32 import java.awt.AWTEvent ; 33 import java.awt.EventQueue ; 34 import java.awt.Component ; 35 import javax.swing.JComponent ; 36 import javax.swing.RepaintManager ; 37 38 51 public class SwingUtilities3 { 52 55 private static final Object DELEGATE_REPAINT_MANAGER_KEY = 56 new StringBuilder ("DelegateRepaintManagerKey"); 57 58 61 public static void setDelegateRepaintManager(JComponent component, 62 RepaintManager repaintManager) { 63 66 AppContext.getAppContext().put(DELEGATE_REPAINT_MANAGER_KEY, 67 Boolean.TRUE); 68 69 component.putClientProperty(DELEGATE_REPAINT_MANAGER_KEY, 70 repaintManager); 71 } 72 73 76 public static RepaintManager getDelegateRepaintManager(Component 77 component) { 78 RepaintManager delegate = null; 79 if (Boolean.TRUE == AppContext.getAppContext().get( 80 DELEGATE_REPAINT_MANAGER_KEY)) { 81 while (delegate == null && component != null) { 82 while (component != null 83 && ! (component instanceof JComponent )) { 84 component = component.getParent(); 85 } 86 if (component != null) { 87 delegate = (RepaintManager ) 88 ((JComponent ) component) 89 .getClientProperty(DELEGATE_REPAINT_MANAGER_KEY); 90 component = component.getParent(); 91 } 92 93 } 94 } 95 return delegate; 96 } 97 98 102 public static void setEventQueueDelegate( 103 Map <String , Map <String , Object >> map) { 104 EventQueueDelegate.setDelegate(new EventQueueDelegateFromMap(map)); 105 } 106 107 private static class EventQueueDelegateFromMap 108 implements EventQueueDelegate.Delegate { 109 private final AWTEvent [] afterDispatchEventArgument; 110 private final Object [] afterDispatchHandleArgument; 111 private final Callable <Void > afterDispatchCallable; 112 113 private final AWTEvent [] beforeDispatchEventArgument; 114 private final Callable <Object > beforeDispatchCallable; 115 116 private final EventQueue [] getNextEventEventQueueArgument; 117 private final Callable <AWTEvent > getNextEventCallable; 118 119 @SuppressWarnings ("unchecked") 120 public EventQueueDelegateFromMap(Map <String , Map <String , Object >> objectMap) { 121 Map <String , Object > methodMap = objectMap.get("afterDispatch"); 122 afterDispatchEventArgument = (AWTEvent []) methodMap.get("event"); 123 afterDispatchHandleArgument = (Object []) methodMap.get("handle"); 124 afterDispatchCallable = (Callable <Void >) methodMap.get("method"); 125 126 methodMap = objectMap.get("beforeDispatch"); 127 beforeDispatchEventArgument = (AWTEvent []) methodMap.get("event"); 128 beforeDispatchCallable = (Callable <Object >) methodMap.get("method"); 129 130 methodMap = objectMap.get("getNextEvent"); 131 getNextEventEventQueueArgument = 132 (EventQueue []) methodMap.get("eventQueue"); 133 getNextEventCallable = (Callable <AWTEvent >) methodMap.get("method"); 134 } 135 136 @Override 137 public void afterDispatch(AWTEvent event, Object handle) { 138 afterDispatchEventArgument[0] = event; 139 afterDispatchHandleArgument[0] = handle; 140 try { 141 afterDispatchCallable.call(); 142 } catch (Exception e) { 143 if (e instanceof RuntimeException ) { 144 throw (RuntimeException ) e; 145 } 146 } 147 } 148 149 @Override 150 public Object beforeDispatch(AWTEvent event) { 151 beforeDispatchEventArgument[0] = event; 152 try { 153 return beforeDispatchCallable.call(); 154 } catch (Exception e) { 155 if (e instanceof RuntimeException ) { 156 throw (RuntimeException ) e; 157 } 158 } 159 return null; 160 } 161 162 @Override 163 public AWTEvent getNextEvent(EventQueue eventQueue) { 164 getNextEventEventQueueArgument[0] = eventQueue; 165 try { 166 return getNextEventCallable.call(); 167 } catch (Exception e) { 168 if (e instanceof RuntimeException ) { 169 throw (RuntimeException ) e; 170 } 171 } 172 return null; 173 } 174 } 175 } 176 | Popular Tags |