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