001    /*
002     * Created on Jul 17, 2008
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2008-2010 the original author or authors.
015     */
016    package org.fest.swing.driver;
017    
018    import static org.fest.swing.format.Formatting.format;
019    import static org.fest.swing.query.ComponentShowingQuery.isShowing;
020    import static org.fest.util.Strings.concat;
021    
022    import java.awt.Component;
023    
024    import org.fest.swing.timing.Condition;
025    
026    /**
027     * Understands a condition that verifies that a <code>{@link Component}</code> is showing on the screen.
028     *
029     * @author Yvonne Wang
030     */
031    public class WaitForComponentToShowCondition extends Condition {
032    
033      private Component c;
034    
035      /**
036       * Creates a new </code>{@link WaitForComponentToShowCondition}</code>.
037       * @param c the <code>Component</code> to verify.
038       * @return the created condition.
039       * @throws NullPointerException if the <code>Component</code> is <code>null</code>.
040       */
041      public static WaitForComponentToShowCondition untilIsShowing(Component c) {
042        return new WaitForComponentToShowCondition(c);
043      }
044    
045      private WaitForComponentToShowCondition(Component c) {
046        super(concat("Component ", format(c), " to show on the screen"));
047        if (c == null) throw new NullPointerException("The component to verify should not be null");
048        this.c = c;
049      }
050    
051      /**
052       * Indicates whether the <code>{@link Component}</code> in this condition is showing on the screen or not.
053       * @return <code>true</code> if the <code>Component</code> in this condition is showing on the screen,
054       * <code>false</code> otherwise
055       */
056      public boolean test() {
057        return isShowing(c);
058      }
059    
060      /** ${@inheritDoc} */
061      @Override protected void done() {
062        c = null;
063      }
064    }