1 | package de.ugoe.cs.eventbench.swt;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.SortedSet;
|
---|
5 | import java.util.TreeSet;
|
---|
6 |
|
---|
7 | import org.eclipse.swt.widgets.Dialog;
|
---|
8 | import org.eclipse.swt.widgets.Display;
|
---|
9 | import org.eclipse.swt.widgets.MessageBox;
|
---|
10 | import org.eclipse.swt.widgets.Shell;
|
---|
11 | import org.eclipse.swt.layout.GridLayout;
|
---|
12 | import org.eclipse.swt.widgets.List;
|
---|
13 | import org.eclipse.swt.SWT;
|
---|
14 | import org.eclipse.swt.widgets.Button;
|
---|
15 | import org.eclipse.swt.layout.GridData;
|
---|
16 |
|
---|
17 | import de.ugoe.cs.eventbench.SequenceInstanceOf;
|
---|
18 | import de.ugoe.cs.eventbench.data.Event;
|
---|
19 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
20 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
21 | import org.eclipse.swt.events.SelectionEvent;
|
---|
22 |
|
---|
23 | public class SequencesDialog extends Dialog {
|
---|
24 |
|
---|
25 | private String sequencesName;
|
---|
26 |
|
---|
27 | private List sequenceList;
|
---|
28 | private Collection<java.util.List<Event<?>>> sequences;
|
---|
29 | private SortedSet<String> targets;
|
---|
30 |
|
---|
31 | protected Shell shell;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create the dialog.
|
---|
35 | * @param parent
|
---|
36 | * @param style
|
---|
37 | */
|
---|
38 | public SequencesDialog(Shell parent, int style) {
|
---|
39 | super(parent, style);
|
---|
40 | setText("SWT Dialog");
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Open the dialog.
|
---|
45 | */
|
---|
46 | public void open(String sequencesName) {
|
---|
47 | this.sequencesName = sequencesName;
|
---|
48 | sequences = null;
|
---|
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 | }
|
---|
59 |
|
---|
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(248, 299);
|
---|
66 | shell.setText(getText());
|
---|
67 | shell.setLayout(new GridLayout(2, false));
|
---|
68 |
|
---|
69 | sequenceList = new List(shell, SWT.BORDER | SWT.V_SCROLL);
|
---|
70 | sequenceList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
---|
71 | updateSequenceList();
|
---|
72 |
|
---|
73 | Button btnShow = new Button(shell, SWT.NONE);
|
---|
74 | btnShow.addSelectionListener(new SelectionAdapter() {
|
---|
75 | @Override
|
---|
76 | public void widgetSelected(SelectionEvent e) {
|
---|
77 | int index = sequenceList.getSelectionIndex();
|
---|
78 | if( index==-1 ) {
|
---|
79 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
80 | messageBox.setMessage("No sequence selected!");
|
---|
81 | messageBox.setText("Error");
|
---|
82 | messageBox.open();
|
---|
83 | } else {
|
---|
84 | EditSequenceDialog editSequenceDialog = new EditSequenceDialog(shell, SWT.NONE, targets);
|
---|
85 | int counter = 0;
|
---|
86 | java.util.List<Event<?>> selectedSequence = null;
|
---|
87 | for( java.util.List<Event<?>> sequence : sequences ) {
|
---|
88 | if( counter==index ) {
|
---|
89 | selectedSequence = sequence;
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | counter++;
|
---|
93 | }
|
---|
94 | editSequenceDialog.open(selectedSequence);
|
---|
95 | updateSequenceList();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | });
|
---|
99 | btnShow.setText("Show");
|
---|
100 |
|
---|
101 | Button btnClose = new Button(shell, SWT.NONE);
|
---|
102 | btnClose.addSelectionListener(new SelectionAdapter() {
|
---|
103 | @Override
|
---|
104 | public void widgetSelected(SelectionEvent e) {
|
---|
105 | shell.dispose();
|
---|
106 | }
|
---|
107 | });
|
---|
108 | btnClose.setText("Close");
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | @SuppressWarnings("unchecked")
|
---|
113 | private void updateSequenceList() {
|
---|
114 | sequenceList.removeAll();
|
---|
115 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
|
---|
116 | if( SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
117 | sequences = (Collection<java.util.List<Event<?>>>) dataObject;
|
---|
118 | int seqDigits = Integer.toString(sequences.size()).length();
|
---|
119 | int counter = 1;
|
---|
120 | for( java.util.List<Event<?>> sequence : sequences ) {
|
---|
121 | String seqName = "#"+String.format("%0"+seqDigits+"d", counter)+": "+sequence.size();
|
---|
122 | sequenceList.add(seqName);
|
---|
123 | counter++;
|
---|
124 | }
|
---|
125 | Object targetObject = GlobalDataContainer.getInstance().getData(sequencesName+"_targets");
|
---|
126 | targets = null;
|
---|
127 | if( targetObject instanceof SortedSet ) {
|
---|
128 | if( !((SortedSet<?>) targetObject).isEmpty() ) {
|
---|
129 | if( ((SortedSet<?>) targetObject).first() instanceof String ) {
|
---|
130 | targets = (SortedSet<String>) targetObject;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | if( targets==null ) {
|
---|
135 | targets = new TreeSet<String>();
|
---|
136 | for( java.util.List<Event<?>> sequence : sequences ) {
|
---|
137 | for( Event<?> event : sequence ) {
|
---|
138 | String target = event.getTarget();
|
---|
139 | if( target!=null ) {
|
---|
140 | targets.add(target);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
147 | messageBox.setMessage("Internal error. Sequences object not of expected type!");
|
---|
148 | messageBox.setText("Error");
|
---|
149 | messageBox.open();
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | }
|
---|