~tapaal-contributor/tapaal/change-export-batch-shortcut-1820012

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
package dk.aau.cs.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map.Entry;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import dk.aau.cs.TCTL.visitors.BooleanResult;
import dk.aau.cs.TCTL.visitors.ContainsSharedPlaceVisitor;
import dk.aau.cs.TCTL.visitors.ContainsSharedTransitionVisitor;
import dk.aau.cs.gui.SharedPlacesAndTransitionsPanel.SharedPlacesListModel;
import dk.aau.cs.gui.SharedPlacesAndTransitionsPanel.SharedTransitionsListModel;
import dk.aau.cs.gui.undo.Command;
import dk.aau.cs.gui.undo.DeleteQueriesCommand;
import dk.aau.cs.gui.undo.DeleteSharedPlaceCommand;
import dk.aau.cs.gui.undo.DeleteSharedTransitionCommand;
import dk.aau.cs.gui.undo.RenameTimedPlaceCommand;
import dk.aau.cs.gui.undo.RenameTimedTransitionCommand;
import dk.aau.cs.gui.undo.UnsharePlaceCommand;
import dk.aau.cs.gui.undo.UnshareTransitionCommand;
import dk.aau.cs.model.tapn.LocalTimedPlace;
import dk.aau.cs.model.tapn.SharedPlace;
import dk.aau.cs.model.tapn.SharedTransition;
import dk.aau.cs.model.tapn.TimedPlace;
import dk.aau.cs.model.tapn.TimedTransition;
import pipe.dataLayer.TAPNQuery;
import pipe.dataLayer.Template;
import pipe.gui.CreateGui;
import pipe.gui.DrawingSurfaceImpl;
import pipe.gui.graphicElements.Arc;
import pipe.gui.graphicElements.tapn.TimedInhibitorArcComponent;
import pipe.gui.graphicElements.tapn.TimedInputArcComponent;
import pipe.gui.graphicElements.tapn.TimedOutputArcComponent;
import pipe.gui.graphicElements.tapn.TimedPlaceComponent;
import pipe.gui.graphicElements.tapn.TimedTransitionComponent;
import pipe.gui.graphicElements.tapn.TimedTransportArcComponent;
import pipe.gui.undo.DeleteTimedInhibitorArcCommand;
import pipe.gui.undo.DeleteTimedInputArcCommand;
import pipe.gui.undo.DeleteTimedOutputArcCommand;
import pipe.gui.undo.DeleteTimedPlaceCommand;
import pipe.gui.undo.DeleteTimedTransitionCommand;
import pipe.gui.undo.DeleteTransportArcCommand;
import pipe.gui.undo.UndoManager;

public class DeleteSharedPlaceOrTransition implements ActionListener{
	
	private static final String TRANSITION_IS_USED_MESSAGE = "<html>The shared transition is used in one or more components.<br/>TAPAAL will unshare all transitions under this name,<br/>but leave the transitions in the components.</html>";
	private static final String PLACE_IS_USED_MESSAGE = "<html>The shared place is used in one or more components.<br/>TAPAAL will unshare all places under this name,<br/>but leave the places in the components.</html>";
	
	JList list;
	SharedPlacesAndTransitionsPanel sharedPlacesAndTransitionsPanel;
	TabContent tab;
	UndoManager undoManager;
	SharedPlacesListModel sharedPlacesListModel;
	SharedTransitionsListModel sharedTransitionsListModel;
	NameGenerator nameGenerator;
	boolean messageShown;
	
	public DeleteSharedPlaceOrTransition(JList list, SharedPlacesAndTransitionsPanel sharedPlacesAndTransitionsPanel, TabContent tab, 
			SharedPlacesListModel sharedPlacesListModel, SharedTransitionsListModel sharedTransitionsListModel, NameGenerator nameGenerator) {
		this.list = list;
		this.sharedPlacesAndTransitionsPanel = sharedPlacesAndTransitionsPanel;
		this.tab = tab;
		undoManager = tab.drawingSurface().getUndoManager();
		this.sharedPlacesListModel = sharedPlacesListModel;
		this.sharedTransitionsListModel = sharedTransitionsListModel;
		this.nameGenerator = nameGenerator;
	}
	
	public DeleteSharedResult showDeleteDialog(List<String> affectedComponents) {
		int result;
		JCheckBox checkBox = new JCheckBox("Delete from all components");

		JLabel label = new JLabel(sharedPlacesAndTransitionsPanel.isDisplayingTransitions() ? TRANSITION_IS_USED_MESSAGE : PLACE_IS_USED_MESSAGE);
		JList listOfComponents = new JList(affectedComponents.toArray());
		JScrollPane scrollPane = new JScrollPane(listOfComponents);
		Object[] params = {label, checkBox, new JLabel("Components affected:"), scrollPane};
		result = JOptionPane.showConfirmDialog(CreateGui.getApp(), params, "Warning", JOptionPane.WARNING_MESSAGE);
		boolean deleteFromTemplates = checkBox.isSelected();
		return new DeleteSharedResult(result, deleteFromTemplates);
	}
	
	public void actionPerformed(ActionEvent arg0) {
		messageShown = false;
		if(list.getSelectedValuesList() != null){
			ArrayList<String> affectedComponents = new ArrayList<String>();
			if(sharedPlacesAndTransitionsPanel.isDisplayingTransitions()){
				for(Object transition : list.getSelectedValuesList()) {
					for(TimedTransition t : ((SharedTransition)transition).transitions()){
						if(!(affectedComponents.contains(t.model().name())))
							affectedComponents.add(t.model().name());
					}
				}
			} else {
				ArrayList<String> affectedComponentsWithDupes = new ArrayList<String>();
				for(Object place : list.getSelectedValuesList()) {
					affectedComponentsWithDupes.addAll(((SharedPlace)place).getComponentsUsingThisPlace());
				}
				for(String component : affectedComponentsWithDupes) {
					if(!(affectedComponents.contains(component))) {
						affectedComponents.add(component);
					}
				}
			}
			
			DeleteSharedResult result = new DeleteSharedResult(JOptionPane.OK_OPTION, false);
			if(!affectedComponents.isEmpty()){
				result = showDeleteDialog(affectedComponents);
			}
				
			if(result.choice == JOptionPane.OK_OPTION){
				undoManager.newEdit();
				Collection<TAPNQuery> affectedQueries = new ArrayList<TAPNQuery>();
				
				if(sharedPlacesAndTransitionsPanel.isDisplayingTransitions()){
					affectedQueries = findAffectedTransitionQueries(list.getSelectedValuesList());
					for(Object transition : list.getSelectedValuesList()) {
						deleteSharedTransition(result.deleteFromTemplates, (SharedTransition) transition, affectedQueries);
					}
						
				}else{
					affectedQueries = findAffectedPlaceQueries(list.getSelectedValuesList());
					for(Object place : list.getSelectedValuesList()) {
						deleteSharedPlace(result.deleteFromTemplates, (SharedPlace) place, affectedQueries);
					}
				}
			}
		}
	}

	private void deleteSharedPlace(boolean deleteFromTemplates, SharedPlace placeToRemove, Collection<TAPNQuery> affectedQueries) {
		SharedPlace sharedPlace = placeToRemove;
		if(affectedQueries.size() > 0 && messageShown == false){
			messageShown = true;
			StringBuffer buffer = new StringBuffer("The following queries contains the shared place and will also be deleted:");
			buffer.append(System.getProperty("line.separator"));
			buffer.append(System.getProperty("line.separator"));
			
			for(TAPNQuery query : affectedQueries){
				buffer.append(query.getName());
				buffer.append(System.getProperty("line.separator"));
			}
			buffer.append(System.getProperty("line.separator"));
			buffer.append("Do you want to continue?");
			int choice = JOptionPane.showConfirmDialog(CreateGui.getApp(), buffer.toString(), "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
			if(choice == JOptionPane.NO_OPTION) return;
			
			Command cmd = new DeleteQueriesCommand(tab, affectedQueries);
			cmd.redo();
			undoManager.addEdit(cmd);
		}
		if(deleteFromTemplates){
			for(Template template : tab.allTemplates()){ // TODO: Get rid of pipe references somehow
				TimedPlaceComponent place = (TimedPlaceComponent)template.guiModel().getPlaceByName(sharedPlace.name());
				if(place != null){
					for(Arc arc : place.getPreset()){
						deleteArc(arc, template);
					}

					for(Arc arc : place.getPostset()){
						deleteArc(arc, template);
					}

					Command cmd = new DeleteTimedPlaceCommand(place, template.model(), template.guiModel(), tab.drawingSurface());
					cmd.redo();
					undoManager.addEdit(cmd);
				}
			}
			tab.drawingSurface().repaint();
			sharedPlacesListModel.removeElement(sharedPlace);
			undoManager.addEdit(new DeleteSharedPlaceCommand(sharedPlace, sharedPlacesListModel));
		}else{
			Hashtable<LocalTimedPlace, String> createdPlaces = new Hashtable<LocalTimedPlace, String>();
			for(Template template : tab.allTemplates()){
				TimedPlace place = template.model().getPlaceByName(sharedPlace.name());
				TimedPlaceComponent component = (TimedPlaceComponent) template.guiModel().getPlaceByName(sharedPlace.name());
				if(place != null){
					String name = nameGenerator.getNewPlaceName(template.model());
					LocalTimedPlace localPlace = new LocalTimedPlace(name);
					createdPlaces.put(localPlace, name);
					Command cmd = new UnsharePlaceCommand(template.model(), sharedPlace, localPlace, component);
					cmd.redo();
					undoManager.addEdit(cmd);
				}
			}
			Command deleteCmd = new DeleteSharedPlaceCommand(sharedPlace, sharedPlacesListModel);
			deleteCmd.redo();
			undoManager.addEdit(deleteCmd);
			
			// We introduced temporary name before, to avoid exceptions, so we rename the places to the correct names here
			for(Entry<LocalTimedPlace, String> entry : createdPlaces.entrySet()){
				Command renameCmd = new RenameTimedPlaceCommand(tab, entry.getKey(), entry.getValue(), sharedPlace.name());
				renameCmd.redo();
				undoManager.addEdit(renameCmd);
			}
		}
	}

	private Collection<TAPNQuery> findAffectedPlaceQueries(List<Object> sharedPlaces) {
		ArrayList<TAPNQuery> queries = new ArrayList<TAPNQuery>();
		for(Object sharedPlace : sharedPlaces) {
			ContainsSharedPlaceVisitor visitor = new ContainsSharedPlaceVisitor(((SharedPlace)sharedPlace).name());
	
			for(TAPNQuery query : tab.queries()){
				BooleanResult result = new BooleanResult();
				query.getProperty().accept(visitor, result);
				if(result.result() && !(queries.contains(query))){
					queries.add(query);
				}
			}
		}
		return queries;
	}
        
	private Collection<TAPNQuery> findAffectedTransitionQueries(List<Object> sharedTransitions) {
		ArrayList<TAPNQuery> queries = new ArrayList<TAPNQuery>();
		for(Object sharedTransition : sharedTransitions) {
			ContainsSharedTransitionVisitor visitor = new ContainsSharedTransitionVisitor(((SharedTransition)sharedTransition).name());
			
			for(TAPNQuery query : tab.queries()){
				BooleanResult result = new BooleanResult();
				query.getProperty().accept(visitor, result);
				if(result.result() && !(queries.contains(query))){
					queries.add(query);
				}
			}
		}
		return queries;
	}
        

	private Command createDeleteArcCommand(Template template, Arc arc, DrawingSurfaceImpl drawingSurface) {
		if(arc instanceof TimedInhibitorArcComponent){
			return new DeleteTimedInhibitorArcCommand((TimedInhibitorArcComponent)arc, template.model(), template.guiModel(), drawingSurface);
		}else if(arc instanceof TimedTransportArcComponent){
			TimedTransportArcComponent component = (TimedTransportArcComponent)arc;
			return new DeleteTransportArcCommand(component, component.underlyingTransportArc(), template.model(), template.guiModel(), drawingSurface);
		}else if(arc instanceof TimedInputArcComponent){
			return new DeleteTimedInputArcCommand((TimedInputArcComponent)arc, template.model(), template.guiModel(), drawingSurface);
		}else{
			return new DeleteTimedOutputArcCommand((TimedOutputArcComponent)arc, template.model(), template.guiModel(), drawingSurface);
		}
	}
	
	private void deleteArc(Arc arc, Template template){
		Command cmd = createDeleteArcCommand(template, arc, tab.drawingSurface()); 
		cmd.redo();
		undoManager.addEdit(cmd);
	}
	
	private void deleteSharedTransition(boolean deleteFromTemplates, SharedTransition transitionToBeRemoved, Collection<TAPNQuery> affectedQueries) {
		SharedTransition sharedTransition = transitionToBeRemoved;
		if(affectedQueries.size() > 0 && !messageShown){
			messageShown = true;
	        StringBuffer buffer = new StringBuffer("The following queries contains the shared transition and will also be deleted:");
	        buffer.append(System.getProperty("line.separator"));
	        buffer.append(System.getProperty("line.separator"));
	
	        for(TAPNQuery query : affectedQueries){
	                buffer.append(query.getName());
	                buffer.append(System.getProperty("line.separator"));
	        }
	        buffer.append(System.getProperty("line.separator"));
	        buffer.append("Do you want to continue?");
	        int choice = JOptionPane.showConfirmDialog(CreateGui.getApp(), buffer.toString(), "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
	        if(choice == JOptionPane.NO_OPTION) return;
	
	        Command cmd = new DeleteQueriesCommand(tab, affectedQueries);
	        cmd.redo();
	        undoManager.addEdit(cmd);
		}
		if(deleteFromTemplates){
			for(Template template : tab.allTemplates()){ // TODO: Get rid of pipe references somehow
				TimedTransitionComponent transition = (TimedTransitionComponent)template.guiModel().getTransitionByName(sharedTransition.name());
				if(transition != null){
					for(Arc arc : transition.getPreset()){
						deleteArc(arc, template);
					}

					for(Arc arc : transition.getPostset()){
						deleteArc(arc, template);
					}

					undoManager.addEdit(new DeleteTimedTransitionCommand(transition, transition.underlyingTransition().model(), template.guiModel(), tab.drawingSurface()));
					transition.delete();
				}
			}
			tab.drawingSurface().repaint();
			sharedTransitionsListModel.removeElement(sharedTransition);
			undoManager.addEdit(new DeleteSharedTransitionCommand(sharedTransition, sharedTransitionsListModel));
		}else{
			Collection<TimedTransition> copy = sharedTransition.transitions();
			for(TimedTransition transition : copy){
				transition.unshare();
				undoManager.addEdit(new UnshareTransitionCommand(sharedTransition, transition));
			}
			sharedTransitionsListModel.removeElement(sharedTransition);
			undoManager.addEdit(new DeleteSharedTransitionCommand(sharedTransition, sharedTransitionsListModel));
			for(TimedTransition transition : copy){
				String name = nameGenerator.getNewTransitionName(transition.model());
				// We add this invisible transition renaming to avoid problems with undo
				undoManager.addEdit(new RenameTimedTransitionCommand(tab, transition, name, transition.name())); 
			}
		}
	}
	
	private class DeleteSharedResult{
		public int choice;
		public boolean deleteFromTemplates;
		
		public DeleteSharedResult(int choice, boolean deleteFromTemplates) {
			this.choice = choice;
			this.deleteFromTemplates = deleteFromTemplates;
		}
	}
}