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