All Downloads are FREE. Search and download functionalities are using the official Maven repository.

protelis.parser.ui.internal.ParserActivator Maven / Gradle / Ivy

There is a newer version: 11.0.3
Show newest version
/*
 * generated by Xtext 2.16.0
 */
package protelis.parser.ui.internal;

import com.google.common.collect.Maps;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.Collections;
import java.util.Map;
import org.apache.log4j.Logger;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtext.ui.shared.SharedStateModule;
import org.eclipse.xtext.util.Modules2;
import org.osgi.framework.BundleContext;
import org.protelis.parser.ProtelisRuntimeModule;
import org.protelis.parser.ui.ProtelisUiModule;

/**
 * This class was generated. Customizations should only happen in a newly
 * introduced subclass. 
 */
public class ParserActivator extends AbstractUIPlugin {

	public static final String PLUGIN_ID = "protelis.parser.ui";
	public static final String ORG_PROTELIS_PARSER_PROTELIS = "org.protelis.parser.Protelis";
	
	private static final Logger logger = Logger.getLogger(ParserActivator.class);
	
	private static ParserActivator INSTANCE;
	
	private Map injectors = Collections.synchronizedMap(Maps. newHashMapWithExpectedSize(1));
	
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		INSTANCE = this;
	}
	
	@Override
	public void stop(BundleContext context) throws Exception {
		injectors.clear();
		INSTANCE = null;
		super.stop(context);
	}
	
	public static ParserActivator getInstance() {
		return INSTANCE;
	}
	
	public Injector getInjector(String language) {
		synchronized (injectors) {
			Injector injector = injectors.get(language);
			if (injector == null) {
				injectors.put(language, injector = createInjector(language));
			}
			return injector;
		}
	}
	
	protected Injector createInjector(String language) {
		try {
			com.google.inject.Module runtimeModule = getRuntimeModule(language);
			com.google.inject.Module sharedStateModule = getSharedStateModule();
			com.google.inject.Module uiModule = getUiModule(language);
			com.google.inject.Module mergedModule = Modules2.mixin(runtimeModule, sharedStateModule, uiModule);
			return Guice.createInjector(mergedModule);
		} catch (Exception e) {
			logger.error("Failed to create injector for " + language);
			logger.error(e.getMessage(), e);
			throw new RuntimeException("Failed to create injector for " + language, e);
		}
	}
	
	protected com.google.inject.Module getRuntimeModule(String grammar) {
		if (ORG_PROTELIS_PARSER_PROTELIS.equals(grammar)) {
			return new ProtelisRuntimeModule();
		}
		throw new IllegalArgumentException(grammar);
	}
	
	protected com.google.inject.Module getUiModule(String grammar) {
		if (ORG_PROTELIS_PARSER_PROTELIS.equals(grammar)) {
			return new ProtelisUiModule(this);
		}
		throw new IllegalArgumentException(grammar);
	}
	
	protected com.google.inject.Module getSharedStateModule() {
		return new SharedStateModule();
	}
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy