blob: dc9c6277be92eda91b4f5548b7cfac02f3cc5ab4 [file] [log] [blame]
/*****************************************************************************
* Copyright (c) 2019 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Xavier Le Pallec (for CEA LIST) xlepallec@lilo.org - Bug 558456
*
*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.clazz.lf.classtextualedition.mapping.java2js;
import java.util.List;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.PapyrusCompartmentEditPart;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.ResizeableListCompartmentEditPart;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassAttributeCompartmentEditPart;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassAttributeCompartmentEditPartCN;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassNameEditPart;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassNameEditPartCN;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassOperationCompartmentEditPart;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassOperationCompartmentEditPartCN;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.OperationForClassEditPart;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.PropertyForClassEditPart;
import org.eclipse.papyrus.uml.diagram.clazz.lf.classtextualedition.Activator;
import org.eclipse.papyrus.uml.diagram.clazz.lf.classtextualedition.mapping.jsonstructures.JsonStructuresFactory;
import org.eclipse.papyrus.uml.diagram.clazz.lf.classtextualedition.mapping.jsonstructures.JsonUmlClass;
//import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassEditPart;
import org.eclipse.papyrus.uml.diagram.common.editparts.ClassEditPart;
import org.eclipse.uml2.uml.NamedElement;
/**
* This class is dedicated to fed an instance of
* {@link org.eclipse.papyrus.uml.diagram.clazz.lf.classtextualedition.mapping.jsonstructures.JsonUmlClass}
* from a real Papyrus class represented by a class edit part:
* <OL>
* <LI>The constructor takes a class edit part as a parameter, creates an
* instance of UmlClass and invokes the following method</LI>
* <LI>The method {@link JsonUmlClassFromEditPart#createFromEditPart()} feeds the
* previous instance</LI>
* <LI>Finally, the method {@link JsonUmlClassFromEditPart#getClass()} allows to get
* the previous instance</LI>
* </OL>
*
*
*/
public class JsonUmlClassFromEditPart {
private JsonUmlClass theClass;
private ClassEditPart editPart;
private static final Activator LOGGER = Activator.getDefault();
/**
* The constructor takes a class edit part as a parameter, creates an
* instance of UmlClass and invokes {@link JsonUmlClassFromEditPart#createFromEditPart()}
*
* @param editPart
* the edit part referenced the real class.
*/
public JsonUmlClassFromEditPart(ClassEditPart editPart) {
super();
this.editPart = editPart;
this.theClass = JsonStructuresFactory.getInstance().createJsonUmlClass();
createFromEditPart();
}
/**
* This method feeds the instance of UmlClass by exploring the nested
* ClassNameEditPart, ClassAttributeCompartmentEditPart and
* ClassOperationCompartmentEditPart to get data for name, properties and
* operations. The method uses getName method and the both create*FromEdit ones.
*/
protected void createFromEditPart() {
List<?> children = editPart.getChildren();
for (Object child : children) {
if (child instanceof ClassNameEditPart || child instanceof ClassNameEditPartCN) {
getName((PapyrusCompartmentEditPart) child);
}
if (child instanceof ClassAttributeCompartmentEditPart || child instanceof ClassAttributeCompartmentEditPartCN) {
createPropertiesFromEdit((ResizeableListCompartmentEditPart) child);
}
if (child instanceof ClassOperationCompartmentEditPart || child instanceof ClassOperationCompartmentEditPartCN) {
createOperationsFromEdit((ResizeableListCompartmentEditPart) child);
}
}
}
/**
* This method will explore the properties compartment and for each found
* property, it will use the class {@link JsonUmlPropertyFromEditPart} to create and
* feed an instance of UmlProperty.
*
* @param compartment
* the properties compartment.
*/
private void createPropertiesFromEdit(ResizeableListCompartmentEditPart compartment) {
for (Object child : compartment.getChildren()) {
if (child instanceof PropertyForClassEditPart) {
JsonUmlPropertyFromEditPart mapper = FromEditPartFactory.getInstance().createJsonUmlPropertyFromEditPart((PropertyForClassEditPart) child);
this.theClass.getProperties().add(mapper.getProperty());
}
}
}
/**
* This method will explore the operations compartment and for each found
* operation, it will use the class {@link JsonUmlOperationFromEditPart} to create
* and feed an instance of UmlOperation.
*
* @param compartment
* the operations compartment.
*/
private void createOperationsFromEdit(ResizeableListCompartmentEditPart compartment) {
for (Object child : compartment.getChildren()) {
if (child instanceof OperationForClassEditPart) {
JsonUmlOperationFromEditPart mapper = FromEditPartFactory.getInstance().createJsonUmlOperationFromEditPart(OperationForClassEditPart.class.cast(child));
this.theClass.getOperations().add(mapper.getOperation());
}
}
}
/**
* This method uses the ClassNameEditPart to get the name of the class (and set
* it within the instance of UmlClass).
*
* @param classNameEditPart
* the class Name edit part
*/
private void getName(PapyrusCompartmentEditPart classNameEditPart) {
if (classNameEditPart.getModel() instanceof View) {
View node = View.class.cast(classNameEditPart.getModel());
if (node.getElement() instanceof NamedElement) {
NamedElement clazz = NamedElement.class.cast(node.getElement());
theClass.setName(clazz.getName());
} else {
LOGGER.logCastProblem("node.getElement()", NamedElement.class); //$NON-NLS-1$
}
} else {
LOGGER.logCastProblem("classNameEditPart.getModel()", View.class); //$NON-NLS-1$
}
}
/**
* Gets the instance of UmlClass. It may be not fed.
*
* @return the instance of UmlClass.
*/
public JsonUmlClass getTheClass() {
return theClass;
}
/**
* Sets the instance of UmlClass.
*
* @param theClass
* the instance of UmlClass
*/
protected void setTheClass(JsonUmlClass theClass) {
this.theClass = theClass;
}
}