package org.jbpm.ui.common;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.jbpm.ui.DesignerLogger;
import org.jbpm.ui.common.figure.GridSupportLayer;
import org.jbpm.ui.common.figure.NodeFigure;
import org.jbpm.ui.common.figure.TransitionFigure;
import org.jbpm.ui.common.model.GraphElement;
import org.jbpm.ui.common.model.ProcessDefinition;
public class ElementTypeDefinition {
private final String name;
private final IConfigurationElement configElement;
public ElementTypeDefinition(IConfigurationElement configElement) {
this.configElement = configElement;
this.name = configElement.getAttribute("name");
}
public IConfigurationElement getEntryConfigElement() {
IConfigurationElement[] entries = configElement.getChildren("entry");
if (entries.length == 0)
return null;
return entries[0];
}
public String getName() {
return name;
}
private <T> T createExecutableExtension(String propertyName) {
try {
if (configElement.getAttribute(propertyName) == null) {
return null;
}
return (T) configElement.createExecutableExtension(propertyName);
} catch (CoreException e) {
DesignerLogger.logError("Unable to create element '" + name + "'(unable to load property='" + propertyName + "')", e);
return null;
}
}
public <T extends GraphElement> T createElement() {
GraphElement element = createExecutableExtension("model");
element.setTypeName(name);
return (T) element;
}
private EditPart createEditPart(String propertyName, GraphElement element) {
EditPart editPart = createExecutableExtension(propertyName);
if (editPart != null) {
editPart.setModel(element);
}
return editPart;
}
public EditPart createGraphicalEditPart(GraphElement element) {
return createEditPart("graphicalEditPart", element);
}
public EditPart createTreeEditPart(GraphElement element) {
return createEditPart("treeEditPart", element);
}
public <T extends IFigure> T createFigure(ProcessDefinition definition) {
T figure = createExecutableExtension("figure");
if (figure instanceof NodeFigure) {
((NodeFigure) figure).init(definition.isBPMNNotation());
}
if (figure instanceof TransitionFigure) {
((TransitionFigure) figure).init(definition.isBPMNNotation());
}
if (figure instanceof GridSupportLayer) {
((GridSupportLayer) figure).setDefinition(definition);
}
return figure;
}
}