001    /*
002     * Created on Oct 23, 2008
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * 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 is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2008-2010 the original author or authors.
014     */
015    package org.fest.swing.driver;
016    
017    import static org.fest.swing.format.Formatting.format;
018    import static org.fest.util.Strings.concat;
019    
020    import java.awt.Component;
021    
022    import org.fest.swing.annotation.RunsInCurrentThread;
023    
024    /**
025     * Understands validation of the state of a <code>{@link Component}</code>.
026     *
027     * @author Alex Ruiz
028     */
029    public final class ComponentStateValidator {
030    
031      /**
032       * Asserts that the <code>{@link Component}</code> is enabled and showing.
033       * <p>
034       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
035       * responsible for calling this method from the EDT.
036       * </p>
037       * @param c the target component.
038       * @throws IllegalStateException if the <code>Component</code> is disabled.
039       * @throws IllegalStateException if the <code>Component</code> is not showing on the screen.
040       */
041      @RunsInCurrentThread
042      public static void validateIsEnabledAndShowing(Component c) {
043        validateIsEnabled(c);
044        validateIsShowing(c);
045      }
046    
047      /**
048       * Asserts that the <code>{@link Component}</code> is enabled.
049       * <p>
050       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
051       * responsible for calling this method from the EDT.
052       * </p>
053       * @param c the target component.
054       * @throws IllegalStateException if the <code>Component</code> is disabled.
055       */
056      @RunsInCurrentThread
057      public static void validateIsEnabled(Component c) {
058        if (!c.isEnabled()) throw new IllegalStateException(concat("Expecting component ", format(c), " to be enabled"));
059      }
060    
061      /**
062       * Asserts that the <code>{@link Component}</code> is showing on the screen.
063       * <p>
064       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
065       * responsible for calling this method from the EDT.
066       * </p>
067       * @param c the target component.
068       * @throws IllegalStateException if the <code>Component</code> is not showing on the screen.
069       */
070      @RunsInCurrentThread
071      public static void validateIsShowing(Component c) {
072        if (!c.isShowing()) throw componentNotShowingOnScreenFailure(c);
073      }
074    
075      /**
076       * Throws a <code>{@link IllegalStateException}</code> when a <code>{@link Component}</code> is not showing on the
077       * screen.
078       * <p>
079       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
080       * responsible for calling this method from the EDT.
081       * </p>
082       * @param c the target component.
083       * @return the thrown exception.
084       */
085      @RunsInCurrentThread
086      public static IllegalStateException componentNotShowingOnScreenFailure(Component c) {
087        return new IllegalStateException(concat("Expecting component ", format(c), " to be showing on the screen"));
088      }
089    
090      private ComponentStateValidator() {}
091    }