@Test public void testSetAction() { WLink link = new WLink(); Action action = new TestAction(); AjaxTarget target1 = new WPanel(); AjaxTarget target2 = new WPanel(); Assert.assertNull("Action should be null by default", link.getAction()); Assert.assertNull("Action targets should be null by default", link.getActionTargets()); link.setAction(action); Assert.assertEquals("Incorrect action returned", action, link.getAction()); Assert.assertNotNull("Action targets should not be null", link.getActionTargets()); Assert.assertEquals("Action targets should be empty", 0, link.getActionTargets().length); link.setAction(action, target1, target2); Assert.assertEquals("Incorrect action returned", action, link.getAction()); Assert.assertNotNull("Action targets should not be null", link.getActionTargets()); Assert.assertEquals("Incorrect action targets returned", 2, link.getActionTargets().length); Assert.assertEquals("Incorrect action target returned for target1", target1, link. getActionTargets()[0]); Assert.assertEquals("Incorrect action target returned for target2", target2, link. getActionTargets()[1]); }
/** * Override preparePaintComponent to register an AJAX operation if this link has an action. * * @param request the request being responded to. */ @Override protected void preparePaintComponent(final Request request) { super.preparePaintComponent(request); UIContext uic = UIContextHolder.getCurrent(); // If the link has an action, register it for AJAX final Action action = getAction(); final AjaxTarget[] actionTargets = getActionTargets(); if (action != null && uic.getUI() != null) { // Register AJAX operation if AJAX targets are set, otherwise rely on InternalAjax request if (actionTargets != null && actionTargets.length > 0) { List<String> targetIds = new ArrayList<>(); for (AjaxTarget target : actionTargets) { targetIds.add(target.getId()); } // Register the action targets AjaxHelper.registerComponents(targetIds, getId()); } } }
AjaxTarget[] actionTargets = link.getActionTargets();