Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ff447ec3ccd62e60f87df4ac3ca7fd8a1e7db48c (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) 2018 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.nattable.matrix.validator;

import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResourceSet;
import org.eclipse.papyrus.infra.nattable.manager.table.IMatrixTableWidgetManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.EObjectTreeItemAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablecelleditor.GenericRelationshipMatrixCellEditorConfiguration;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablewrapper.EObjectWrapper;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.types.ElementTypeConfiguration;
import org.eclipse.papyrus.infra.types.MetamodelTypeConfiguration;
import org.eclipse.papyrus.infra.ui.emf.dialog.NestedEditingDialogContext;
import org.eclipse.papyrus.uml.nattable.matrix.Activator;
import org.eclipse.papyrus.uml.nattable.matrix.messages.Messages;

/**
 * @author Vincent LORENZO
 *
 *         This class provides a validate method to check the used owner during a relationship's creation. We assume the checked object is compliant with the strategy defined by the user.
 *         If not, the returned message in the returned status will be invalid!!!
 *
 */
public class RelationshipOwnerValidator implements IValidator {


	/**
	 * the cell editor configuration of the current matrix
	 */
	private final GenericRelationshipMatrixCellEditorConfiguration conf;

	/**
	 * Constructor.
	 *
	 */
	public RelationshipOwnerValidator(final IMatrixTableWidgetManager matrixManager) {
		this.conf = (GenericRelationshipMatrixCellEditorConfiguration) matrixManager.getTable().getOwnedCellEditorConfigurations();
	}

	/**
	 * 
	 * @param message
	 *            the message to set in the error IStatus
	 * @return
	 * 		the created IStatus error
	 */
	private final IStatus createErrorStatus(final String message) {
		return new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
	}

	/**
	 * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
	 *
	 * @param value
	 *            a value to validate. We assume the value to check is consistent with the owner strategy mode defined in the {@link GenericRelationshipMatrixCellEditorConfiguration}.
	 *            If not the returned message will be probably invalid!!!!
	 * @return
	 */
	@Override
	public IStatus validate(final Object value) {
		EObject realEObject = null;
		if (value instanceof EObjectWrapper) {
			realEObject = ((EObjectWrapper) value).getElement();
		} else if (value instanceof EObjectTreeItemAxis) {
			realEObject = ((EObjectTreeItemAxis) value).getElement();
		} else if (value instanceof EObject) {
			realEObject = (EObject) value;
		}

		if (null == realEObject) {
			return createErrorStatus(NLS.bind(Messages.RelationshipOwnerValidator_ElementCantBeResolvedAsEObject, value));
		}

		final IStatus relationshipDefinition = hasADefinedRelationship();
		if (false == relationshipDefinition.isOK()) {
			return relationshipDefinition;
		}

		boolean hasAValidFeature = hasFeatureAcceptingRelationShip(realEObject);
		// We assume the value to check is consistent with the owner strategy mode defined in the {@link GenericRelationshipMatrixCellEditorConfiguration}.
		// If not the returned message will be probably invalid!!!!
		if (false == hasAValidFeature) {
			final ILabelProvider labelProvider = getLabelProvider(realEObject);
			final String realObjectLabel = null != labelProvider ? labelProvider.getText(realEObject) : realEObject.toString();

			switch (conf.getRelationshipOwnerStrategy()) {
			case COLUMN_AS_OWNER:
				return createErrorStatus(NLS.bind(Messages.RelationshipOwnerValidator_ColumnCantbeUsedAsOwner, realObjectLabel));
			case COLUMN_OWNER:
				return createErrorStatus(NLS.bind(Messages.RelationshipOwnerValidator_ColumnOwnerCantbeUsedAsOwner, realObjectLabel));
			case DEFAULT:
				// we hope it never appears!
				return createErrorStatus(NLS.bind(Messages.RelationshipOwnerValidator_ItIsAPapyrusBug, realObjectLabel));
			case OTHER:
				return createErrorStatus(NLS.bind(Messages.RelationshipOwnerValidator_TheChosenElementCantBeUsedAsOwner, realObjectLabel));
			case ROW_AS_OWNER:
				return createErrorStatus(NLS.bind(Messages.RelationshipOwnerValidator_RowCantbeUsedAsOwner, realObjectLabel));
			case ROW_OWNER:
				return createErrorStatus(NLS.bind(Messages.RelationshipOwnerValidator_RowOwnerCantbeUsedAsOwner, realObjectLabel));
			case TABLE_CONTEXT:
				return createErrorStatus(NLS.bind(Messages.RelationshipOwnerValidator_TableContextCantbeUsedAsOwner, realObjectLabel));
			default:
				break;
			}
		}
		return Status.OK_STATUS;
	}

	/**
	 * 
	 * @return
	 * 		a IStatus indicating if the relationship is defined and valid or not
	 */
	private final IStatus hasADefinedRelationship() {
		final ElementTypeConfiguration etc = this.conf.getEditedElement();
		if (null == etc) {
			return createErrorStatus(Messages.RelationshipOwnerValidator_NoRelationshipDefined);
		}
		final EClass eclass = ((MetamodelTypeConfiguration) etc).getEClass();
		if (eclass.isAbstract()) {
			return createErrorStatus(Messages.RelationshipOwnerValidator_ChosenRelationshipIsAbstract); // very unlucky case, probably impossible
		}
		return Status.OK_STATUS;
	}


	/**
	 * 
	 * @param wantedOwner
	 *            the wanted owner for the created relationship
	 * @return
	 * 		<code>true</code> if the wanted owner has a feature accepting the created relationship
	 */
	private final boolean hasFeatureAcceptingRelationShip(final EObject wantedOwner) {
		Assert.isNotNull(wantedOwner);
		final ElementTypeConfiguration etc = this.conf.getEditedElement();
		final EClass eclass = ((MetamodelTypeConfiguration) etc).getEClass();
		final EObject dummyRelationship = eclass.getEPackage().getEFactoryInstance().create(eclass);
		for (EStructuralFeature current : wantedOwner.eClass().getEAllContainments()) {
			if (current.getEType().isInstance(dummyRelationship)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * @param eobject
	 *            an eobject
	 * @return
	 * 		the label provider to use
	 */
	private final ILabelProvider getLabelProvider(final EObject eobject) {
		LabelProviderService lpSvc = null;
		try {
			lpSvc = (eobject.eResource() != null)
					? ServiceUtilsForEObject.getInstance().getService(LabelProviderService.class, eobject)
					: ServiceUtilsForResourceSet.getInstance().getService(LabelProviderService.class, NestedEditingDialogContext.getInstance().getResourceSet());
		} catch (ServiceException e) {
			Activator.log.error(e);
		}
		if (null != lpSvc) {
			return lpSvc.getLabelProvider();
		}
		return null;
	}

}

Back to the top