1 19 20 package org.netbeans.modules.search; 21 22 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.lang.ref.Reference ; 26 import java.lang.ref.WeakReference ; 27 import java.util.Iterator ; 28 import javax.swing.Action ; 29 import javax.swing.ActionMap ; 30 import org.openide.ErrorManager; 31 32 import org.openide.actions.FindAction; 33 import org.openide.text.CloneableEditorSupport; 34 import org.openide.util.SharedClassObject; 35 import org.openide.util.WeakSet; 36 import org.openide.util.Mutex; 37 import org.openide.windows.TopComponent; 38 39 40 49 final class FindActionManager implements PropertyChangeListener , Runnable { 50 51 52 private static final String MAPPED_FIND_ACTION 53 = FindActionManager.class.getName() + " - FindActionImpl"; 55 57 private static FindActionManager instance; 58 59 private final SearchPerformer performer; 60 61 private final WeakSet activatedOnWindows = new WeakSet(8); 62 63 64 private Object findActionMapKey; 65 66 68 private FindActionManager() { 69 performer = (SearchPerformer) 70 SharedClassObject.findObject(SearchPerformer.class, true); 71 } 72 73 75 static FindActionManager getInstance() { 76 if (instance == null) { 77 instance = new FindActionManager(); 78 } 79 return instance; 80 } 81 82 84 void init() { 85 TopComponent.getRegistry().addPropertyChangeListener(this); 86 87 Mutex.EVENT.writeAccess(this); 88 } 89 90 92 void cleanup() { 93 TopComponent.getRegistry().removePropertyChangeListener(this); 95 96 105 Mutex.EVENT.readAccess(new Mutex.Action() { 106 public Object run() { 107 cleanupWindowRegistry(); 108 return null; 109 } 110 }); 111 } 112 113 115 public void run() { 116 someoneActivated(); 117 } 118 119 121 private void cleanupWindowRegistry() { 122 final Object findActionKey = getFindActionMapKey(); 124 125 for (Iterator i = activatedOnWindows.iterator(); i.hasNext(); ) { 126 TopComponent tc = (TopComponent) i.next(); 127 129 Action origFindAction = null, currFindAction = null; 130 131 Object origFindActionRef = tc.getClientProperty(MAPPED_FIND_ACTION); 132 if (origFindActionRef instanceof Reference ) { 133 Object origFindActionObj = ((Reference )origFindActionRef).get(); 134 if (origFindActionObj instanceof Action ) { 135 origFindAction = (Action ) origFindActionObj; 136 } 137 } 138 139 if (origFindAction != null) { 140 currFindAction = tc.getActionMap().get(findActionKey); 141 } 142 143 if ((currFindAction != null) && (currFindAction == origFindAction)){ 144 tc.getActionMap().put(findActionKey, null); 145 } else { 147 ErrorManager.getDefault().log( 149 ErrorManager.WARNING, 150 "ActionMap mapping of FindAction changed" + " for window " + tc.getName()); } 153 154 if (origFindActionRef != null) { 155 tc.putClientProperty(MAPPED_FIND_ACTION, null); 156 } 157 } 158 activatedOnWindows.clear(); 159 } 160 161 163 private void someoneActivated() { 164 TopComponent window = TopComponent.getRegistry().getActivated(); 165 166 if ((window == null) || (window instanceof CloneableEditorSupport.Pane)) { 167 return; 168 } 169 170 Object key = getFindActionMapKey(); 171 ActionMap actionMap = window.getActionMap(); 172 173 if ((actionMap.get(key) == null) && activatedOnWindows.add(window)) { 174 176 Action a = performer.createContextAwareInstance(window.getLookup()); 177 178 actionMap.put(key, a); 179 window.putClientProperty(MAPPED_FIND_ACTION, new WeakReference (a)); 180 } 181 } 182 183 184 public void propertyChange(PropertyChangeEvent evt) { 185 if (TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())){ 186 someoneActivated(); 187 } 188 } 189 190 192 private Object getFindActionMapKey() { 193 if (findActionMapKey == null) { 194 SharedClassObject findAction = 195 SharedClassObject.findObject(FindAction.class); 196 assert findAction != null; 197 198 findActionMapKey = ((FindAction) findAction).getActionMapKey(); 199 } 200 return findActionMapKey; 201 } 202 203 } 204 | Popular Tags |