blob: c2860dca6792b67f2dce6efd86843d931f69aafa [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Lunifera GmbH (Gross Enzersdorf), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* 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:
* Florian Pirchner (Lunifera GmbH) - initial implementation
*/
package org.eclipse.osbp.tools.graphical.entity.lib;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.osbp.dsl.semantic.common.types.LTypedPackage;
import org.eclipse.osbp.dsl.semantic.entity.LEntity;
import org.eclipse.osbp.dsl.semantic.entity.LEntityFeature;
import org.eclipse.osbp.dsl.semantic.entity.LEntityReference;
import org.eclipse.osbp.xtext.oxtype.oxtype.OXImportDeclaration;
import org.eclipse.osbp.xtext.oxtype.oxtype.OXtypeFactory;
import org.eclipse.sirius.tools.api.ui.IExternalJavaAction;
import org.eclipse.xtext.xtype.XImportSection;
public class LEntityReferencePackageImportToOne implements IExternalJavaAction {
private List<LEntityReference> oldReferences;
public LEntityReferencePackageImportToOne() {
oldReferences = new ArrayList<LEntityReference>();
}
@Override
public void execute(Collection<? extends EObject> selections,
Map<String, Object> parameters) {
LEntityReference newRef = getNewReference(selections);
LTypedPackage sourcePackage = (LTypedPackage) newRef.getEntity()
.eContainer();
LTypedPackage targetPackage = (LTypedPackage) newRef.getType()
.eContainer();
XImportSection section = Util.getImportSecion(sourcePackage);
if (targetPackage == sourcePackage) {
return;
} else {
OXImportDeclaration newImport = OXtypeFactory.eINSTANCE.createOXImportDeclaration();
newImport.setImportedFullyQualifiedName(Util.getFQN(newRef.getType()));
newImport.setFqnImport(true);
section.getImportDeclarations().add(newImport);
}
}
@Override
public boolean canExecute(Collection<? extends EObject> selections) {
oldReferences = filterEntityReferences(selections);
return true;
}
private LEntityReference getNewReference(
Collection<? extends EObject> selections) {
List<LEntityReference> newTempRefs = filterEntityReferences(selections);
newTempRefs.removeAll(oldReferences);
LEntityReference newReference = newTempRefs.get(0);
newTempRefs.addAll(oldReferences);
return newReference;
}
private List<LEntityReference> filterEntityReferences(
Collection<? extends EObject> selections) {
List<LEntityReference> tmpList = new ArrayList<LEntityReference>();
for (EObject eObject : selections) {
for (LEntityFeature x : ((LEntity) eObject).getFeatures()) {
if (x instanceof LEntityReference && !tmpList.contains(x)) {
tmpList.add((LEntityReference) x);
}
}
}
return tmpList;
}
}