Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d3657be045f8846c449250c7c11ae60830b9e6b8 (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
/*****************************************************************************
 * Copyright (c) 2016 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:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.common.commands;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;

/**
 * This class serves to resize the figure bound of a edit part (at this moment, used only for Port Edit Part)
 * 
 * @author Trung-Truc Nguyen
 *
 */
public class ResizeParentFigureCommand extends AbstractTransactionalCommand {

	private DefaultSizeNodeFigure parent = null;
	private ChangeBoundsRequest request;
	
	//for undo - redo
	private int oldWidth;
	private int oldHeight;
	
	private int newWidth = -1;
	private int newHeight = -1;
	/**
	 * 
	 * Constructor.
	 *
	 * @param domain
	 * @param portFigure the port figure to change its bound
	 * @param iFigure 
	 * @param request
	 */
	public ResizeParentFigureCommand(TransactionalEditingDomain domain, DefaultSizeNodeFigure portFigure, ChangeBoundsRequest request) {
		super(domain, "Resize Full Port Figure", null);
		this.parent = portFigure;
		this.request = request;
		oldHeight = portFigure.getBounds().height;
		oldWidth  = portFigure.getBounds().width;
	}

	/**
	 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
	 *
	 * @param monitor
	 * @param info
	 * @return
	 * @throws ExecutionException
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		
		newWidth = oldWidth + request.getSizeDelta().width;
		newHeight = oldHeight + request.getSizeDelta().height;
				
		parent.getBounds().setSize(newWidth, newHeight);
		parent.getDefaultSize().setSize(newWidth, newHeight);
		
		return CommandResult.newOKCommandResult();
	}

	protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info)
	        throws ExecutionException {

			if(parent != null) {
				parent.getBounds().setSize(oldWidth, oldHeight);
				parent.getDefaultSize().setSize(oldWidth, oldHeight);
			}
			return super.doUndo(monitor, info);
	    }

		/**
	     * Overrides superclass to set the command result.
	     */
	    protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info)
	        throws ExecutionException {

	    	if(parent != null && newHeight != -1) {
				parent.getBounds().setSize(newWidth, newHeight);
				parent.getDefaultSize().setSize(newWidth, newHeight);
			}

	        return super.doRedo(monitor, info);
	    }
	    
}

Back to the top