|
Added
Link Here
|
| 1 |
package org.eclipse.sapphire.ui.diagram.editor; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collections; |
| 5 |
import java.util.List; |
| 6 |
|
| 7 |
import org.eclipse.sapphire.modeling.ElementProperty; |
| 8 |
import org.eclipse.sapphire.modeling.IModelElement; |
| 9 |
import org.eclipse.sapphire.modeling.ListProperty; |
| 10 |
import org.eclipse.sapphire.modeling.ModelElementList; |
| 11 |
import org.eclipse.sapphire.modeling.ModelElementType; |
| 12 |
import org.eclipse.sapphire.modeling.ModelProperty; |
| 13 |
import org.eclipse.sapphire.modeling.Value; |
| 14 |
import org.eclipse.sapphire.modeling.ValueProperty; |
| 15 |
import org.eclipse.sapphire.modeling.el.Function; |
| 16 |
import org.eclipse.sapphire.modeling.el.FunctionContext; |
| 17 |
import org.eclipse.sapphire.modeling.el.ModelElementFunctionContext; |
| 18 |
import org.eclipse.sapphire.modeling.el.parser.ExpressionLanguageParser; |
| 19 |
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionDef; |
| 20 |
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionEndpointDef; |
| 21 |
import org.eclipse.sapphire.ui.internal.SapphireUiFrameworkPlugin; |
| 22 |
|
| 23 |
public class DiagramConnectionTemplate |
| 24 |
{ |
| 25 |
private SapphireDiagramEditorPart diagramEditor; |
| 26 |
private IDiagramConnectionDef definition; |
| 27 |
private IModelElement modelElement; |
| 28 |
private ModelProperty modelProperty; |
| 29 |
private String toolPaletteLabel; |
| 30 |
private String toolPaletteDesc; |
| 31 |
|
| 32 |
private List<DiagramConnectionPart> diagramConnections; |
| 33 |
|
| 34 |
public DiagramConnectionTemplate(final SapphireDiagramEditorPart diagramEditor, |
| 35 |
IDiagramConnectionDef definition, IModelElement modelElement) |
| 36 |
{ |
| 37 |
this.diagramEditor = diagramEditor; |
| 38 |
this.modelElement = modelElement; |
| 39 |
this.definition = definition; |
| 40 |
|
| 41 |
this.toolPaletteLabel = this.definition.getToolPaletteLabel().getContent(); |
| 42 |
this.toolPaletteDesc = this.definition.getToolPaletteDesc().getContent(); |
| 43 |
|
| 44 |
this.diagramConnections = new ArrayList<DiagramConnectionPart>(); |
| 45 |
|
| 46 |
String propertyName = this.definition.getProperty().getContent(); |
| 47 |
this.modelProperty = resolve(this.modelElement, propertyName); |
| 48 |
if (this.modelProperty instanceof ListProperty) |
| 49 |
{ |
| 50 |
ListProperty listProperty = (ListProperty)this.modelProperty; |
| 51 |
ModelElementList<?> list = this.modelElement.read(listProperty); |
| 52 |
for( IModelElement listEntryModelElement : list ) |
| 53 |
{ |
| 54 |
DiagramConnectionPart connection = new DiagramConnectionPart(this); |
| 55 |
connection.init(this.diagramEditor, listEntryModelElement, definition, |
| 56 |
Collections.<String,String>emptyMap()); |
| 57 |
this.diagramConnections.add(connection); |
| 58 |
} |
| 59 |
} |
| 60 |
else if (this.modelProperty instanceof ElementProperty) |
| 61 |
{ |
| 62 |
ElementProperty elementProperty = (ElementProperty)this.modelProperty; |
| 63 |
if (this.modelElement.read(elementProperty) != null) |
| 64 |
{ |
| 65 |
IModelElement localModelElement = this.modelElement.read(elementProperty).element(); |
| 66 |
DiagramConnectionPart connection = new DiagramConnectionPart(this); |
| 67 |
connection.init(this.diagramEditor, localModelElement, definition, |
| 68 |
Collections.<String,String>emptyMap()); |
| 69 |
|
| 70 |
this.diagramConnections.add(connection); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public List<DiagramConnectionPart> getDiagramConnections() |
| 76 |
{ |
| 77 |
return this.diagramConnections; |
| 78 |
} |
| 79 |
|
| 80 |
public String getToolPaletteLabel() |
| 81 |
{ |
| 82 |
return this.toolPaletteLabel; |
| 83 |
} |
| 84 |
|
| 85 |
public String getToolPaletteDesc() |
| 86 |
{ |
| 87 |
return this.toolPaletteDesc; |
| 88 |
} |
| 89 |
|
| 90 |
public DiagramConnectionPart createNewDiagramConnection(DiagramNodePart srcNode, |
| 91 |
DiagramNodePart targetNode) |
| 92 |
{ |
| 93 |
IModelElement newElement = null; |
| 94 |
if (this.modelProperty instanceof ListProperty) |
| 95 |
{ |
| 96 |
ListProperty listProperty = (ListProperty)this.modelProperty; |
| 97 |
ModelElementList<?> list = this.modelElement.read(listProperty); |
| 98 |
newElement = list.addNewElement(); |
| 99 |
} |
| 100 |
else if (this.modelProperty instanceof ElementProperty) |
| 101 |
{ |
| 102 |
// TODO what if the element property does exist? |
| 103 |
ElementProperty elementProperty = (ElementProperty)this.modelProperty; |
| 104 |
newElement = this.modelElement.read(elementProperty).element(true); |
| 105 |
} |
| 106 |
|
| 107 |
IDiagramConnectionEndpointDef srcAnchorDef = this.definition.getEndpoint1().element(); |
| 108 |
String srcProperty = srcAnchorDef.getProperty().getContent(); |
| 109 |
String srcExpr = srcAnchorDef.getValue().getContent(); |
| 110 |
Function srcFunc = getNodeReferenceFunction(srcNode, srcExpr); |
| 111 |
Object srcValue = srcFunc.value(); |
| 112 |
srcFunc.dispose(); |
| 113 |
setModelProperty(newElement, srcProperty, srcValue); |
| 114 |
|
| 115 |
IDiagramConnectionEndpointDef targetAnchorDef = this.definition.getEndpoint2().element(); |
| 116 |
String targetProperty = targetAnchorDef.getProperty().getContent(); |
| 117 |
String targetExpr = targetAnchorDef.getValue().getContent(); |
| 118 |
Function targetFunc = getNodeReferenceFunction(targetNode, targetExpr); |
| 119 |
Object targetValue = targetFunc.value(); |
| 120 |
targetFunc.dispose(); |
| 121 |
setModelProperty(newElement, targetProperty, targetValue); |
| 122 |
|
| 123 |
DiagramConnectionPart newConn = new DiagramConnectionPart(this); |
| 124 |
newConn.init(this.diagramEditor, newElement, this.definition, |
| 125 |
Collections.<String,String>emptyMap()); |
| 126 |
this.diagramConnections.add(newConn); |
| 127 |
return newConn; |
| 128 |
} |
| 129 |
|
| 130 |
private ModelProperty resolve(final IModelElement modelElement, |
| 131 |
String propertyName) |
| 132 |
{ |
| 133 |
if (propertyName != null) |
| 134 |
{ |
| 135 |
final ModelElementType type = modelElement.getModelElementType(); |
| 136 |
final ModelProperty property = type.getProperty( propertyName ); |
| 137 |
if( property == null ) |
| 138 |
{ |
| 139 |
throw new RuntimeException( "Could not find property " + propertyName + " in " + type.getQualifiedName() ); |
| 140 |
} |
| 141 |
return property; |
| 142 |
} |
| 143 |
return null; |
| 144 |
} |
| 145 |
|
| 146 |
private void setModelProperty(final IModelElement modelElement, |
| 147 |
String propertyName, Object value) |
| 148 |
{ |
| 149 |
if (propertyName != null) |
| 150 |
{ |
| 151 |
final ModelElementType type = modelElement.getModelElementType(); |
| 152 |
final ModelProperty property = type.getProperty( propertyName ); |
| 153 |
if( property == null ) |
| 154 |
{ |
| 155 |
throw new RuntimeException( "Could not find property " + propertyName + " in " + type.getQualifiedName() ); |
| 156 |
} |
| 157 |
if (!(property instanceof ValueProperty)) |
| 158 |
{ |
| 159 |
throw new RuntimeException( "Property " + propertyName + " not a ValueProperty"); |
| 160 |
} |
| 161 |
|
| 162 |
modelElement.write((ValueProperty)property, ((Value<?>)value).getContent()); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
private Function getNodeReferenceFunction(final DiagramNodePart nodePart, |
| 167 |
final String expression) |
| 168 |
{ |
| 169 |
final FunctionContext context = new ModelElementFunctionContext( nodePart.getLocalModelElement() ); |
| 170 |
Function result = null; |
| 171 |
|
| 172 |
if( expression != null ) |
| 173 |
{ |
| 174 |
try |
| 175 |
{ |
| 176 |
result = ExpressionLanguageParser.parse( context, expression ); |
| 177 |
} |
| 178 |
catch( Exception e ) |
| 179 |
{ |
| 180 |
SapphireUiFrameworkPlugin.log( e ); |
| 181 |
result = null; |
| 182 |
} |
| 183 |
} |
| 184 |
return result; |
| 185 |
} |
| 186 |
} |