/
AbstractWindow.java
246 lines (218 loc) · 6.9 KB
/
AbstractWindow.java
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
/*******************************************************************************
* Copyright (c) 2017 Red Hat, 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:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.jface.window;
import org.eclipse.reddeer.common.logging.Logger;
import org.eclipse.reddeer.common.matcher.MatcherBuilder;
import org.eclipse.reddeer.common.wait.TimePeriod;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.core.handler.WidgetHandler;
import org.eclipse.reddeer.core.lookup.ShellLookup;
import org.eclipse.reddeer.core.matcher.WithTextMatcher;
import org.eclipse.reddeer.jface.api.Window;
import org.eclipse.reddeer.jface.condition.WindowIsAvailable;
import org.eclipse.reddeer.jface.exception.JFaceLayerException;
import org.eclipse.reddeer.jface.matcher.WindowMatcher;
import org.eclipse.reddeer.swt.api.Shell;
import org.eclipse.reddeer.swt.impl.shell.DefaultShell;
import org.eclipse.swt.widgets.Control;
import org.hamcrest.Matcher;
/**
* Represends JFace Window
*
* @author rawagner
*
*/
public abstract class AbstractWindow implements Window{
protected final Logger log = Logger.getLogger(this.getClass());
protected Shell shell;
protected Matcher<?>[] windowMatchers;
protected Openable openAction;
/**
* Finds shell with given text. Found shell must be instance of Eclipse
* Window
*
* @param text
* shell's text
*/
public AbstractWindow(String text) {
this(new WithTextMatcher(text));
}
/**
* Implementations are responsible for making sure given shell is Eclipse
* Window.
*
* @param shell
* instance of Eclipse Window
*/
public AbstractWindow(Shell shell) {
if(shell == null){
throw new JFaceLayerException("Shell cannot be null");
}
this.shell = shell;
}
public AbstractWindow(){
}
/**
* Finds shell matching given matcher. Found shell must be instance of
* Eclipse Window
*
* @param matchers
* to match shell
*/
public AbstractWindow(Matcher<?>...matchers) {
WindowMatcher wm = new WindowMatcher(getEclipseClass());
Matcher<?>[] allMatchers = MatcherBuilder.getInstance().addMatcher(matchers, wm);
this.windowMatchers = matchers;
this.shell = new DefaultShell(ShellLookup.getInstance().getShell(allMatchers));
}
/**
* Finds shell matching given matchers and set it as the shell of this window.
* The matchers given as constructor parameters will be applied. If they are
* not set, the matchers from OpenAction will be used.
*/
public void activate() {
activate(TimePeriod.DEFAULT);
}
/**
* Finds shell matching given matchers and set it as the shell of this window.
* The matchers given as constructor parameters will be applied. If they are
* not set, the matchers from OpenAction will be used.
*
* @param timeout
* period to wait for
*/
public void activate(TimePeriod timeout) {
WindowIsAvailable cond;
if(getWindowMatchers() != null){
cond = new WindowIsAvailable(getEclipseClass(), getWindowMatchers());
} else if(getOpenAction() != null){
cond = new WindowIsAvailable(getEclipseClass(), getOpenAction().getShellMatchers());
} else {
throw new JFaceLayerException("Unable to activate window. No matcher was set.");
}
new WaitUntil(cond,timeout);
setShell(new DefaultShell(cond.getResult()));
}
/**
* Gets the control.
*
* @return the control
*/
public Shell getShell() {
return shell;
}
public void open(){
if(getOpenAction() == null){
throw new JFaceLayerException("Unable to open window because open action is not defined");
}
if(!isOpen()){
getOpenAction().run();
setShell(new DefaultShell(getOpenAction().getShellMatchers()));
}
}
/**
* Checks if window is open. If window is already open, it will be focused.
* @return true if window is open, false otherwise
*/
public boolean isOpen(){
if(shell != null){
if(!shell.isDisposed() && shell.isVisible()){
shell.setFocus();
return true;
}
return false;
}
WindowIsAvailable cond;
if(getWindowMatchers() != null){
cond = new WindowIsAvailable(getEclipseClass(), getWindowMatchers());
} else if(getOpenAction() != null){
cond = new WindowIsAvailable(getEclipseClass(), getOpenAction().getShellMatchers());
} else {
throw new JFaceLayerException("Unable to check if window is open");
}
boolean open = cond.test();
if(open){
setShell(new DefaultShell(cond.getResult()));
}
return open;
}
protected Openable getOpenAction() {
return openAction != null ? openAction:getDefaultOpenAction();
}
public void setOpenAction(Openable openAction) {
this.openAction = openAction;
}
/**
* Set Default Openable action which will be called on open(). User can override by {@link #setOpenAction()}
* @return Openable action
*/
public abstract Openable getDefaultOpenAction();
private boolean isWindow(Shell shell) {
return getEclipseClass().isInstance(WidgetHandler.getInstance().getData(shell.getSWTWidget()));
}
public void setShell(Shell swtShell) {
checkShell(swtShell);
if (!isWindow(swtShell)) {
String msg = "Provided shell type is '" + getShellType(swtShell) + "' and expected is '" + getEclipseClass() + "'";
throw new JFaceLayerException(msg);
}
this.shell = swtShell;
}
private String getShellType(Shell swtShell) {
Object shellData = WidgetHandler.getInstance().getData(swtShell.getSWTWidget());
String shellType;
if (shellData == null) {
shellType = Shell.class.toString();
} else {
shellType = shellData.getClass().toString();
}
return shellType;
}
private void checkShell(Shell shell) {
if (shell == null) {
throw new JFaceLayerException("Provided shell is null");
}
if (shell.isDisposed()) {
throw new JFaceLayerException("Provided shell is disposed");
}
log.trace("Shell "+shell.getText()+" is not null and is not disposed");
}
protected void checkShell() {
if (shell == null) {
throw new JFaceLayerException("Window was not initialized");
}
if (shell.isDisposed()) {
throw new JFaceLayerException("Window shell is disposed");
}
log.trace("Shell "+shell.getText()+" is not null and is not disposed");
}
@Override
public Class<? extends org.eclipse.jface.window.Window> getEclipseClass() {
return org.eclipse.jface.window.Window.class;
}
/**
* Returns matchers which matches this window
* @return
*/
public Matcher<?>[] getWindowMatchers(){
return windowMatchers;
}
@Override
public Control getControl() {
if(shell == null) {
return null;
}
if(shell.isDisposed()){
throw new JFaceLayerException("Window is disposed");
}
return shell.getSWTWidget();
}
}