~tapaal-contributor/tapaal/unselect-objects-after-undo-1894108

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
package pipe.gui.widgets;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.event.CaretListener;
import net.tapaal.swinghelpers.GridBagHelper;
import dk.aau.cs.gui.undo.*;
import net.tapaal.swinghelpers.WidthAdjustingComboBox;
import pipe.gui.CreateGui;
import pipe.gui.graphicElements.tapn.TimedTransitionComponent;
import dk.aau.cs.gui.Context;
import dk.aau.cs.model.tapn.Bound;
import dk.aau.cs.model.tapn.SharedTransition;
import dk.aau.cs.model.tapn.TimedInhibitorArc;
import dk.aau.cs.model.tapn.TimedInputArc;
import dk.aau.cs.model.tapn.TimedOutputArc;
import dk.aau.cs.model.tapn.TimedTransition;
import dk.aau.cs.model.tapn.TransportArc;
import dk.aau.cs.util.RequireException;

import static net.tapaal.swinghelpers.GridBagHelper.Fill;
import static net.tapaal.swinghelpers.GridBagHelper.Anchor;

public class TAPNTransitionEditor extends JPanel {

	private static final String untimed_preset_warning = "Incoming arcs to urgent transitions must have the interval [0,inf).";
	private static final String transport_destination_invariant_warning = "Transport arcs going through urgent transitions cannot have an invariant at the destination.";
	private final TimedTransitionComponent transition;
	private final JRootPane rootPane;
	private final Context context;
	
	private final int maxNumberOfTransitionsToShowAtOnce = 20;
	boolean doNewEdit = true;

	public TAPNTransitionEditor(JRootPane _rootPane, TimedTransitionComponent _transition, Context context) {
		rootPane = _rootPane;
		transition = _transition;
		this.context = context;
		initComponents();
        hideTimedInformation();
		rootPane.setDefaultButton(okButton);
	}

	private void hideTimedInformation(){
	    if(!transition.isTimed()) {
            urgentCheckBox.setVisible(false);
        }
    }

	private void initComponents() {
		GridBagConstraints gridBagConstraints;

		transitionEditorPanel = new JPanel();
		nameLabel = new JLabel();
		nameTextField = new JTextField();
		nameTextField.setPreferredSize(new Dimension(290,27));
		rotationLabel = new JLabel();
		rotationComboBox = new JComboBox<>();
		buttonPanel = new JPanel();
		cancelButton = new JButton();
		makeSharedButton = new JButton();
		okButton = new JButton();
		sharedCheckBox = new JCheckBox("Shared");
		urgentCheckBox = new JCheckBox("Urgent");
		uncontrollableCheckBox = new JCheckBox("Uncontrollable");
		attributesCheckBox = new JCheckBox("Show transition name");


		sharedTransitionsComboBox = new WidthAdjustingComboBox<>(maxNumberOfTransitionsToShowAtOnce);
		sharedTransitionsComboBox.setPreferredSize(new Dimension(290,27));
		sharedTransitionsComboBox.addActionListener(e -> {
			if(((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).transitions().isEmpty()){
                ((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).setUrgent(urgentCheckBox.isSelected());
                ((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).setUncontrollable(uncontrollableCheckBox.isSelected());
			}else{
                urgentCheckBox.setSelected(((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).isUrgent());
                uncontrollableCheckBox.setSelected(((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).isUncontrollable());
			}
		});

		setLayout(new java.awt.GridBagLayout());

		transitionEditorPanel.setLayout(new java.awt.GridBagLayout());
		transitionEditorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Transition Editor"));

		sharedCheckBox.addActionListener(arg0 -> {
            JCheckBox box = (JCheckBox)arg0.getSource();
            if(box.isSelected()){
                switchToNameDropDown();
                makeSharedButton.setEnabled(false);
            }else{
                switchToNameTextField();
                nameTextField.setText(transition.underlyingTransition().isShared()?
                CreateGui.getDrawingSurface().getNameGenerator().getNewTransitionName(context.activeModel()) : transition.getName());
                makeSharedButton.setEnabled(true);
            }
        });
		gridBagConstraints = GridBagHelper.as(2, 1, Anchor.WEST, new Insets(3, 3, 3, 3));
		transitionEditorPanel.add(sharedCheckBox, gridBagConstraints);	
		
		
		makeSharedButton = new JButton();
		makeSharedButton.setText("Make shared");
		makeSharedButton.setMaximumSize(new java.awt.Dimension(110, 25));
		makeSharedButton.setMinimumSize(new java.awt.Dimension(110, 25));
		makeSharedButton.setPreferredSize(new java.awt.Dimension(110, 25));
		
		makeSharedButton.addActionListener(evt -> {
            makeNewShared = true;
            if(okButtonHandler(evt)){
                makeSharedButton.setEnabled(false);
                sharedCheckBox.setEnabled(true);
                sharedCheckBox.setSelected(true);
                setupInitialState();
            }
            makeNewShared = false;
        });
		
		gridBagConstraints = GridBagHelper.as(3,1, Anchor.WEST, new Insets(5, 5, 5, 5));
		transitionEditorPanel.add(makeSharedButton, gridBagConstraints);
		
		nameLabel.setText("Name:");
		gridBagConstraints = GridBagHelper.as(0,1, Anchor.EAST, new Insets(3, 3, 3, 3));
		transitionEditorPanel.add(nameLabel, gridBagConstraints);

		nameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
			@Override
			public void focusGained(java.awt.event.FocusEvent evt) {
				nameTextFieldFocusGained(evt);
			}

			@Override
			public void focusLost(java.awt.event.FocusEvent evt) {
				nameTextFieldFocusLost(evt);
			}
		});
		
		gridBagConstraints = GridBagHelper.as(2, 2, Anchor.WEST, new Insets(3, 3, 3, 3));
		transitionEditorPanel.add(urgentCheckBox, gridBagConstraints);
		
		urgentCheckBox.addActionListener(e -> {
			if(!isUrgencyOK()){
				urgentCheckBox.setSelected(false);
			}
		});

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        if (context.tabContent().getLens().isGame()) {
            transitionEditorPanel.add(uncontrollableCheckBox, gridBagConstraints);

            uncontrollableCheckBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    JCheckBox box = (JCheckBox) arg0.getSource();
                    uncontrollableCheckBox.setSelected(box.isSelected());
                }
            });
        }
	
		rotationLabel.setText("Rotate:");
		gridBagConstraints = GridBagHelper.as(0,2, Anchor.NORTH, new Insets(3, 3, 3, 3));
		transitionEditorPanel.add(rotationLabel, gridBagConstraints);

		gridBagConstraints = GridBagHelper.as(1,2, Anchor.NORTHWEST, new Insets(3, 3, 3, 3));
		transitionEditorPanel.add(rotationComboBox, gridBagConstraints);

		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
		add(transitionEditorPanel, gridBagConstraints);

		buttonPanel.setLayout(new java.awt.GridBagLayout());

		okButton.setText("OK");
		okButton.setMaximumSize(new java.awt.Dimension(100, 25));
		okButton.setMinimumSize(new java.awt.Dimension(100, 25));
		okButton.setPreferredSize(new java.awt.Dimension(100, 25));
		okButton.addActionListener(evt -> {
			if(okButtonHandler(evt)){
				exit();
			}
		});

		cancelButton.setText("Cancel");
		cancelButton.setMaximumSize(new java.awt.Dimension(100, 25));
		cancelButton.setMinimumSize(new java.awt.Dimension(100, 25));
		cancelButton.setPreferredSize(new java.awt.Dimension(100, 25));
		cancelButton.addActionListener(this::cancelButtonHandler);
		
		gridBagConstraints = GridBagHelper.as(0,1, Anchor.EAST, new Insets(3, 3, 3, 3));
		buttonPanel.add(cancelButton, gridBagConstraints);

		

		gridBagConstraints = GridBagHelper.as(1,1, Anchor.WEST, new Insets(3, 3, 3, 3));
		buttonPanel.add(okButton, gridBagConstraints);

		gridBagConstraints = GridBagHelper.as(0,3, Anchor.EAST, new Insets(5, 0, 8, 3));
		add(buttonPanel, gridBagConstraints);

		attributesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
		attributesCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
		gridBagConstraints = GridBagHelper.as(1,3, Anchor.WEST, new Insets(3, 3, 3, 3));
		transitionEditorPanel.add(attributesCheckBox, gridBagConstraints);
		
		setupInitialState();

	}	
	
	private void setupInitialState(){
		sharedTransitions = new Vector<>(context.network().sharedTransitions());
		ArrayList<SharedTransition> usedTransitions = new ArrayList<SharedTransition>();
		
		for (TimedTransition tt : context.activeModel().transitions()){
			if(tt.isShared()){
				usedTransitions.add(tt.sharedTransition());
			}
		}
		
		sharedTransitions.removeAll(usedTransitions);
		if (transition.underlyingTransition().isShared()){
			sharedTransitions.add(transition.underlyingTransition().sharedTransition());
		}
		
		sharedTransitions.sort((o1, o2) -> o1.name().compareToIgnoreCase(o2.name()));
		
		rotationComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] {"0\u00B0", "+45\u00B0", "+90\u00B0", "-45\u00B0" }));
		nameTextField.setText(transition.getName());
		sharedTransitionsComboBox.setModel(new DefaultComboBoxModel<>(sharedTransitions));
		sharedCheckBox.setEnabled(sharedTransitions.size() > 0 && !hasArcsToSharedPlaces(transition.underlyingTransition()));
		urgentCheckBox.setSelected(transition.isUrgent());
		uncontrollableCheckBox.setSelected(transition.isUncontrollable());

		if(transition.underlyingTransition().isShared()){
			switchToNameDropDown();
			sharedCheckBox.setSelected(true);
			sharedTransitionsComboBox.setSelectedItem(transition.underlyingTransition().sharedTransition());
		}else{
			switchToNameTextField();
		}
		makeSharedButton.setEnabled(!sharedCheckBox.isSelected() && !hasArcsToSharedPlaces(transition.underlyingTransition()));
		attributesCheckBox.setSelected(transition.getAttributesVisible());
	}
	
	private boolean hasArcsToSharedPlaces(TimedTransition underlyingTransition) {
		for(TimedInputArc arc : context.activeModel().inputArcs()){
			if(arc.destination().equals(underlyingTransition) && arc.source().isShared()) return true;
		}
		
		for(TimedOutputArc arc : context.activeModel().outputArcs()){
			if(arc.source().equals(underlyingTransition) && arc.destination().isShared()) return true;
		}
		
		for(TransportArc arc : context.activeModel().transportArcs()){
			if(arc.transition().equals(underlyingTransition) && arc.source().isShared()) return true;
			if(arc.transition().equals(underlyingTransition) && arc.destination().isShared()) return true;
		}
		
		for(TimedInhibitorArc arc : context.activeModel().inhibitorArcs()){
			if(arc.destination().equals(underlyingTransition) && arc.source().isShared()) return true;
		}
		
		return false;
	}

	protected void switchToNameTextField() {
		transitionEditorPanel.remove(sharedTransitionsComboBox);
		GridBagConstraints gbc = GridBagHelper.as(1,1, Fill.HORIZONTAL, new Insets(3, 3, 3, 3));
		urgentCheckBox.setSelected(transition.isUrgent());
		uncontrollableCheckBox.setSelected(transition.isUncontrollable());
		transitionEditorPanel.add(nameTextField, gbc);
		transitionEditorPanel.validate();
		transitionEditorPanel.repaint();
	}

	protected void switchToNameDropDown() {
		transitionEditorPanel.remove(nameTextField);
		GridBagConstraints gbc = GridBagHelper.as(1,1,Fill.HORIZONTAL,new Insets(3, 3, 3, 3));

		transitionEditorPanel.add(sharedTransitionsComboBox, gbc);
		if(((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).transitions().isEmpty()){
            ((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).setUrgent(urgentCheckBox.isSelected());
            ((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).setUncontrollable(uncontrollableCheckBox.isSelected());
        }else{
            urgentCheckBox.setSelected(((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).isUrgent());
            uncontrollableCheckBox.setSelected(((SharedTransition)sharedTransitionsComboBox.getSelectedItem()).isUncontrollable());
		}
    		transitionEditorPanel.validate();
		transitionEditorPanel.repaint();
	}

	private void nameTextFieldFocusLost(java.awt.event.FocusEvent evt) {
		focusLost(nameTextField);
	}
	
	private void nameTextFieldFocusGained(java.awt.event.FocusEvent evt) {
		focusGained(nameTextField);
	}

	private void focusGained(JTextField textField) {
		textField.setCaretPosition(0);
		textField.moveCaretPosition(textField.getText().length());
	}

	private void focusLost(JTextField textField) {
		textField.setCaretPosition(0);
	}

	CaretListener caretListener = evt -> {
		JTextField textField = (JTextField) evt.getSource();
		textField.setBackground(new Color(255, 255, 255));
		// textField.removeChangeListener(this);
	};
	
	private boolean isUrgencyOK(){
		if(!transition.hasUntimedPreset()){
			JOptionPane.showMessageDialog(transitionEditorPanel, untimed_preset_warning, "Error", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		for(TransportArc arc : transition.underlyingTransition().getTransportArcsGoingThrough()){
			if(arc.destination().invariant().upperBound() != Bound.Infinity){
				JOptionPane.showMessageDialog(transitionEditorPanel, transport_destination_invariant_warning, "Error", JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}
		return true;
	}

	private boolean okButtonHandler(java.awt.event.ActionEvent evt) {
		String newName = nameTextField.getText();
		
		// Check urgent constrain
		if(urgentCheckBox.isSelected() && !isUrgencyOK()){
			return false;
		}
		//Only do new edit if it has not already been done
		if(doNewEdit) {
			context.undoManager().newEdit(); // new "transaction""
			doNewEdit = false;
		}
		
		boolean wasShared = transition.underlyingTransition().isShared() && !sharedCheckBox.isSelected();
		if(transition.underlyingTransition().isShared()){
			context.undoManager().addEdit(new UnshareTransitionCommand(transition.underlyingTransition().sharedTransition(), transition.underlyingTransition()));
			transition.underlyingTransition().unshare();
		}
		
		if(sharedCheckBox.isSelected()){
			SharedTransition selectedTransition = (SharedTransition)sharedTransitionsComboBox.getSelectedItem();
            Command command = new MakeTransitionSharedCommand(context.activeModel(), selectedTransition, transition.underlyingTransition(), context.tabContent());
			context.undoManager().addEdit(command);
			try{
				command.redo();
			}catch(RequireException e){
				context.undoManager().undo();
				JOptionPane.showMessageDialog(this,"Another transition in the same component is already shared under that name", "Error", JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}else{		
			if(transition.underlyingTransition().model().isNameUsed(newName) && (wasShared || !transition.underlyingTransition().name().equalsIgnoreCase(newName))){
				context.undoManager().undo(); 
				JOptionPane.showMessageDialog(this,
						"The specified name is already used by another place or transition.",
						"Error", JOptionPane.ERROR_MESSAGE);
				return false;
			}
			try{
				String oldName = transition.underlyingTransition().name();
				transition.underlyingTransition().setName(newName);
				Command renameCommand = new RenameTimedTransitionCommand(context.tabContent(), transition.underlyingTransition(), oldName, newName);
				context.undoManager().addEdit(renameCommand);
				// set name
				renameCommand.redo();
			}catch(RequireException e){
				context.undoManager().undo(); 
				JOptionPane.showMessageDialog(this,
						"Acceptable names for transitions are defined by the regular expression:\n[a-zA-Z][_a-zA-Z0-9]*",
						"Error", JOptionPane.ERROR_MESSAGE);
				return false;
			}
			context.nameGenerator().updateIndices(transition.underlyingTransition().model(), newName);
		
			
			if(makeNewShared){
				Command command = new MakeTransitionNewSharedCommand(context.activeModel(), newName, transition.underlyingTransition(), context.tabContent(), false);
				context.undoManager().addEdit(command);
				try{
					command.redo();
				}catch(RequireException e){
					context.undoManager().undo();
					//This is checked as a transition cannot be shared if there exists a place with the same name
					if(transition.underlyingTransition().model().parentNetwork().isNameUsedForTransitionsOnly(newName)) {
						int dialogResult = JOptionPane.showConfirmDialog(this, "A transition with the specified name already exists in one or more components, or the specified name is invalid.\n\nAcceptable names for transitions are defined by the regular expression:\n[a-zA-Z][_a-zA-Z0-9]*\n\nNote that \"true\" and \"false\" are reserved keywords. \n\nThis transition name will be changed into shared one also in all other components.", "Error", JOptionPane.OK_CANCEL_OPTION);
						if(dialogResult == JOptionPane.OK_OPTION) {
							Command cmd = new MakeTransitionNewSharedMultiCommand(context, newName, transition);	
							cmd.redo();
							context.undoManager().addEdit(cmd);
						} else {
							return false;
						}
					} else {
						JOptionPane.showMessageDialog(this, "A place with the specified name already exists in one or more components, or the specified name is invalid.\n\nAcceptable names for transitions are defined by the regular expression:\n[a-zA-Z][_a-zA-Z0-9]*\n\nNote that \"true\" and \"false\" are reserved keywords.", "Error", JOptionPane.ERROR_MESSAGE);
						return false;
					}
				}
				transition.setUrgent(urgentCheckBox.isSelected());
				transition.setUncontrollable(uncontrollableCheckBox.isSelected());
			}
		}
		
		if(transition.isUrgent() != urgentCheckBox.isSelected()){
			context.undoManager().addEdit(new ToggleTransitionUrgent(transition.underlyingTransition(), context.tabContent()));
			transition.setUrgent(urgentCheckBox.isSelected());
		}
        if(transition.isUncontrollable() != uncontrollableCheckBox.isSelected()){
            context.undoManager().addEdit(new ToggleTransitionUncontrollable(transition.underlyingTransition(), context.tabContent()));
            transition.setUncontrollable(uncontrollableCheckBox.isSelected());
        }

		int rotationIndex = rotationComboBox.getSelectedIndex();
		if (rotationIndex > 0) {
			int angle = 0;
			switch (rotationIndex) {
			case 1:
				angle = 45;
				break;
			case 2:
				angle = 90;
				break;
			case 3:
				angle = 135; // -45
				break;
			default:
				break;
			}
			if (angle != 0) {
				context.undoManager().addEdit(transition.rotate(angle));
			}
		}
		
		if(transition.getAttributesVisible() && !attributesCheckBox.isSelected() || (!transition.getAttributesVisible() && attributesCheckBox.isSelected())) {
			transition.toggleAttributesVisible();
		}
		
		transition.update(true);
		
		return true;
	}

	private void exit() {
		rootPane.getParent().setVisible(false);
	}

	private void cancelButtonHandler(java.awt.event.ActionEvent evt) {
		exit();
	}

	private JPanel buttonPanel;
	private JButton cancelButton;
	private JLabel nameLabel;
	private JTextField nameTextField;
	private JButton okButton;
	private JButton makeSharedButton;
	private javax.swing.JComboBox<String> rotationComboBox;
	private JLabel rotationLabel;
	private JPanel transitionEditorPanel;
	private javax.swing.JCheckBox sharedCheckBox;
	private javax.swing.JComboBox<SharedTransition> sharedTransitionsComboBox;

	private javax.swing.JCheckBox urgentCheckBox;
    private Vector<SharedTransition> sharedTransitions;

    private javax.swing.JCheckBox uncontrollableCheckBox;

	private boolean makeNewShared = false;
	private javax.swing.JCheckBox attributesCheckBox;
}