1 19 20 package org.netbeans.core.multiview; 21 22 23 import javax.swing.Action ; 24 import javax.swing.ActionMap ; 25 import javax.swing.JComponent ; 26 import java.util.Arrays ; 27 import org.openide.windows.TopComponent; 28 29 30 35 final class MultiViewActionMap extends ActionMap { 36 private ActionMap delegate; 37 private ActionMap topComponentMap; 38 private TopComponent component; 39 40 private boolean preventRecursive = false; 41 private Object LOCK = new Object (); 42 43 public MultiViewActionMap(TopComponent tc, ActionMap tcMap) { 44 topComponentMap = tcMap; 45 component = tc; 46 } 47 48 public void setDelegateMap(ActionMap map) { 49 delegate = map; 50 } 51 52 public int size() { 53 return keys ().length; 54 } 55 56 public Action get(Object key) { 57 javax.swing.ActionMap m = topComponentMap; 59 if (m != null) { 60 Action a = m.get (key); 61 if (a != null) { 62 return a; 63 } 64 } 65 m = delegate; 67 if (m != null) { 68 synchronized (LOCK) { 72 if (preventRecursive) { 73 preventRecursive = false; 74 return null; 75 } 76 preventRecursive = true; 77 Action a = m.get (key); 78 preventRecursive = false; 79 if (a != null) { 80 return a; 81 } 82 } 83 } 84 85 java.awt.Component owner = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 86 Action found = null; 87 while (owner != null && owner != component) { 88 if (found == null && (owner instanceof JComponent )) { 89 m = ((JComponent )owner).getActionMap (); 90 if (m != null) { 91 found = m.get (key); 92 } 93 } 94 owner = owner.getParent (); 95 } 96 97 return owner == component ? found : null; 98 } 99 100 public Object [] allKeys() { 101 return keys (true); 102 } 103 104 public Object [] keys() { 105 return keys (false); 106 } 107 108 109 private Object [] keys(boolean all) { 110 java.util.Set keys = new java.util.HashSet (); 111 112 if (delegate != null) { 113 java.util.List l; 114 if (all) { 115 l = Arrays.asList(delegate.allKeys()); 116 } else { 117 l = Arrays.asList(delegate.keys()); 118 } 119 keys.addAll(l); 120 } 121 122 if (topComponentMap != null) { 123 java.util.List l; 124 125 if (all) { 126 l = Arrays.asList (topComponentMap.allKeys ()); 127 } else { 128 l = Arrays.asList (topComponentMap.keys ()); 129 } 130 131 keys.addAll (l); 132 } 133 134 return keys.toArray(); 135 } 136 137 public void remove(Object key) { 141 topComponentMap.remove(key); 142 } 143 144 public void setParent(ActionMap map) { 145 topComponentMap.setParent(map); 146 } 147 148 public void clear() { 149 topComponentMap.clear(); 150 } 151 152 public void put(Object key, Action action) { 153 topComponentMap.put (key, action); 154 } 155 156 public ActionMap getParent() { 157 return topComponentMap.getParent(); 158 } 159 160 } 161 | Popular Tags |