1 | package de.ugoe.cs.eventbench.swing;
|
---|
2 |
|
---|
3 | import java.awt.EventQueue;
|
---|
4 | import java.awt.Rectangle;
|
---|
5 |
|
---|
6 | import javax.swing.DefaultListModel;
|
---|
7 | import javax.swing.JFrame;
|
---|
8 | import javax.swing.JButton;
|
---|
9 | import javax.swing.JScrollBar;
|
---|
10 |
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import javax.swing.JList;
|
---|
14 | import java.util.List;
|
---|
15 | import javax.swing.ListSelectionModel;
|
---|
16 | import javax.swing.border.SoftBevelBorder;
|
---|
17 | import javax.swing.border.BevelBorder;
|
---|
18 | import javax.swing.event.ListSelectionListener;
|
---|
19 | import javax.swing.event.ListSelectionEvent;
|
---|
20 |
|
---|
21 | import de.ugoe.cs.eventbench.data.Event;
|
---|
22 |
|
---|
23 | import javax.swing.JLabel;
|
---|
24 | import java.awt.event.WindowAdapter;
|
---|
25 | import java.awt.event.WindowEvent;
|
---|
26 | import javax.swing.JScrollPane;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.border.EtchedBorder;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * <p>
|
---|
32 | * This class provides a dialog to have a look on the events of the sequence,
|
---|
33 | * that was selected in {@link DlgSequences}. Furthermore, assertions can be
|
---|
34 | * added.
|
---|
35 | * </p>
|
---|
36 | *
|
---|
37 | * @author Jeffrey Hall
|
---|
38 | * @version 1.0
|
---|
39 | */
|
---|
40 | public class DlgSequenceDetails {
|
---|
41 |
|
---|
42 | private JFrame frmEvents;
|
---|
43 | private JFrame frmParent;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>
|
---|
47 | * Create the dialog.
|
---|
48 | * </p>
|
---|
49 | *
|
---|
50 | * @param parent
|
---|
51 | * the parent window of type {@link DlgSequences}.
|
---|
52 | * @param events
|
---|
53 | * list of events that is to be displayed.
|
---|
54 | */
|
---|
55 | public DlgSequenceDetails(JFrame parent, final List<Event<?>> events) {
|
---|
56 | frmParent = parent;
|
---|
57 | initialize(events);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * <p>
|
---|
62 | * Launch the dialog.
|
---|
63 | * </p>
|
---|
64 | *
|
---|
65 | * @param parent
|
---|
66 | * the parent window of type {@link DlgSequences}.
|
---|
67 | * @param events
|
---|
68 | * list of events that is to be displayed.
|
---|
69 | */
|
---|
70 | public void showDialog(JFrame parent, final List<Event<?>> events) {
|
---|
71 | frmParent = parent;
|
---|
72 |
|
---|
73 | EventQueue.invokeLater(new Runnable() {
|
---|
74 | public void run() {
|
---|
75 | try {
|
---|
76 | DlgSequenceDetails window = new DlgSequenceDetails(
|
---|
77 | frmParent, events);
|
---|
78 | window.frmEvents.setVisible(true);
|
---|
79 | } catch (Exception e) {
|
---|
80 | e.printStackTrace();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | });
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * <p>
|
---|
88 | * Initialize the contents of the frame.
|
---|
89 | * </p>
|
---|
90 | *
|
---|
91 | * @param events
|
---|
92 | * list of events that is to be displayed.
|
---|
93 | */
|
---|
94 | private void initialize(final List<Event<?>> events) {
|
---|
95 |
|
---|
96 | final DefaultListModel modelListEvents = new DefaultListModel();
|
---|
97 | final DefaultListModel modelListTargets = new DefaultListModel();
|
---|
98 | final JList listEvents = new JList(modelListEvents);
|
---|
99 | final JList listTargets = new JList(modelListTargets);
|
---|
100 |
|
---|
101 | frmEvents = new JFrame();
|
---|
102 | frmEvents.setTitle("Sequence details");
|
---|
103 | frmEvents.setBounds(100, 100, 731, 589);
|
---|
104 | frmEvents.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
---|
105 | frmEvents.getContentPane().setLayout(null);
|
---|
106 |
|
---|
107 | // before closing the window, set parent to visible
|
---|
108 | frmEvents.addWindowListener(new WindowAdapter() {
|
---|
109 | public void windowClosing(WindowEvent arg0) {
|
---|
110 | frmParent.setVisible(true);
|
---|
111 | frmEvents.dispose();
|
---|
112 | }
|
---|
113 | });
|
---|
114 |
|
---|
115 | JPanel panel = new JPanel();
|
---|
116 | panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
|
---|
117 | panel.setBounds(10, 498, 705, 52);
|
---|
118 | frmEvents.getContentPane().add(panel);
|
---|
119 | panel.setLayout(null);
|
---|
120 |
|
---|
121 | final JButton btnInsertBefore = new JButton("Insert before");
|
---|
122 | btnInsertBefore.setEnabled(false);
|
---|
123 | btnInsertBefore.setBounds(10, 11, 135, 31);
|
---|
124 | panel.add(btnInsertBefore);
|
---|
125 |
|
---|
126 | final JButton btnInsertAfter = new JButton("Insert after");
|
---|
127 | btnInsertAfter.setEnabled(false);
|
---|
128 | btnInsertAfter.setBounds(155, 11, 135, 31);
|
---|
129 | panel.add(btnInsertAfter);
|
---|
130 |
|
---|
131 | updateLists(events, modelListEvents, modelListTargets, listEvents,
|
---|
132 | listTargets);
|
---|
133 |
|
---|
134 | // listener for clicking the "Insert before" button
|
---|
135 | btnInsertBefore.addMouseListener(new MouseAdapter() {
|
---|
136 | public void mouseClicked(MouseEvent arg0) {
|
---|
137 | if (!btnInsertBefore.isEnabled())
|
---|
138 | return;
|
---|
139 |
|
---|
140 | addAssertion(events, modelListEvents, modelListTargets,
|
---|
141 | listEvents, listTargets, true);
|
---|
142 | }
|
---|
143 | });
|
---|
144 |
|
---|
145 | // listener for clicking the "Insert after" button
|
---|
146 | btnInsertAfter.addMouseListener(new MouseAdapter() {
|
---|
147 | public void mouseClicked(MouseEvent arg0) {
|
---|
148 | if (!btnInsertAfter.isEnabled())
|
---|
149 | return;
|
---|
150 |
|
---|
151 | addAssertion(events, modelListEvents, modelListTargets,
|
---|
152 | listEvents, listTargets, false);
|
---|
153 | }
|
---|
154 | });
|
---|
155 |
|
---|
156 | final JButton btnClose = new JButton("Back to sequences");
|
---|
157 | btnClose.setBounds(544, 11, 150, 31);
|
---|
158 | panel.add(btnClose);
|
---|
159 |
|
---|
160 | JPanel panel_1 = new JPanel();
|
---|
161 | panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
|
---|
162 | panel_1.setBounds(10, 11, 705, 476);
|
---|
163 | frmEvents.getContentPane().add(panel_1);
|
---|
164 | panel_1.setLayout(null);
|
---|
165 |
|
---|
166 | final JScrollPane scrollPaneEvents = new JScrollPane();
|
---|
167 | scrollPaneEvents.setBounds(10, 29, 209, 436);
|
---|
168 | panel_1.add(scrollPaneEvents);
|
---|
169 | scrollPaneEvents.setViewportView(listEvents);
|
---|
170 |
|
---|
171 | listEvents.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null,
|
---|
172 | null, null, null));
|
---|
173 | listEvents.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
174 |
|
---|
175 | final JScrollPane scrollPaneTargets = new JScrollPane();
|
---|
176 | scrollPaneTargets.setBounds(229, 29, 466, 436);
|
---|
177 | panel_1.add(scrollPaneTargets);
|
---|
178 |
|
---|
179 | // if there are more events than the list is able to display without
|
---|
180 | // vertical scrollbar, the event list has to be resized to fit to the
|
---|
181 | // target list
|
---|
182 | Rectangle r = scrollPaneEvents.getBounds();
|
---|
183 | r.height -= 18;
|
---|
184 | if (scrollPaneEvents.getVerticalScrollBar().isVisible()) {
|
---|
185 | scrollPaneEvents.setBounds(r);
|
---|
186 | }
|
---|
187 |
|
---|
188 | // listener for selecting a target: select the fitting event
|
---|
189 | listTargets.addListSelectionListener(new ListSelectionListener() {
|
---|
190 | public void valueChanged(ListSelectionEvent arg0) {
|
---|
191 | if (listTargets.getSelectedIndex() > -1) {
|
---|
192 |
|
---|
193 | changeSelection(listEvents, listTargets, btnInsertBefore,
|
---|
194 | btnInsertAfter, scrollPaneEvents, scrollPaneTargets);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | });
|
---|
198 | scrollPaneTargets.setViewportView(listTargets);
|
---|
199 |
|
---|
200 | listTargets.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null,
|
---|
201 | null, null, null));
|
---|
202 | listTargets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
203 |
|
---|
204 | JLabel lblEvents = new JLabel("Events:");
|
---|
205 | lblEvents.setBounds(10, 11, 46, 14);
|
---|
206 | panel_1.add(lblEvents);
|
---|
207 |
|
---|
208 | JLabel lblTargets = new JLabel("Targets:");
|
---|
209 | lblTargets.setBounds(229, 11, 58, 14);
|
---|
210 | panel_1.add(lblTargets);
|
---|
211 |
|
---|
212 | // listener for selecting an event: select the fitting target
|
---|
213 | listEvents.addListSelectionListener(new ListSelectionListener() {
|
---|
214 | public void valueChanged(ListSelectionEvent arg0) {
|
---|
215 | if (listEvents.getSelectedIndex() > -1) {
|
---|
216 |
|
---|
217 | changeSelection(listTargets, listEvents, btnInsertBefore,
|
---|
218 | btnInsertAfter, scrollPaneTargets, scrollPaneEvents);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | });
|
---|
222 |
|
---|
223 | // before closing the window, set parent to visible
|
---|
224 | btnClose.addMouseListener(new MouseAdapter() {
|
---|
225 | public void mouseClicked(MouseEvent arg0) {
|
---|
226 | frmParent.setVisible(true);
|
---|
227 | frmEvents.dispose();
|
---|
228 | }
|
---|
229 | });
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * <p>
|
---|
234 | * the value of one of the lists changed so the other has to be set to the
|
---|
235 | * fitting selection.
|
---|
236 | * </p>
|
---|
237 | *
|
---|
238 | * @param listValueHasToBeChanged
|
---|
239 | * the selection of this list has to be corrected.
|
---|
240 | * @param listValueHasBeenChanged
|
---|
241 | * the selection of this list is already correct.
|
---|
242 | * @param btnInsertBefore
|
---|
243 | * to enable the "Insert before" button.
|
---|
244 | * @param btnInsertAfter
|
---|
245 | * to enable the "Insert after" button.
|
---|
246 | * @param scrollPaneValueHasToBeChanged
|
---|
247 | * the position of the scrollBar of this scrollPane has to be
|
---|
248 | * corrected.
|
---|
249 | * @param scrollPaneValueHasBeenChanged
|
---|
250 | * the position of the scrollBar of this scrollPane is already
|
---|
251 | * correct.
|
---|
252 | */
|
---|
253 | private void changeSelection(final JList listValueHasToBeChanged,
|
---|
254 | final JList listValueHasBeenChanged, final JButton btnInsertBefore,
|
---|
255 | final JButton btnInsertAfter,
|
---|
256 | final JScrollPane scrollPaneValueHasToBeChanged,
|
---|
257 | final JScrollPane scrollPaneValueHasBeenChanged) {
|
---|
258 |
|
---|
259 | JScrollBar bar1 = scrollPaneValueHasBeenChanged.getVerticalScrollBar();
|
---|
260 | JScrollBar bar2 = scrollPaneValueHasToBeChanged.getVerticalScrollBar();
|
---|
261 | bar2.setValue(bar1.getValue());
|
---|
262 |
|
---|
263 | listValueHasToBeChanged.setSelectedIndex(listValueHasBeenChanged
|
---|
264 | .getSelectedIndex());
|
---|
265 |
|
---|
266 | btnInsertBefore.setEnabled(true);
|
---|
267 | btnInsertAfter.setEnabled(true);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * <p>
|
---|
272 | * updates both lists, eventList and targetList to display the current
|
---|
273 | * status.
|
---|
274 | * </p>
|
---|
275 | *
|
---|
276 | * @param events
|
---|
277 | * list of the current events that has to be displayed.
|
---|
278 | * @param modelListEvents
|
---|
279 | * DefaultListModel to display the events.
|
---|
280 | * @param modelListTargets
|
---|
281 | * DefaultListModel to display the targets.
|
---|
282 | * @param listEvents
|
---|
283 | * the listEvents to store and reset the selection.
|
---|
284 | * @param listTargets
|
---|
285 | * the listTargets to reset the selection after updating.
|
---|
286 | */
|
---|
287 | private void updateLists(final List<Event<?>> events,
|
---|
288 | final javax.swing.DefaultListModel modelListEvents,
|
---|
289 | final javax.swing.DefaultListModel modelListTargets,
|
---|
290 | JList listEvents, JList listTargets) {
|
---|
291 |
|
---|
292 | int selectedIndex = listEvents.getSelectedIndex();
|
---|
293 |
|
---|
294 | modelListEvents.clear();
|
---|
295 | modelListTargets.clear();
|
---|
296 | for (int i = 0; i < events.size(); i++) {
|
---|
297 | modelListEvents.addElement(events.get(i).getType());
|
---|
298 | modelListTargets.addElement(events.get(i).getTarget());
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (selectedIndex > -1) {
|
---|
302 | listEvents.setSelectedIndex(selectedIndex);
|
---|
303 | listTargets.setSelectedIndex(selectedIndex);
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * <p>
|
---|
309 | * opens {@link DlgInsert} dialog to insert an assertion and updates the
|
---|
310 | * lists.
|
---|
311 | * </p>
|
---|
312 | *
|
---|
313 | * @param events
|
---|
314 | * list of the current events.
|
---|
315 | * @param modelListEvents
|
---|
316 | * needed to call updateList.
|
---|
317 | * @param modelListTargets
|
---|
318 | * needed to call updateList.
|
---|
319 | * @param listEvents
|
---|
320 | * needed to get the currently selected index and to update the
|
---|
321 | * lists.
|
---|
322 | * @param listTargets
|
---|
323 | * needed to update the lists.
|
---|
324 | * @param insertBefore
|
---|
325 | * to decide if the assertions has to be inserted before or after
|
---|
326 | * the current selection.
|
---|
327 | */
|
---|
328 | private void addAssertion(final List<Event<?>> events,
|
---|
329 | final DefaultListModel modelListEvents,
|
---|
330 | final DefaultListModel modelListTargets, final JList listEvents,
|
---|
331 | final JList listTargets, boolean insertBefore) {
|
---|
332 |
|
---|
333 | int selectedIndex = listEvents.getSelectedIndex();
|
---|
334 | DlgInsert.showDialog(events, selectedIndex, insertBefore);
|
---|
335 | updateLists(events, modelListEvents, modelListTargets, listEvents,
|
---|
336 | listTargets);
|
---|
337 | }
|
---|
338 | }
|
---|