001    /*
002     * Created on Apr 28, 2009
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 @2009-2010 the original author or authors.
015     */
016    package org.fest.swing.fixture;
017    
018    import static org.fest.swing.edt.GuiActionRunner.execute;
019    
020    import java.awt.Container;
021    
022    import javax.swing.JFrame;
023    
024    import org.fest.swing.annotation.RunsInEDT;
025    import org.fest.swing.core.Robot;
026    import org.fest.swing.edt.GuiQuery;
027    
028    /**
029     * Understands utility methods related to <code>{@link Container}</code>s.
030     * @since 1.2
031     *
032     * @author Alex Ruiz
033     */
034    public final class Containers {
035    
036      /** Name of the <code>JFrame</code>s created by this class. */
037      public static final String CREATED_FRAME_NAME = "org.fest.swing.CreatedFrameForContainer";
038    
039      /**
040       * Creates a new <code>{@link JFrame}</code> and uses the given <code>{@link Container}</code> as its content pane.
041       * The created <code>JFrame</code> is wrapped and displayed by a <code>{@link FrameFixture}</code>.
042       * <p>
043       * <strong>Note:</strong>This method creates a new <code>{@link Robot}</code>. When using this method, please do not
044       * create any additional instances of <code>Robot</code>. Only one instance of <code>Robot</code> can exist per
045       * test class.
046       * </p>
047       * @param contentPane the <code>Container</code> to use as content pane for the <code>JFrame</code> to create.
048       * @return the created <code>FrameFixture</code>.
049       * @see #frameFor(Container)
050       */
051      @RunsInEDT
052      public static FrameFixture showInFrame(Container contentPane) {
053        FrameFixture frameFixture = frameFixtureFor(contentPane);
054        frameFixture.show();
055        return frameFixture;
056      }
057    
058      /**
059       * Creates a new <code>{@link JFrame}</code> and uses the given <code>{@link Container}</code> as its content pane.
060       * The created <code>JFrame</code> is wrapped by a <code>{@link FrameFixture}</code>. Unlike
061       * <code>{@link #showInFrame(Container)}</code>, this method does <strong>not</strong> display the created
062       * <code>JFrame</code>.
063       * <p>
064       * <strong>Note:</strong>This method creates a new <code>{@link Robot}</code>. When using this method, please do not
065       * create any additional instances of <code>Robot</code>. Only one instance of <code>Robot</code> can exist per
066       * test class.
067       * </p>
068       * @param contentPane the <code>Container</code> to use as content pane for the <code>JFrame</code> to create.
069       * @return the created <code>FrameFixture</code>.
070       * @see #frameFor(Container)
071       */
072      @RunsInEDT
073      public static FrameFixture frameFixtureFor(Container contentPane) {
074        return new FrameFixture(frameFor(contentPane));
075      }
076    
077      /**
078       * Creates a new <code>{@link JFrame}</code> and uses the given <code>{@link Container}</code> as its content pane.
079       * The created <code>JFrame</code> has the name specified by <code>{@link #CREATED_FRAME_NAME}</code>. This method
080       * is executed in the Event Dispatch Thread (EDT.)
081       * @param contentPane the <code>Container</code> to use as content pane for the <code>JFrame</code> to create.
082       * @return the created <code>JFrame</code>.
083       */
084      @RunsInEDT
085      public static JFrame frameFor(final Container contentPane) {
086        return execute(new GuiQuery<JFrame>() {
087          protected JFrame executeInEDT() throws Throwable {
088            JFrame frame = new JFrame("Created by FEST");
089            frame.setName(CREATED_FRAME_NAME);
090            frame.setContentPane(contentPane);
091            return frame;
092          }
093        });
094      }
095    
096      private Containers() {}
097    }