/** * Provides a {@link SelectionSupport} instance for object selections. * * @param type The type of selected objects whose selection state to observe. * @return A selection support instance for the given object type, or {@code null}. */ public <T> SelectionSupport<T> getSelectionSupport(Class<T> type) { @SuppressWarnings("unchecked") DefaultSelectionSupport<T> selectionChangeSupport = (DefaultSelectionSupport<T>) selectionChangeSupports.get(type); if (selectionChangeSupport == null) { selectionChangeSupport = new DefaultSelectionSupport<>(type); selectionChangeSupports.put(type, selectionChangeSupport); } return selectionChangeSupport; }
public DefaultSelectionSupport(Class<T> type) { this.type = type; currentlySelectedItems = Collections.emptyList(); itemResult = Utilities.actionsGlobalContext().lookupResult(type); handlerList = new LinkedList<>(); lookupListener = createLookupListener(); }
@Test public void testSingleSelection() throws Exception { Assume.assumeFalse(GraphicsEnvironment.isHeadless()); ContextGlobalExtender globalExtender = Utilities.actionsGlobalContext().lookup(ContextGlobalExtender.class); DefaultSelectionSupport<ProductSceneView> selectionChangeSupport = new DefaultSelectionSupport<>(ProductSceneView.class); MySelectionChangeHandler changeListener = new MySelectionChangeHandler(); selectionChangeSupport.addHandler(changeListener); ProductSceneView sceneView1 = Mockito.mock(ProductSceneView.class); globalExtender.put("view", sceneView1); assertEquals(1, changeListener.count); globalExtender.remove("view"); assertEquals(0, changeListener.count); }
@Test public void testMultiSelection() throws Exception { Assume.assumeFalse(GraphicsEnvironment.isHeadless()); ContextGlobalExtender globalExtender = Utilities.actionsGlobalContext().lookup(ContextGlobalExtender.class); DefaultSelectionSupport<ProductSceneView> selectionChangeSupport = new DefaultSelectionSupport<>(ProductSceneView.class); MySelectionChangeHandler changeListener = new MySelectionChangeHandler(); selectionChangeSupport.addHandler(changeListener); ProductSceneView sceneView1 = Mockito.mock(ProductSceneView.class); ProductSceneView sceneView2 = Mockito.mock(ProductSceneView.class); ProductSceneView sceneView3 = Mockito.mock(ProductSceneView.class); globalExtender.put("view1", sceneView1); assertEquals(1, changeListener.count); globalExtender.put("view2", sceneView2); assertEquals(2, changeListener.count); globalExtender.put("view3", sceneView3); assertEquals(3, changeListener.count); globalExtender.remove("view2"); assertEquals(2, changeListener.count); }