InputDriverInstaller.java revision 13978:1993af50385d
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;
24
25import org.netbeans.jemmy.EventDispatcher;
26import org.netbeans.jemmy.JemmyException;
27import org.netbeans.jemmy.JemmyProperties;
28import org.netbeans.jemmy.Timeout;
29import org.netbeans.jemmy.drivers.input.KeyEventDriver;
30import org.netbeans.jemmy.drivers.input.KeyRobotDriver;
31import org.netbeans.jemmy.drivers.input.MouseEventDriver;
32import org.netbeans.jemmy.drivers.input.MouseRobotDriver;
33
34/**
35 * Installs drivers for low-level drivers.
36 *
37 * @author Alexandre Iline(alexandre.iline@oracle.com)
38 */
39public class InputDriverInstaller {
40
41    Timeout robotAutoDelay;
42    boolean useEventDrivers;
43    boolean smooth = false;
44
45    /**
46     * Constructs an InputDriverInstaller object.
47     *
48     * @param useEventDrivers Tells whether to use event drivers, otherwise
49     * robot drivers.
50     * @param robotAutoDelay Time for {@code Robot.setAutoDelay(long)}
51     * method.
52     */
53    public InputDriverInstaller(boolean useEventDrivers, Timeout robotAutoDelay) {
54        this.robotAutoDelay = robotAutoDelay;
55        this.useEventDrivers = useEventDrivers;
56    }
57
58    /**
59     * Constructs an InputDriverInstaller object. Takes autodelay time from
60     * JemmyProperties' timeouts.
61     *
62     * @param useEventDrivers Tells whether to use event drivers, otherwise
63     * robot drivers.
64     */
65    public InputDriverInstaller(boolean useEventDrivers) {
66        this(useEventDrivers,
67                JemmyProperties.getCurrentTimeouts().
68                create("EventDispatcher.RobotAutoDelay"));
69    }
70
71    /**
72     * Constructs an InputDriverInstaller object. Takes autodelay time from
73     * JemmyProperties' timeouts.
74     *
75     * @param useEventDrivers Tells whether to use event drivers, otherwise
76     * robot drivers.
77     * @param smooth whether to move mouse smoothly.
78     */
79    public InputDriverInstaller(boolean useEventDrivers, boolean smooth) {
80        this(useEventDrivers);
81        this.smooth = smooth;
82    }
83
84    /**
85     * Constructs an InputDriverInstaller object. Uses event drivers.
86     *
87     * @param robotAutoDelay Time for {@code Robot.setAutoDelay(long)}
88     * method.
89     */
90    public InputDriverInstaller(Timeout robotAutoDelay) {
91        this(true,
92                robotAutoDelay);
93    }
94
95    /**
96     * Constructs an InputDriverInstaller object. Takes autodelay time from
97     * JemmyProperties' timeouts. Uses event drivers.
98     */
99    public InputDriverInstaller() {
100        this(true);
101    }
102
103    static {
104        EventDispatcher.performInit();
105    }
106
107    /**
108     * Installs input drivers.
109     */
110    public void install() {
111        if (useEventDrivers) {
112            LightDriver keyE = new KeyEventDriver();
113            LightDriver mouseE = new MouseEventDriver();
114            DriverManager.removeDriver(DriverManager.KEY_DRIVER_ID,
115                    keyE.getSupported());
116            DriverManager.removeDriver(DriverManager.MOUSE_DRIVER_ID,
117                    mouseE.getSupported());
118            DriverManager.setDriver(DriverManager.KEY_DRIVER_ID, keyE);
119            DriverManager.setDriver(DriverManager.MOUSE_DRIVER_ID, mouseE);
120            try {
121                String[] awtOperators
122                        = {
123                            "org.netbeans.jemmy.operators.ButtonOperator",
124                            "org.netbeans.jemmy.operators.CheckboxOperator",
125                            "org.netbeans.jemmy.operators.ChoiceOperator",
126                            "org.netbeans.jemmy.operators.LabelOperator",
127                            "org.netbeans.jemmy.operators.ListOperator",
128                            "org.netbeans.jemmy.operators.ScrollPaneOperator",
129                            "org.netbeans.jemmy.operators.ScrollbarOperator",
130                            "org.netbeans.jemmy.operators.TextAreaOperator",
131                            "org.netbeans.jemmy.operators.TextComponentOperator",
132                            "org.netbeans.jemmy.operators.TextFieldOperator"
133                        };
134                LightDriver keyR = new KeyRobotDriver(robotAutoDelay, awtOperators);
135                LightDriver mouseR = new MouseRobotDriver(robotAutoDelay, awtOperators);
136                DriverManager.removeDriver(DriverManager.KEY_DRIVER_ID,
137                        keyR.getSupported());
138                DriverManager.removeDriver(DriverManager.MOUSE_DRIVER_ID,
139                        mouseR.getSupported());
140                DriverManager.setDriver(DriverManager.KEY_DRIVER_ID, keyR);
141                DriverManager.setDriver(DriverManager.MOUSE_DRIVER_ID, mouseR);
142            } catch (JemmyException e) {
143                if (!(e.getInnerThrowable() instanceof ClassNotFoundException)) {
144                    throw (e);
145                }
146            }
147        } else {
148            LightDriver keyR = new KeyRobotDriver(robotAutoDelay);
149            LightDriver mouseR = new MouseRobotDriver(robotAutoDelay, smooth);
150            DriverManager.removeDriver(DriverManager.KEY_DRIVER_ID,
151                    keyR.getSupported());
152            DriverManager.removeDriver(DriverManager.MOUSE_DRIVER_ID,
153                    mouseR.getSupported());
154            DriverManager.setDriver(DriverManager.KEY_DRIVER_ID, keyR);
155            DriverManager.setDriver(DriverManager.MOUSE_DRIVER_ID, mouseR);
156        }
157    }
158}
159