1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.ui.swt;
|
---|
16 |
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | import org.eclipse.swt.widgets.Composite;
|
---|
20 | import org.eclipse.swt.layout.GridLayout;
|
---|
21 | import org.eclipse.swt.widgets.Label;
|
---|
22 | import org.eclipse.swt.SWT;
|
---|
23 | import org.eclipse.swt.widgets.Text;
|
---|
24 | import org.eclipse.swt.layout.GridData;
|
---|
25 | import org.eclipse.swt.widgets.Tree;
|
---|
26 | import org.eclipse.swt.widgets.Button;
|
---|
27 | import org.eclipse.swt.widgets.TreeItem;
|
---|
28 |
|
---|
29 | import de.ugoe.cs.autoquest.assertions.TextEqualsAssertEventType;
|
---|
30 | import de.ugoe.cs.autoquest.assertions.TextEqualsReplay;
|
---|
31 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
32 | import de.ugoe.cs.autoquest.eventcore.IEventTarget;
|
---|
33 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
|
---|
34 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
|
---|
35 |
|
---|
36 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
37 | import org.eclipse.swt.events.SelectionEvent;
|
---|
38 |
|
---|
39 | public class InsertTextEquals extends AbstractInsertEventComposite {
|
---|
40 | private Text expectedText;
|
---|
41 | private Tree guiTree;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Create the composite.
|
---|
45 | *
|
---|
46 | * @param parent
|
---|
47 | * @param style
|
---|
48 | */
|
---|
49 | public InsertTextEquals(Composite parent, int style, GUIModel guiModel) {
|
---|
50 | super(parent, style, guiModel);
|
---|
51 | setLayout(new GridLayout(3, false));
|
---|
52 |
|
---|
53 | Label lblExpectedValue = new Label(this, SWT.NONE);
|
---|
54 | lblExpectedValue.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
|
---|
55 | lblExpectedValue.setText("Expected Value:");
|
---|
56 |
|
---|
57 | expectedText = new Text(this, SWT.BORDER);
|
---|
58 | expectedText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
|
---|
59 |
|
---|
60 | guiTree = new Tree(this, SWT.BORDER);
|
---|
61 | guiTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
---|
62 | buildGuiTree();
|
---|
63 | new Label(this, SWT.NONE);
|
---|
64 |
|
---|
65 | Button btnExpandAll = new Button(this, SWT.NONE);
|
---|
66 | btnExpandAll.addSelectionListener(new SelectionAdapter() {
|
---|
67 | @Override
|
---|
68 | public void widgetSelected(SelectionEvent e) {
|
---|
69 | expandAll(guiTree, true);
|
---|
70 | }
|
---|
71 | });
|
---|
72 | btnExpandAll.setText("Expand all");
|
---|
73 |
|
---|
74 | Button btnCollapseAll = new Button(this, SWT.NONE);
|
---|
75 | btnCollapseAll.addSelectionListener(new SelectionAdapter() {
|
---|
76 | @Override
|
---|
77 | public void widgetSelected(SelectionEvent e) {
|
---|
78 | expandAll(guiTree, false);
|
---|
79 | }
|
---|
80 | });
|
---|
81 | btnCollapseAll.setText("Collapse all");
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | protected void checkSubclass() {
|
---|
87 | // Disable the check that prevents subclassing of SWT components
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public Event getEvent() {
|
---|
92 | // TODO possibly display error if no target is selected
|
---|
93 | TreeItem[] selection = guiTree.getSelection();
|
---|
94 | IEventTarget target = null;
|
---|
95 | TextEqualsReplay replay = null;
|
---|
96 | if (selection.length == 1) {
|
---|
97 | target = (IEventTarget) selection[0].getData();
|
---|
98 | replay = new TextEqualsReplay(expectedText.getText(), target.toString());
|
---|
99 | }
|
---|
100 |
|
---|
101 | Event event = new Event(new TextEqualsAssertEventType(), target);
|
---|
102 |
|
---|
103 | event.setTarget(target);
|
---|
104 | if (replay != null) {
|
---|
105 | event.addReplayable(replay);
|
---|
106 | }
|
---|
107 |
|
---|
108 | return event;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void buildGuiTree() {
|
---|
112 | for (IGUIElement element : guiModel.getRootElements()) {
|
---|
113 | TreeItem child = new TreeItem(guiTree, SWT.NULL);
|
---|
114 | child.setText(element.toString());
|
---|
115 | child.setData(element);
|
---|
116 | buildGuiTree(child, guiModel.getChildren(element));
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void buildGuiTree(TreeItem currentParent, List<IGUIElement> elements) {
|
---|
121 | for (IGUIElement element : elements) {
|
---|
122 | TreeItem child = new TreeItem(currentParent, SWT.NULL);
|
---|
123 | child.setText(element.toString());
|
---|
124 | child.setData(element);
|
---|
125 | buildGuiTree(child, guiModel.getChildren(element));
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void expandAll(Tree tree, boolean expanded) {
|
---|
130 | for (TreeItem item : tree.getItems()) {
|
---|
131 | expandAll(item, expanded);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void expandAll(TreeItem item, boolean expanded) {
|
---|
136 | item.setExpanded(expanded);
|
---|
137 | for (TreeItem childItem : item.getItems()) {
|
---|
138 | expandAll(childItem, expanded);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|