1/*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.netbeans.jemmy.drivers.trees;
24
25import javax.swing.text.JTextComponent;
26
27import org.netbeans.jemmy.Timeout;
28import org.netbeans.jemmy.drivers.DriverManager;
29import org.netbeans.jemmy.drivers.LightSupportiveDriver;
30import org.netbeans.jemmy.drivers.TextDriver;
31import org.netbeans.jemmy.drivers.TreeDriver;
32import org.netbeans.jemmy.operators.ComponentOperator;
33import org.netbeans.jemmy.operators.JTextComponentOperator;
34import org.netbeans.jemmy.operators.JTreeOperator;
35
36/**
37 * TreeDriver for javax.swing.JTree component type. Uses API calls.
38 *
39 * @author Alexandre Iline(alexandre.iline@oracle.com)
40 */
41public class JTreeAPIDriver extends LightSupportiveDriver implements TreeDriver {
42
43    /**
44     * Constructs a JTreeAPIDriver.
45     */
46    public JTreeAPIDriver() {
47        super(new String[]{"org.netbeans.jemmy.operators.JTreeOperator"});
48    }
49
50    @Override
51    public void selectItem(ComponentOperator oper, int index) {
52        selectItems(oper, new int[]{index});
53    }
54
55    @Override
56    public void selectItems(ComponentOperator oper, int[] indices) {
57        checkSupported(oper);
58        ((JTreeOperator) oper).clearSelection();
59        ((JTreeOperator) oper).addSelectionRows(indices);
60    }
61
62    @Override
63    public void expandItem(ComponentOperator oper, int index) {
64        checkSupported(oper);
65        ((JTreeOperator) oper).expandRow(index);
66    }
67
68    @Override
69    public void collapseItem(ComponentOperator oper, int index) {
70        checkSupported(oper);
71        ((JTreeOperator) oper).collapseRow(index);
72    }
73
74    @Override
75    public void editItem(ComponentOperator oper, int index, Object newValue, Timeout waitEditorTime) {
76        JTextComponentOperator textoper = startEditingAndReturnEditor(oper, index, waitEditorTime);
77        TextDriver text = DriverManager.getTextDriver(JTextComponentOperator.class);
78        text.clearText(textoper);
79        text.typeText(textoper, newValue.toString(), 0);
80        ((JTreeOperator) oper).stopEditing();
81    }
82
83    @Override
84    public void startEditing(ComponentOperator oper, int index, Timeout waitEditorTime) {
85        startEditingAndReturnEditor(oper, index, waitEditorTime);
86    }
87
88    private JTextComponentOperator startEditingAndReturnEditor(ComponentOperator oper, int index, Timeout waitEditorTime) {
89        checkSupported(oper);
90        JTreeOperator toper = (JTreeOperator) oper;
91        toper.startEditingAtPath(toper.getPathForRow(index));
92        toper.getTimeouts().
93                setTimeout("ComponentOperator.WaitComponentTimeout", waitEditorTime.getValue());
94        return (new JTextComponentOperator((JTextComponent) toper.
95                waitSubComponent(new JTextComponentOperator.JTextComponentFinder())));
96    }
97}
98