| /******************************************************************************* |
| * Copyright (c) 2011-2012 EclipseSource Muenchen 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 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Eike Stepper - initial API and implementation |
| * Eugen Neufeld - JavaDoc |
| *******************************************************************************/ |
| package org.eclipse.emf.ecp.reflective; |
| |
| import java.io.File; |
| import java.io.FilenameFilter; |
| import java.util.List; |
| |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EPackage; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl; |
| import org.eclipse.emf.ecp.reflective.handlers.EPackageHelper; |
| import org.eclipse.jface.resource.ImageDescriptor; |
| import org.eclipse.ui.plugin.AbstractUIPlugin; |
| import org.osgi.framework.BundleContext; |
| |
| /** |
| * The activator class controls the plug-in life cycle |
| * |
| * @author Tobias Verhoeven |
| */ |
| public class Activator extends AbstractUIPlugin { |
| |
| // The plug-in ID |
| public static final String PLUGIN_ID = "org.eclipse.emf.ecp.reflective"; //$NON-NLS-1$ |
| |
| public final File location = this.getStateLocation().toFile(); |
| |
| // The shared instance |
| private static Activator plugin; |
| |
| /** |
| * The constructor |
| */ |
| public Activator() { |
| } |
| |
| /* |
| * (non-Javadoc) |
| * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) |
| */ |
| public void start(BundleContext context) throws Exception { |
| super.start(context); |
| plugin = this; |
| this.loadDynamicModels(); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) |
| */ |
| public void stop(BundleContext context) throws Exception { |
| plugin = null; |
| super.stop(context); |
| } |
| |
| /** |
| * Returns the shared instance |
| * |
| * @return the shared instance |
| */ |
| public static Activator getDefault() { |
| return plugin; |
| } |
| |
| /** |
| * Returns an image descriptor for the image file at the given |
| * plug-in relative path |
| * |
| * @param path the path |
| * @return the image descriptor |
| */ |
| public static ImageDescriptor getImageDescriptor(String path) { |
| return imageDescriptorFromPlugin(PLUGIN_ID, path); |
| } |
| |
| private void loadDynamicModels() { |
| File dir = this.location; |
| File[] files = null; |
| |
| files = dir.listFiles(new FilenameFilter() { |
| public boolean accept(File d, String name) { |
| return name.endsWith(".ecore"); |
| } |
| }); |
| |
| if (files != null) { |
| for (File file : files) { |
| ResourceSet resourceSet = new ResourceSetImpl(); |
| resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
| .put("ecore", new EcoreResourceFactoryImpl()); |
| Resource resource = resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true); |
| EPackage model = (EPackage) resource.getContents().get(0); |
| EPackage.Registry.INSTANCE.put(model.getNsURI(), model); |
| List<EPackage> packages = EPackageHelper.getAllSubPackages(model); |
| for (EPackage subPkg : packages) { |
| EPackage.Registry.INSTANCE.put(subPkg.getNsURI(), subPkg); |
| } |
| System.out.println("Dynamic Model \"" + model.getNsURI() + "\" loaded."); |
| } |
| } |
| } |
| } |