001    /*
002     * Created on Mar 16, 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.fixture;
017    
018    import java.util.regex.Pattern;
019    
020    import javax.swing.JPopupMenu;
021    import javax.swing.table.JTableHeader;
022    
023    import org.fest.swing.core.MouseClickInfo;
024    import org.fest.swing.core.Robot;
025    import org.fest.swing.driver.JTableHeaderDriver;
026    import org.fest.swing.exception.ComponentLookupException;
027    import org.fest.swing.exception.LocationUnavailableException;
028    
029    /**
030     * Understands functional testing of <code>{@link JTableHeader}</code>s:
031     * <ul>
032     * <li>user input simulation</li>
033     * <li>state verification</li>
034     * <li>property value query</li>
035     * </ul>
036     *
037     * @author Yvonne Wang
038     * @author Alex Ruiz
039     */
040    public class JTableHeaderFixture extends ComponentFixture<JTableHeader> implements JComponentFixture {
041    
042      private JTableHeaderDriver driver;
043    
044      /**
045       * Creates a new </code>{@link JTableHeaderFixture}</code>.
046       * @param robot performs simulation of user events on the given <code>JTableHeader</code>.
047       * @param target the <code>JTableHeader</code> to be managed by this fixture.
048       * @throws NullPointerException if <code>robot</code> is <code>null</code>.
049       * @throws NullPointerException if <code>target</code> is <code>null</code>.
050       */
051      public JTableHeaderFixture(Robot robot, JTableHeader target) {
052        super(robot, target);
053        driver(new JTableHeaderDriver(robot));
054      }
055    
056      /**
057       * Sets the <code>{@link JTableHeaderDriver}</code> to be used by this fixture.
058       * @param newDriver the new <code>JTableHeaderDriver</code>.
059       * @throws NullPointerException if the given driver is <code>null</code>.
060       */
061      protected final void driver(JTableHeaderDriver newDriver) {
062        validateNotNull(newDriver);
063        driver = newDriver;
064      }
065    
066      /**
067       * Simulates a user clicking the column under the given index, in this fixture's <code>{@link JTableHeader}</code>.
068       * @param index the index of the column to click.
069       * @return this fixture.
070       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
071       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
072       * @throws IndexOutOfBoundsException if the index is out of bounds.
073       */
074      public JTableHeaderFixture clickColumn(int index) {
075        driver.clickColumn(target, index);
076        return this;
077      }
078    
079      /**
080       * Shows a pop-up menu using this fixture's <code>{@link JTableHeader}</code> as the invoker of the pop-up menu.
081       * @param columnIndex the index of the column where the pop-up menu will be displayed.
082       * @return a fixture that manages the displayed pop-up menu.
083       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
084       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
085       * @throws IndexOutOfBoundsException if the index is out of bounds.
086       * @throws ComponentLookupException if a pop-up menu cannot be found.
087       */
088      public JPopupMenuFixture showPopupMenuAt(int columnIndex) {
089        JPopupMenu popupMenu = driver.showPopupMenu(target, columnIndex);
090        return new JPopupMenuFixture(robot, popupMenu);
091      }
092    
093      /**
094       * Shows a pop-up menu using this fixture's <code>{@link JTableHeader}</code> as the invoker of the pop-up menu.
095       * @param columnName the name of the column where the pop-up menu will be displayed. It can be a regular expression.
096       * @return a fixture that manages the displayed pop-up menu.
097       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
098       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
099       * @throws ComponentLookupException if a pop-up menu cannot be found.
100       */
101      public JPopupMenuFixture showPopupMenuAt(String columnName) {
102        JPopupMenu popupMenu = driver.showPopupMenu(target, columnName);
103        return new JPopupMenuFixture(robot, popupMenu);
104      }
105    
106      /**
107       * Shows a pop-up menu using this fixture's <code>{@link JTableHeader}</code> as the invoker of the pop-up menu. The
108       * name of the column to use must match the given regular expression pattern.
109       * @param columnNamePattern the regular expression pattern to match.
110       * @return a fixture that manages the displayed pop-up menu.
111       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
112       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
113       * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
114       * @throws ComponentLookupException if a pop-up menu cannot be found.
115       * @since 1.2
116       */
117      public JPopupMenuFixture showPopupMenuAt(Pattern columnNamePattern) {
118        JPopupMenu popupMenu = driver.showPopupMenu(target, columnNamePattern);
119        return new JPopupMenuFixture(robot, popupMenu);
120      }
121    
122      /**
123       * Simulates a user clicking the column under the given index, in this fixture's <code>{@link JTableHeader}</code>,
124       * using the given mouse button, the given number of times.
125       * @param index the index of the column to click.
126       * @param mouseClickInfo specifies the mouse button to use and the number of times to click.
127       * @return this fixture.
128       * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
129       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
130       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
131       * @throws IndexOutOfBoundsException if the index is out of bounds.
132       */
133      public JTableHeaderFixture clickColumn(int index, MouseClickInfo mouseClickInfo) {
134        validateNotNull(mouseClickInfo);
135        driver.clickColumn(target, index, mouseClickInfo.button(), mouseClickInfo.times());
136        return this;
137      }
138    
139      /**
140       * Simulates a user clicking the column which name matches the given value, in this fixture's
141       * <code>{@link JTableHeader}</code>.
142       * @param columnName the column name to match. It can be a regular expression.
143       * @return this fixture.
144       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
145       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
146       * @throws LocationUnavailableException if a column with a matching name cannot be found.
147       */
148      public JTableHeaderFixture clickColumn(String columnName) {
149        driver.clickColumn(target, columnName);
150        return this;
151      }
152    
153      /**
154       * Simulates a user clicking the column which name matches the given regular expression pattern, in this fixture's
155       * <code>{@link JTableHeader}</code>.
156       * @param columnNamePattern the regular expression pattern to match.
157       * @return this fixture.
158       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
159       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
160       * @throws NullPointerException if the given regular expression is <code>null</code>.
161       * @throws LocationUnavailableException if a column with a matching name cannot be found.
162       * @since 1.2
163       */
164      public JTableHeaderFixture clickColumn(Pattern columnNamePattern) {
165        driver.clickColumn(target, columnNamePattern);
166        return this;
167      }
168    
169      /**
170       * Simulates a user clicking the column which name matches the given one, in this fixture's
171       * <code>{@link JTableHeader}</code>, using the given mouse button, the given number of times.
172       * @param columnName the column name to match. It can be a regular expression.
173       * @param mouseClickInfo specifies the mouse button to use and the number of times to click.
174       * @return this fixture.
175       * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
176       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
177       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
178       * @throws LocationUnavailableException if a column with a matching name cannot be found.
179       */
180      public JTableHeaderFixture clickColumn(String columnName, MouseClickInfo mouseClickInfo) {
181        validateNotNull(mouseClickInfo);
182        driver.clickColumn(target, columnName, mouseClickInfo.button(), mouseClickInfo.times());
183        return this;
184      }
185    
186      /**
187       * Simulates a user clicking the column which name matches the given regular expression pattern, in this fixture's
188       * <code>{@link JTableHeader}</code>, using the given mouse button, the given number of times.
189       * @param columnNamePattern the regular expression pattern to match.
190       * @param mouseClickInfo specifies the mouse button to use and the number of times to click.
191       * @return this fixture.
192       * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
193       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is disabled.
194       * @throws IllegalStateException if this fixture's <code>JTableHeader</code> is not showing on the screen.
195       * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
196       * @throws LocationUnavailableException if a column with a matching name cannot be found.
197       * @since 1.2
198       */
199      public JTableHeaderFixture clickColumn(Pattern columnNamePattern, MouseClickInfo mouseClickInfo) {
200        validateNotNull(mouseClickInfo);
201        driver.clickColumn(target, columnNamePattern, mouseClickInfo.button(), mouseClickInfo.times());
202        return this;
203      }
204    
205      private void validateNotNull(MouseClickInfo mouseClickInfo) {
206        if (mouseClickInfo == null) throw new NullPointerException("The given MouseClickInfo should not be null");
207      }
208    
209    
210      /**
211       * Asserts that the toolTip in this fixture's <code>{@link JTableHeader}</code> matches the given value.
212       * @param expected the given value. It can be a regular expression.
213       * @return this fixture.
214       * @throws AssertionError if the toolTip in this fixture's <code>JTableHeader</code> does not match the given value.
215       * @since 1.2
216       */
217      public JTableHeaderFixture requireToolTip(String expected) {
218        driver.requireToolTip(target, expected);
219        return this;
220      }
221    
222      /**
223       * Asserts that the toolTip in this fixture's <code>{@link JTableHeader}</code> matches the given regular expression
224       * pattern.
225       * @param pattern the regular expression pattern to match.
226       * @return this fixture.
227       * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
228       * @throws AssertionError if the toolTip in this fixture's <code>JTableHeader</code> does not match the given regular
229       * expression.
230       * @since 1.2
231       */
232      public JTableHeaderFixture requireToolTip(Pattern pattern) {
233        driver.requireToolTip(target, pattern);
234        return this;
235      }
236    
237      /**
238       * Returns the client property stored in this fixture's <code>{@link JTableHeader}</code>, under the given key.
239       * @param key the key to use to retrieve the client property.
240       * @return the value of the client property stored under the given key, or <code>null</code> if the property was
241       * not found.
242       * @throws NullPointerException if the given key is <code>null</code>.
243       * @since 1.2
244       */
245      public Object clientProperty(Object key) {
246        return driver.clientProperty(target, key);
247      }
248    }