Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: efa862f775c97fbd2a012604fc7c1435fbd7b498 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*****************************************************************************
 * Copyright (c) 2010, 2015 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Mickael ADAM (ALL4TEC) mickael.adam@all4tec.net - Initial API and Implementation
 * Benoit  Maggi (CEA) - #492073: Reconcilier only for not already BasicCompartment
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.reconciler;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
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.BasicCompartment;
import org.eclipse.gmf.runtime.notation.DecorationNode;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NotationFactory;
import org.eclipse.gmf.runtime.notation.View;

/**
 * Class Diagram Reconciler from 1.1.0 to 1.2.0. Migrates all compartments Views from DecorationNode to BasicCompartment.
 */
abstract public class ReconcilerForCompartment extends DiagramReconciler {


	/**
	 * Gets the reconcile command.
	 *
	 * @param diagram
	 *            the diagram
	 * @return the reconcile command
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.reconciler.DiagramReconciler#getReconcileCommand(org.eclipse.gmf.runtime.notation.Diagram)
	 */
	@Override
	public ICommand getReconcileCommand(Diagram diagram) {
		return new UpdateCompartmentsCommand(diagram);
	}

	/**
	 * Gets the compartments visual id which needs to migrate from DecorationNode to BasicCompartment.
	 *
	 * @return the compartments visual id
	 */
	abstract public List<String> getCompartmentsVisualID();


	/**
	 * The Class UpdateCompartmentsCommand.
	 */
	protected class UpdateCompartmentsCommand extends AbstractCommand {

		/** The diagram. */
		protected final Diagram diagram;

		/**
		 * Instantiates a new update compartments command.
		 *
		 * @param diagram
		 *            the diagram
		 */
		public UpdateCompartmentsCommand(Diagram diagram) {
			super("Update Compartment notation element");
			this.diagram = diagram;
		}

		/**
		 * Do execute with result.
		 *
		 * @param progressMonitor
		 *            the progress monitor
		 * @param info
		 *            the info
		 * @return the command result
		 * @throws ExecutionException
		 *             the execution exception
		 * @see org.eclipse.gmf.runtime.common.core.command.AbstractCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
		 */
		@SuppressWarnings("unchecked")
		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {

			TreeIterator<EObject> allContentIterator = diagram.eAllContents();
			List<DecorationNode> oldComparments = new ArrayList<DecorationNode>();
			
			while (allContentIterator.hasNext()) {
				EObject eObject = allContentIterator.next();
				if (eObject instanceof DecorationNode &&!(eObject instanceof BasicCompartment) && isCompartment((View) eObject)) {
					oldComparments.add((DecorationNode)eObject);
				}
			}
			
			for(DecorationNode oldCompartment : oldComparments) {
					BasicCompartment newCompartment = NotationFactory.eINSTANCE.createBasicCompartment();
					newCompartment.setType(oldCompartment.getType());
					newCompartment.setVisible(oldCompartment.isVisible());
					newCompartment.setMutable(oldCompartment.isMutable());
					newCompartment.setLayoutConstraint(oldCompartment.getLayoutConstraint());
					newCompartment.getStyles().addAll(oldCompartment.getStyles());
					newCompartment.getPersistedChildren().addAll(oldCompartment.getPersistedChildren());
					if (oldCompartment.isSetElement()) {
						newCompartment.setElement(oldCompartment.getElement());
					}
					EcoreUtil.replace(oldCompartment, newCompartment);					
			}

			return CommandResult.newOKCommandResult();
		}

		/**
		 * Can undo.
		 *
		 * @return true, if successful
		 * @see org.eclipse.core.commands.operations.AbstractOperation#canUndo()
		 */
		@Override
		public boolean canUndo() {
			return false;
		}

		/**
		 * Can redo.
		 *
		 * @return true, if successful
		 * @see org.eclipse.core.commands.operations.AbstractOperation#canRedo()
		 */
		@Override
		public boolean canRedo() {
			return false;
		}

		/**
		 * Do redo with result.
		 *
		 * @param progressMonitor
		 *            the progress monitor
		 * @param info
		 *            the info
		 * @return the command result
		 * @throws ExecutionException
		 *             the execution exception
		 * @see org.eclipse.gmf.runtime.common.core.command.AbstractCommand#doRedoWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
		 */
		@Override
		protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			throw new ExecutionException("Should not be called, canRedo false"); //$NON-NLS-1$
		}

		/**
		 * Do undo with result.
		 *
		 * @param progressMonitor
		 *            the progress monitor
		 * @param info
		 *            the info
		 * @return the command result
		 * @throws ExecutionException
		 *             the execution exception
		 * @see org.eclipse.gmf.runtime.common.core.command.AbstractCommand#doUndoWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
		 */
		@Override
		protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			throw new ExecutionException("Should not be called, canUndo false"); //$NON-NLS-1$
		}
	}


	/**
	 * Checks if i's a compartment. to check that it verify on the list of compartment return by {@link getCompartmentsVisualID()}.
	 *
	 * @param view
	 *            the view
	 * @return true, if is compartment
	 */
	protected boolean isCompartment(View view) {
		return getCompartmentsVisualID().contains(view.getType());
	}

}

Back to the top