diff --git a/src/org/yakindu/sct/generator/core/GeneratorExecutor.java b/src/org/yakindu/sct/generator/core/GeneratorExecutor.java index d4fe5f7ea0..690f879ac9 100644 --- a/src/org/yakindu/sct/generator/core/GeneratorExecutor.java +++ b/src/org/yakindu/sct/generator/core/GeneratorExecutor.java @@ -20,7 +20,7 @@ import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.yakindu.sct.generator.core.extensions.GeneratorExtensions; -import org.yakindu.sct.generator.core.extensions.GeneratorExtensions.GeneratorDescriptor; +import org.yakindu.sct.generator.core.extensions.IGeneratorDescriptor; import org.yakindu.sct.model.sgen.GeneratorEntry; import org.yakindu.sct.model.sgen.GeneratorModel; @@ -40,8 +40,8 @@ public void executeGenerator(IFile file) { GeneratorModel model = (GeneratorModel) resource.getContents().get(0); String generatorId = model.getGeneratorId(); - GeneratorDescriptor description = GeneratorExtensions - .getGeneratorDescriptorForId(generatorId); + IGeneratorDescriptor description = GeneratorExtensions + .getGeneratorDescriptor(generatorId); if (description == null) return; final ISCTGenerator generator = description.createGenerator(); @@ -66,8 +66,8 @@ protected IStatus run(IProgressMonitor monitor) { public void executeGenerator(GeneratorModel model) { String generatorId = model.getGeneratorId(); - GeneratorDescriptor description = GeneratorExtensions - .getGeneratorDescriptorForId(generatorId); + IGeneratorDescriptor description = GeneratorExtensions + .getGeneratorDescriptor(generatorId); if (description == null) throw new RuntimeException("No generator registered for ID: " + generatorId); final ISCTGenerator generator = description.createGenerator(); diff --git a/src/org/yakindu/sct/generator/core/extensions/GeneratorExtensions.java b/src/org/yakindu/sct/generator/core/extensions/GeneratorExtensions.java index add7d021e1..8378226d8f 100644 --- a/src/org/yakindu/sct/generator/core/extensions/GeneratorExtensions.java +++ b/src/org/yakindu/sct/generator/core/extensions/GeneratorExtensions.java @@ -10,9 +10,6 @@ */ package org.yakindu.sct.generator.core.extensions; -import static com.google.common.collect.Iterables.transform; -import static com.google.common.collect.Lists.newArrayList; - import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -26,9 +23,9 @@ import org.osgi.framework.Bundle; import org.yakindu.sct.generator.core.ISCTGenerator; -import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; /** * @author holger willebrandt - Initial contribution and API @@ -46,9 +43,9 @@ public class GeneratorExtensions { private static final String ATTRIBUTE_ICON = "icon"; private static final String ATTRIBUTE_DESCRIPTION = "description"; - private static Iterable generatorDescriptors; + private static List descriptors; - public static class GeneratorDescriptor { + private static class GeneratorDescriptor implements IGeneratorDescriptor { private final IConfigurationElement configElement; @@ -58,6 +55,7 @@ public static class GeneratorDescriptor { this.configElement = configElement; } + @Override public ISCTGenerator createGenerator() { try { return (ISCTGenerator) configElement.createExecutableExtension(ATTRIBUTE_CLASS); @@ -67,6 +65,7 @@ public ISCTGenerator createGenerator() { return null; } + @Override public List getLibraryIDs() { List libs = new ArrayList(); for (IConfigurationElement child : configElement.getChildren(LIBRARY_CONFIG_ELEMENT)) { @@ -78,17 +77,17 @@ public List getLibraryIDs() { return libs; } + @Override public String getId() { return configElement.getAttribute(ATTRIBUTE_ID); } + @Override public String getName() { return configElement.getAttribute(ATTRIBUTE_NAME); } - /** - * may return null!! - */ + @Override public Image getImage() { if (image != null) return image; @@ -103,14 +102,17 @@ public Image getImage() { return image; } + @Override public String getContentType() { return configElement.getAttribute(ATTRIBUTE_CONTENT_TYPE); } + @Override public String getDescription() { return configElement.getAttribute(ATTRIBUTE_DESCRIPTION); } + @Override public String getElementRefType() { try { return configElement.getAttribute(ATTRIBUTE_ELEMENT_REF_TYPE); @@ -119,58 +121,34 @@ public String getElementRefType() { return null; } } + } - @Override - public int hashCode() { - String id = getId(); - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - GeneratorDescriptor other = (GeneratorDescriptor) obj; - String id = getId(); - if (id == null) { - if (other.getId() != null) { - return false; - } - } else if (!id.equals(other.getId())) { - return false; + public static List getGeneratorDescriptors() { + if (descriptors == null) { + descriptors = Lists.newArrayList(); + if (Platform.isRunning()) { + initFromExtensions(); } - return true; } - - + return descriptors; } - public static Iterable getGeneratorDescriptors() { - IConfigurationElement[] configurationElements = Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID); - if (generatorDescriptors == null) { - generatorDescriptors = transform(newArrayList(configurationElements), new CreateGeneratorDescriptor()); + protected static void initFromExtensions() { + IConfigurationElement[] configurationElements = Platform.getExtensionRegistry() + .getConfigurationElementsFor(EXTENSION_POINT_ID); + for (IConfigurationElement iConfigurationElement : configurationElements) { + descriptors.add(new GeneratorDescriptor(iConfigurationElement)); } - return generatorDescriptors; } /** * returns the Generator Descriptor for the given generator id, or null, if * the id is unknown */ - public static GeneratorDescriptor getGeneratorDescriptorForId(final String generatorId) { + public static IGeneratorDescriptor getGeneratorDescriptor(final String generatorId) { try { - return Iterables.find(getGeneratorDescriptors(), new Predicate() { - public boolean apply(GeneratorDescriptor input) { + return Iterables.find(getGeneratorDescriptors(), new Predicate() { + public boolean apply(IGeneratorDescriptor input) { return input != null && input.getId() != null && input.getId().equals(generatorId); } }); @@ -179,11 +157,4 @@ public boolean apply(GeneratorDescriptor input) { } } - private static final class CreateGeneratorDescriptor implements Function { - - public GeneratorDescriptor apply(IConfigurationElement from) { - return new GeneratorDescriptor(from); - } - } - } diff --git a/src/org/yakindu/sct/generator/core/extensions/IGeneratorDescriptor.java b/src/org/yakindu/sct/generator/core/extensions/IGeneratorDescriptor.java new file mode 100644 index 0000000000..fc33e37598 --- /dev/null +++ b/src/org/yakindu/sct/generator/core/extensions/IGeneratorDescriptor.java @@ -0,0 +1,29 @@ +package org.yakindu.sct.generator.core.extensions; + +import java.util.List; + +import org.eclipse.swt.graphics.Image; +import org.yakindu.sct.generator.core.ISCTGenerator; + +public interface IGeneratorDescriptor { + + ISCTGenerator createGenerator(); + + List getLibraryIDs(); + + String getId(); + + String getName(); + + /** + * may return null!! + */ + Image getImage(); + + String getContentType(); + + String getDescription(); + + String getElementRefType(); + +} \ No newline at end of file