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