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.lists;
26 
27 import java.awt.Rectangle;
28 import java.awt.event.InputEvent;
29 
30 import org.netbeans.jemmy.QueueTool;
31 import org.netbeans.jemmy.drivers.DriverManager;
32 import org.netbeans.jemmy.drivers.LightSupportiveDriver;
33 import org.netbeans.jemmy.drivers.MultiSelListDriver;
34 import org.netbeans.jemmy.operators.ComponentOperator;
35 import org.netbeans.jemmy.operators.JListOperator;
36 import org.netbeans.jemmy.operators.Operator;
37 
38 /**
39  * List driver for javax.swing.JList component type.
40  *
41  * @author Alexandre Iline(alexandre.iline@oracle.com)
42  */
43 public class JListMouseDriver extends LightSupportiveDriver implements MultiSelListDriver {
44 
45     QueueTool queueTool;
46 
47     /**
48      * Constructs a JListMouseDriver.
49      */
JListMouseDriver()50     public JListMouseDriver() {
51         super(new String[]{"org.netbeans.jemmy.operators.JListOperator"});
52         queueTool = new QueueTool();
53     }
54 
55     @Override
selectItem(ComponentOperator oper, int index)56     public void selectItem(ComponentOperator oper, int index) {
57         clickOnItem((JListOperator) oper, index);
58     }
59 
60     @Override
selectItems(ComponentOperator oper, int[] indices)61     public void selectItems(ComponentOperator oper, int[] indices) {
62         clickOnItem((JListOperator) oper, indices[0]);
63         for (int i = 1; i < indices.length; i++) {
64             clickOnItem((JListOperator) oper, indices[i], InputEvent.CTRL_MASK);
65         }
66     }
67 
68     /**
69      * Clicks on a list item.
70      *
71      * @param oper an operator to click on.
72      * @param index item index.
73      */
clickOnItem(JListOperator oper, int index)74     protected void clickOnItem(JListOperator oper, int index) {
75         clickOnItem(oper, index, 0);
76     }
77 
78     /**
79      * Clicks on a list item.
80      *
81      * @param oper an operator to click on.
82      * @param index item index.
83      * @param modifiers a combination of {@code InputEvent.*_MASK} fields.
84      */
clickOnItem(final JListOperator oper, final int index, final int modifiers)85     protected void clickOnItem(final JListOperator oper, final int index, final int modifiers) {
86         if (!QueueTool.isDispatchThread()) {
87             oper.scrollToItem(index);
88         }
89         queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") {
90             @Override
91             public Void launch() {
92                 Rectangle rect = oper.getCellBounds(index, index);
93                 DriverManager.getMouseDriver(oper).
94                         clickMouse(oper,
95                                 rect.x + rect.width / 2,
96                                 rect.y + rect.height / 2,
97                                 1, Operator.getDefaultMouseButton(), modifiers,
98                                 oper.getTimeouts().create("ComponentOperator.MouseClickTimeout"));
99                 return null;
100             }
101         });
102     }
103 }
104