/
ComboRule.java
168 lines (144 loc) · 4.25 KB
/
ComboRule.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
/*******************************************************************************
* Copyright (c) 2017 Red Hat, Inc and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.swt.generator.framework.rules.simple;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.generator.framework.WidgetUtils;
import org.eclipse.reddeer.swt.generator.framework.referencedComposite.ReferencedComposite;
import org.eclipse.reddeer.swt.generator.framework.rules.RedDeerUtils;
public class ComboRule extends AbstractSimpleRedDeerRule{
private String text;
private String label;
private int index;
private int selection;
private List<ReferencedComposite> composites;
@Override
public boolean appliesTo(Event event) {
return event.widget instanceof Combo && event.type == SWT.Modify;
}
@Override
public void initializeForEvent(Event event) { // check combo style and decide if setText or setSelection is better ?
Combo c = (Combo) event.widget;
this.widget = c;
this.setText(c.getText());
this.setLabel(WidgetUtils.getLabel(c));
this.setIndex(WidgetUtils.getIndex(c));
this.setSelection(c.getSelectionIndex());
this.setComposites(RedDeerUtils.getComposites(c));
Shell s = WidgetUtils.getShell((Combo)event.widget);
if(s!=null){
setShellTitle(s.getText());
}
}
@Override
public List<String> getActions() {
StringBuilder builder = new StringBuilder();
List<String> toReturn = new ArrayList<String>();
if(label != null){
builder.append("new LabeledCombo(");
builder.append(RedDeerUtils.getReferencedCompositeString(composites));
builder.append("\""+label+"\"");
} else {
builder.append("new DefaultCombo(");
builder.append(RedDeerUtils.getReferencedCompositeString(composites));
builder.append(index);
}
builder.append(")");
if(selection >= 0){
builder.append(".setSelection("+selection+")");
} else {
builder.append(".setText(\""+text+"\")");
}
toReturn.add(builder.toString());
return toReturn;
}
@Override
public List<String> getImports() {
List<String> toReturn = new ArrayList<String>();
if(label != null){
toReturn.add("org.eclipse.reddeer.swt.impl.combo.LabeledCombo");
} else {
toReturn.add("org.eclipse.reddeer.swt.impl.combo.DefaultCombo");
}
for(ReferencedComposite r: composites){
toReturn.add(r.getImport());
}
return toReturn;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getSelection() {
return selection;
}
public void setSelection(int selection) {
this.selection = selection;
}
public List<ReferencedComposite> getComposites() {
return composites;
}
public void setComposites(List<ReferencedComposite> composites) {
this.composites = composites;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((composites == null) ? 0 : composites.hashCode());
result = prime * result + index;
result = prime * result + ((label == null) ? 0 : label.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ComboRule other = (ComboRule) obj;
if (composites == null) {
if (other.composites != null)
return false;
} else if (!composites.equals(other.composites))
return false;
if (index != other.index)
return false;
if (label == null) {
if (other.label != null)
return false;
} else if (!label.equals(other.label))
return false;
return true;
}
}