Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1402672cf397ca8b1f00068c505bb3b380f7c6f0 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*******************************************************************************
 * Copyright (c) 2004, 2009 MAKE Technologies Inc 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:
 *     MAKE Technologies Inc - initial API and implementation
 *     Mariot Chauvin <mariot.chauvin@obeo.fr> - refactoring
 *******************************************************************************/
package org.eclipse.swtbot.eclipse.gef.finder.widgets;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.Result;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.hamcrest.Matcher;

/**
 * represent an edit part of a graphical viewer.
 * 
 * @author David Green
 * 
 * @see SWTBotGefEditor
 */
public class SWTBotGefEditPart {
	protected final EditPart part;
	protected final SWTBotGefEditor graphicalEditor;
	
	/**
	 * 
	 * @param graphicalEditor 
	 * @param parent the parent, or null if this is the root edit part
	 * @param part the GEF part
	 */
	SWTBotGefEditPart(final SWTBotGefEditor graphicalEditor, final EditPart part) {
		this.graphicalEditor = graphicalEditor;
		this.part = part;
	}

	/**
	 * get the parent, or null if this is the root edit part.
	 */
	public SWTBotGefEditPart parent() {
		return UIThreadRunnable.syncExec(new Result<SWTBotGefEditPart>() {
			public SWTBotGefEditPart run() {
				return graphicalEditor.createEditPart(part.getParent());
			}
		});
	}

	/**
	 * Get the children of this edit part.
	 * @return the edit part's children
	 */
	@SuppressWarnings("unchecked")
	public List<SWTBotGefEditPart> children() {
		return UIThreadRunnable.syncExec(new Result<List<SWTBotGefEditPart>>() {
			public List<SWTBotGefEditPart> run() {
				List<SWTBotGefEditPart> children = new ArrayList<SWTBotGefEditPart>();
				for (org.eclipse.gef.EditPart child: ((List<org.eclipse.gef.EditPart>)part.getChildren())) {
					children.add(graphicalEditor.createEditPart(child));
				}
				return children;
			}
		});
	}

	/**
	 * find descendants that match.
	 * 
	 * @param matcher the matcher that matches against {@link org.eclipse.gef.EditPart}
	 * 
	 * @return a list of matches or an empty list if there are none
	 */
	@SuppressWarnings("unchecked")
	public List<SWTBotGefEditPart> descendants(final Matcher<? extends EditPart> matcher) {
		return  UIThreadRunnable.syncExec(new Result<List<SWTBotGefEditPart>>() {
			public List<SWTBotGefEditPart> run() {
				List<SWTBotGefEditPart> descendants = new ArrayList<SWTBotGefEditPart>();
				Stack<SWTBotGefEditPart> parts = new Stack<SWTBotGefEditPart>();
				parts.push(SWTBotGefEditPart.this);
				while (!parts.isEmpty()) {
					SWTBotGefEditPart part = parts.pop();
					for (org.eclipse.gef.EditPart child: ((List<org.eclipse.gef.EditPart>) part.part.getChildren())) {
						SWTBotGefEditPart childPart = graphicalEditor.createEditPart(child);
						if (matcher.matches(child)) {
							descendants.add(childPart);
						}
						parts.push(childPart);
					}	
				}
				return descendants;
			}
		});
	}
	
	/**
	 * get the underlying wrapped {@link EditPart} instance
	 * @return the wrapped {@link EditPart}. 
	 */
	public EditPart part() {
		return part;
	}

	/**
	 * focus on this edit part
	 */
	public void focus() {
		UIThreadRunnable.syncExec(new VoidResult() {
			public void run() {
				graphicalEditor.graphicalViewer.setFocus(part);
			}
		});
	}
	
	/**
	 * select this edit part as a single selection
	 */
	public SWTBotGefEditPart select() {
		graphicalEditor.select(this);
		return this;
	}

	/**
	 * click on the edit part.
	 */
	public SWTBotGefEditPart click() {
		final Rectangle bounds = getBounds();
		return click(bounds.getTopLeft());
	}
	
	/**
	 * click on the edit part at the specified location
	 */
	public SWTBotGefEditPart click(final Point location) {
		UIThreadRunnable.asyncExec(new VoidResult() {
			public void run() {
				graphicalEditor.getCanvas().mouseEnterLeftClickAndExit(location.x, location.y);
			}		
		});
		return this;
	}

	/**
	 * double click on the edit part.
	 */
	public SWTBotGefEditPart doubleClick() {
		final Rectangle bounds = getBounds();
		UIThreadRunnable.asyncExec(new VoidResult() {
			public void run() {
				graphicalEditor.getCanvas().mouseMoveDoubleClick(bounds.x, bounds.y);
			}		
		});
		return this;
	}

	private Rectangle getBounds() {
		final IFigure figure = ((GraphicalEditPart) part).getFigure();
		final Rectangle bounds = figure.getBounds().getCopy();
		figure.translateToAbsolute(bounds);
		return bounds;
	}
	
	public SWTBotGefEditPart activateDirectEdit() {
		return activateDirectEdit(null);
	}
	
	
	public SWTBotGefEditPart activateDirectEdit(final Object feature) {
		UIThreadRunnable.asyncExec(new VoidResult() {
			public void run() {
				DirectEditRequest request = new DirectEditRequest();
				if (feature != null)
					request.setDirectEditFeature(feature);
				part().performRequest(request);
			}
		});
		return this;
	}


	
	/**
	 * provide a description of this edit part that is useful for debugging purposes.
	 */
	public String toString() {
		StringWriter writer = new StringWriter();
		PrintWriter out = new PrintWriter(writer);
		
		describe(out,0);
		
		out.close();
		return writer.toString();
	}


	private void describe(PrintWriter out,int indent) {
		out.print(indent(indent));
		out.print(part.getClass().getName());
		List<SWTBotGefEditPart> children = children();
		out.print(" children="+children.size());
		out.println();
		for (SWTBotGefEditPart child: children) {
			child.describe(out, indent+1);
		}
	}
	
	private String indent(int size) {
		if (size == 0) {
			return "";
		}
		StringBuilder buf = new StringBuilder(size);
		for (int x = 0;x<size;++x) {
			buf.append("\t");
		}
		return buf.toString();
	}

	@SuppressWarnings("unchecked")
	public List<SWTBotGefConnectionEditPart> sourceConnections() {
		return UIThreadRunnable.syncExec(new Result<List<SWTBotGefConnectionEditPart>>() {
			public List<SWTBotGefConnectionEditPart> run() {
				List<SWTBotGefConnectionEditPart> connections = new ArrayList<SWTBotGefConnectionEditPart>();
				List<org.eclipse.gef.ConnectionEditPart> sourceConnections = ((GraphicalEditPart)part).getSourceConnections();
				for (org.eclipse.gef.ConnectionEditPart c: sourceConnections) {
					connections.add(graphicalEditor.createEditPart(c));
				}
				return connections;
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	public List<SWTBotGefConnectionEditPart> targetConnections() {
		return UIThreadRunnable.syncExec(new Result<List<SWTBotGefConnectionEditPart>>() {
			public List<SWTBotGefConnectionEditPart> run() {
				List<SWTBotGefConnectionEditPart> connections = new ArrayList<SWTBotGefConnectionEditPart>();
				List<org.eclipse.gef.ConnectionEditPart> targetConnections = ((GraphicalEditPart)part).getTargetConnections();
				for (org.eclipse.gef.ConnectionEditPart c: targetConnections) {
					connections.add(graphicalEditor.createEditPart(c));
				}
				return connections;
			}
		});
	}	
}

Back to the top