1 | package de.ugoe.cs.eventbench.swt;
|
---|
2 |
|
---|
3 | import java.util.SortedSet;
|
---|
4 |
|
---|
5 | import org.eclipse.swt.widgets.Composite;
|
---|
6 | import org.eclipse.swt.layout.GridLayout;
|
---|
7 | import org.eclipse.swt.widgets.Label;
|
---|
8 | import org.eclipse.swt.SWT;
|
---|
9 | import org.eclipse.swt.widgets.Text;
|
---|
10 | import org.eclipse.swt.layout.GridData;
|
---|
11 | import org.eclipse.swt.widgets.Tree;
|
---|
12 | import org.eclipse.swt.widgets.Button;
|
---|
13 | import org.eclipse.swt.widgets.TreeItem;
|
---|
14 |
|
---|
15 | import de.ugoe.cs.eventbench.assertions.AssertEvent;
|
---|
16 | import de.ugoe.cs.eventbench.assertions.TextEqualsReplay;
|
---|
17 | import de.ugoe.cs.eventbench.data.Event;
|
---|
18 | import de.ugoe.cs.util.ArrayTools;
|
---|
19 |
|
---|
20 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
21 | import org.eclipse.swt.events.SelectionEvent;
|
---|
22 |
|
---|
23 | public class InsertTextEquals extends AbstractInsertEventComposite {
|
---|
24 | private Text expectedText;
|
---|
25 | private Tree targetTree;
|
---|
26 | private Text targetText;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Create the composite.
|
---|
30 | * @param parent
|
---|
31 | * @param style
|
---|
32 | */
|
---|
33 | public InsertTextEquals(Composite parent, int style, SortedSet<String> targets) {
|
---|
34 | super(parent, style, targets);
|
---|
35 | setLayout(new GridLayout(3, false));
|
---|
36 |
|
---|
37 | Label lblExpectedValue = new Label(this, SWT.NONE);
|
---|
38 | lblExpectedValue.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
|
---|
39 | lblExpectedValue.setText("Expected Value:");
|
---|
40 |
|
---|
41 | expectedText = new Text(this, SWT.BORDER);
|
---|
42 | expectedText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
|
---|
43 |
|
---|
44 | Label lblTargetWidget = new Label(this, SWT.NONE);
|
---|
45 | lblTargetWidget.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
|
---|
46 | lblTargetWidget.setText("Target Widget:");
|
---|
47 |
|
---|
48 | targetText = new Text(this, SWT.BORDER);
|
---|
49 | targetText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
|
---|
50 | new Label(this, SWT.NONE);
|
---|
51 |
|
---|
52 | targetTree = new Tree(this, SWT.BORDER);
|
---|
53 | targetTree.addSelectionListener(new SelectionAdapter() {
|
---|
54 | @Override
|
---|
55 | public void widgetSelected(SelectionEvent e) {
|
---|
56 | TreeItem[] selection = targetTree.getSelection();
|
---|
57 | if( selection.length==1 ) {
|
---|
58 | TreeItem item = selection[0];
|
---|
59 | String targetString = item.getText();
|
---|
60 | item = item.getParentItem();
|
---|
61 | while( item!=null ) {
|
---|
62 | // TODO the "." is hard coded for the JFCMonitor. should be flexible
|
---|
63 | targetString = item.getText()+"."+targetString;
|
---|
64 | item = item.getParentItem();
|
---|
65 | }
|
---|
66 | targetText.setText(targetString);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | });
|
---|
70 | targetTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
---|
71 | buildTargetTree();
|
---|
72 | new Label(this, SWT.NONE);
|
---|
73 |
|
---|
74 | Button btnExpandAll = new Button(this, SWT.NONE);
|
---|
75 | btnExpandAll.addSelectionListener(new SelectionAdapter() {
|
---|
76 | @Override
|
---|
77 | public void widgetSelected(SelectionEvent e) {
|
---|
78 | expandAll(targetTree, true);
|
---|
79 | }
|
---|
80 | });
|
---|
81 | btnExpandAll.setText("Expand all");
|
---|
82 |
|
---|
83 | Button btnCollapseAll = new Button(this, SWT.NONE);
|
---|
84 | btnCollapseAll.addSelectionListener(new SelectionAdapter() {
|
---|
85 | @Override
|
---|
86 | public void widgetSelected(SelectionEvent e) {
|
---|
87 | expandAll(targetTree, false);
|
---|
88 | }
|
---|
89 | });
|
---|
90 | btnCollapseAll.setText("Collapse all");
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | protected void checkSubclass() {
|
---|
96 | // Disable the check that prevents subclassing of SWT components
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public Event<?> getEvent() {
|
---|
101 | String target = targetText.getText();
|
---|
102 | TextEqualsReplay replay = new TextEqualsReplay(expectedText.getText(), target);
|
---|
103 | AssertEvent<TextEqualsReplay> event = new AssertEvent<TextEqualsReplay>("TextEqualsAssertion");
|
---|
104 | event.setTarget(target);
|
---|
105 | event.addReplayEvent(replay);
|
---|
106 | return event;
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void buildTargetTree() {
|
---|
110 | for( String target : targets ) {
|
---|
111 | TreeItem currentParent = null;
|
---|
112 | TreeItem[] currentItems = targetTree.getItems();
|
---|
113 |
|
---|
114 | //TODO needs rule for target splitting. currently its hard coded for JFCEvent targets.
|
---|
115 | String[] targetParts = target.split("\\.\\[");
|
---|
116 | for( String targetPart : targetParts) {
|
---|
117 | String[] currentTexts = new String[currentItems.length];
|
---|
118 | for( int i=0; i<currentItems.length ; i++ ) {
|
---|
119 | currentTexts[i] = currentItems[i].getText();
|
---|
120 | }
|
---|
121 | if( currentParent!=null ) {
|
---|
122 | targetPart = "["+targetPart;
|
---|
123 | }
|
---|
124 | int index = ArrayTools.findIndex(currentTexts, targetPart);
|
---|
125 | if( index>= 0 ) {
|
---|
126 | currentParent = currentItems[index];
|
---|
127 | } else {
|
---|
128 | if( currentParent==null ) {
|
---|
129 | currentParent = new TreeItem(targetTree, SWT.NULL);
|
---|
130 | currentParent.setText(targetPart);
|
---|
131 | } else {
|
---|
132 | currentParent = new TreeItem(currentParent, SWT.NULL);
|
---|
133 | currentParent.setText(targetPart);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | currentItems = currentParent.getItems();
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 | private void expandAll(Tree tree, boolean expanded) {
|
---|
143 | for( TreeItem item : tree.getItems() ) {
|
---|
144 | expandAll(item, expanded);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | private void expandAll(TreeItem item, boolean expanded) {
|
---|
149 | item.setExpanded(expanded);
|
---|
150 | for( TreeItem childItem : item.getItems() ) {
|
---|
151 | expandAll(childItem, expanded);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | }
|
---|