Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ad47a39aad6dcd0abaa071eb569beff1690b276b (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
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
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 * 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:
 *		
 *		CEALIST - Initial API and implementation (Adapted code from DeferredLayoutCommand)
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.commands;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
import org.eclipse.gef.SnapToHelper;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.ui.editparts.BorderedBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.util.EditPartUtil;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.snap.BorderNodeSnapHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.snap.NodeSnapHelper;

/**
 * This command is used to snap editparts on the grid, when only the view
 * adapters are available at the time of creating the command. It is necessary
 * to have the editparts when creating a snap command so this command defers
 * the creation of the layout command until execution time at which point it can
 * get the editparts from the editpart registry using the view adapters.
 * 
 * @author vlorenzo
 */
//TODO : creation from the palette should use me
//TODO : move action could use me???
public class DeferredSnapToGridCommand extends AbstractTransactionalCommand {

	/** the IAdaptables from which an View can be retrieved */
	protected List<?> viewAdapters;

	/** the diagram editpart used to get the editpart registry */
	protected IGraphicalEditPart containerEP;

	/**
	 * Constructor for <code>DefferedSnapToGridCommand</code>.
	 * 
	 * @param editingDomain
	 *        the editing domain through which model changes are made
	 * @param viewAdapters
	 *        the IAdaptables from which an IView can be retrieved
	 * @param containerEP
	 *        the container editpart used to get the editpart registry
	 */
	public DeferredSnapToGridCommand(TransactionalEditingDomain editingDomain, List<?> viewAdapters, IGraphicalEditPart containerEP) {
		super(editingDomain, "Deferred Snap to grid command", null); //$NON-NLS-1$
		this.viewAdapters = viewAdapters;
		this.containerEP = containerEP;
	}

	/**
	 * 
	 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#getAffectedFiles()
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getAffectedFiles() {
		if(containerEP != null) {
			View view = (View)containerEP.getModel();
			if(view != null) {
				IFile f = WorkspaceSynchronizer.getFile(view.eResource());
				return f != null ? Collections.singletonList(f) : Collections.EMPTY_LIST;
			}
		}
		return super.getAffectedFiles();
	}

	/**
	 * Executes a layout command with all the editparts for the view adapters.
	 * 
	 */
	protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {

		final RunnableWithResult<List<IGraphicalEditPart>> refreshRunnable = new RunnableWithResult<List<IGraphicalEditPart>>() {

			private IStatus status;

			private List<IGraphicalEditPart> result;

			public List<IGraphicalEditPart> getResult() {
				return result;
			}

			public void setStatus(IStatus status) {
				this.status = status;
			}

			public IStatus getStatus() {
				return status;
			}

			public void run() {
				containerEP.refresh();

				// The layout command requires that the figure world is updated.
				getContainerFigure().invalidate();
				getContainerFigure().validate();

				List<IGraphicalEditPart> editParts = new ArrayList<IGraphicalEditPart>(viewAdapters.size());
				Map<?, ?> epRegistry = containerEP.getRoot().getViewer().getEditPartRegistry();
				for(Iterator<?> iter = viewAdapters.iterator(); iter.hasNext();) {
					IAdaptable ad = (IAdaptable)iter.next();
					View view = (View)ad.getAdapter(View.class);
					Object ep = epRegistry.get(view);
					if(ep instanceof IGraphicalEditPart) {
						editParts.add((IGraphicalEditPart)ep);
					}
				}

				if(editParts.isEmpty()) {
					result = editParts;
					return;
				}

				//probably useless
				//				Set<IGraphicalEditPart> layoutSet = new HashSet<IGraphicalEditPart>(editParts.size());
				//				layoutSet.addAll(editParts);
				//
				//				// refresh source and target connections of any shapes in the container not being considered for layout
				//				Iterator<?> iter = containerEP.getChildren().iterator();
				//				while(iter.hasNext()) {
				//					Object obj = iter.next();
				//					if(!layoutSet.contains(obj) && obj instanceof IGraphicalEditPart) {
				//						IGraphicalEditPart ep = (IGraphicalEditPart)obj;
				//						ep.refresh();
				//					}
				//				}

				result = editParts;
			}
		};

		EditPartUtil.synchronizeRunnableToMainThread(containerEP, refreshRunnable);
		List<IGraphicalEditPart> editParts = refreshRunnable.getResult();
		if(editParts == null || editParts.isEmpty()) {
			return CommandResult.newOKCommandResult();
		}

		//	add an arrange command, to layout the related shapes
		CompoundCommand cc = new CompoundCommand("Snap Command"); //$NON-NLS-1$
		for(final IGraphicalEditPart current : editParts) {
			final SnapToHelper snapHelper = (SnapToHelper)((IAdaptable)current).getAdapter(SnapToHelper.class);
			final NodeSnapHelper nodeSnapHelper;
			final PrecisionRectangle boundsFigure = new PrecisionRectangle(((GraphicalEditPart)current).getFigure().getBounds());
			current.getFigure().translateToAbsolute(boundsFigure);
			final PrecisionRectangle result = new PrecisionRectangle(boundsFigure);
			if(current instanceof BorderedBorderItemEditPart) {
				nodeSnapHelper = new BorderNodeSnapHelper(snapHelper, boundsFigure);
			} else {
				nodeSnapHelper = new NodeSnapHelper(snapHelper, boundsFigure);
			}
			ChangeBoundsRequest request = new ChangeBoundsRequest("move"); //$NON-NLS-1$
			request.setEditParts(Collections.singletonList(current));
			request.setSnapToEnabled(true);
			request.setLocation(boundsFigure.getLocation());
			nodeSnapHelper.snapPoint(request);
			request.setLocation(result.getLocation());
			cc.add(((GraphicalEditPart)current).getCommand(request));
		}


		if(!cc.isEmpty() && cc.canExecute()) {
			cc.execute();
		}
		return CommandResult.newOKCommandResult();
	}

	/**
	 * 
	 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#cleanup()
	 * 
	 */
	protected void cleanup() {
		containerEP = null;//for garbage collection
		viewAdapters = null;
		super.cleanup();
	}

	/**
	 * gets the container edit part's figure
	 * 
	 * @return the container figure
	 */
	protected IFigure getContainerFigure() {
		return containerEP.getFigure();
	}

	/**
	 * gets the container edit part
	 * 
	 * @return the container edit part
	 */
	protected IGraphicalEditPart getContainerEP() {
		return containerEP;
	}

	/**
	 * gets a list of <code>IAdaptable</code> that can adapt to <code>
	 * View</code>
	 * 
	 * @return view adapters
	 */
	protected List<?> getViewAdapters() {
		return viewAdapters;
	}

	/**
	 * 
	 * @see org.eclipse.core.commands.operations.AbstractOperation#canExecute()
	 * 
	 * @return
	 */
	@Override
	public boolean canExecute() {
		return super.canExecute() && containerEP != null;
	}
}

Back to the top