001    /*
002     * Created on Mar 7, 2010
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 @2010 the original author or authors.
015     */
016    package org.fest.swing.core;
017    
018    import static org.fest.swing.edt.GuiActionRunner.execute;
019    
020    import org.fest.assertions.Description;
021    import org.fest.swing.edt.GuiQuery;
022    import org.fest.swing.timing.Condition;
023    
024    /**
025     * Understands a <code>{@link Condition}</code> that is evaluated in the event dispatch thread (EDT.)
026     *
027     * @author Alex Ruiz
028     *
029     * @since 1.2
030     */
031    public abstract class EdtSafeCondition extends Condition {
032    
033      /**
034       * Creates a new </code>{@link EdtSafeCondition}</code>.
035       * @param description describes this condition.
036       */
037      public EdtSafeCondition(String description) {
038        super(description);
039      }
040    
041      /**
042       * Creates a new </code>{@link EdtSafeCondition}</code>.
043       * @param description describes this condition.
044       */
045      public EdtSafeCondition(Description description) {
046        super(description);
047      }
048    
049      /**
050       * Checks if the condition has been satisfied.
051       * @return <code>true</code> if the condition has been satisfied, otherwise <code>false</code>.
052       */
053      public final boolean test() {
054        boolean result = execute(new GuiQuery<Boolean>() {
055          protected Boolean executeInEDT() {
056            return testInEDT();
057          }
058        });
059        return result;
060      }
061    
062      /**
063       * Checks if the condition has been satisfied. This method is guaranteed to be executed in the event dispatch thread.
064       * @return <code>true</code> if the condition has been satisfied, otherwise <code>false</code>.
065       */
066      protected abstract boolean testInEDT();
067    }