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;
|
---|
7 | import org.eclipse.swt.widgets.Listener;
|
---|
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;
|
---|
19 | import org.eclipse.swt.graphics.Point;
|
---|
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;
|
---|
29 |
|
---|
30 | private java.util.List<Event<?>> sequence;
|
---|
31 | private SortedSet<String> targets;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create the dialog.
|
---|
35 | *
|
---|
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 | */
|
---|
48 | public void open(java.util.List<Event<?>> sequence) {
|
---|
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() {
|
---|
65 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER
|
---|
66 | | SWT.APPLICATION_MODAL);
|
---|
67 | shell.setSize(450, 300);
|
---|
68 | shell.setText(getText());
|
---|
69 | shell.setLayout(new GridLayout(3, false));
|
---|
70 |
|
---|
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);
|
---|
75 |
|
---|
76 | tblclmnEventType = new TableColumn(table, SWT.NONE);
|
---|
77 | tblclmnEventType.setWidth(100);
|
---|
78 | tblclmnEventType.setText("Event Type");
|
---|
79 |
|
---|
80 | tblclmnEventTarget = new TableColumn(table, SWT.NONE);
|
---|
81 | tblclmnEventTarget.setWidth(100);
|
---|
82 | tblclmnEventTarget.setText("Event Target");
|
---|
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 | }
|
---|
105 | case SWT.EraseItem:
|
---|
106 | event.detail &= ~SWT.FOREGROUND;
|
---|
107 | break;
|
---|
108 | default:
|
---|
109 | break;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | };
|
---|
114 | table.addListener(SWT.MeasureItem, paintListener);
|
---|
115 | table.addListener(SWT.PaintItem, paintListener);
|
---|
116 | table.addListener(SWT.EraseItem, paintListener);
|
---|
117 |
|
---|
118 | updateTableContents();
|
---|
119 |
|
---|
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();
|
---|
125 | if (index == -1) {
|
---|
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");
|
---|
136 |
|
---|
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 | } else {
|
---|
148 | openInsertDialog(index + 1);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | });
|
---|
152 | btnInsertAfter.setText("Insert After");
|
---|
153 |
|
---|
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 | }
|
---|
164 |
|
---|
165 | private void updateTableContents() {
|
---|
166 | table.removeAll();
|
---|
167 | for (Event<?> event : sequence) {
|
---|
168 | TableItem tableItem = new TableItem(table, SWT.NONE);
|
---|
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 });
|
---|
176 | }
|
---|
177 | for (int i = 0; i < table.getColumnCount(); i++) {
|
---|
178 | table.getColumn(i).pack();
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void openInsertDialog(int position) {
|
---|
183 | InsertAssertionDialog insertDialog = new InsertAssertionDialog(shell,
|
---|
184 | SWT.NONE, targets);
|
---|
185 | Event<?> event = insertDialog.open();
|
---|
186 | if (event != null) {
|
---|
187 | sequence.add(position, event);
|
---|
188 | updateTableContents();
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|