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 Object result;
|
---|
32 | protected Shell shell;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Create the dialog.
|
---|
36 | * @param parent
|
---|
37 | * @param style
|
---|
38 | */
|
---|
39 | public SequencesDialog(Shell parent, int style) {
|
---|
40 | super(parent, style);
|
---|
41 | setText("SWT Dialog");
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Open the dialog.
|
---|
46 | * @return the result
|
---|
47 | */
|
---|
48 | public Object open(String sequencesName) {
|
---|
49 | this.sequencesName = sequencesName;
|
---|
50 | sequences = null;
|
---|
51 | createContents();
|
---|
52 | shell.open();
|
---|
53 | shell.layout();
|
---|
54 | Display display = getParent().getDisplay();
|
---|
55 | while (!shell.isDisposed()) {
|
---|
56 | if (!display.readAndDispatch()) {
|
---|
57 | display.sleep();
|
---|
58 | }
|
---|
59 | }
|
---|
60 | return result;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Create contents of the dialog.
|
---|
65 | */
|
---|
66 | private void createContents() {
|
---|
67 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
|
---|
68 | shell.setSize(248, 299);
|
---|
69 | shell.setText(getText());
|
---|
70 | shell.setLayout(new GridLayout(2, false));
|
---|
71 |
|
---|
72 | sequenceList = new List(shell, SWT.BORDER | SWT.V_SCROLL);
|
---|
73 | sequenceList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
---|
74 | updateSequenceList();
|
---|
75 |
|
---|
76 | Button btnShow = new Button(shell, SWT.NONE);
|
---|
77 | btnShow.addSelectionListener(new SelectionAdapter() {
|
---|
78 | @Override
|
---|
79 | public void widgetSelected(SelectionEvent e) {
|
---|
80 | int index = sequenceList.getSelectionIndex();
|
---|
81 | if( index==-1 ) {
|
---|
82 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
83 | messageBox.setMessage("No sequence selected!");
|
---|
84 | messageBox.setText("Error");
|
---|
85 | messageBox.open();
|
---|
86 | } else {
|
---|
87 | EditSequenceDialog editSequenceDialog = new EditSequenceDialog(shell, SWT.NONE, targets);
|
---|
88 | int counter = 0;
|
---|
89 | java.util.List<Event<?>> selectedSequence = null;
|
---|
90 | for( java.util.List<Event<?>> sequence : sequences ) {
|
---|
91 | if( counter==index ) {
|
---|
92 | selectedSequence = sequence;
|
---|
93 | break;
|
---|
94 | }
|
---|
95 | counter++;
|
---|
96 | }
|
---|
97 | editSequenceDialog.open(selectedSequence);
|
---|
98 | updateSequenceList();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | });
|
---|
102 | btnShow.setText("Show");
|
---|
103 |
|
---|
104 | Button btnClose = new Button(shell, SWT.NONE);
|
---|
105 | btnClose.addSelectionListener(new SelectionAdapter() {
|
---|
106 | @Override
|
---|
107 | public void widgetSelected(SelectionEvent e) {
|
---|
108 | shell.dispose();
|
---|
109 | }
|
---|
110 | });
|
---|
111 | btnClose.setText("Close");
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | @SuppressWarnings("unchecked")
|
---|
116 | private void updateSequenceList() {
|
---|
117 | sequenceList.removeAll();
|
---|
118 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
|
---|
119 | if( SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
120 | sequences = (Collection<java.util.List<Event<?>>>) dataObject;
|
---|
121 | int seqDigits = 4; // TODO calculate seqDigits dynamically
|
---|
122 | int counter = 1;
|
---|
123 | for( java.util.List<Event<?>> sequence : sequences ) {
|
---|
124 | String seqName = "#"+Integer.toString(counter, seqDigits)+": "+sequence.size();
|
---|
125 | sequenceList.add(seqName);
|
---|
126 | counter++;
|
---|
127 | }
|
---|
128 | Object targetObject = GlobalDataContainer.getInstance().getData(sequencesName+"_targets");
|
---|
129 | targets = null;
|
---|
130 | if( targetObject instanceof SortedSet ) {
|
---|
131 | if( !((SortedSet<?>) targetObject).isEmpty() ) {
|
---|
132 | if( ((SortedSet<?>) targetObject).first() instanceof String ) {
|
---|
133 | targets = (SortedSet<String>) targetObject;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | if( targets==null ) {
|
---|
138 | targets = new TreeSet<String>();
|
---|
139 | for( java.util.List<Event<?>> sequence : sequences ) {
|
---|
140 | for( Event<?> event : sequence ) {
|
---|
141 | String target = event.getTarget();
|
---|
142 | if( target!=null ) {
|
---|
143 | targets.add(target);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | } else {
|
---|
149 | // TODO this should not happen - needs to be handled anyways
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | }
|
---|