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.tables;
24
25import java.awt.Point;
26import java.awt.event.KeyEvent;
27
28import javax.swing.text.JTextComponent;
29
30import org.netbeans.jemmy.QueueTool;
31import org.netbeans.jemmy.drivers.DriverManager;
32import org.netbeans.jemmy.drivers.LightSupportiveDriver;
33import org.netbeans.jemmy.drivers.TableDriver;
34import org.netbeans.jemmy.drivers.TextDriver;
35import org.netbeans.jemmy.operators.ComponentOperator;
36import org.netbeans.jemmy.operators.JTableOperator;
37import org.netbeans.jemmy.operators.JTextComponentOperator;
38import org.netbeans.jemmy.operators.Operator;
39
40/**
41 * TableDriver for javax.swing.JTableDriver component type.
42 *
43 * @author Alexandre Iline(alexandre.iline@oracle.com)
44 */
45public class JTableMouseDriver extends LightSupportiveDriver implements TableDriver {
46
47    QueueTool queueTool;
48
49    /**
50     * Constructs a JTableMouseDriver.
51     */
52    public JTableMouseDriver() {
53        super(new String[]{"org.netbeans.jemmy.operators.JTableOperator"});
54        queueTool = new QueueTool();
55    }
56
57    @Override
58    public void selectCell(ComponentOperator oper, int row, int column) {
59        clickOnCell((JTableOperator) oper, row, column, 1);
60    }
61
62    @Override
63    public void editCell(ComponentOperator oper, int row, int column, Object value) {
64        JTableOperator toper = (JTableOperator) oper;
65        toper.scrollToCell(row, column);
66        if (!toper.isEditing()
67                || toper.getEditingRow() != row
68                || toper.getEditingColumn() != column) {
69            clickOnCell((JTableOperator) oper, row, column, 2);
70        }
71        JTextComponentOperator textoper
72                = new JTextComponentOperator((JTextComponent) toper.
73                        waitSubComponent(new JTextComponentOperator.JTextComponentFinder()));
74        TextDriver text = DriverManager.getTextDriver(JTextComponentOperator.class);
75        text.clearText(textoper);
76        text.typeText(textoper, value.toString(), 0);
77        DriverManager.getKeyDriver(oper).
78                pushKey(textoper, KeyEvent.VK_ENTER, 0,
79                        oper.getTimeouts().
80                        create("ComponentOperator.PushKeyTimeout"));
81    }
82
83    /**
84     * Clicks on JTable cell.
85     *
86     * @param oper Table operator.
87     * @param row Cell row index.
88     * @param column Cell column index.
89     * @param clickCount Count to click.
90     */
91    protected void clickOnCell(final JTableOperator oper, final int row, final int column, final int clickCount) {
92        queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") {
93            @Override
94            public Void launch() {
95                Point point = oper.getPointToClick(row, column);
96                DriverManager.getMouseDriver(oper).
97                        clickMouse(oper, point.x, point.y, clickCount,
98                                Operator.getDefaultMouseButton(),
99                                0,
100                                oper.getTimeouts().create("ComponentOperator.MouseClickTimeout"));
101                return null;
102            }
103        });
104    }
105}
106