1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.ui.swt;
|
---|
16 |
|
---|
17 | import java.util.Collection;
|
---|
18 |
|
---|
19 | import org.eclipse.swt.widgets.Dialog;
|
---|
20 | import org.eclipse.swt.widgets.Display;
|
---|
21 | import org.eclipse.swt.widgets.MessageBox;
|
---|
22 | import org.eclipse.swt.widgets.Shell;
|
---|
23 | import org.eclipse.swt.layout.GridLayout;
|
---|
24 | import org.eclipse.swt.widgets.List;
|
---|
25 | import org.eclipse.swt.SWT;
|
---|
26 | import org.eclipse.swt.widgets.Button;
|
---|
27 | import org.eclipse.swt.layout.GridData;
|
---|
28 |
|
---|
29 | import de.ugoe.cs.autoquest.SequenceInstanceOf;
|
---|
30 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
31 | import de.ugoe.cs.autoquest.eventcore.IHierarchicalEventTargetModel;
|
---|
32 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
33 |
|
---|
34 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
35 | import org.eclipse.swt.events.SelectionEvent;
|
---|
36 |
|
---|
37 | public class SequencesDialog extends Dialog {
|
---|
38 |
|
---|
39 | private String sequencesName;
|
---|
40 |
|
---|
41 | private List sequenceList;
|
---|
42 | private Collection<java.util.List<Event>> sequences;
|
---|
43 | private IHierarchicalEventTargetModel<?> eventTargetModel;
|
---|
44 |
|
---|
45 | protected Shell shell;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Create the dialog.
|
---|
49 | *
|
---|
50 | * @param parent
|
---|
51 | * @param style
|
---|
52 | */
|
---|
53 | public SequencesDialog(Shell parent, int style) {
|
---|
54 | super(parent, style);
|
---|
55 | setText("SWT Dialog");
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Open the dialog.
|
---|
60 | */
|
---|
61 | public void open(String sequencesName) {
|
---|
62 | this.sequencesName = sequencesName;
|
---|
63 | sequences = null;
|
---|
64 | createContents();
|
---|
65 | shell.open();
|
---|
66 | shell.layout();
|
---|
67 | Display display = getParent().getDisplay();
|
---|
68 | while (!shell.isDisposed()) {
|
---|
69 | if (!display.readAndDispatch()) {
|
---|
70 | display.sleep();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Create contents of the dialog.
|
---|
77 | */
|
---|
78 | private void createContents() {
|
---|
79 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
|
---|
80 | shell.setSize(248, 299);
|
---|
81 | shell.setText(getText());
|
---|
82 | shell.setLayout(new GridLayout(2, false));
|
---|
83 |
|
---|
84 | sequenceList = new List(shell, SWT.BORDER | SWT.V_SCROLL);
|
---|
85 | sequenceList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
---|
86 | updateSequenceList();
|
---|
87 |
|
---|
88 | Button btnShow = new Button(shell, SWT.NONE);
|
---|
89 | btnShow.addSelectionListener(new SelectionAdapter() {
|
---|
90 | @Override
|
---|
91 | public void widgetSelected(SelectionEvent e) {
|
---|
92 | int index = sequenceList.getSelectionIndex();
|
---|
93 | if (index == -1) {
|
---|
94 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
95 | messageBox.setMessage("No sequence selected!");
|
---|
96 | messageBox.setText("Error");
|
---|
97 | messageBox.open();
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | EditSequenceDialog editSequenceDialog =
|
---|
101 | new EditSequenceDialog(shell, SWT.NONE, eventTargetModel);
|
---|
102 | int counter = 0;
|
---|
103 | java.util.List<Event> selectedSequence = null;
|
---|
104 | for (java.util.List<Event> sequence : sequences) {
|
---|
105 | if (counter == index) {
|
---|
106 | selectedSequence = sequence;
|
---|
107 | break;
|
---|
108 | }
|
---|
109 | counter++;
|
---|
110 | }
|
---|
111 | editSequenceDialog.open(selectedSequence);
|
---|
112 | updateSequenceList();
|
---|
113 | }
|
---|
114 | }
|
---|
115 | });
|
---|
116 | btnShow.setText("Show");
|
---|
117 |
|
---|
118 | Button btnClose = new Button(shell, SWT.NONE);
|
---|
119 | btnClose.addSelectionListener(new SelectionAdapter() {
|
---|
120 | @Override
|
---|
121 | public void widgetSelected(SelectionEvent e) {
|
---|
122 | shell.dispose();
|
---|
123 | }
|
---|
124 | });
|
---|
125 | btnClose.setText("Close");
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | @SuppressWarnings("unchecked")
|
---|
130 | private void updateSequenceList() {
|
---|
131 | sequenceList.removeAll();
|
---|
132 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
|
---|
133 | if (SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
134 | sequences = (Collection<java.util.List<Event>>) dataObject;
|
---|
135 | int seqDigits = Integer.toString(sequences.size()).length();
|
---|
136 | int counter = 1;
|
---|
137 | for (java.util.List<Event> sequence : sequences) {
|
---|
138 | String seqName =
|
---|
139 | "#" + String.format("%0" + seqDigits + "d", counter) + ": " + sequence.size();
|
---|
140 | sequenceList.add(seqName);
|
---|
141 | counter++;
|
---|
142 | }
|
---|
143 | Object targetObject =
|
---|
144 | GlobalDataContainer.getInstance().getData(sequencesName + "_targets");
|
---|
145 | eventTargetModel = null;
|
---|
146 | if (targetObject instanceof IHierarchicalEventTargetModel) {
|
---|
147 | eventTargetModel = (IHierarchicalEventTargetModel<?>) targetObject;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | else {
|
---|
151 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
|
---|
152 | messageBox.setMessage("Internal error. Sequences object not of expected type!");
|
---|
153 | messageBox.setText("Error");
|
---|
154 | messageBox.open();
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | }
|
---|