DefaultVisualizer.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.util;
24
25import java.awt.Component;
26import java.awt.Container;
27import java.awt.Dialog;
28
29import javax.swing.JInternalFrame;
30import javax.swing.JScrollPane;
31import javax.swing.JTabbedPane;
32
33import org.netbeans.jemmy.JemmyException;
34import org.netbeans.jemmy.JemmyInputException;
35import org.netbeans.jemmy.JemmyProperties;
36import org.netbeans.jemmy.TimeoutExpiredException;
37import org.netbeans.jemmy.operators.ComponentOperator;
38import org.netbeans.jemmy.operators.JDialogOperator;
39import org.netbeans.jemmy.operators.JInternalFrameOperator;
40import org.netbeans.jemmy.operators.JScrollPaneOperator;
41import org.netbeans.jemmy.operators.JTabbedPaneOperator;
42import org.netbeans.jemmy.operators.Operator;
43import org.netbeans.jemmy.operators.WindowOperator;
44import org.netbeans.jemmy.operators.Operator.ComponentVisualizer;
45
46/**
47 *
48 * Used as component visualizer by default.
49 *
50 * @see
51 * org.netbeans.jemmy.operators.Operator#setVisualizer(Operator.ComponentVisualizer)
52 * @see org.netbeans.jemmy.operators.Operator.ComponentVisualizer
53 *
54 * @author Alexandre Iline (alexandre.iline@oracle.com)
55 *
56 */
57public class DefaultVisualizer implements ComponentVisualizer, Cloneable {
58
59    private boolean window = true;
60    private boolean internalFrame = true;
61    private boolean scroll = false;
62    private boolean switchTab = false;
63    private boolean modal = false;
64
65    public DefaultVisualizer() {
66    }
67
68    /**
69     * Forces vizualizer to check that component is on the top modal dialog or
70     * no modal dialog displayed.
71     *
72     * @param yesOrNo If true, JemmyInputException will be throught if component
73     * is not on the top modal dialog and a modal dialog is dislayed.
74     */
75    public void checkForModal(boolean yesOrNo) {
76        modal = yesOrNo;
77    }
78
79    /**
80     * Informs that a window contained component should be activated.
81     *
82     * @param yesOrNo true if windows need to be activated.
83     */
84    public void activateWindow(boolean yesOrNo) {
85        window = yesOrNo;
86    }
87
88    /**
89     * Informs that an internal frame contained component should be activated.
90     *
91     * @param yesOrNo true if internal frames need to be activated.
92     */
93    public void activateInternalFrame(boolean yesOrNo) {
94        internalFrame = yesOrNo;
95    }
96
97    /**
98     * Informs that scrolling should be made.
99     *
100     * @param yesOrNo true if scroll panes need to be scrolled.
101     */
102    public void scroll(boolean yesOrNo) {
103        scroll = yesOrNo;
104    }
105
106    /**
107     * Informs that tab switching should be made.
108     *
109     * @param yesOrNo true if tabbed panes need to be switched.
110     */
111    public void switchTab(boolean yesOrNo) {
112        switchTab = yesOrNo;
113    }
114
115    /**
116     * Returns true if window is active.
117     *
118     * @param winOper an operator representing the window.
119     * @return true is window is active.
120     */
121    protected boolean isWindowActive(WindowOperator winOper) {
122        return winOper.isFocused() && winOper.isActive();
123    }
124
125    /**
126     * Performs an atomic window-activization precedure. A window is sopposed to
127     * be prepared for the activization (i.e. put "to front").
128     *
129     * @param winOper an operator representing the window.
130     */
131    protected void makeWindowActive(WindowOperator winOper) {
132        winOper.activate();
133    }
134
135    /**
136     * Activates a window. Uses makeWindowActive if necessary.
137     *
138     * @param winOper an operator representing the window.
139     * @see #makeWindowActive
140     */
141    protected void activate(WindowOperator winOper) {
142        boolean active = isWindowActive(winOper);
143        winOper.toFront();
144        if (!active) {
145            makeWindowActive(winOper);
146        }
147    }
148
149    /**
150     * Inits an internal frame.
151     *
152     * @param intOper an operator representing the frame.
153     */
154    protected void initInternalFrame(JInternalFrameOperator intOper) {
155        if (!intOper.isSelected()) {
156            intOper.activate();
157        }
158    }
159
160    /**
161     * Scrolls JScrollPane to make the component visible.
162     *
163     * @param scrollOper an operator representing a scroll pane.
164     * @param target a component - target to be made visible.
165     */
166    protected void scroll(JScrollPaneOperator scrollOper, Component target) {
167        if (!scrollOper.checkInside(target)) {
168            scrollOper.scrollToComponent(target);
169        }
170    }
171
172    /**
173     * Switches tabs to make the component visible.
174     *
175     * @param tabOper an operator representing a tabbed pane.
176     * @param target a component - target to be made visible.
177     */
178    protected void switchTab(JTabbedPaneOperator tabOper, Component target) {
179        int tabInd = 0;
180        for (int j = 0; j < tabOper.getTabCount(); j++) {
181            if (target == tabOper.getComponentAt(j)) {
182                tabInd = j;
183                break;
184            }
185        }
186        if (tabOper.getSelectedIndex() != tabInd) {
187            tabOper.selectPage(tabInd);
188        }
189    }
190
191    /**
192     * Prepares the component for user input.
193     *
194     * @param compOper an operator representing the component.
195     * @throws JemmyInputException
196     * @see #checkForModal(boolean)
197     */
198    @Override
199    public void makeVisible(ComponentOperator compOper) {
200        try {
201            if (modal) {
202                Dialog modalDialog = JDialogOperator.getTopModalDialog();
203                if (modalDialog != null
204                        && compOper.getWindow() != modalDialog) {
205                    throw (new JemmyInputException("Component is not on top modal dialog.",
206                            compOper.getSource()));
207                }
208            }
209            WindowOperator winOper = new WindowOperator(compOper.getWindow());
210            if (window) {
211                winOper.copyEnvironment(compOper);
212                winOper.setVisualizer(new EmptyVisualizer());
213                activate(winOper);
214            }
215            if (internalFrame && compOper instanceof JInternalFrameOperator) {
216                initInternalFrame((JInternalFrameOperator) compOper);
217            }
218            Container[] conts = compOper.getContainers();
219            for (int i = conts.length - 1; i >= 0; i--) {
220                if (internalFrame && conts[i] instanceof JInternalFrame) {
221                    JInternalFrameOperator intOper = new JInternalFrameOperator((JInternalFrame) conts[i]);
222                    intOper.copyEnvironment(compOper);
223                    intOper.setVisualizer(new EmptyVisualizer());
224                    initInternalFrame(intOper);
225                } else if (scroll && conts[i] instanceof JScrollPane) {
226                    JScrollPaneOperator scrollOper = new JScrollPaneOperator((JScrollPane) conts[i]);
227                    scrollOper.copyEnvironment(compOper);
228                    scrollOper.setVisualizer(new EmptyVisualizer());
229                    scroll(scrollOper, compOper.getSource());
230                } else if (switchTab && conts[i] instanceof JTabbedPane) {
231                    JTabbedPaneOperator tabOper = new JTabbedPaneOperator((JTabbedPane) conts[i]);
232                    tabOper.copyEnvironment(compOper);
233                    tabOper.setVisualizer(new EmptyVisualizer());
234                    switchTab(tabOper, i == 0 ? compOper.getSource() : conts[i - 1]);
235                }
236            }
237        } catch (TimeoutExpiredException e) {
238            JemmyProperties.getProperties().getOutput().printStackTrace(e);
239        }
240    }
241
242    /**
243     * Creates an exact copy of this visualizer.
244     *
245     * @return new instance.
246     */
247    public DefaultVisualizer cloneThis() {
248        try {
249            return (DefaultVisualizer) super.clone();
250        } catch (CloneNotSupportedException e) {
251            //that's impossible
252            throw (new JemmyException("Even impossible happens :)", e));
253        }
254    }
255
256}
257