| /******************************************************************************* |
| * Copyright (c) 2014 Bosch Software Innovations GmbH and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * and Eclipse Distribution License v1.0 which accompany this distribution. |
| * |
| * The Eclipse Public License is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * The Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * Bosch Software Innovations GmbH - Please refer to git log |
| * |
| *******************************************************************************/ |
| package org.eclipse.vorto.codegen.internal.handler; |
| |
| import static org.eclipse.vorto.codegen.internal.extensioninterface.PopulateGeneratorsMenu.GENERATE_ALL; |
| |
| import org.eclipse.core.commands.AbstractHandler; |
| import org.eclipse.core.commands.ExecutionEvent; |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.runtime.IConfigurationElement; |
| import org.eclipse.core.runtime.IExtension; |
| import org.eclipse.core.runtime.IExtensionRegistry; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.vorto.api.common.project.FunctionBlockProjectFactory; |
| import org.eclipse.vorto.api.common.project.IProjectSelectionFactory; |
| import org.eclipse.vorto.api.common.project.IoTModelProject; |
| import org.eclipse.vorto.codegen.api.ICodeGenerator; |
| import org.eclipse.vorto.functionblock.FunctionblockModel; |
| |
| |
| public class EvaluateContributionsHandler extends AbstractHandler { |
| |
| private static final String GENERATOR_ID = ICodeGenerator.GENERATOR_ID; |
| |
| @Override |
| public Object execute(ExecutionEvent event) throws ExecutionException { |
| IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry(); |
| final String generatorIdentifier = event |
| .getParameter("org.eclipse.vorto.codegen.generator.commandParameter"); |
| evaluate(extensionRegistry, generatorIdentifier); |
| return null; |
| } |
| |
| private void evaluate(IExtensionRegistry registry, String generatorName) { |
| |
| final IConfigurationElement[] configElements = getUserSelectedGenerators( |
| registry, generatorName); |
| final FunctionblockModel model = getSelectedFbDesignProject() |
| .getFunctionBlockModel(); |
| |
| GenerateService.runGenerateCodeInBackground(configElements, model); |
| } |
| |
| private IConfigurationElement[] getUserSelectedGenerators( |
| IExtensionRegistry registry, String generatorIdentifier) { |
| IConfigurationElement[] configurationElements; |
| if (GENERATE_ALL.equals(generatorIdentifier)) { |
| configurationElements = registry |
| .getConfigurationElementsFor(GENERATOR_ID); |
| } else { |
| IExtension extension = registry.getExtension( |
| ICodeGenerator.GENERATOR_ID, generatorIdentifier); |
| configurationElements = extension.getConfigurationElements(); |
| } |
| return configurationElements; |
| } |
| |
| private IoTModelProject getSelectedFbDesignProject() { |
| try { |
| FunctionBlockProjectFactory factory = FunctionBlockProjectFactory |
| .getInstance(); |
| |
| IProject projectFromSelection = IProjectSelectionFactory |
| .getInstance().getProjectFromSelection(); |
| if (projectFromSelection != null) |
| return factory.getProject(projectFromSelection, |
| IoTModelProject.class); |
| |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| |
| } |