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. Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package org.netbeans.jemmy.drivers.tables;
26 
27 import java.awt.Point;
28 import java.awt.event.KeyEvent;
29 
30 import javax.swing.text.JTextComponent;
31 
32 import org.netbeans.jemmy.QueueTool;
33 import org.netbeans.jemmy.drivers.DriverManager;
34 import org.netbeans.jemmy.drivers.LightSupportiveDriver;
35 import org.netbeans.jemmy.drivers.TableDriver;
36 import org.netbeans.jemmy.drivers.TextDriver;
37 import org.netbeans.jemmy.operators.ComponentOperator;
38 import org.netbeans.jemmy.operators.JTableOperator;
39 import org.netbeans.jemmy.operators.JTextComponentOperator;
40 import org.netbeans.jemmy.operators.Operator;
41 
42 /**
43  * TableDriver for javax.swing.JTableDriver component type.
44  *
45  * @author Alexandre Iline(alexandre.iline@oracle.com)
46  */
47 public class JTableMouseDriver extends LightSupportiveDriver implements TableDriver {
48 
49     QueueTool queueTool;
50 
51     /**
52      * Constructs a JTableMouseDriver.
53      */
JTableMouseDriver()54     public JTableMouseDriver() {
55         super(new String[]{"org.netbeans.jemmy.operators.JTableOperator"});
56         queueTool = new QueueTool();
57     }
58 
59     @Override
selectCell(ComponentOperator oper, int row, int column)60     public void selectCell(ComponentOperator oper, int row, int column) {
61         clickOnCell((JTableOperator) oper, row, column, 1);
62     }
63 
64     @Override
editCell(ComponentOperator oper, int row, int column, Object value)65     public void editCell(ComponentOperator oper, int row, int column, Object value) {
66         JTableOperator toper = (JTableOperator) oper;
67         toper.scrollToCell(row, column);
68         if (!toper.isEditing()
69                 || toper.getEditingRow() != row
70                 || toper.getEditingColumn() != column) {
71             clickOnCell((JTableOperator) oper, row, column, 2);
72         }
73         JTextComponentOperator textoper
74                 = new JTextComponentOperator((JTextComponent) toper.
75                         waitSubComponent(new JTextComponentOperator.JTextComponentFinder()));
76         TextDriver text = DriverManager.getTextDriver(JTextComponentOperator.class);
77         text.clearText(textoper);
78         text.typeText(textoper, value.toString(), 0);
79         DriverManager.getKeyDriver(oper).
80                 pushKey(textoper, KeyEvent.VK_ENTER, 0,
81                         oper.getTimeouts().
82                         create("ComponentOperator.PushKeyTimeout"));
83     }
84 
85     /**
86      * Clicks on JTable cell.
87      *
88      * @param oper Table operator.
89      * @param row Cell row index.
90      * @param column Cell column index.
91      * @param clickCount Count to click.
92      */
clickOnCell(final JTableOperator oper, final int row, final int column, final int clickCount)93     protected void clickOnCell(final JTableOperator oper, final int row, final int column, final int clickCount) {
94         queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") {
95             @Override
96             public Void launch() {
97                 Point point = oper.getPointToClick(row, column);
98                 DriverManager.getMouseDriver(oper).
99                         clickMouse(oper, point.x, point.y, clickCount,
100                                 Operator.getDefaultMouseButton(),
101                                 0,
102                                 oper.getTimeouts().create("ComponentOperator.MouseClickTimeout"));
103                 return null;
104             }
105         });
106     }
107 }
108