[927] | 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.
|
---|
[659] | 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.ui.swt;
|
---|
[526] | 16 |
|
---|
[659] | 17 | import java.util.List;
|
---|
[526] | 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 |
|
---|
[918] | 29 | import de.ugoe.cs.autoquest.assertions.TextEqualsAssertEventType;
|
---|
| 30 | import de.ugoe.cs.autoquest.assertions.TextEqualsReplay;
|
---|
[922] | 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;
|
---|
[526] | 35 |
|
---|
| 36 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
| 37 | import org.eclipse.swt.events.SelectionEvent;
|
---|
| 38 |
|
---|
| 39 | public class InsertTextEquals extends AbstractInsertEventComposite {
|
---|
[570] | 40 | private Text expectedText;
|
---|
[659] | 41 | private Tree guiTree;
|
---|
[526] | 42 |
|
---|
[570] | 43 | /**
|
---|
| 44 | * Create the composite.
|
---|
| 45 | *
|
---|
| 46 | * @param parent
|
---|
| 47 | * @param style
|
---|
| 48 | */
|
---|
[659] | 49 | public InsertTextEquals(Composite parent, int style, GUIModel guiModel) {
|
---|
| 50 | super(parent, style, guiModel);
|
---|
[570] | 51 | setLayout(new GridLayout(3, false));
|
---|
[526] | 52 |
|
---|
[570] | 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:");
|
---|
[526] | 56 |
|
---|
[570] | 57 | expectedText = new Text(this, SWT.BORDER);
|
---|
| 58 | expectedText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
|
---|
[526] | 59 |
|
---|
[659] | 60 | guiTree = new Tree(this, SWT.BORDER);
|
---|
| 61 | guiTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
---|
| 62 | buildGuiTree();
|
---|
[570] | 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) {
|
---|
[659] | 69 | expandAll(guiTree, true);
|
---|
[570] | 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) {
|
---|
[659] | 78 | expandAll(guiTree, false);
|
---|
[570] | 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() {
|
---|
[659] | 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 |
|
---|
[570] | 108 | return event;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[659] | 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 | }
|
---|
[570] | 119 |
|
---|
[659] | 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));
|
---|
[570] | 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 |
|
---|
[526] | 142 | }
|
---|