@Test public void test2() { FocusManager focusManager = new FocusManagerImpl(); String componentId = "someComponentId"; String clientId = "someClientId"; when(component.getClientId(facesContext)).thenReturn(clientId); when(viewRoot.findComponent(componentId)).thenReturn(component); focusManager.focus(componentId); assertEquals(clientId, attributes.get(FocusManager.FOCUS_CONTEXT_ATTRIBUTE)); assertTrue(attributes.containsKey(FocusManager.FOCUS_CONTEXT_ATTRIBUTE)); assertTrue(FocusRendererUtils.isFocusEnforced(facesContext)); verify(javaScriptService).addPageReadyScript(Mockito.any(FacesContext.class), Mockito.any(JSLiteral.class)); }
@Override public void focus(String componentId) { FacesContext context = FacesContext.getCurrentInstance(); if (context == null) { throw new IllegalStateException(FocusManager.class.getSimpleName() + " can't be used without FacesContext available"); } if (componentId == null) { setContextAttribute(context, null); } else { UIComponent currentComponent = UIComponent.getCurrentComponent(context); if (currentComponent == null) { currentComponent = context.getViewRoot(); } Pattern rowPattern = Pattern.compile(ROW_PATTERN); Matcher rowMatcher = rowPattern.matcher(componentId); String newId = componentId; if (rowMatcher.matches()) { newId = rowMatcher.group(2); newId = newId.replaceAll(ROW_ID, String.valueOf(SeparatorChar.SEPARATOR_CHAR)); } UIComponent component = RendererUtils.getInstance().findComponentFor(currentComponent, newId); if (component == null) { logNotFound(context, componentId); } else { String clientId = component.getClientId(context); if (rowMatcher.matches()) { int end = clientId.lastIndexOf(SeparatorChar.SEPARATOR_CHAR + newId); if (end != -1) { clientId = clientId.substring(0, end) + componentId; } else { logNotFound(context, componentId); return; } } setContextAttribute(context, clientId); JavaScriptService javaScriptService = ServiceTracker.getService(context, JavaScriptService.class); javaScriptService.addPageReadyScript(context, new JSLiteral(String.format(SCRIPT, clientId))); } } }
FocusManagerImpl implements FocusManager { @Override public void focus(String componentId) { FacesContext context = FacesContext.getCurrentInstance(); if (context == null) { throw new IllegalStateException(FocusManager.class.getSimpleName() + " can't be used without FacesContext available"); } if (componentId == null) { setContextAttribute(context, null); } else { UIComponent currentComponent = UIComponent.getCurrentComponent(context); if (currentComponent == null) { currentComponent = context.getViewRoot(); } Pattern rowPattern = Pattern.compile(ROW_PATTERN); Matcher rowMatcher = rowPattern.matcher(componentId); String newId = componentId; if (rowMatcher.matches()) { newId = rowMatcher.group(2); newId = newId.replaceAll(ROW_ID, String.valueOf(SeparatorChar.SEPARATOR_CHAR)); } UIComponent component = RendererUtils.getInstance().findComponentFor(currentComponent, newId); if (component == null) { logNotFound(context, componentId); } else { String clientId = component.getClientId(context); if (rowMatcher.matches()) { int end = clientId.lastIndexOf(SeparatorChar.SEPARATOR_CHAR + newId); if (end != -1) { clientId = clientId.substring(0, end) + componentId; } else { logNotFound(context, componentId); return; } } setContextAttribute(context, clientId); JavaScriptService javaScriptService = ServiceTracker.getService(context, JavaScriptService.class); javaScriptService.addPageReadyScript(context, new JSLiteral(String.format(SCRIPT, clientId))); } } } }
FocusManagerImpl implements FocusManager { @Override public void focus(String componentId) { FacesContext context = FacesContext.getCurrentInstance(); if (context == null) { throw new IllegalStateException(FocusManager.class.getSimpleName() + " can't be used without FacesContext available"); } if (componentId == null) { setContextAttribute(context, null); } else { UIComponent currentComponent = UIComponent.getCurrentComponent(context); if (currentComponent == null) { currentComponent = context.getViewRoot(); } Pattern rowPattern = Pattern.compile(ROW_PATTERN); Matcher rowMatcher = rowPattern.matcher(componentId); String newId = componentId; if (rowMatcher.matches()) { newId = rowMatcher.group(2); newId = newId.replaceAll(ROW_ID, String.valueOf(SeparatorChar.SEPARATOR_CHAR)); } UIComponent component = RendererUtils.getInstance().findComponentFor(currentComponent, newId); if (component == null) { logNotFound(context, componentId); } else { String clientId = component.getClientId(context); if (rowMatcher.matches()) { int end = clientId.lastIndexOf(SeparatorChar.SEPARATOR_CHAR + newId); if (end != -1) { clientId = clientId.substring(0, end) + componentId; } else { logNotFound(context, componentId); return; } } setContextAttribute(context, clientId); JavaScriptService javaScriptService = ServiceTracker.getService(context, JavaScriptService.class); javaScriptService.addPageReadyScript(context, new JSLiteral(String.format(SCRIPT, clientId))); } } }  }
FocusManagerImpl implements FocusManager { @Override public void focus(String componentId) { FacesContext context = FacesContext.getCurrentInstance(); if (context == null) { throw new IllegalStateException(FocusManager.class.getSimpleName() + " can't be used without FacesContext available"); } if (componentId == null) { setContextAttribute(context, null); } else { UIComponent currentComponent = UIComponent.getCurrentComponent(context); if (currentComponent == null) { currentComponent = context.getViewRoot(); } Pattern rowPattern = Pattern.compile(ROW_PATTERN); Matcher rowMatcher = rowPattern.matcher(componentId); String newId = componentId; if (rowMatcher.matches()) { newId = rowMatcher.group(2); newId = newId.replaceAll(ROW_ID, String.valueOf(SeparatorChar.SEPARATOR_CHAR)); } UIComponent component = RendererUtils.getInstance().findComponentFor(currentComponent, newId); if (component == null) { logNotFound(context, componentId); } else { String clientId = component.getClientId(context); if (rowMatcher.matches()) { int end = clientId.lastIndexOf(SeparatorChar.SEPARATOR_CHAR + newId); if (end != -1) { clientId = clientId.substring(0, end) + componentId; } else { logNotFound(context, componentId); return; } } setContextAttribute(context, clientId); JavaScriptService javaScriptService = ServiceTracker.getService(context, JavaScriptService.class); javaScriptService.addPageReadyScript(context, new JSLiteral(String.format(SCRIPT, clientId))); } } }  @Override void focus(String componentId); }
FocusManagerImpl implements FocusManager { @Override public void focus(String componentId) { FacesContext context = FacesContext.getCurrentInstance(); if (context == null) { throw new IllegalStateException(FocusManager.class.getSimpleName() + " can't be used without FacesContext available"); } if (componentId == null) { setContextAttribute(context, null); } else { UIComponent currentComponent = UIComponent.getCurrentComponent(context); if (currentComponent == null) { currentComponent = context.getViewRoot(); } Pattern rowPattern = Pattern.compile(ROW_PATTERN); Matcher rowMatcher = rowPattern.matcher(componentId); String newId = componentId; if (rowMatcher.matches()) { newId = rowMatcher.group(2); newId = newId.replaceAll(ROW_ID, String.valueOf(SeparatorChar.SEPARATOR_CHAR)); } UIComponent component = RendererUtils.getInstance().findComponentFor(currentComponent, newId); if (component == null) { logNotFound(context, componentId); } else { String clientId = component.getClientId(context); if (rowMatcher.matches()) { int end = clientId.lastIndexOf(SeparatorChar.SEPARATOR_CHAR + newId); if (end != -1) { clientId = clientId.substring(0, end) + componentId; } else { logNotFound(context, componentId); return; } } setContextAttribute(context, clientId); JavaScriptService javaScriptService = ServiceTracker.getService(context, JavaScriptService.class); javaScriptService.addPageReadyScript(context, new JSLiteral(String.format(SCRIPT, clientId))); } } }  @Override void focus(String componentId);  }