Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5d78aab4cdf1062cbdc8c260b909c1d12d86df48 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Juergen Haug (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.ui.common.base.refactoring

import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.Multimap
import com.google.inject.Inject
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.IProgressMonitor
import org.eclipse.core.runtime.Path
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.etrice.core.fsm.fSM.ModelComponent
import org.eclipse.etrice.ui.common.base.support.DiagramAccessBase
import org.eclipse.graphiti.mm.pictograms.Diagram
import org.eclipse.ltk.core.refactoring.resource.RenameResourceChange
import org.eclipse.xtext.resource.IReferenceDescription
import org.eclipse.xtext.ui.refactoring.ElementRenameArguments
import org.eclipse.xtext.ui.refactoring.IRefactoringUpdateAcceptor
import org.eclipse.xtext.ui.refactoring.impl.EmfResourceChangeUtil
import org.eclipse.xtext.ui.refactoring.impl.EmfResourceReferenceUpdater

/**
 *  Update diagrams in case of xtext rename refactoring
 */
class DiagramReferenceUpdater extends EmfResourceReferenceUpdater {
	
	@Inject DiagramAccessBase diagramAccess
	@Inject EmfResourceChangeUtil changeUtil

	
	override protected createReferenceUpdates(ElementRenameArguments elementRenameArguments, Multimap<URI, IReferenceDescription> resource2references, ResourceSet resourceSet, IRefactoringUpdateAcceptor updateAcceptor, IProgressMonitor monitor) {
		
		//-- update references in diagrams
		super.createReferenceUpdates(elementRenameArguments, resource2references, resourceSet, updateAcceptor, monitor)
		
		//-- rename diagrams
		val diagramMap = ArrayListMultimap.create => [map |
			// there could be several (orphaned) diagrams for one ActorClass
			resource2references.keySet.map[uri | resourceSet.getResource(uri, false)].map[contents].flatten.filter(Diagram).forEach[diagram |
				map.put(diagram.link.businessObjects.head, diagram)
			]
		]
		elementRenameArguments.renamedElementURIs.map[origURI | resourceSet.getEObject(elementRenameArguments.getNewElementURI(origURI), false)].filter(ModelComponent).forEach[mc |
			val wsDiagrams = diagramMap.get(mc).filter[eResource.URI.isPlatformResource].toList
			val newFileName = diagramAccess.getDigramFileName(mc)
			
			val workspaceRoot = ResourcesPlugin.workspace.root
			val parents = wsDiagrams.groupBy[workspaceRoot.getFile(new Path(eResource.URI.segmentsList.tail.join('/'))).parent]
			parents.forEach[parent, diagrams | 
				// avoid file name conflicts => choose latest modified
				val diagram = diagrams.maxBy[eResource.timeStamp]
				val diagFile = workspaceRoot.getFile(new Path(diagram.eResource.URI.segmentsList.tail.join('/')))
				updateAcceptor.accept(diagram.eResource.URI, new RenameResourceChange(diagFile.fullPath, newFileName))
				// TODO update name of diagram
			]
		]
		
	}
	
}

Back to the top