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