| /** |
| * |
| * 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * 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.LImport; |
| import org.eclipse.osbp.dsl.semantic.common.types.LTypedPackage; |
| import org.eclipse.osbp.dsl.semantic.common.types.OSBPTypesFactory; |
| import org.eclipse.osbp.dsl.semantic.entity.LBean; |
| import org.eclipse.osbp.dsl.semantic.entity.LBeanFeature; |
| import org.eclipse.osbp.dsl.semantic.entity.LBeanReference; |
| import org.eclipse.sirius.tools.api.ui.IExternalJavaAction; |
| |
| public class LBeanReferencePackageImportToOne implements IExternalJavaAction { |
| private List<LBeanReference> oldReferences; |
| |
| public LBeanReferencePackageImportToOne() { |
| oldReferences = new ArrayList<LBeanReference>(); |
| } |
| |
| @Override |
| public void execute(Collection<? extends EObject> selections, |
| Map<String, Object> parameters) { |
| LBeanReference newRef = getNewReference(selections); |
| LTypedPackage sourcePackage = (LTypedPackage) newRef.getBean() |
| .eContainer(); |
| LTypedPackage targetPackage = (LTypedPackage) newRef.getType() |
| .eContainer(); |
| |
| if (targetPackage == sourcePackage) { |
| return; |
| } else { |
| LImport newImport = OSBPTypesFactory.eINSTANCE.createLImport(); |
| newImport.setImportedNamespace(targetPackage.getName() + ".*"); |
| boolean alreadyimported = false; |
| for (LImport imp : sourcePackage.getImports()) { |
| if (newImport.getImportedNamespace().equals( |
| imp.getImportedNamespace())) { |
| alreadyimported = true; |
| } |
| } |
| if (!alreadyimported) { |
| sourcePackage.getImports().add(newImport); |
| } |
| } |
| |
| } |
| |
| @Override |
| public boolean canExecute(Collection<? extends EObject> selections) { |
| oldReferences = filterBeanReferences(selections); |
| return true; |
| } |
| |
| private LBeanReference getNewReference( |
| Collection<? extends EObject> selections) { |
| |
| List<LBeanReference> newTempRefs = filterBeanReferences(selections); |
| newTempRefs.removeAll(oldReferences); |
| LBeanReference newReference = newTempRefs.get(0); |
| newTempRefs.addAll(oldReferences); |
| return newReference; |
| } |
| |
| private List<LBeanReference> filterBeanReferences( |
| Collection<? extends EObject> selections) { |
| List<LBeanReference> tmpList = new ArrayList<LBeanReference>(); |
| for (EObject eObject : selections) { |
| for (LBeanFeature x : ((LBean) eObject).getFeatures()) { |
| if (x instanceof LBeanReference && !tmpList.contains(x)) { |
| tmpList.add((LBeanReference) x); |
| } |
| } |
| } |
| return tmpList; |
| } |
| |
| } |