1 19 20 21 package org.netbeans.core.multiview; 22 import java.awt.event.ActionEvent ; 23 24 import javax.swing.AbstractAction ; 25 26 import javax.swing.Action ; 27 import javax.swing.ActionMap ; 28 import javax.swing.JComponent ; 29 30 31 32 33 import junit.framework.Test; 34 import junit.framework.TestSuite; 35 import junit.textui.TestRunner; 36 import org.netbeans.core.api.multiview.MultiViewHandler; 37 import org.netbeans.core.api.multiview.MultiViews; 38 import org.netbeans.core.spi.multiview.MultiViewDescription; 39 import org.netbeans.core.spi.multiview.MultiViewElement; 40 41 import org.netbeans.core.spi.multiview.MultiViewFactory; 42 import org.netbeans.junit.NbTestCase; 43 import org.netbeans.junit.NbTestSuite; 44 45 import org.openide.util.Lookup; 46 import org.openide.util.LookupEvent; 47 48 49 import org.openide.util.LookupListener; 50 import org.openide.util.Utilities; 51 import org.openide.util.lookup.Lookups; 52 53 54 import org.openide.windows.TopComponent; 55 56 57 58 62 public class MultiViewActionMapTest extends NbTestCase { 63 64 65 public MultiViewActionMapTest(String name) { 66 super (name); 67 } 68 69 72 public static void main(java.lang.String [] args) { 73 TestRunner.run(suite()); 74 } 75 76 public static Test suite() { 77 TestSuite suite = new NbTestSuite(MultiViewActionMapTest.class); 78 79 return suite; 80 } 81 82 protected boolean runInEQ () { 83 return true; 84 } 85 86 87 public void testElementIsTopComponent() throws Exception { 88 MVElemTopComponent elem1 = new MVElemTopComponent(); 89 MVElemTopComponent elem2 = new MVElemTopComponent(); 90 MVElemTopComponent elem3 = new MVElemTopComponent(); 91 doTestActionMap(elem1, elem2, elem3); 92 } 93 94 public void testElementIsNotTC() throws Exception { 95 MVElem elem1 = new MVElem(); 96 MVElem elem2 = new MVElem(); 97 MVElem elem3 = new MVElem(); 98 doTestActionMap(elem1, elem2, elem3); 99 } 100 101 private void doTestActionMap(MultiViewElement elem1, MultiViewElement elem2, MultiViewElement elem3) { 102 MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1); 103 MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2); 104 MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3); 105 106 MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 }; 107 TopComponent tc = MultiViewFactory.createMultiView(descs, desc1); 108 tc.open(); 110 111 Action act = new TestAction("MultiViewAction"); 112 tc.getActionMap().put("testkey", act); 114 ActionMap obj = (ActionMap )tc.getLookup().lookup(ActionMap .class); 115 assertNotNull(obj); 116 assertEquals(obj.getClass(), MultiViewTopComponentLookup.LookupProxyActionMap.class); 117 Action res = (Action )obj.get("testkey"); 118 assertNotNull(res); 119 assertEquals("MultiViewAction", res.getValue(Action.NAME)); 120 tc.getActionMap().remove("testkey"); 122 obj = (ActionMap )tc.getLookup().lookup(ActionMap .class); 123 res = (Action )obj.get("testkey"); 124 assertNull(res); 125 126 JComponent elemtc = elem1.getVisualRepresentation(); 128 Action innerAct = new TestAction("InnerAction"); 129 elemtc.getActionMap().put("testkey", innerAct); 130 assertNotNull(elemtc.getActionMap().get("testkey")); 131 obj = (ActionMap )tc.getLookup().lookup(ActionMap .class); 132 assertNotNull(obj); 134 res = (Action )obj.get("testkey"); 135 assertNotNull(res); 136 tc.getActionMap().put("testkey", act); 138 assertNotNull(obj); 139 res = (Action )obj.get("testkey"); 140 assertNotNull(res); 141 assertEquals("MultiViewAction", res.getValue(Action.NAME)); 142 tc.getActionMap().remove("testkey"); 144 res = (Action )obj.get("testkey"); 145 assertNotNull(res); 146 assertEquals("InnerAction", res.getValue(Action.NAME)); 147 MultiViewHandler handler = MultiViews.findMultiViewHandler(tc); 149 Accessor.DEFAULT.createPerspective(desc2); 151 handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2)); 152 obj = (ActionMap )tc.getLookup().lookup(ActionMap .class); 153 res = (Action )obj.get("testkey"); 154 assertNull(res); } 156 157 public void testActionMapChanges() throws Exception { 158 MVElemTopComponent elem1 = new MVElemTopComponent(); 159 MVElemTopComponent elem2 = new MVElemTopComponent(); 160 MVElem elem3 = new MVElem(); 161 MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1); 162 MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2); 163 MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3); 164 165 MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 }; 166 TopComponent tc = MultiViewFactory.createMultiView(descs, desc1); 167 Lookup.Result result = tc.getLookup().lookup(new Lookup.Template(ActionMap .class)); 169 LookListener list = new LookListener(); 170 list.resetCount(); 171 result.addLookupListener(list); 172 result.allItems(); 173 174 tc.open(); 175 assertEquals(1, list.getCount()); 176 177 MultiViewHandler handler = MultiViews.findMultiViewHandler(tc); 178 Accessor.DEFAULT.createPerspective(desc2); 180 handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2)); 181 assertEquals(2, list.getCount()); 182 183 Accessor.DEFAULT.createPerspective(desc3); 184 handler.requestVisible(Accessor.DEFAULT.createPerspective(desc3)); 185 assertEquals(3, list.getCount()); 186 } 187 188 public void testSimplifiedActionMapChanges81117() { 189 MultiViewTopComponentLookup.InitialProxyLookup lookup = new MultiViewTopComponentLookup.InitialProxyLookup(new ActionMap ()); 190 Lookup.Result res = lookup.lookup(new Lookup.Template(ActionMap .class)); 191 LookListener list = new LookListener(); 192 list.resetCount(); 193 res.addLookupListener(list); 194 assertEquals(0, list.getCount()); 196 lookup.refreshLookup(); 197 assertEquals(1, list.getCount()); 198 200 MultiViewTopComponentLookup lookup2 = new MultiViewTopComponentLookup(new ActionMap ()); 201 res = lookup2.lookup(new Lookup.Template(ActionMap .class)); 202 list = new LookListener(); 203 list.resetCount(); 204 res.addLookupListener(list); 205 assertEquals(0, list.getCount()); 207 lookup2.setElementLookup(Lookups.fixed(new Object [] {new Object ()} )); 208 assertEquals(1, list.getCount()); 209 211 } 212 213 214 public void testActionMapChangesForElementsWithComponentShowingInit() throws Exception { 215 Action act1 = new TestAction("MultiViewAction1"); 216 Action act2 = new TestAction("MultiViewAction2"); 217 MVElemTopComponent elem1 = new ComponentShowingElement("testAction", act1); 218 MVElemTopComponent elem2 = new ComponentShowingElement("testAction", act2); 219 MVElem elem3 = new MVElem(); 220 MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1); 221 MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2); 222 MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3); 223 224 MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 }; 225 TopComponent tc = MultiViewFactory.createMultiView(descs, desc1); 226 Lookup.Result result = tc.getLookup().lookup(new Lookup.Template(ActionMap .class)); 227 LookListener2 list = new LookListener2(); 228 result.addLookupListener(list); 229 list.setCorrectValues("testAction", act1); 230 tc.open(); 232 assertEquals(1, list.getCount()); 233 MultiViewHandler handler = MultiViews.findMultiViewHandler(tc); 234 Accessor.DEFAULT.createPerspective(desc2); 236 list.setCorrectValues("testAction", act2); 237 handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2)); 238 assertEquals(2, list.getCount()); 239 Accessor.DEFAULT.createPerspective(desc3); 240 list.setCorrectValues("testAction", null); 241 handler.requestVisible(Accessor.DEFAULT.createPerspective(desc3)); 242 assertEquals(3, list.getCount()); 243 } 244 245 246 public class ComponentShowingElement extends MVElemTopComponent { 247 private String key; 248 private Action action; 249 250 public ComponentShowingElement(String actionkey, Action value) { 251 action = value; 252 key = actionkey; 253 } 254 255 public void componentShowing() { 256 super.componentShowing(); 257 getActionMap().put(key, action); 258 } 259 260 } 261 262 private class LookListener2 implements LookupListener { 263 private String key; 264 private Action action; 265 int count = 0; 266 private ActionMap lastMap; 267 268 public void setCorrectValues(String keyValue, Action actionValue) { 269 action = actionValue; 270 key = keyValue; 271 } 272 273 public int getCount() { 274 return count; 275 } 276 277 public void resultChanged (LookupEvent ev) { 278 Lookup.Result res = (Lookup.Result)ev.getSource(); 279 assertEquals(1, res.allInstances().size()); 280 ActionMap map = (ActionMap )res.allInstances().iterator().next(); 281 if (lastMap != null) { 282 assertNotSame(map, lastMap); 284 } 285 lastMap = map; 286 Action act = map.get(key); 287 assertEquals(action, act); 288 count++; 289 } 290 } 291 292 374 public void testActionsGlobalContext() throws Exception { 375 Lookup look = Utilities.actionsGlobalContext(); 376 MVElemTopComponent elem1 = new MVElemTopComponent(); 377 MVElemTopComponent elem2 = new MVElemTopComponent(); 378 MVElemTopComponent elem3 = new MVElemTopComponent(); 379 MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1); 380 MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2); 381 MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3); 382 MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 }; 383 TopComponent tc = MultiViewFactory.createMultiView(descs, desc1); 384 tc.open(); 386 tc.requestActive(); 387 388 ActionMap map = (ActionMap )look.lookup(ActionMap .class); 389 assertNotNull("is null", map); 390 assertEquals("is wrong class=" + map.getClass(), map.getClass(), MultiViewTopComponentLookup.LookupProxyActionMap.class); 391 Action res = map.get("testkey"); 392 assertNull(res); 393 Action act = new TestAction("MultiViewAction"); 394 elem1.getVisualRepresentation().getActionMap().put("testkey", act); 396 res = map.get("testkey"); 397 assertNotNull(res); 398 399 TopComponent tc2 = new TopComponent(); 401 tc2.open(); 402 tc2.requestActive(); 403 map = (ActionMap )look.lookup(ActionMap .class); 404 res = map.get("testkey"); 405 assertNull(res); 406 407 tc.requestActive(); 409 map = (ActionMap )look.lookup(ActionMap .class); 410 MultiViewHandler handler = MultiViews.findMultiViewHandler(tc); 411 Accessor.DEFAULT.createPerspective(desc2); 413 handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2)); 414 res = map.get("testkey"); 415 assertNull(res); 416 Accessor.DEFAULT.createPerspective(desc1); 418 handler.requestVisible(Accessor.DEFAULT.createPerspective(desc1)); 419 res = map.get("testkey"); 420 assertNotNull(res); 421 422 } 423 424 425 private class TestAction extends AbstractAction { 426 public TestAction(String name) { 427 super(name); 428 } 429 430 public void actionPerformed(ActionEvent event) { 431 432 } 433 434 } 435 436 private class LookListener implements LookupListener { 437 int count = 0; 438 439 public void resetCount() { 440 count = 0; 441 } 442 443 444 public int getCount() { 445 return count; 446 } 447 448 public void resultChanged (LookupEvent ev) { 449 count++; 450 } 451 } 452 453 } 454 455 | Popular Tags |