001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id: DefaultVisualizer.java,v 1.8 2007/10/05 11:36:21 jskrivanek Exp $ $Revision: 1.8 $ $Date: 2007/10/05 11:36:21 $
044: *
045: */
046:
047: package org.netbeans.jemmy.util;
048:
049: import org.netbeans.jemmy.JemmyException;
050: import org.netbeans.jemmy.JemmyInputException;
051: import org.netbeans.jemmy.JemmyProperties;
052: import org.netbeans.jemmy.TestOut;
053: import org.netbeans.jemmy.TimeoutExpiredException;
054: import org.netbeans.jemmy.Timeouts;
055:
056: import org.netbeans.jemmy.operators.ComponentOperator;
057: import org.netbeans.jemmy.operators.Operator.ComponentVisualizer;
058: import org.netbeans.jemmy.operators.JDialogOperator;
059: import org.netbeans.jemmy.operators.JInternalFrameOperator;
060: import org.netbeans.jemmy.operators.JScrollPaneOperator;
061: import org.netbeans.jemmy.operators.JTabbedPaneOperator;
062: import org.netbeans.jemmy.operators.WindowOperator;
063:
064: import java.awt.Component;
065: import java.awt.Container;
066: import java.awt.Dialog;
067:
068: import javax.swing.JInternalFrame;
069: import javax.swing.JScrollPane;
070: import javax.swing.JTabbedPane;
071:
072: /**
073: *
074: * Used as component visualizer by default.
075: *
076: * @see org.netbeans.jemmy.operators.Operator#setVisualizer(Operator.ComponentVisualizer)
077: * @see org.netbeans.jemmy.operators.Operator.ComponentVisualizer
078: *
079: * @author Alexandre Iline (alexandre.iline@sun.com)
080: *
081: */
082: public class DefaultVisualizer implements ComponentVisualizer,
083: Cloneable {
084: private boolean window = true;
085: private boolean internalFrame = true;
086: private boolean scroll = false;
087: private boolean switchTab = false;
088: private boolean modal = false;
089:
090: public DefaultVisualizer() {
091: }
092:
093: /**
094: * Forces vizualizer to check that component is
095: * on the top modal dialog or no modal dialog
096: * displayed.
097: * @param yesOrNo If true, JemmyInputException will be throught
098: * if component is not on the top modal dialog and a modal dialog
099: * is dislayed.
100: */
101: public void checkForModal(boolean yesOrNo) {
102: modal = yesOrNo;
103: }
104:
105: /**
106: * Informs that a window contained component should be activated.
107: * @param yesOrNo true if windows need to be activated.
108: */
109: public void activateWindow(boolean yesOrNo) {
110: window = yesOrNo;
111: }
112:
113: /**
114: * Informs that an internal frame contained component
115: * should be activated.
116: * @param yesOrNo true if internal frames need to be activated.
117: */
118: public void activateInternalFrame(boolean yesOrNo) {
119: internalFrame = yesOrNo;
120: }
121:
122: /**
123: * Informs that scrolling should be made.
124: * @param yesOrNo true if scroll panes need to be scrolled.
125: */
126: public void scroll(boolean yesOrNo) {
127: scroll = yesOrNo;
128: }
129:
130: /**
131: * Informs that tab switching should be made.
132: * @param yesOrNo true if tabbed panes need to be switched.
133: */
134: public void switchTab(boolean yesOrNo) {
135: switchTab = yesOrNo;
136: }
137:
138: /**
139: * Returns true if window is active.
140: * @param winOper an operator representing the window.
141: * @return true is window is active.
142: */
143: protected boolean isWindowActive(WindowOperator winOper) {
144: return (winOper.isFocused() && winOper.isActive());
145: }
146:
147: /**
148: * Performs an atomic window-activization precedure.
149: * A window is sopposed to be prepared for the activization
150: * (i.e. put "to front").
151: * @param winOper an operator representing the window.
152: */
153: protected void makeWindowActive(WindowOperator winOper) {
154: winOper.activate();
155: }
156:
157: /**
158: * Activates a window. Uses makeWindowActive if necessary.
159: * @param winOper an operator representing the window.
160: * @see #makeWindowActive
161: */
162: protected void activate(WindowOperator winOper) {
163: boolean active = isWindowActive(winOper);
164: winOper.toFront();
165: if (!active) {
166: makeWindowActive(winOper);
167: }
168: }
169:
170: /**
171: * Inits an internal frame.
172: * @param intOper an operator representing the frame.
173: */
174: protected void initInternalFrame(JInternalFrameOperator intOper) {
175: if (!intOper.isSelected()) {
176: intOper.activate();
177: }
178: }
179:
180: /**
181: * Scrolls JScrollPane to make the component visible.
182: * @param scrollOper an operator representing a scroll pane.
183: * @param target a component - target to be made visible.
184: */
185: protected void scroll(JScrollPaneOperator scrollOper,
186: Component target) {
187: if (!scrollOper.checkInside(target)) {
188: scrollOper.scrollToComponent(target);
189: }
190: }
191:
192: /**
193: * Switches tabs to make the component visible.
194: * @param tabOper an operator representing a tabbed pane.
195: * @param target a component - target to be made visible.
196: */
197: protected void switchTab(JTabbedPaneOperator tabOper,
198: Component target) {
199: int tabInd = 0;
200: for (int j = 0; j < tabOper.getTabCount(); j++) {
201: if (target == tabOper.getComponentAt(j)) {
202: tabInd = j;
203: break;
204: }
205: }
206: if (tabOper.getSelectedIndex() != tabInd) {
207: tabOper.selectPage(tabInd);
208: }
209: }
210:
211: /**
212: * Prepares the component for user input.
213: * @param compOper an operator representing the component.
214: * @throws JemmyInputException
215: * @see #checkForModal(boolean)
216: */
217: public void makeVisible(ComponentOperator compOper) {
218: try {
219: if (modal) {
220: Dialog modalDialog = JDialogOperator
221: .getTopModalDialog();
222: if (modalDialog != null
223: && compOper.getWindow() != modalDialog) {
224: throw (new JemmyInputException(
225: "Component is not on top modal dialog.",
226: compOper.getSource()));
227: }
228: }
229: WindowOperator winOper = new WindowOperator(compOper
230: .getWindow());
231: if (window) {
232: winOper.copyEnvironment(compOper);
233: winOper.setVisualizer(new EmptyVisualizer());
234: activate(winOper);
235: }
236: if (internalFrame
237: && compOper instanceof JInternalFrameOperator) {
238: initInternalFrame((JInternalFrameOperator) compOper);
239: }
240: Container[] conts = compOper.getContainers();
241: for (int i = conts.length - 1; i >= 0; i--) {
242: if (internalFrame && conts[i] instanceof JInternalFrame) {
243: JInternalFrameOperator intOper = new JInternalFrameOperator(
244: (JInternalFrame) conts[i]);
245: intOper.copyEnvironment(compOper);
246: intOper.setVisualizer(new EmptyVisualizer());
247: initInternalFrame(intOper);
248: } else if (scroll && conts[i] instanceof JScrollPane) {
249: JScrollPaneOperator scrollOper = new JScrollPaneOperator(
250: (JScrollPane) conts[i]);
251: scrollOper.copyEnvironment(compOper);
252: scrollOper.setVisualizer(new EmptyVisualizer());
253: scroll(scrollOper, compOper.getSource());
254: } else if (switchTab && conts[i] instanceof JTabbedPane) {
255: JTabbedPaneOperator tabOper = new JTabbedPaneOperator(
256: (JTabbedPane) conts[i]);
257: tabOper.copyEnvironment(compOper);
258: tabOper.setVisualizer(new EmptyVisualizer());
259: switchTab(tabOper, i == 0 ? compOper.getSource()
260: : conts[i - 1]);
261: }
262: }
263: } catch (TimeoutExpiredException e) {
264: JemmyProperties.getProperties().getOutput()
265: .printStackTrace(e);
266: }
267: }
268:
269: /**
270: * Creates an exact copy of this visualizer.
271: * @return new instance.
272: */
273: public DefaultVisualizer cloneThis() {
274: try {
275: return ((DefaultVisualizer) super .clone());
276: } catch (CloneNotSupportedException e) {
277: //that's impossible
278: throw (new JemmyException("Even impossible happens :)", e));
279: }
280: }
281:
282: }
|