| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * 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: |
| * Pierre Allard, |
| * Regent L'Archeveque, |
| <<<<<<< HEAD |
| * Sebastien Gemme |
| * |
| ======= |
| * Sebastien Gemme - initial API and implementation |
| * |
| >>>>>>> refs/heads/eclipse_pa |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.common.topology.ui.adapters; |
| |
| import java.io.IOException; |
| import java.util.Collections; |
| |
| import org.eclipse.apogy.common.topology.GroupNode; |
| import org.eclipse.apogy.common.topology.ui.ApogyCommonTopologyUIFactory; |
| import org.eclipse.apogy.common.topology.ui.GraphicsContext; |
| import org.eclipse.apogy.common.topology.ui.GraphicsContextAdapter; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.xmi.XMIResource; |
| import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class TopoFileGraphicsContextAdapter implements GraphicsContextAdapter { |
| |
| private static final Logger Logger = LoggerFactory.getLogger(TopoFileGraphicsContextAdapter.class); |
| |
| private static final String SUPPORTED_EXTENSION = ".topo"; |
| |
| @Override |
| public boolean isAdapterFor(Object obj) { |
| |
| boolean adapterFor = false; |
| |
| if (obj instanceof IFile) { |
| IFile file = (IFile) obj; |
| adapterFor = file.getName().endsWith(SUPPORTED_EXTENSION); |
| } |
| |
| return adapterFor; |
| } |
| |
| @Override |
| public GraphicsContext getAdapter(Object obj, Object context) { |
| |
| GraphicsContext ctx = null; |
| |
| if (isAdapterFor(obj)) { |
| |
| ctx = ApogyCommonTopologyUIFactory.eINSTANCE.createGraphicsContext(); |
| |
| IProgressMonitor monitor = new NullProgressMonitor(); |
| |
| if (context instanceof IProgressMonitor) { |
| monitor = (IProgressMonitor) context; |
| } |
| |
| IFile file = (IFile) obj; |
| |
| monitor.beginTask("Importing", 1); |
| monitor.subTask("Loading file " + file.getName()); |
| |
| XMIResource res = new XMIResourceImpl(URI.createFileURI(file.getLocation().toOSString())); |
| |
| try { |
| res.load(Collections.EMPTY_MAP); |
| |
| EObject eObject = res.getContents().get(0); |
| |
| monitor.worked(1); |
| |
| if (eObject instanceof GroupNode) { |
| GroupNode root = (GroupNode) eObject; |
| |
| ctx.setTopology(root); |
| } |
| |
| monitor.done(); |
| } catch (IOException e) { |
| monitor.done(); |
| Logger.error(e.getMessage(), e); |
| } |
| |
| } |
| |
| return ctx; |
| } |
| |
| @Override |
| public Class<?> getAdaptedClass() { |
| return IFile.class; |
| } |
| |
| } |