001    /*
002     * Created on Dec 26, 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 javax.swing.JTree;
019    
020    import org.fest.swing.core.MouseButton;
021    import org.fest.swing.core.MouseClickInfo;
022    import org.fest.swing.exception.ActionFailedException;
023    import org.fest.swing.exception.ComponentLookupException;
024    
025    /**
026     * Understands functional testing of single nodes, referenced by their row indices, in <code>{@link JTree}</code>s:
027     * <ul>
028     * <li>user input simulation</li>
029     * <li>state verification</li>
030     * <li>property value query</li>
031     * </ul>
032     *
033     * @author Alex Ruiz
034     *
035     * @since 1.2
036     */
037    public class JTreeRowFixture implements JTreeNodeFixture {
038    
039      private final int index;
040      private final JTreeFixture tree;
041    
042      /**
043       * Creates a new </code>{@link JTreeRowFixture}</code>.
044       * @param tree handles the <code>JTree</code> containing the node with the given row index.
045       * @param index the given row index.
046       */
047      protected JTreeRowFixture(JTreeFixture tree, int index) {
048        this.tree = tree;
049        this.index = index;
050      }
051    
052      /**
053       * Simulates a user expanding this fixture's tree node.
054       * @return this fixture.
055       * @throws IllegalStateException if the <code>JTree</code> is disabled.
056       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
057       * @throws ActionFailedException if this method fails to expand the row.
058       */
059      public JTreeRowFixture expand() {
060        tree.expandRow(index);
061        return this;
062      }
063    
064      /**
065       * Simulates a user collapsing this fixture's tree node.
066       * @return this fixture.
067       * @throws IllegalStateException if the <code>JTree</code> is disabled.
068       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
069       * @throws ActionFailedException if this method fails to collapse the row.
070       */
071      public JTreeRowFixture collapse() {
072        tree.collapseRow(index);
073        return this;
074      }
075    
076      /**
077       * Selects the this fixture's tree node, expanding parent nodes if necessary. This method will not click the node if
078       * it is already selected.
079       * @return this fixture.
080       * @throws IllegalStateException if this fixture's <code>JTree</code> is disabled.
081       * @throws IllegalStateException if this fixture's <code>JTree</code> is not showing on the screen.
082       */
083      public JTreeRowFixture select() {
084        tree.selectRow(index);
085        return this;
086      }
087    
088      /**
089       * Simulates a user clicking this fixture's tree node.
090       * @return this fixture.
091       * @throws IllegalStateException if the <code>JTree</code> is disabled.
092       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
093       */
094      public JTreeRowFixture click() {
095        tree.clickRow(index);
096        return this;
097      }
098    
099      /**
100       * Simulates a user clicking this fixture's tree node.
101       * @param button the button to click.
102       * @return this fixture.
103       * @throws NullPointerException if the given button is <code>null</code>.
104       * @throws IllegalStateException if the <code>JTree</code> is disabled.
105       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
106       */
107      public JTreeRowFixture click(MouseButton button) {
108        tree.clickRow(index, button);
109        return this;
110      }
111    
112      /**
113       * Simulates a user clicking this fixture's tree node.
114       * @param mouseClickInfo specifies the button to click and the times the button should be clicked.
115       * @return this fixture.
116       * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
117       * @throws IllegalStateException if the <code>JTree</code> is disabled.
118       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
119       */
120      public JTreeRowFixture click(MouseClickInfo mouseClickInfo) {
121        tree.clickRow(index, mouseClickInfo);
122        return this;
123      }
124    
125      /**
126       * Simulates a user double-clicking this fixture's tree node.
127       * @return this fixture.
128       * @throws IllegalStateException if the <code>JTree</code> is disabled.
129       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
130       */
131      public JTreeRowFixture doubleClick() {
132        tree.doubleClickRow(index);
133        return this;
134      }
135    
136      /**
137       * Simulates a user right-clicking this fixture's tree node.
138       * @return this fixture.
139       * @throws IllegalStateException if the <code>JTree</code> is disabled.
140       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
141       */
142      public JTreeRowFixture rightClick() {
143        tree.rightClickRow(index);
144        return this;
145      }
146    
147      /**
148       * Simulates a user dragging this fixture's tree node.
149       * @return this fixture.
150       * @throws IllegalStateException if the <code>JTree</code> is disabled.
151       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
152       */
153      public JTreeRowFixture drag() {
154        tree.drag(index);
155        return this;
156      }
157    
158      /**
159       * Simulates a user dropping relative to this fixture's tree node.
160       * @return this fixture.
161       * @throws IllegalStateException if the <code>JTree</code> is disabled.
162       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
163       * @throws ActionFailedException if there is no drag action in effect.
164       */
165      public JTreeRowFixture drop() {
166        tree.drop(index);
167        return this;
168      }
169    
170      /**
171       * Shows a pop-up menu using this fixture's tree node as the invoker of the pop-up menu.
172       * @return a fixture that handles functional testing of the displayed pop-up menu.
173       * @throws IllegalStateException if the <code>JTree</code> is disabled.
174       * @throws IllegalStateException if the <code>JTree</code> is not showing on the screen.
175       * @throws ComponentLookupException if a pop-up menu cannot be found.
176       */
177      public JPopupMenuFixture showPopupMenu() {
178        return tree.showPopupMenuAt(index);
179      }
180    
181      /**
182       * Returns the <code>String</code> representation of this fixture's tree node.
183       * @return the <code>String</code> representation of this fixture's tree node.
184       */
185      public String value() {
186        return tree.valueAt(index);
187      }
188    
189      /**
190       * Returns the row index of the node.
191       * @return the row index of the node.
192       */
193      public int index() {
194        return index;
195      }
196    }