Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software distributed under the License
   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
   * or implied. See the License for the specific language governing permissions and limitations under
  * the License.
  * 
  * Copyright @2008-2010 the original author or authors.
  */
 
 package org.assertj.swing.conditions;
 
 
A Condition that requires the focus to be inside the component.

Author(s):
Peter Murray
 
 public class FocusInContainerCondition extends Condition {
 
   private static final Logger LOGGER = LoggerFactory.getLogger("fest.conditions");
 
   private final ComponentFinder _finder;
 
   private final ComponentMatcher _matcher;

  
Creates a new FocusInContainerCondition

Parameters:
name The name of the component to find.
type The type of the component to find.
 
   public FocusInContainerCondition(String nameClass<? extends Componenttype) {
 	this(BasicComponentFinder.finderWithCurrentAwtHierarchy(),
 	     new NameMatcher(nametypetrue));
   }

  
Creates a new FocusInContainerCondition

Parameters:
name The name of the component to find.
 
   public FocusInContainerCondition(String name) {
 	this(BasicComponentFinder.finderWithCurrentAwtHierarchy(),
 	     new NameMatcher(nametrue));
   }

  
Creates a new FocusInContainerCondition

Parameters:
finder performs the component search.
matcher specifies the condition that the component we are looking for needs to match.
 
 	                               ComponentMatcher matcher) {
 	this("Container contains component with focus"findermatcher);
   }

  
Creates a new FocusInContainerCondition

Parameters:
description the description of this condition.
finder performs the component search.
matcher specifies the condition that the component we are looking for needs to match.
 
   public FocusInContainerCondition(String description,
 	                               ComponentFinder finder,
 	                               ComponentMatcher matcher) {
 	super(description);
 	 = finder;
 	 = matcher;
   }

  
Returns true if the focus is in either the container that we are matching on, or one of its children contained inside it.

Returns:
true if the focus is in the component or contained within it, false otherwise.
 
   @Override
   public boolean test() {
	try {
	  Component found = .find();
	  debug("testing focus inside component :: {}"found);
	  Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager()
		                                         .getFocusOwner();
	  debug("\tActual focus owner: {}"focusOwner);
	  if (found == focusOwner) {
		debug("Focus is on the actual component :: returning true");
		return true;
	  } else if (found instanceof Container) {
		Container container = (Containerfound;
		boolean containsFocus = container.isAncestorOf(focusOwner);
		debug("Component is a Container, does it contain the focus? {}"containsFocus);
		return containsFocus;
	  }
catch (ComponentLookupException e) {
	  debug("Component was not found {}"e);
	}
	debug("Focus is not within the container being tested");
	return false;
  }
  private void debug(String messageObject... args) {
	  .debug(messageargs);
	}
  }
New to GrepCode? Check out our FAQ X