Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2a834f2d659d705bd8ae69bee458e61157858728 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
 * Copyright (c) 2009, 2010 Obeo and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Mariot Chauvin <mariot.chauvin@obeo.fr> - initial API and implementation
 *******************************************************************************/

package org.eclipse.swtbot.eclipse.gef.finder.widgets;

import org.eclipse.draw2d.EventDispatcher;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl;

/**
 * A bot  which wraps the swt canvas control.
 * @author mchauvin
 */
public class SWTBotGefFigureCanvas extends AbstractSWTBotControl<Canvas>{

    protected EventDispatcher eventDispatcher;
    
    /**
     * Constructs a new instance from a {@link FigureCanvas}.
     * @param canvas the canvas to wrap
     * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
     */
    public SWTBotGefFigureCanvas(FigureCanvas canvas) throws WidgetNotFoundException {
        this(canvas, canvas.getLightweightSystem());
    }

    /**
     * Constructs a new instance from a {@link Canvas} and a {@link LightweightSystem}. If the canvas is an instance of {@link FigureCanvas}, use {@link SWTBotGefFigureCanvas#SWTBotGefFigureCanvas(FigureCanvas)} instead.
     * @param canvas the canvas to wrap
     * @param lightweightSystem the lightweight system to use 
     * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
     */
    public SWTBotGefFigureCanvas(Canvas canvas, LightweightSystem lightweightSystem) throws WidgetNotFoundException {
        super(canvas);
        eventDispatcher = lightweightSystem.getRootFigure().internalGetEventDispatcher();
    }
    
    /**
     * this method emits mouse events that handle a mouse move and double click to the specified position within the canvas.
     * @param xPosition the relative x position
     * @param yPosition the relative y position
     */
    public void mouseMoveDoubleClick(final int xPosition, final int yPosition) {
    UIThreadRunnable.asyncExec(new VoidResult() {
    		public void run() {
    	        org.eclipse.swt.events.MouseEvent meMove = wrapMouseEvent(xPosition, yPosition, 0, 0, 0);
    	        eventDispatcher.dispatchMouseMoved(meMove);
    	        org.eclipse.swt.events.MouseEvent meDown = wrapMouseEvent(xPosition, yPosition, 1, SWT.None, 1);
    	        eventDispatcher.dispatchMousePressed(meDown);
    	        org.eclipse.swt.events.MouseEvent meUp = wrapMouseEvent(xPosition, yPosition, 1 , SWT.BUTTON1, 1);
    	        eventDispatcher.dispatchMouseReleased(meUp);
    	        org.eclipse.swt.events.MouseEvent meDown2 = wrapMouseEvent(xPosition, yPosition, 1, SWT.None, 2);
    	        eventDispatcher.dispatchMousePressed(meDown2);
    	        org.eclipse.swt.events.MouseEvent meDoubleClick = wrapMouseEvent(xPosition, yPosition, 1, SWT.None, 2);
    	        meDoubleClick.time = meDown2.time;
    	        eventDispatcher.dispatchMouseDoubleClicked(meDoubleClick);
    	        org.eclipse.swt.events.MouseEvent meUp2 = wrapMouseEvent(xPosition, yPosition, 1 , SWT.BUTTON1, 2);
    	        eventDispatcher.dispatchMouseReleased(meUp2); 
    		}
    	});
    }
    
    private org.eclipse.swt.events.MouseEvent wrapMouseEvent(int x, int y, int button, int stateMask, int count) {
        return new org.eclipse.swt.events.MouseEvent(createMouseEvent(x, y, button, stateMask, count));
    }

    
    /**
     * this method emits mouse events that handle drags within the canvas
     * 
     * @param fromXPosition the relative x position within the canvas to drag from
     * @param fromYPosition the relative y position within the canvas to drag from
     * @param toXPosition the relative x position within the canvas to drag to
     * @param toYPosition the relative y position within the canvas to drag to
     */
    public void mouseDrag(final int fromXPosition, final int fromYPosition, final int toXPosition, final int toYPosition) {
        UIThreadRunnable.asyncExec(new VoidResult() {
            public void run() {
            	org.eclipse.swt.events.MouseEvent meMove = wrapMouseEvent(fromXPosition, fromYPosition, 0, 0, 0);
            	eventDispatcher.dispatchMouseMoved(meMove);
            	org.eclipse.swt.events.MouseEvent meDown = wrapMouseEvent(fromXPosition, fromYPosition, 1, SWT.BUTTON1, 1);
            	eventDispatcher.dispatchMousePressed(meDown);
            	org.eclipse.swt.events.MouseEvent meMoveTarget = wrapMouseEvent(toXPosition, toYPosition, 1, SWT.BUTTON1, 0);
            	eventDispatcher.dispatchMouseMoved(meMoveTarget);
            	org.eclipse.swt.events.MouseEvent meUp = wrapMouseEvent(toXPosition, toYPosition, 1 , SWT.BUTTON1, 1);
            	eventDispatcher.dispatchMouseReleased(meUp);
            }
        });
    }
    
    public void mouseMoveLeftClick(final int xPosition, final int yPosition) {
        UIThreadRunnable.asyncExec(new VoidResult() {
            public void run() {
            	org.eclipse.swt.events.MouseEvent meMove = wrapMouseEvent(xPosition, yPosition, 0, 0, 0);
            	eventDispatcher.dispatchMouseMoved(meMove);
            	org.eclipse.swt.events.MouseEvent meDown = wrapMouseEvent(xPosition, yPosition, 1, SWT.BUTTON1, 1);
            	eventDispatcher.dispatchMousePressed(meDown);
            	org.eclipse.swt.events.MouseEvent meUp = wrapMouseEvent(xPosition, yPosition, 1 , SWT.BUTTON1, 1);
            	eventDispatcher.dispatchMouseReleased(meUp);
            }
        });
    }
    
    public void mouseEnterLeftClickAndExit(final int xPosition, final int yPosition) {
		UIThreadRunnable.asyncExec(new VoidResult() {
			public void run() {
				eventDispatcher.dispatchMouseEntered(wrapMouseEvent(xPosition, yPosition, 0, 0, 0));
				eventDispatcher.dispatchMouseMoved(wrapMouseEvent(xPosition, yPosition, 0, 0, 0));
				eventDispatcher.dispatchMousePressed(wrapMouseEvent(xPosition, yPosition, 1, SWT.BUTTON1, 1));
				eventDispatcher.dispatchMouseReleased(wrapMouseEvent(xPosition, yPosition, 1, SWT.BUTTON1, 1));
				eventDispatcher.dispatchMouseExited(wrapMouseEvent(xPosition, yPosition, 0, 0, 0));
			}
		});
    }
    
    public void typeText(final Text textControl, final String text) {
    	oldTypeText(textControl, text);
    	//TODO need to improve type text before that
    	//final SWTBotText textBot = new SWTBotText(textControl);
        //textBot.typeText(text + "\r");          
    }
    
    private void oldTypeText(final Text textControl, final String text) {
        
        UIThreadRunnable.syncExec(new VoidResult() {
            public void run() {
                textControl.setText("");
            }
        });
        for (int x = 0; x < text.length(); ++x) {
            final char c = text.charAt(x);
            UIThreadRunnable.syncExec(new VoidResult() {
                public void run() {
                    textControl.setFocus();
                    textControl.notifyListeners(SWT.KeyDown, keyEvent(SWT.NONE, c, 0));
                    textControl.notifyListeners(SWT.KeyUp, keyEvent(SWT.NONE, c, 0));
                    textControl.setText(textControl.getText() + c);
                }
            });
            try {
                Thread.sleep(50L);
            } catch (InterruptedException e) {
            }
        }

        // apply the value with a default selection event
        UIThreadRunnable.syncExec(new VoidResult() {
            public void run() {
                textControl.setFocus();
                textControl.notifyListeners(SWT.DefaultSelection, createEvent());
            }
        });
    }
    
    private Event keyEvent(int modificationKey, char c, int keyCode) {
        Event keyEvent = createEvent();
        keyEvent.stateMask = modificationKey;
        keyEvent.character = c;
        keyEvent.keyCode = keyCode;
        return keyEvent;
    }

    
    
}

Back to the top