[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.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.ui.swt;
|
---|
[526] | 16 |
|
---|
| 17 | import org.eclipse.swt.widgets.Dialog;
|
---|
| 18 | import org.eclipse.swt.widgets.Display;
|
---|
| 19 | import org.eclipse.swt.widgets.Listener;
|
---|
| 20 | import org.eclipse.swt.widgets.MessageBox;
|
---|
| 21 | import org.eclipse.swt.widgets.Shell;
|
---|
| 22 | import org.eclipse.swt.widgets.TableItem;
|
---|
| 23 | import org.eclipse.swt.layout.GridLayout;
|
---|
| 24 | import org.eclipse.swt.SWT;
|
---|
| 25 | import org.eclipse.swt.widgets.Button;
|
---|
| 26 | import org.eclipse.swt.layout.GridData;
|
---|
| 27 | import org.eclipse.swt.widgets.Table;
|
---|
| 28 | import org.eclipse.swt.widgets.TableColumn;
|
---|
| 29 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
| 30 | import org.eclipse.swt.events.SelectionEvent;
|
---|
| 31 | import org.eclipse.swt.graphics.Point;
|
---|
| 32 |
|
---|
[922] | 33 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 34 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
|
---|
[526] | 35 |
|
---|
| 36 | public class EditSequenceDialog extends Dialog {
|
---|
| 37 |
|
---|
[570] | 38 | protected Shell shell;
|
---|
| 39 | private Table table;
|
---|
[750] | 40 | private TableColumn tblclmnEventIndex;
|
---|
[570] | 41 | private TableColumn tblclmnEventType;
|
---|
| 42 | private TableColumn tblclmnEventTarget;
|
---|
[526] | 43 |
|
---|
[570] | 44 | private java.util.List<Event> sequence;
|
---|
[659] | 45 | private GUIModel guiModel;
|
---|
[526] | 46 |
|
---|
[570] | 47 | /**
|
---|
| 48 | * Create the dialog.
|
---|
| 49 | *
|
---|
| 50 | * @param parent
|
---|
| 51 | * @param style
|
---|
| 52 | */
|
---|
[659] | 53 | public EditSequenceDialog(Shell parent, int style, GUIModel guiModel) {
|
---|
[570] | 54 | super(parent, style);
|
---|
| 55 | setText("SWT Dialog");
|
---|
[659] | 56 | this.guiModel = guiModel;
|
---|
[570] | 57 | }
|
---|
[526] | 58 |
|
---|
[570] | 59 | /**
|
---|
| 60 | * Open the dialog.
|
---|
| 61 | */
|
---|
| 62 | public void open(java.util.List<Event> sequence) {
|
---|
| 63 | this.sequence = sequence;
|
---|
| 64 | createContents();
|
---|
| 65 | shell.open();
|
---|
| 66 | shell.layout();
|
---|
| 67 | Display display = getParent().getDisplay();
|
---|
| 68 | while (!shell.isDisposed()) {
|
---|
| 69 | if (!display.readAndDispatch()) {
|
---|
| 70 | display.sleep();
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
[526] | 74 |
|
---|
[570] | 75 | /**
|
---|
| 76 | * Create contents of the dialog.
|
---|
| 77 | */
|
---|
| 78 | private void createContents() {
|
---|
| 79 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
|
---|
| 80 | shell.setSize(450, 300);
|
---|
| 81 | shell.setText(getText());
|
---|
| 82 | shell.setLayout(new GridLayout(3, false));
|
---|
[526] | 83 |
|
---|
[570] | 84 | table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
|
---|
| 85 | table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
|
---|
| 86 | table.setHeaderVisible(true);
|
---|
| 87 | table.setLinesVisible(true);
|
---|
[750] | 88 |
|
---|
| 89 | tblclmnEventIndex = new TableColumn(table, SWT.NONE);
|
---|
| 90 | tblclmnEventIndex.setAlignment(20);
|
---|
| 91 |
|
---|
[526] | 92 |
|
---|
[570] | 93 | tblclmnEventType = new TableColumn(table, SWT.NONE);
|
---|
| 94 | tblclmnEventType.setWidth(100);
|
---|
| 95 | tblclmnEventType.setText("Event Type");
|
---|
[526] | 96 |
|
---|
[570] | 97 | tblclmnEventTarget = new TableColumn(table, SWT.NONE);
|
---|
| 98 | tblclmnEventTarget.setWidth(100);
|
---|
| 99 | tblclmnEventTarget.setText("Event Target");
|
---|
[526] | 100 |
|
---|
[570] | 101 | // this listener makes the table entries multiline
|
---|
| 102 | Listener paintListener = new Listener() {
|
---|
| 103 | public void handleEvent(org.eclipse.swt.widgets.Event event) {
|
---|
| 104 | switch (event.type)
|
---|
| 105 | {
|
---|
| 106 | case SWT.MeasureItem: {
|
---|
| 107 | TableItem item = (TableItem) event.item;
|
---|
| 108 | String text = item.getText(event.index);
|
---|
| 109 | Point size = event.gc.textExtent(text);
|
---|
| 110 | event.width = size.x;
|
---|
| 111 | event.height = Math.max(event.height, size.y);
|
---|
| 112 | break;
|
---|
| 113 | }
|
---|
| 114 | case SWT.PaintItem: {
|
---|
| 115 | TableItem item = (TableItem) event.item;
|
---|
| 116 | String text = item.getText(event.index);
|
---|
| 117 | Point size = event.gc.textExtent(text);
|
---|
| 118 | int offset =
|
---|
| 119 | event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
|
---|
| 120 | event.gc.drawText(text, event.x, event.y + offset, true);
|
---|
| 121 | break;
|
---|
| 122 | }
|
---|
| 123 | case SWT.EraseItem:
|
---|
| 124 | event.detail &= ~SWT.FOREGROUND;
|
---|
| 125 | break;
|
---|
| 126 | default:
|
---|
| 127 | break;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
[526] | 130 |
|
---|
[570] | 131 | };
|
---|
| 132 | table.addListener(SWT.MeasureItem, paintListener);
|
---|
| 133 | table.addListener(SWT.PaintItem, paintListener);
|
---|
| 134 | table.addListener(SWT.EraseItem, paintListener);
|
---|
[526] | 135 |
|
---|
[570] | 136 | updateTableContents();
|
---|
[526] | 137 |
|
---|
[570] | 138 | Button btnInsertBefore = new Button(shell, SWT.NONE);
|
---|
| 139 | btnInsertBefore.addSelectionListener(new SelectionAdapter() {
|
---|
| 140 | @Override
|
---|
| 141 | public void widgetSelected(SelectionEvent e) {
|
---|
| 142 | int index = table.getSelectionIndex();
|
---|
| 143 | if (index == -1) {
|
---|
| 144 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
| 145 | messageBox.setMessage("No event selected!");
|
---|
| 146 | messageBox.setText("Error");
|
---|
| 147 | messageBox.open();
|
---|
| 148 | }
|
---|
| 149 | else {
|
---|
| 150 | openInsertDialog(index);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | });
|
---|
| 154 | btnInsertBefore.setText("Insert Before");
|
---|
[526] | 155 |
|
---|
[570] | 156 | Button btnInsertAfter = new Button(shell, SWT.NONE);
|
---|
| 157 | btnInsertAfter.addSelectionListener(new SelectionAdapter() {
|
---|
| 158 | @Override
|
---|
| 159 | public void widgetSelected(SelectionEvent e) {
|
---|
| 160 | int index = table.getSelectionIndex();
|
---|
| 161 | if (index == -1) {
|
---|
| 162 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
| 163 | messageBox.setMessage("No event selected!");
|
---|
| 164 | messageBox.setText("Error");
|
---|
| 165 | messageBox.open();
|
---|
| 166 | }
|
---|
| 167 | else {
|
---|
| 168 | openInsertDialog(index + 1);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | });
|
---|
| 172 | btnInsertAfter.setText("Insert After");
|
---|
[526] | 173 |
|
---|
[570] | 174 | Button btnClose = new Button(shell, SWT.NONE);
|
---|
| 175 | btnClose.addSelectionListener(new SelectionAdapter() {
|
---|
| 176 | @Override
|
---|
| 177 | public void widgetSelected(SelectionEvent e) {
|
---|
| 178 | shell.dispose();
|
---|
| 179 | }
|
---|
| 180 | });
|
---|
| 181 | btnClose.setText("Close");
|
---|
[526] | 182 |
|
---|
[570] | 183 | }
|
---|
[526] | 184 |
|
---|
[570] | 185 | private void updateTableContents() {
|
---|
| 186 | table.removeAll();
|
---|
[750] | 187 | int index = 1;
|
---|
[570] | 188 | for (Event event : sequence) {
|
---|
| 189 | TableItem tableItem = new TableItem(table, SWT.NONE);
|
---|
[659] | 190 | String target = null;
|
---|
| 191 | if( event.getTarget()!=null ) {
|
---|
| 192 | target = event.getTarget().toString();
|
---|
| 193 | }
|
---|
[570] | 194 | if (target != null) {
|
---|
| 195 | // the target is split into multiple lines, as one line may
|
---|
| 196 | // only be 259 characters in tables with Windows
|
---|
| 197 | target = target.replace("].", "].\n");
|
---|
| 198 | }
|
---|
| 199 | tableItem.setText(new String[]
|
---|
[750] | 200 | { ""+(index++), event.getType().toString(), target });
|
---|
[570] | 201 | }
|
---|
| 202 | for (int i = 0; i < table.getColumnCount(); i++) {
|
---|
| 203 | table.getColumn(i).pack();
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[526] | 206 |
|
---|
[570] | 207 | private void openInsertDialog(int position) {
|
---|
[750] | 208 | if (guiModel == null) {
|
---|
| 209 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
| 210 | messageBox.setMessage("Operation not supported!\nOnly works for GUI sequences.");
|
---|
| 211 | messageBox.setText("Error");
|
---|
| 212 | messageBox.open();
|
---|
| 213 | } else {
|
---|
| 214 | InsertAssertionDialog insertDialog = new InsertAssertionDialog(shell, SWT.NONE, guiModel);
|
---|
| 215 | Event event = insertDialog.open();
|
---|
| 216 | if (event != null) {
|
---|
| 217 | sequence.add(position, event);
|
---|
| 218 | updateTableContents();
|
---|
| 219 | }
|
---|
[570] | 220 | }
|
---|
| 221 | }
|
---|
[526] | 222 |
|
---|
| 223 | }
|
---|