001    /*
002     * Created on Jul 18, 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.fixture;
016    
017    import java.awt.Component;
018    
019    import org.fest.swing.core.*;
020    import org.fest.swing.driver.ComponentDriver;
021    import org.fest.swing.timing.Timeout;
022    
023    /**
024     * A generic component fixture providing basic keyboard and mouse input operations. Useful as a base class for
025     * specialized fixtures providing higher level input operations for custom components.
026     * <p>
027     * Example:
028     *
029     * <pre>
030     * public class MyWidget extends JComponent {
031     *  ...
032     *  public void paintComponent(Graphics g) {
033     *     ...
034     *  }
035     *  ...
036     * }
037     *
038     * public class MyWidgetFixture extends GenericComponentFixture&lt;MyWidget&gt; {
039     *   public MyWidgetFixture(Robot robot, MyWidget target) {
040     *     super(robot, target);
041     *   }
042     *
043     *   public void clickAndDrag(Point start, Point end) {
044     *     robot.pressMouse(target, start);
045     *     robot.moveMouse(target, end);
046     *     robot.releaseAllMouseButtons();
047     *   }
048     * }
049     *
050     * </pre>
051     *
052     * </p>
053     * @param <T> the type of <code>Component</code> that this fixture can manage.
054     *
055     * @author <a href="mailto:simeon.fitch@mseedsoft.com">Simeon H.K. Fitch</a>
056     */
057    public abstract class GenericComponentFixture<T extends Component> extends ComponentFixture<T> implements
058        CommonComponentFixture {
059    
060      /** Delegate for constructing and passing input operations to Robot. */
061      private ComponentDriver driver;
062    
063      /**
064       * Creates a new <code>{@link GenericComponentFixture}</code>.
065       * @param robot performs simulation of user events on the given target component.
066       * @param target the target <code>Component</code> to be managed by this fixture.
067       * @throws NullPointerException if <code>robot</code> is <code>null</code>.
068       * @throws NullPointerException if <code>target</code> is <code>null</code>.
069       */
070      public GenericComponentFixture(Robot robot, T target) {
071        this(robot, new ComponentDriver(robot), target);
072      }
073    
074      /**
075       * Creates a new <code>{@link GenericComponentFixture}</code> using a provided driver.
076       * @param robot performs simulation of user events on the given target component.
077       * @param driver provided driver to handle input requests.
078       * @param target the target <code>Component</code> to be managed by this fixture.
079       * @throws NullPointerException if <code>robot</code> is <code>null</code>.
080       * @throws NullPointerException if <code>driver</code> is <code>null</code>.
081       * @throws NullPointerException if <code>target</code> is <code>null</code>.
082       */
083      public GenericComponentFixture(Robot robot, ComponentDriver driver, T target) {
084        super(robot, target);
085        driver(driver);
086      }
087    
088      /**
089       * Sets the <code>{@link ComponentDriver}</code> to be used by this fixture.
090       * @param newDriver the new <code>ComponentDriver</code>.
091       * @throws NullPointerException if the given driver is <code>null</code>.
092       */
093      protected final void driver(ComponentDriver newDriver) {
094        validateNotNull(newDriver);
095        this.driver = newDriver;
096      }
097    
098      /**
099       * Returns the <code>{@link ComponentDriver}</code> used by this fixture.
100       * @return the driver used by this fixture.
101       */
102      protected final ComponentDriver driver() {
103        return driver;
104      }
105    
106      /** {@inheritDoc} */
107      public GenericComponentFixture<T> click() {
108        driver.click(target);
109        return this;
110      }
111    
112      /** {@inheritDoc} */
113      public GenericComponentFixture<T> click(MouseButton button) {
114        driver.click(target, button);
115        return this;
116      }
117    
118      /** {@inheritDoc} */
119      public GenericComponentFixture<T> click(MouseClickInfo mouseClickInfo) {
120        driver.click(target, mouseClickInfo);
121        return this;
122      }
123    
124      /** {@inheritDoc} */
125      public GenericComponentFixture<T> doubleClick() {
126        driver.doubleClick(target);
127        return this;
128      }
129    
130    
131      /** {@inheritDoc} */
132      public GenericComponentFixture<T> rightClick() {
133        driver.rightClick(target);
134        return this;
135      }
136    
137      /** {@inheritDoc} */
138      public GenericComponentFixture<T> focus() {
139        driver.focus(target);
140        return this;
141      }
142    
143      /** {@inheritDoc} */
144      public GenericComponentFixture<T> pressAndReleaseKey(KeyPressInfo keyPressInfo) {
145        driver.pressAndReleaseKey(target, keyPressInfo);
146        return this;
147      }
148    
149      /** {@inheritDoc} */
150      public GenericComponentFixture<T> pressAndReleaseKeys(int... keyCodes) {
151        driver.pressAndReleaseKeys(target, keyCodes);
152        return this;
153      }
154    
155      /** {@inheritDoc} */
156      public GenericComponentFixture<T> pressKey(int keyCode) {
157        driver.pressKey(target, keyCode);
158        return this;
159      }
160    
161      /** {@inheritDoc} */
162      public GenericComponentFixture<T> releaseKey(int keyCode) {
163        driver.releaseKey(target, keyCode);
164        return this;
165      }
166    
167      /** {@inheritDoc} */
168      public GenericComponentFixture<T> requireEnabled() {
169        driver.requireEnabled(target);
170        return this;
171      }
172    
173      /** {@inheritDoc} */
174      public GenericComponentFixture<T> requireEnabled(Timeout timeout) {
175        driver.requireEnabled(target, timeout);
176        return this;
177      }
178    
179      /** {@inheritDoc} */
180      public GenericComponentFixture<T> requireDisabled() {
181        driver.requireDisabled(target);
182        return this;
183      }
184    
185      /** {@inheritDoc} */
186      public GenericComponentFixture<T> requireVisible() {
187        driver.requireVisible(target);
188        return this;
189      }
190    
191      /** {@inheritDoc} */
192      public GenericComponentFixture<T> requireNotVisible() {
193        driver.requireNotVisible(target);
194        return this;
195      }
196    
197      /** {@inheritDoc} */
198      public GenericComponentFixture<T> requireFocused() {
199        driver.requireFocused(target);
200        return this;
201      }
202    }