Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08b9424e5c642ab9614d2e36cbd0f01698b2e563 (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
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
package org.eclipse.papyrus.robotml.diagram.common.migration;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gmf.runtime.common.core.command.AbstractCommand;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.infra.gmfdiag.common.reconciler.DiagramReconciler;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils;
import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker;
import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;
import org.eclipse.papyrus.robotml.diagram.common.factory.RobotMLArchitecture2CompositeDiagramFactory;
import org.eclipse.papyrus.robotml.diagram.common.factory.RobotMLComponent2CompositeDiagramFactory;
import org.eclipse.papyrus.robotml.diagram.common.factory.RobotMLDatatype2ClassDiagramFactory;
import org.eclipse.papyrus.robotml.diagram.common.factory.RobotMLInterface2ClassDiagramFactory;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ModelEditPart;
import org.eclipse.papyrus.uml.diagram.composite.edit.parts.CompositeStructureDiagramEditPart;


public class RobotMLReconciler extends DiagramReconciler {

	private static final String OLD_ARCHITECTURE_TYPE = RobotMLArchitecture2CompositeDiagramFactory.OLD_ROBOT_ML_TYPE;

	private static final String OLD_COMPONENT_TYPE = RobotMLComponent2CompositeDiagramFactory.OLD_ROBOT_ML_TYPE;

	private static final String OLD_INTERFACE_TYPE = RobotMLInterface2ClassDiagramFactory.OLD_ROBOT_ML_TYPE;

	private static final String OLD_DATATYPE_TYPE = RobotMLDatatype2ClassDiagramFactory.OLD_ROBOT_ML_TYPE;

	private static final String NEW_CLASS_DIAGRAM = ModelEditPart.MODEL_ID;

	private static final String NEW_COMPOSITE_DIAGRAM = CompositeStructureDiagramEditPart.MODEL_ID;

	private static final MigrationData ARCHITECTURE = new MigrationData(OLD_ARCHITECTURE_TYPE, NEW_COMPOSITE_DIAGRAM, "RobotML Architecture");

	private static final MigrationData COMPONENT = new MigrationData(OLD_COMPONENT_TYPE, NEW_COMPOSITE_DIAGRAM, "RobotML Component");

	private static final MigrationData INTERFACE = new MigrationData(OLD_INTERFACE_TYPE, NEW_CLASS_DIAGRAM, "RobotML Interface");

	private static final MigrationData DATATYPE = new MigrationData(OLD_DATATYPE_TYPE, NEW_CLASS_DIAGRAM, "RobotML Datatype");

	/**
	 * Original code in {@link MigrateDiagramsHandler} uses {@link String#equalsIgnoreCase(String)}.
	 * <p/>
	 * So the keys in this map are old types toLoserCase.
	 */
	private static final Map<String, MigrationData> ourMigrations;

	static {
		ourMigrations = new HashMap<String, MigrationData>();
		for(MigrationData next : new MigrationData[]{ ARCHITECTURE, COMPONENT, INTERFACE, DATATYPE }) {
			ourMigrations.put(safeToLowerCase(next.getOldType()), next);
		}
	}

	@Override
	public ICommand getReconcileCommand(Diagram diagram) {
		MigrationData data = findMigration(diagram);
		return data == null ? null : new RobotMLReconcileCommand(diagram, data);
	}

	/**
	 * Ignores case
	 */
	private MigrationData findMigration(Diagram diagram) {
		return ourMigrations.get(safeToLowerCase(diagram.getType()));
	}

	private static String safeToLowerCase(String text) {
		return text == null ? null : text.toLowerCase();
	}

	private static class RobotMLReconcileCommand extends AbstractCommand {

		private final Diagram myDiagram;

		private final MigrationData myMigrationData;

		public RobotMLReconcileCommand(Diagram diagram, MigrationData data) {
			super("Migrate old RobotML diagram");
			myDiagram = diagram;
			myMigrationData = data;
		}

		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			DiagramUtils.setPrototype(myDiagram, myMigrationData.getPrototype());
			DiagramUtils.setOwner(myDiagram, myDiagram.getElement());
			myDiagram.setType(myMigrationData.getNewType());

			return CommandResult.newOKCommandResult();
		}

		@Override
		public boolean canUndo() {
			return false;
		}

		@Override
		public boolean canRedo() {
			return false;
		}

		@Override
		protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			throw new ExecutionException("Should not be called, canRedo false");
		}

		@Override
		protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			throw new ExecutionException("Should not be called, canUndo false");
		}

	}

	/**
	 * Encapsulates migration scheme from given old RobotML, new UML diagram type and given prototype.
	 * <p/>
	 * Instances of this class are considered invalid if the prototype is not known.
	 */
	private static class MigrationData {

		private ViewPrototype myCachedPrototype;

		private final String myNewType;

		private final String myOldType;

		private final String myPrototypeLabel;

		public MigrationData(String oldType, String newType, String prototypeLabel) {
			myOldType = oldType;
			myNewType = newType;
			myPrototypeLabel = prototypeLabel;
		}

		public String getPrototypeLabel() {
			return myPrototypeLabel;
		}

		public String getOldType() {
			return myOldType;
		}

		public String getNewType() {
			return myNewType;
		}

		public ViewPrototype getPrototype() {
			if(myCachedPrototype == null) {
				myCachedPrototype = findPrototype(myPrototypeLabel);
			}
			return myCachedPrototype;
		}

		private static ViewPrototype findPrototype(String label) {
			for(ViewPrototype proto : PolicyChecker.getCurrent().getAllPrototypes()) {
				if(proto.getLabel().contains(label)) {
					return proto;
				}
			}
			return null;
		}

	}
}

Back to the top