Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5e6639ff67448b908ca844d867a09daf77ec6a7d (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
/*****************************************************************************
 * Copyright (c) 2013 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:
 *  Camille Letavernier (camille.letavernier@cea.fr) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.table.cell;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.papyrus.infra.nattable.manager.cell.AbstractCellManager;
import org.eclipse.papyrus.infra.nattable.manager.cell.ICellManager;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.FeatureIdAxis;
import org.eclipse.papyrus.uml.tools.commands.SetMultiplicityCommand;
import org.eclipse.papyrus.uml.tools.util.MultiplicityParser;
import org.eclipse.papyrus.views.properties.table.axis.DerivedUMLPropertiesAxisManager;
import org.eclipse.uml2.uml.MultiplicityElement;


public class DerivedUMLPropertiesCellManager extends AbstractCellManager implements ICellManager {

	public final static String CELL_MANAGER_ID = "org.eclipse.papyrus.uml.nattable.derived.features.cell.manager"; //$NON-NLS-1$

	/**
	 * {@inheritDoc}
	 */
	public boolean handles(Object columnElement, Object rowElement) {
		return getMultiplicityElement(columnElement, rowElement) != null && getMultiplicityHandler(columnElement, rowElement) != null;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean handlesAxisElement(Object obj) {
		return DerivedUMLPropertiesAxisManager.MULTIPLICITY.equals(obj);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Object doGetValue(Object columnElement, Object rowElement, INattableModelManager tableManager) {
		MultiplicityElement element = getMultiplicityElement(columnElement, rowElement);
		if(element == null) {
			return null;
		}

		return getValue(element);
	}

	protected Object getValue(MultiplicityElement multiplicityElement) {
		int lower = multiplicityElement.getLower(), upper = multiplicityElement.getUpper();
		return MultiplicityParser.getMultiplicity(lower, upper);
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean isCellEditable(Object columnElement, Object rowElement) {
		return getMultiplicityElement(columnElement, rowElement) != null;
	}

	/**
	 * {@inheritDoc}
	 */
	public Command getSetValueCommand(EditingDomain domain, Object columnElement, Object rowElemenet, Object newValue) {

		MultiplicityElement element = getMultiplicityElement(columnElement, rowElemenet);

		if(element == null) {
			return null;
		}

		if(newValue == null || newValue instanceof String) {
			return getSetValueCommand(element, (String)newValue);
		}

		return null;
	}

	protected MultiplicityElement getMultiplicityElement(Object obj1, Object obj2) {
		if(obj1 instanceof MultiplicityElement) {
			return (MultiplicityElement)obj1;
		}
		if(obj2 instanceof MultiplicityElement) {
			return (MultiplicityElement)obj2;
		}

		return null;
	}

	private String getMultiplicityHandler(Object obj1, Object obj2) {
		String featureId = getFeatureId(obj1);
		if(featureId != null) {
			return featureId;
		}

		featureId = getFeatureId(obj2);
		if(featureId != null) {
			return featureId;
		}
		return null;
	}

	private String getFeatureId(Object object) {
		if(object instanceof FeatureIdAxis) {
			FeatureIdAxis idAxis = (FeatureIdAxis)object;
			if(DerivedUMLPropertiesAxisManager.MULTIPLICITY.equals(idAxis.getElement())) {
				return DerivedUMLPropertiesAxisManager.MULTIPLICITY;
			}
		}
		return null;
	}

	protected Command getSetValueCommand(MultiplicityElement element, String newValue) {
		return new SetMultiplicityCommand(element, newValue);
	}

}

Back to the top