~yrke/tapaal/tapaal-gradlebuild

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package pipe.gui.widgets;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import pipe.dataLayer.TAPNQuery;
import pipe.gui.CreateGui;
import pipe.gui.MessengerImpl;
import pipe.gui.Verifier;
import pipe.gui.undo.AddQueryCommand;
import pipe.gui.undo.RemoveQueriesCommand;
import pipe.gui.undo.UndoManager;
import pipe.gui.widgets.QueryDialog.QueryDialogueOption;
import dk.aau.cs.Messenger;
import dk.aau.cs.gui.BatchProcessingDialog;
import dk.aau.cs.gui.TabContent;
import dk.aau.cs.gui.undo.Command;
import dk.aau.cs.gui.undo.SortQueriesCommand;
import dk.aau.cs.gui.components.NonsearchableJList;
import dk.aau.cs.translations.ReductionOption;
import dk.aau.cs.util.Require;

public class QueryPane extends JPanel {
	private static final long serialVersionUID = 4062539545170994654L;
	private JPanel queryCollectionPanel;
	private JPanel buttonsPanel;
	private DefaultListModel listModel;
	private JList queryList;
	private List<TAPNQuery> selectedQueries;
	private JScrollPane queryScroller;
	private Messenger messenger =  new MessengerImpl();

	private JButton addQueryButton;
	private JButton editQueryButton;
	private JButton verifyButton;

	private JButton removeQueryButton;
	private TabContent tabContent;
	private UndoManager undoManager;
	private JButton moveUpButton;
	private JButton moveDownButton;
	private JButton sortButton;
	private static File tempFile;

	private static final String toolTipNewQuery = "Create a new query";
	private static final String toolTipEditQuery="Edit the selected query";
	private static final String toolTipRemoveQuery="Remove the selected query";
	private static final String toolTipVerifyQuery="Verify the selected query";
	private static final String toolTipSortQueries="Sort the queries alphabetically";
	private final static String toolTipMoveUp = "Move the selected query up; only one query can be moved at a time";
	private final static String toolTipMoveDown = "Move the selected query down; only one query can be moved at a time";

	//private static final String toolTipQueryPane = "Here you can manage queries. Queries can explore properties of the Net.";

	public QueryPane(ArrayList<TAPNQuery> queriesToSet,	TabContent tabContent) {
		this.tabContent = tabContent;
		undoManager = tabContent.drawingSurface().getUndoManager();
		queryCollectionPanel = new JPanel(new GridBagLayout());
		buttonsPanel = new JPanel(new GridBagLayout());
		listModel = new DefaultListModel();

		listModel.addListDataListener(new ListDataListener() {
			public void contentsChanged(ListDataEvent arg0) {
			}

			public void intervalAdded(ListDataEvent arg0) {
				queryList.setSelectedIndex(arg0.getIndex0());
				queryList.ensureIndexIsVisible(arg0.getIndex0());
			}

			public void intervalRemoved(ListDataEvent arg0) {
				int index = (arg0.getIndex0() == 0) ? 0 : (arg0.getIndex0() - 1);
				queryList.setSelectedIndex(index);
				queryList.ensureIndexIsVisible(index);
			}
		});

		queryList = new NonsearchableJList(listModel);
		queryList.setCellRenderer(new QueryCellRenderer());
		queryList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		queryList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if (!(e.getValueIsAdjusting())) {
					queryList.ensureIndexIsVisible(queryList.getSelectedIndex());
					updateQueryButtons();
				}
			}
		});
		queryList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (!queryList.isSelectionEmpty()) {
					if (arg0.getButton() == MouseEvent.BUTTON1
							&& arg0.getClickCount() == 2) {
						int index = queryList.locationToIndex(arg0.getPoint());
						queryList.ensureIndexIsVisible(index);

						showEditDialog();
					}
				}
			}
		});

		addQueriesComponents();
		addButtons();

		setLayout(new BorderLayout());
		this.add(queryCollectionPanel, BorderLayout.CENTER);
		this.add(buttonsPanel, BorderLayout.PAGE_END);
		//this.setToolTipText(toolTipQueryPane);
		setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Queries"), BorderFactory.createEmptyBorder(3, 3, 3, 3)));
		this.setToolTipText("List of verification queries (double click to edit)");

		this.addComponentListener(new ComponentListener() {
			int minimumHegiht = QueryPane.this.getMinimumSize().height;
			public void componentShown(ComponentEvent e) {
			}

			
			public void componentResized(ComponentEvent e) {
				if(QueryPane.this.getSize().height <= minimumHegiht){
					sortButton.setVisible(false);
				} else {
					sortButton.setVisible(true);
				}
			}

			
			public void componentMoved(ComponentEvent e) {
			}

			
			public void componentHidden(ComponentEvent e) {
			}
		});

		this.setMinimumSize(new Dimension(this.getMinimumSize().width, this.getMinimumSize().height - sortButton.getMinimumSize().height));
	}
	
	public void updateQueryButtons() {
		TAPNQuery query = (TAPNQuery)queryList.getSelectedValue();
		if (queryList.getSelectedIndex() == -1 || !query.isActive()) {
			editQueryButton.setEnabled(false);
			verifyButton.setEnabled(false);
			removeQueryButton.setEnabled(false);
		} else {
			editQueryButton.setEnabled(true);
			verifyButton.setEnabled(true);
			removeQueryButton.setEnabled(true);
		}
		int index = queryList.getSelectedIndex();
		if(index > 0 && queryList.getSelectedIndices().length == 1)
			moveUpButton.setEnabled(true);
		else
			moveUpButton.setEnabled(false);


		if(index < listModel.size()-1 && queryList.getSelectedIndices().length == 1)
			moveDownButton.setEnabled(true);
		else
			moveDownButton.setEnabled(false);
	}

	private void addQueriesComponents() {
		queryScroller = new JScrollPane(queryList);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridheight = 3;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		queryCollectionPanel.add(queryScroller, gbc);

		moveUpButton = new JButton(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("resources/Images/Up.png")));
		moveUpButton.setEnabled(false);
		moveUpButton.setToolTipText(toolTipMoveUp);
		moveUpButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = queryList.getSelectedIndex();

				if(index > 0 && queryList.getSelectedIndices().length == 1) {
					swapQueries(index, index-1);
					queryList.setSelectedIndex(index-1);
				}
				else
					moveUpButton.setEnabled(false);
			}
		});

		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.SOUTH;
		queryCollectionPanel.add(moveUpButton,gbc);

		moveDownButton = new JButton(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("resources/Images/Down.png")));
		moveDownButton.setEnabled(false);
		moveDownButton.setToolTipText(toolTipMoveDown);
		moveDownButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = queryList.getSelectedIndex();

				if(index < listModel.size()-1 && queryList.getSelectedIndices().length == 1) {
					swapQueries(index, index+1);
					queryList.setSelectedIndex(index+1);
				}
				else
					moveDownButton.setEnabled(false);
			}
		});

		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.NORTH;
		queryCollectionPanel.add(moveDownButton,gbc);

		//Sort button
		sortButton = new JButton(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("resources/Images/Sort.png")));
		sortButton.setToolTipText(toolTipSortQueries);
		sortButton.setEnabled(true);
		sortButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Command c = new SortQueriesCommand(listModel);
				undoManager.addNewEdit(c);
				c.redo();
			}
		});

		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.NORTH;
		queryCollectionPanel.add(sortButton,gbc);
	}

	private void addButtons() {
		editQueryButton = new JButton("Edit");
		editQueryButton.setEnabled(false);
		editQueryButton.setToolTipText(toolTipEditQuery);
		Dimension dimension = new Dimension(82, 23);
		editQueryButton.setPreferredSize(dimension);
		editQueryButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(queryList.getSelectedIndices().length == 1)
					showEditDialog();
				else
					messenger.displayErrorMessage("It is only possible to edit 1 query at a time. Only verification can be done with multiple queries.");
			}
		});
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.WEST;
		buttonsPanel.add(editQueryButton, gbc);

		verifyButton = new JButton("Verify");
		verifyButton.setEnabled(false);
		verifyButton.setToolTipText(toolTipVerifyQuery);
		verifyButton.setPreferredSize(dimension);
		verifyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				verifyQuery();
			}
		});
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.WEST;
		buttonsPanel.add(verifyButton, gbc);

		removeQueryButton = new JButton("Remove");
		removeQueryButton.setEnabled(false);
		removeQueryButton.setToolTipText(toolTipRemoveQuery);
		removeQueryButton.setPreferredSize(dimension);
		removeQueryButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				removeQueries();
			}
		});
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.WEST;
		buttonsPanel.add(removeQueryButton, gbc);

		addQueryButton = new JButton("New");
		addQueryButton.setPreferredSize(dimension);
		addQueryButton.setToolTipText(toolTipNewQuery);
		addQueryButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {				
				int openCTLDialog = JOptionPane.YES_OPTION;
				boolean netIsUntimed = tabContent.network().isUntimed();
				String optionText = "Do you want to create a CTL query (use for untimed nets) \n or a Reachability query (use for timed nets)?";
				
				// YES_OPTION = CTL dialog, NO_OPTION = Reachability dialog
				Object[] options = {
					"CTL",
					"Reachability"};
				
				TAPNQuery q = null;
				if(netIsUntimed){
					openCTLDialog = JOptionPane.showOptionDialog(CreateGui.getApp(), optionText, "Query Dialog", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
					if(openCTLDialog == JOptionPane.YES_OPTION){
						q = CTLQueryDialog.showQueryDialogue(CTLQueryDialog.QueryDialogueOption.Save, null, tabContent.network(), tabContent.getGuiModels());
					} else if(openCTLDialog == JOptionPane.NO_OPTION){
						q = QueryDialog.showQueryDialogue(QueryDialogueOption.Save, null, tabContent.network(), tabContent.getGuiModels());
					}
				} else{
					q = QueryDialog.showQueryDialogue(QueryDialogueOption.Save, null, tabContent.network(), tabContent.getGuiModels());
				}
				if (q != null) {
					undoManager.addNewEdit(new AddQueryCommand(q, tabContent));
					addQuery(q);
				}
			}
		});

		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.WEST;
		buttonsPanel.add(addQueryButton, gbc);
	}

	private void swapQueries(int currentIndex, int newIndex) {
		TAPNQuery temp = (TAPNQuery)listModel.get(currentIndex);
		listModel.set(currentIndex, listModel.get(newIndex));
		listModel.set(newIndex, temp);
	}
	
	private void removeQueries() {
		undoManager.addNewEdit(new RemoveQueriesCommand((List<TAPNQuery>) queryList.getSelectedValuesList(), tabContent));
		if(listModel.getSize() > 0 && isQueryPossible()){
			for(Object o : queryList.getSelectedValuesList()) {
				listModel.removeElement(o);
			}
			updateQueryButtons();
		}
	}

	public void showEditDialog() {
		int openCTLDialog = JOptionPane.YES_OPTION;
		boolean netIsUntimed = tabContent.network().isUntimed();
		String optionText = "The net is untimed and the query can be converted for the use with untimed CTL engine.\nDo you want to convert the query (recommended answer is yes if you plan to use only untimed net)?";

		// YES_OPTION = CTL dialog, NO_OPTION = Reachability dialog
		Object[] options = {
				"yes",
				"no"};

		TAPNQuery q = (TAPNQuery) queryList.getSelectedValue();
		TAPNQuery newQuery = null;

		if(q.isActive()) {
			if(netIsUntimed && q.getCategory() != TAPNQuery.QueryCategory.CTL){
				openCTLDialog = JOptionPane.showOptionDialog(CreateGui.getApp(), optionText, "Query Dialog", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
				if(openCTLDialog == JOptionPane.YES_OPTION){
					newQuery = CTLQueryDialog.showQueryDialogue(CTLQueryDialog.QueryDialogueOption.Save, q, tabContent.network(), tabContent.getGuiModels());
				} else if(openCTLDialog == JOptionPane.NO_OPTION){
					newQuery = QueryDialog.showQueryDialogue(QueryDialogueOption.Save, q, tabContent.network(), tabContent.getGuiModels());
				}
			} else {
				if(q.getCategory() == TAPNQuery.QueryCategory.CTL) {
					newQuery = CTLQueryDialog.showQueryDialogue(CTLQueryDialog.QueryDialogueOption.Save, q, tabContent.network(), tabContent.getGuiModels());
				} else {
					newQuery = QueryDialog.showQueryDialogue(QueryDialogueOption.Save, q, tabContent.network(), tabContent.getGuiModels());
				}
			}

			if (newQuery != null)
				updateQuery(q, newQuery);
		}
	}

	public void addQuery(TAPNQuery query) {
		listModel.addElement(query);
	}

	private void updateQuery(TAPNQuery oldQuery, TAPNQuery newQuery) {
		newQuery.setActive(oldQuery.isActive());
		listModel.set(listModel.indexOf(oldQuery), newQuery);
	}

	public Iterable<TAPNQuery> getQueries() {
		ArrayList<TAPNQuery> queries = new ArrayList<TAPNQuery>();

		for (int i = 0; i < listModel.size(); ++i) {
			queries.add((TAPNQuery) listModel.get(i));
		}

		return queries;
	}

	public void setQueries(Iterable<TAPNQuery> queries) {
		Require.that(queries != null, "Queries cannot be null");

		listModel.removeAllElements();

		for (TAPNQuery query : queries) {
			listModel.addElement(query);
		}
	}

	public void removeQuery(TAPNQuery queryToRemove) {
		listModel.removeElement(queryToRemove);
	}

	private class QueryCellRenderer extends DefaultListCellRenderer {
		private static final long serialVersionUID = 3071924451912979500L;

		@Override
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
			Component superRenderer = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
			if(value instanceof TAPNQuery)
				setText(((TAPNQuery)value).getName());
			else
				setText("");
			if (isSelected) {
				setBackground(list.getSelectionBackground());
				setForeground(list.getSelectionForeground());
			} else {
				setBackground(list.getBackground());
				setForeground(list.getForeground());
			}

			setEnabled(list.isEnabled() && ((TAPNQuery)value).isActive());
			if(!isEnabled()) 
				setToolTipText("This query is disabled because it contains propositions involving places from a deactivated component");
			else
				setToolTipText("Double-click or press the edit button to edit this query");
			setFont(list.getFont());
			setOpaque(true);
			return superRenderer;
		}
	}

	public void selectFirst() {
		queryList.setSelectedIndex(0);

	}

	private void verifyQuery() {
		TAPNQuery query = (TAPNQuery) queryList.getSelectedValue();
		int NumberOfSelectedElements = queryList.getSelectedIndices().length;
		
		
		if(NumberOfSelectedElements == 1) {
			if(query.getReductionOption() == ReductionOption.VerifyTAPN || query.getReductionOption() == ReductionOption.VerifyTAPNdiscreteVerification || query.getReductionOption() == ReductionOption.VerifyPN)
				Verifier.runVerifyTAPNVerification(tabContent.network(), query, null, this.tabContent.getGuiModels());
			else
				Verifier.runUppaalVerification(tabContent.network(), query);
		}
		else if(NumberOfSelectedElements > 1) {
			saveNetAndRunBatchProcessing();
		}
	}
	
	private void saveNetAndRunBatchProcessing() {
		getSelectedQueriesForProcessing();
		//Saves the net in a temporary file which is used in batchProcessing
		//File is deleted on exit
		try {
			tempFile = File.createTempFile(CreateGui.getAppGui().getCurrentTabName(), ".xml");
			CreateGui.getAppGui().saveNet(CreateGui.getApp().getSelectedTabIndex(), tempFile, selectedQueries);
			BatchProcessingDialog.showBatchProcessingDialog(queryList);
			tempFile.deleteOnExit();
			if(tempFile == null) {
				throw new IOException();
			}
		}catch(IOException e) {
			messenger.displayErrorMessage("Creation of temporary file needed for verification failed.");
		}
	}
	
	private void getSelectedQueriesForProcessing() {
		selectedQueries = queryList.getSelectedValuesList();
	}
	
	public static File getTemporaryFile() {
		return tempFile;
	}
	
	public boolean isQueryPossible() {
		return (queryList.getModel().getSize() > 0 );
	}

	public void verifySelectedQuery() {
		verifyQuery();
	}
}