Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9ed6864026d98e1a2ff89f06cf4c40bd667aea88 (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
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.menu.actions;

import java.util.List;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.papyrus.uml.diagram.common.layout.DistributionConstants;
import org.eclipse.papyrus.uml.diagram.common.layout.LayoutUtils;
import org.eclipse.papyrus.uml.diagram.common.util.Util;

/**
 * 
 * This class provides actions for the objects distribution.
 * 
 */
public class DistributeLinkNodeAction {

	/** the distribution type */
	private int distribution;

	/** the selected elements */
	private List<IGraphicalEditPart> selectedElements;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param parameter
	 *        the distribution parameter
	 * @param selectedElements
	 *        the selected elements for this action
	 */
	public DistributeLinkNodeAction(String parameter, List<IGraphicalEditPart> selectedElements) {
		this.selectedElements = selectedElements;
		this.distribution = getDistributionValue(parameter);
	}

	/**
	 * Return the distribution type
	 * 
	 * @param param
	 *        the parameter of this action
	 * @return
	 *         the distribution type
	 */
	protected int getDistributionValue(String param) {
		if(param.equals(LayoutUtils.HORIZONTALLY)) {
			return DistributionConstants.DISTRIBUTE_H_CONTAINER_INT;
		} else if(param.equals(LayoutUtils.HORIZONTALLY_BETWEEN_NODES)) {
			return DistributionConstants.DISTRIBUTE_H_NODES_INT;
		} else if(param.equals(LayoutUtils.VERTICALLY)) {
			return DistributionConstants.DISTRIBUTE_V_CONTAINER_INT;
		} else if(param.equals(LayoutUtils.VERTICALLY_BETWEEN_NODES)) {
			return DistributionConstants.DISTRIBUTE_V_NODES_INT;
		}
		return PositionConstants.NONE;
	}

	/**
	 * Return the command for this action
	 * 
	 * @return
	 *         The command for this action
	 */
	public Command getCommand() {
		Command command = null;
		AbstractDistributeAction action;
		int selectionType = getSelectionType(selectedElements);
		switch(selectionType) {
		case 1: //affixed child nodes and links selection
			action = new DistributeAffixedChildNodeLinkAction(distribution, selectedElements);
			//action = new DistributeAffixedChildNodeLinkActionV2(distribution, selectedElements);
			command = action.getCommand();
			break;
		case 2: //others nodes selection
			action = new DistributeNodeAction(distribution, selectedElements);
			command = action.getCommand();
			break;
		case 3: //bad selection (nodes and links)
			command = UnexecutableCommand.INSTANCE;
			break;
		default:
			break;
		}
		return command != null ? command : UnexecutableCommand.INSTANCE;
	}


	/**
	 * 
	 * @param selectedElements
	 *        a list of element
	 * @return
	 *         <ul>
	 *         <li>1</li> the list contains links and affixed Child Nodes and editparts
	 *         <li>2</li> the contains others elements
	 *         <li>3</li> the list is a mised between affixed Child Node/Link and others elements
	 *         </ul>
	 */
	private int getSelectionType(List<IGraphicalEditPart> selectedElements) {
		boolean affixedChildNodeAndLink = false;
		boolean otherElement = false;
		boolean badSelection = false;
		for(IGraphicalEditPart current : selectedElements) {
			if(current instanceof DiagramEditPart) {
				badSelection = true;
			} else if(current instanceof ConnectionEditPart) {
				affixedChildNodeAndLink = true;
			} else if(Util.isAffixedChildNode(current)) {
				affixedChildNodeAndLink = true;
			} else {
				otherElement = true;
			}
		}
		if(badSelection) {
			return 3;
		} else if(otherElement != affixedChildNodeAndLink) {
			if(affixedChildNodeAndLink) {
				return 1;
			}
			if(otherElement) {
				return 2;
			}

		} else {
			return 1;
		}
		return 3;
	}

	//Replaced by a new method in util.Util
	//	/**
	//	 * Test if the element is an Affixed Child Node
	//	 * 
	//	 * @param editpart
	//	 * @return
	//	 *         <ul>
	//	 *         <li> <code>true</code> if the editpart is an Affixed Child Node</li>
	//	 *         <li> <code>false</code>if not</li>
	//	 *         </ul>
	//	 */
	//	private boolean isAffixedChildNode(IGraphicalEditPart editpart) {
	//		if(editpart.getParent() instanceof CompartmentEditPart) {
	//			return false;
	//		} else if(editpart.getParent() instanceof DiagramEditPart) {
	//			return false;
	//		}
	//		return true;
	//	}


}

Back to the top