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: TrialListenerManager.java,v 1.5 2007/10/05 11:37:07 jskrivanek Exp $ $Revision: 1.5 $ $Date: 2007/10/05 11:37:07 $
044: *
045: */
046:
047: package org.netbeans.jemmy.explorer;
048:
049: import org.netbeans.jemmy.JemmyProperties;
050: import org.netbeans.jemmy.Outputable;
051: import org.netbeans.jemmy.TestOut;
052:
053: import java.awt.AWTEvent;
054: import java.awt.Component;
055:
056: import java.awt.event.KeyEvent;
057: import java.awt.event.KeyListener;
058: import java.awt.event.MouseEvent;
059: import java.awt.event.MouseListener;
060: import java.awt.event.MouseMotionListener;
061: import org.netbeans.jemmy.QueueTool;
062:
063: /**
064: * Auxiliary class to find an event sequence which should be posted
065: * to reproduce user actions.
066: *
067: * @author Alexandre Iline (alexandre.iline@sun.com)
068: *
069: */
070:
071: public class TrialListenerManager implements Outputable {
072:
073: Component comp;
074: TrialMouseListener mListener;
075: TrialMouseMotionListener mmListener;
076: TrialKeyListener kListener;
077: TestOut output;
078:
079: /**
080: * Contructor.
081: * @param comp Component to display event sequence for.
082: */
083: public TrialListenerManager(Component comp) {
084: this .comp = comp;
085: mListener = new TrialMouseListener();
086: mmListener = new TrialMouseMotionListener();
087: kListener = new TrialKeyListener();
088: output = JemmyProperties.getCurrentOutput();
089: }
090:
091: public void setOutput(TestOut output) {
092: this .output = output;
093: }
094:
095: public TestOut getOutput() {
096: return (output);
097: }
098:
099: /**
100: * Removes mouse listener.
101: * @see #addMouseListener
102: */
103: public void removeMouseListener() {
104: comp.removeMouseListener(mListener);
105: }
106:
107: /**
108: * Adds mouse listener.
109: * @see #removeMouseListener
110: */
111: public void addMouseListener() {
112: removeMouseListener();
113: comp.addMouseListener(mListener);
114: }
115:
116: /**
117: * Removes mouse motion listener.
118: * @see #addMouseMotionListener
119: */
120: public void removeMouseMotionListener() {
121: comp.removeMouseMotionListener(mmListener);
122: }
123:
124: /**
125: * Adds mouse motion listener.
126: * @see #removeMouseMotionListener
127: */
128: public void addMouseMotionListener() {
129: removeMouseMotionListener();
130: comp.addMouseMotionListener(mmListener);
131: }
132:
133: /**
134: * Removes key listener.
135: * @see #addKeyListener
136: */
137: public void removeKeyListener() {
138: comp.removeKeyListener(kListener);
139: }
140:
141: /**
142: * Adds key listener.
143: * @see #removeKeyListener
144: */
145: public void addKeyListener() {
146: removeKeyListener();
147: comp.addKeyListener(kListener);
148: }
149:
150: void printEvent(final AWTEvent event) {
151: // if event != null run toString in dispatch thread
152: String eventToString = (String) new QueueTool()
153: .invokeSmoothly(new QueueTool.QueueAction(
154: "event.toString()") {
155: public Object launch() {
156: return event.toString();
157: }
158: });
159: output.printLine(eventToString);
160: }
161:
162: private class TrialMouseListener implements MouseListener {
163: public void mouseClicked(MouseEvent e) {
164: printEvent(e);
165: }
166:
167: public void mouseEntered(MouseEvent e) {
168: printEvent(e);
169: }
170:
171: public void mouseExited(MouseEvent e) {
172: printEvent(e);
173: }
174:
175: public void mousePressed(MouseEvent e) {
176: printEvent(e);
177: }
178:
179: public void mouseReleased(MouseEvent e) {
180: printEvent(e);
181: }
182: }
183:
184: private class TrialMouseMotionListener implements
185: MouseMotionListener {
186: public void mouseDragged(MouseEvent e) {
187: printEvent(e);
188: }
189:
190: public void mouseMoved(MouseEvent e) {
191: printEvent(e);
192: }
193: }
194:
195: private class TrialKeyListener implements KeyListener {
196: public void keyPressed(KeyEvent e) {
197: printEvent(e);
198: }
199:
200: public void keyReleased(KeyEvent e) {
201: printEvent(e);
202: }
203:
204: public void keyTyped(KeyEvent e) {
205: printEvent(e);
206: }
207: }
208: }
|