This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#358 : DomainRegistry refactored to allow standalone execution (without
eclipse extensions)
- Loading branch information
Johannes Dicks
committed
Feb 3, 2016
1 parent
d0b016e
commit bd7f90b
Showing
6 changed files
with
568 additions
and
551 deletions.
There are no files selected for viewing
290 changes: 145 additions & 145 deletions
290
plugins/org.yakindu.sct.domain/src/org/yakindu/sct/domain/extension/DomainRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,145 @@ | ||
/** | ||
* Copyright (c) 2015 committers of YAKINDU 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: | ||
* committers of YAKINDU - initial API and implementation | ||
* | ||
*/ | ||
package org.yakindu.sct.domain.extension; | ||
|
||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
import org.eclipse.core.runtime.Assert; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.IConfigurationElement; | ||
import org.eclipse.core.runtime.Platform; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.util.EcoreUtil; | ||
import org.eclipse.jface.resource.ImageDescriptor; | ||
import org.eclipse.swt.graphics.Image; | ||
import org.osgi.framework.Bundle; | ||
import org.yakindu.sct.model.sgraph.SGraphPackage; | ||
import org.yakindu.sct.model.sgraph.Statechart; | ||
|
||
import com.google.common.base.Predicate; | ||
import com.google.common.collect.Iterables; | ||
|
||
/** | ||
* @author andreas muelder - Initial contribution and API | ||
* | ||
*/ | ||
public class DomainRegistry { | ||
|
||
private static final String EXTENSION_POINT = "org.yakindu.sct.domain"; | ||
private static final String DOMAIN_ID = "domainID"; | ||
private static final String DESCRIPTION = "description"; | ||
private static final String IMAGE = "image"; | ||
private static final String NAME = "name"; | ||
private static final String MODULE_PROVIDER = "domainModuleProvider"; | ||
|
||
private DomainRegistry() { | ||
} | ||
|
||
private static List<DomainDescriptor> descriptors = null; | ||
|
||
public static final class DomainDescriptor { | ||
|
||
private final IConfigurationElement configElement; | ||
|
||
private Image image; | ||
|
||
private IDomainInjectorProvider injectorProvider; | ||
|
||
DomainDescriptor(IConfigurationElement configElement) { | ||
this.configElement = configElement; | ||
} | ||
|
||
public String getDomainID() { | ||
return configElement.getAttribute(DOMAIN_ID); | ||
} | ||
|
||
public String getName() { | ||
return configElement.getAttribute(NAME); | ||
} | ||
|
||
public String getDescription() { | ||
return configElement.getAttribute(DESCRIPTION); | ||
} | ||
|
||
public IDomainInjectorProvider getDomainInjectorProvider() { | ||
if (injectorProvider == null) { | ||
try { | ||
injectorProvider = (IDomainInjectorProvider) configElement | ||
.createExecutableExtension(MODULE_PROVIDER); | ||
} catch (CoreException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return injectorProvider; | ||
} | ||
|
||
public Image getImage() { | ||
if (image != null) | ||
return image; | ||
String path = configElement.getAttribute(IMAGE); | ||
if (path == null) | ||
return null; | ||
|
||
Bundle extensionBundle = Platform.getBundle(configElement.getContributor().getName()); | ||
URL entry = extensionBundle.getEntry(path); | ||
ImageDescriptor descriptor = ImageDescriptor.createFromURL(entry); | ||
image = descriptor.createImage(); | ||
return image; | ||
} | ||
|
||
public IConfigurationElement getConfigElement() { | ||
return configElement; | ||
} | ||
|
||
public void setImage(Image image) { | ||
this.image = image; | ||
} | ||
} | ||
|
||
public static List<DomainDescriptor> getDomainDescriptors() { | ||
if (descriptors == null) { | ||
descriptors = new ArrayList<DomainDescriptor>(); | ||
IConfigurationElement[] configurationElements = Platform.getExtensionRegistry() | ||
.getConfigurationElementsFor(EXTENSION_POINT); | ||
for (IConfigurationElement iConfigurationElement : configurationElements) { | ||
descriptors.add(new DomainDescriptor(iConfigurationElement)); | ||
} | ||
} | ||
return descriptors; | ||
} | ||
|
||
public static DomainDescriptor getDomainDescriptor(final String id) { | ||
final String defaultDomainID = SGraphPackage.Literals.STATECHART__DOMAIN_ID.getDefaultValueLiteral(); | ||
try { | ||
return Iterables.find(getDomainDescriptors(), new Predicate<DomainDescriptor>() { | ||
@Override | ||
public boolean apply(DomainDescriptor input) { | ||
return input.getDomainID().equals(id != null ? id : defaultDomainID); | ||
} | ||
}); | ||
} catch (NoSuchElementException e) { | ||
if (defaultDomainID.equals(id)) { | ||
throw new IllegalArgumentException("No default domain found!"); | ||
} | ||
System.err.println("Could not find domain descriptor for id " + id + " - > using default domain"); | ||
return getDomainDescriptor(defaultDomainID); | ||
} | ||
} | ||
|
||
public static DomainDescriptor getDomainDescriptor(EObject object) { | ||
EObject rootContainer = EcoreUtil.getRootContainer(object); | ||
Assert.isTrue(rootContainer instanceof Statechart); | ||
return getDomainDescriptor(((Statechart) rootContainer).getDomainID()); | ||
} | ||
} | ||
/** | ||
* Copyright (c) 2015 committers of YAKINDU 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: | ||
* committers of YAKINDU - initial API and implementation | ||
* | ||
*/ | ||
package org.yakindu.sct.domain.extension; | ||
|
||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
import org.eclipse.core.runtime.Assert; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.IConfigurationElement; | ||
import org.eclipse.core.runtime.Platform; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.util.EcoreUtil; | ||
import org.eclipse.jface.resource.ImageDescriptor; | ||
import org.eclipse.swt.graphics.Image; | ||
import org.osgi.framework.Bundle; | ||
import org.yakindu.sct.model.sgraph.SGraphPackage; | ||
import org.yakindu.sct.model.sgraph.Statechart; | ||
|
||
import com.google.common.base.Predicate; | ||
import com.google.common.collect.Iterables; | ||
|
||
/** | ||
* @author andreas muelder - Initial contribution and API | ||
* | ||
*/ | ||
public class DomainRegistry { | ||
|
||
private static final String EXTENSION_POINT = "org.yakindu.sct.domain"; | ||
private static final String DOMAIN_ID = "domainID"; | ||
private static final String DESCRIPTION = "description"; | ||
private static final String IMAGE = "image"; | ||
private static final String NAME = "name"; | ||
private static final String MODULE_PROVIDER = "domainModuleProvider"; | ||
|
||
private DomainRegistry() { | ||
} | ||
|
||
private static List<IDomainDescriptor> descriptors = null; | ||
|
||
protected static final class ConfigElementDomainDescriptor implements IDomainDescriptor { | ||
|
||
private final IConfigurationElement configElement; | ||
|
||
private Image image; | ||
|
||
private IDomainInjectorProvider injectorProvider; | ||
|
||
ConfigElementDomainDescriptor(IConfigurationElement configElement) { | ||
this.configElement = configElement; | ||
} | ||
|
||
@Override | ||
public String getDomainID() { | ||
return configElement.getAttribute(DOMAIN_ID); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return configElement.getAttribute(NAME); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return configElement.getAttribute(DESCRIPTION); | ||
} | ||
|
||
@Override | ||
public IDomainInjectorProvider getDomainInjectorProvider() { | ||
if (injectorProvider == null) { | ||
try { | ||
injectorProvider = (IDomainInjectorProvider) configElement | ||
.createExecutableExtension(MODULE_PROVIDER); | ||
} catch (CoreException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return injectorProvider; | ||
} | ||
|
||
@Override | ||
public Image getImage() { | ||
if (image != null) | ||
return image; | ||
String path = configElement.getAttribute(IMAGE); | ||
if (path == null) | ||
return null; | ||
|
||
Bundle extensionBundle = Platform.getBundle(configElement.getContributor().getName()); | ||
URL entry = extensionBundle.getEntry(path); | ||
ImageDescriptor descriptor = ImageDescriptor.createFromURL(entry); | ||
image = descriptor.createImage(); | ||
return image; | ||
} | ||
} | ||
|
||
public static List<IDomainDescriptor> getDomainDescriptors() { | ||
if (descriptors == null) { | ||
descriptors = new ArrayList<IDomainDescriptor>(); | ||
|
||
if (Platform.isRunning()) { | ||
IConfigurationElement[] configurationElements = Platform.getExtensionRegistry() | ||
.getConfigurationElementsFor(EXTENSION_POINT); | ||
for (IConfigurationElement iConfigurationElement : configurationElements) { | ||
descriptors.add(new ConfigElementDomainDescriptor(iConfigurationElement)); | ||
} | ||
} | ||
} | ||
return descriptors; | ||
} | ||
|
||
public static IDomainDescriptor getDomainDescriptor(final String id) { | ||
final String defaultDomainID = SGraphPackage.Literals.STATECHART__DOMAIN_ID.getDefaultValueLiteral(); | ||
try { | ||
return Iterables.find(getDomainDescriptors(), new Predicate<IDomainDescriptor>() { | ||
@Override | ||
public boolean apply(IDomainDescriptor input) { | ||
return input.getDomainID().equals(id != null ? id : defaultDomainID); | ||
} | ||
}); | ||
} catch (NoSuchElementException e) { | ||
if (defaultDomainID.equals(id)) { | ||
throw new IllegalArgumentException("No default domain found!"); | ||
} | ||
System.err.println("Could not find domain descriptor for id " + id + " - > using default domain"); | ||
return getDomainDescriptor(defaultDomainID); | ||
} | ||
} | ||
|
||
public static IDomainDescriptor getDomainDescriptor(EObject object) { | ||
EObject rootContainer = EcoreUtil.getRootContainer(object); | ||
Assert.isTrue(rootContainer instanceof Statechart); | ||
return getDomainDescriptor(((Statechart) rootContainer).getDomainID()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
plugins/org.yakindu.sct.domain/src/org/yakindu/sct/domain/extension/IDomainDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.yakindu.sct.domain.extension; | ||
|
||
import org.eclipse.swt.graphics.Image; | ||
|
||
public interface IDomainDescriptor { | ||
|
||
String getDomainID(); | ||
|
||
String getName(); | ||
|
||
String getDescription(); | ||
|
||
IDomainInjectorProvider getDomainInjectorProvider(); | ||
|
||
Image getImage(); | ||
|
||
} |
Oops, something went wrong.