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 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | };
|
---|
113 | table.addListener(SWT.MeasureItem, paintListener);
|
---|
114 | table.addListener(SWT.PaintItem, paintListener);
|
---|
115 | table.addListener(SWT.EraseItem, paintListener);
|
---|
116 |
|
---|
117 | updateTableContents();
|
---|
118 |
|
---|
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 | } else {
|
---|
130 | openInsertDialog(index);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | });
|
---|
134 | btnInsertBefore.setText("Insert Before");
|
---|
135 |
|
---|
136 | Button btnInsertAfter = new Button(shell, SWT.NONE);
|
---|
137 | btnInsertAfter.addSelectionListener(new SelectionAdapter() {
|
---|
138 | @Override
|
---|
139 | public void widgetSelected(SelectionEvent e) {
|
---|
140 | int index = table.getSelectionIndex();
|
---|
141 | if (index == -1) {
|
---|
142 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
143 | messageBox.setMessage("No event selected!");
|
---|
144 | messageBox.setText("Error");
|
---|
145 | messageBox.open();
|
---|
146 | } else {
|
---|
147 | openInsertDialog(index + 1);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | });
|
---|
151 | btnInsertAfter.setText("Insert After");
|
---|
152 |
|
---|
153 | Button btnClose = new Button(shell, SWT.NONE);
|
---|
154 | btnClose.addSelectionListener(new SelectionAdapter() {
|
---|
155 | @Override
|
---|
156 | public void widgetSelected(SelectionEvent e) {
|
---|
157 | shell.dispose();
|
---|
158 | }
|
---|
159 | });
|
---|
160 | btnClose.setText("Close");
|
---|
161 |
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void updateTableContents() {
|
---|
165 | table.removeAll();
|
---|
166 | for (Event<?> event : sequence) {
|
---|
167 | TableItem tableItem = new TableItem(table, SWT.NONE);
|
---|
168 | String target = event.getTarget();
|
---|
169 | if (target != null) {
|
---|
170 | // the target is split into multiple lines, as one line may
|
---|
171 | // only be 259 characters in tables with Windows
|
---|
172 | target = target.replace("].", "].\n");
|
---|
173 | }
|
---|
174 | tableItem.setText(new String[] { event.getType(), target });
|
---|
175 | }
|
---|
176 | for (int i = 0; i < table.getColumnCount(); i++) {
|
---|
177 | table.getColumn(i).pack();
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | private void openInsertDialog(int position) {
|
---|
182 | InsertAssertionDialog insertDialog = new InsertAssertionDialog(shell,
|
---|
183 | SWT.NONE, targets);
|
---|
184 | Event<?> event = insertDialog.open();
|
---|
185 | if (event != null) {
|
---|
186 | sequence.add(position, event);
|
---|
187 | updateTableContents();
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | }
|
---|