|
Added
Link Here
|
| 1 |
package org.eclipse.sapphire.ui.diagram.editor; |
| 2 |
|
| 3 |
import org.eclipse.emf.transaction.RecordingCommand; |
| 4 |
import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 5 |
import org.eclipse.emf.transaction.util.TransactionUtil; |
| 6 |
import org.eclipse.graphiti.features.IAddFeature; |
| 7 |
import org.eclipse.graphiti.features.IFeatureProvider; |
| 8 |
import org.eclipse.graphiti.features.IRemoveFeature; |
| 9 |
import org.eclipse.graphiti.features.context.IRemoveContext; |
| 10 |
import org.eclipse.graphiti.features.context.impl.AddConnectionContext; |
| 11 |
import org.eclipse.graphiti.features.context.impl.RemoveContext; |
| 12 |
import org.eclipse.graphiti.features.context.impl.UpdateContext; |
| 13 |
import org.eclipse.graphiti.mm.pictograms.Connection; |
| 14 |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; |
| 15 |
import org.eclipse.graphiti.mm.pictograms.Diagram; |
| 16 |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; |
| 17 |
import org.eclipse.sapphire.modeling.IModelElement; |
| 18 |
import org.eclipse.sapphire.modeling.ModelElementType; |
| 19 |
import org.eclipse.sapphire.modeling.ModelProperty; |
| 20 |
import org.eclipse.sapphire.modeling.ModelPropertyChangeEvent; |
| 21 |
import org.eclipse.sapphire.modeling.ModelPropertyListener; |
| 22 |
import org.eclipse.sapphire.modeling.ReferenceValue; |
| 23 |
import org.eclipse.sapphire.modeling.Value; |
| 24 |
import org.eclipse.sapphire.modeling.ValueProperty; |
| 25 |
import org.eclipse.sapphire.modeling.el.FunctionResult; |
| 26 |
import org.eclipse.sapphire.ui.SapphirePart; |
| 27 |
import org.eclipse.sapphire.ui.SapphireRenderingContext; |
| 28 |
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionDef; |
| 29 |
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionEndpointDef; |
| 30 |
|
| 31 |
public class DiagramConnectionPart extends SapphirePart |
| 32 |
{ |
| 33 |
protected DiagramConnectionTemplate connectionTemplate; |
| 34 |
private IDiagramConnectionDef localDefinition; |
| 35 |
protected IModelElement modelElement; |
| 36 |
private IDiagramConnectionEndpointDef endpoint1Def; |
| 37 |
private IDiagramConnectionEndpointDef endpoint2Def; |
| 38 |
private IModelElement endpoint1Model; |
| 39 |
private IModelElement endpoint2Model; |
| 40 |
protected FunctionResult labelFunctionResult; |
| 41 |
protected ValueProperty labelProperty; |
| 42 |
protected FunctionResult idFunctionResult; |
| 43 |
private FunctionResult endpoint1FunctionResult; |
| 44 |
private FunctionResult endpoint2FunctionResult; |
| 45 |
protected ModelPropertyListener modelPropertyListener; |
| 46 |
|
| 47 |
public DiagramConnectionPart() {} |
| 48 |
|
| 49 |
public DiagramConnectionPart(DiagramConnectionTemplate connectionTemplate) |
| 50 |
{ |
| 51 |
this.connectionTemplate = connectionTemplate; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected void init() |
| 56 |
{ |
| 57 |
super.init(); |
| 58 |
|
| 59 |
this.localDefinition = (IDiagramConnectionDef)super.definition; |
| 60 |
this.modelElement = getModelElement(); |
| 61 |
this.labelFunctionResult = initExpression |
| 62 |
( |
| 63 |
this.modelElement, |
| 64 |
this.localDefinition.getLabel().element().getContent(), |
| 65 |
new Runnable() |
| 66 |
{ |
| 67 |
public void run() |
| 68 |
{ |
| 69 |
refreshLabel(); |
| 70 |
} |
| 71 |
} |
| 72 |
); |
| 73 |
|
| 74 |
this.labelProperty = FunctionUtil.getFunctionProperty(this.modelElement, |
| 75 |
this.labelFunctionResult); |
| 76 |
|
| 77 |
this.idFunctionResult = initExpression |
| 78 |
( |
| 79 |
this.modelElement, |
| 80 |
this.localDefinition.getInstanceId(), |
| 81 |
new Runnable() |
| 82 |
{ |
| 83 |
public void run() |
| 84 |
{ |
| 85 |
} |
| 86 |
} |
| 87 |
); |
| 88 |
|
| 89 |
this.endpoint1Def = this.localDefinition.getEndpoint1().element(); |
| 90 |
this.endpoint1Model = processEndpoint(this.endpoint1Def); |
| 91 |
if (this.endpoint1Model != null) |
| 92 |
{ |
| 93 |
this.endpoint1FunctionResult = initExpression |
| 94 |
( |
| 95 |
this.endpoint1Model, |
| 96 |
this.endpoint1Def.getValue(), |
| 97 |
new Runnable() |
| 98 |
{ |
| 99 |
public void run() |
| 100 |
{ |
| 101 |
refreshEndpoint1(); |
| 102 |
} |
| 103 |
} |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
this.endpoint2Def = this.localDefinition.getEndpoint2().element(); |
| 108 |
this.endpoint2Model = processEndpoint(this.endpoint2Def); |
| 109 |
if (this.endpoint2Model != null) |
| 110 |
{ |
| 111 |
this.endpoint2FunctionResult = initExpression |
| 112 |
( |
| 113 |
this.endpoint2Model, |
| 114 |
this.endpoint2Def.getValue(), |
| 115 |
new Runnable() |
| 116 |
{ |
| 117 |
public void run() |
| 118 |
{ |
| 119 |
refreshEndpoint2(); |
| 120 |
} |
| 121 |
} |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
// Add model property listener |
| 126 |
this.modelPropertyListener = new ModelPropertyListener() |
| 127 |
{ |
| 128 |
@Override |
| 129 |
public void handlePropertyChangedEvent( final ModelPropertyChangeEvent event ) |
| 130 |
{ |
| 131 |
handleModelPropertyChange( event ); |
| 132 |
} |
| 133 |
}; |
| 134 |
addModelListener(); |
| 135 |
} |
| 136 |
|
| 137 |
public DiagramConnectionTemplate getDiagramConnectionTemplate() |
| 138 |
{ |
| 139 |
return this.connectionTemplate; |
| 140 |
} |
| 141 |
|
| 142 |
public IModelElement getLocalModelElement() |
| 143 |
{ |
| 144 |
return this.modelElement; |
| 145 |
} |
| 146 |
|
| 147 |
public IModelElement getEndpoint1() |
| 148 |
{ |
| 149 |
return this.endpoint1Model; |
| 150 |
} |
| 151 |
|
| 152 |
public IModelElement getEndpoint2() |
| 153 |
{ |
| 154 |
return this.endpoint2Model; |
| 155 |
} |
| 156 |
|
| 157 |
public boolean canEditLabel() |
| 158 |
{ |
| 159 |
return this.labelProperty != null; |
| 160 |
} |
| 161 |
|
| 162 |
public String getLabel() |
| 163 |
{ |
| 164 |
String label = null; |
| 165 |
|
| 166 |
if( this.labelFunctionResult != null ) |
| 167 |
{ |
| 168 |
label = (String) this.labelFunctionResult.value(); |
| 169 |
} |
| 170 |
|
| 171 |
if( label == null ) |
| 172 |
{ |
| 173 |
label = "#null#"; |
| 174 |
} |
| 175 |
|
| 176 |
return label; |
| 177 |
} |
| 178 |
|
| 179 |
public void setLabel(String newValue) |
| 180 |
{ |
| 181 |
if (this.labelProperty != null) |
| 182 |
{ |
| 183 |
this.modelElement.write(this.labelProperty, newValue); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public void refreshLabel() |
| 188 |
{ |
| 189 |
final SapphireDiagramEditorPart diagramEditor = (SapphireDiagramEditorPart)getParentPart(); |
| 190 |
final IFeatureProvider fp = |
| 191 |
diagramEditor.getDiagramEditor().getDiagramTypeProvider().getFeatureProvider(); |
| 192 |
final PictogramElement pe = getConnection(fp, this); |
| 193 |
if (pe != null) |
| 194 |
{ |
| 195 |
UpdateContext context = new UpdateContext(pe); |
| 196 |
fp.updateIfPossible(context); |
| 197 |
} |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
public String getInstanceId() |
| 202 |
{ |
| 203 |
String id = null; |
| 204 |
|
| 205 |
if( this.idFunctionResult != null ) |
| 206 |
{ |
| 207 |
id = (String) this.idFunctionResult.value(); |
| 208 |
} |
| 209 |
|
| 210 |
if( id == null ) |
| 211 |
{ |
| 212 |
id = "#null#"; |
| 213 |
} |
| 214 |
|
| 215 |
return id; |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public void render(SapphireRenderingContext context) |
| 220 |
{ |
| 221 |
throw new UnsupportedOperationException(); |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public void dispose() |
| 226 |
{ |
| 227 |
super.dispose(); |
| 228 |
if (this.labelFunctionResult != null) |
| 229 |
{ |
| 230 |
this.labelFunctionResult.dispose(); |
| 231 |
} |
| 232 |
if (this.idFunctionResult != null) |
| 233 |
{ |
| 234 |
this.idFunctionResult.dispose(); |
| 235 |
} |
| 236 |
if (this.endpoint1FunctionResult != null) |
| 237 |
{ |
| 238 |
this.endpoint1FunctionResult.dispose(); |
| 239 |
} |
| 240 |
if (this.endpoint2FunctionResult != null) |
| 241 |
{ |
| 242 |
this.endpoint2FunctionResult.dispose(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
public void refreshEndpoint1() |
| 247 |
{ |
| 248 |
if (this.endpoint1FunctionResult != null) |
| 249 |
{ |
| 250 |
Object value = this.endpoint1FunctionResult.value(); |
| 251 |
String property = this.endpoint1Def.getProperty().getContent(); |
| 252 |
setModelProperty(this.modelElement, property, value); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
public void refreshEndpoint2() |
| 257 |
{ |
| 258 |
if (this.endpoint2FunctionResult != null) |
| 259 |
{ |
| 260 |
Object value = this.endpoint2FunctionResult.value(); |
| 261 |
String property = this.endpoint2Def.getProperty().getContent(); |
| 262 |
setModelProperty(this.modelElement, property, value); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
protected IModelElement processEndpoint(IDiagramConnectionEndpointDef endpointDef) |
| 267 |
{ |
| 268 |
String propertyName = endpointDef.getProperty().getContent(); |
| 269 |
ModelProperty modelProperty = resolve(this.modelElement, propertyName); |
| 270 |
if (!(modelProperty instanceof ValueProperty)) |
| 271 |
{ |
| 272 |
throw new RuntimeException( "Property " + propertyName + " not a ValueProperty"); |
| 273 |
} |
| 274 |
ValueProperty property = (ValueProperty)modelProperty; |
| 275 |
Value<?> valObj = this.modelElement.read(property); |
| 276 |
if (!(valObj instanceof ReferenceValue)) |
| 277 |
{ |
| 278 |
throw new RuntimeException( "Property " + propertyName + " value not a reference"); |
| 279 |
} |
| 280 |
ReferenceValue<?> refVal = (ReferenceValue<?>)valObj; |
| 281 |
Object targetObj = refVal.resolve(); |
| 282 |
return (IModelElement)targetObj; |
| 283 |
} |
| 284 |
|
| 285 |
protected void setModelProperty(final IModelElement modelElement, |
| 286 |
String propertyName, Object value) |
| 287 |
{ |
| 288 |
if (propertyName != null) |
| 289 |
{ |
| 290 |
final ModelElementType type = modelElement.getModelElementType(); |
| 291 |
final ModelProperty property = type.getProperty( propertyName ); |
| 292 |
if( property == null ) |
| 293 |
{ |
| 294 |
throw new RuntimeException( "Could not find property " + propertyName + " in " + type.getQualifiedName() ); |
| 295 |
} |
| 296 |
if (!(property instanceof ValueProperty)) |
| 297 |
{ |
| 298 |
throw new RuntimeException( "Property " + propertyName + " not a ValueProperty"); |
| 299 |
} |
| 300 |
|
| 301 |
modelElement.write((ValueProperty)property, value); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
public void addModelListener() |
| 306 |
{ |
| 307 |
this.modelElement.addListener(this.modelPropertyListener, |
| 308 |
this.endpoint1Def.getProperty().getContent()); |
| 309 |
this.modelElement.addListener(this.modelPropertyListener, |
| 310 |
this.endpoint2Def.getProperty().getContent()); |
| 311 |
} |
| 312 |
|
| 313 |
public void removeModelListener() |
| 314 |
{ |
| 315 |
this.modelElement.removeListener(this.modelPropertyListener, |
| 316 |
this.endpoint1Def.getProperty().getContent()); |
| 317 |
this.modelElement.removeListener(this.modelPropertyListener, |
| 318 |
this.endpoint2Def.getProperty().getContent()); |
| 319 |
} |
| 320 |
|
| 321 |
protected void handleModelPropertyChange(final ModelPropertyChangeEvent event) |
| 322 |
{ |
| 323 |
final ModelProperty property = event.getProperty(); |
| 324 |
|
| 325 |
if (property.getName().equals(this.endpoint1Def.getProperty().getContent()) || |
| 326 |
property.getName().equals(this.endpoint2Def.getProperty().getContent())) |
| 327 |
{ |
| 328 |
SapphireDiagramEditorPart diagramEditor = (SapphireDiagramEditorPart)getParentPart(); |
| 329 |
final IFeatureProvider fp = diagramEditor.getDiagramEditor().getDiagramTypeProvider().getFeatureProvider(); |
| 330 |
final Diagram diagram = diagramEditor.getDiagramEditor().getDiagramTypeProvider().getDiagram(); |
| 331 |
final TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(diagram); |
| 332 |
|
| 333 |
removeDiagramConnection(fp, ted); |
| 334 |
|
| 335 |
boolean sourceChange = property.getName().equals(this.endpoint1Def.getProperty().getContent()) ? true : false; |
| 336 |
handleEndpointChange(sourceChange); |
| 337 |
// add new connection to the diagram |
| 338 |
addNewConnectionIfPossible(fp, ted, diagramEditor); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
private void handleEndpointChange(boolean sourceChange) |
| 343 |
{ |
| 344 |
if (sourceChange) |
| 345 |
{ |
| 346 |
this.endpoint1Model = processEndpoint(this.endpoint1Def); |
| 347 |
if (this.endpoint1FunctionResult != null) |
| 348 |
{ |
| 349 |
this.endpoint1FunctionResult.dispose(); |
| 350 |
this.endpoint1FunctionResult = null; |
| 351 |
} |
| 352 |
if (this.endpoint1Model != null) |
| 353 |
{ |
| 354 |
this.endpoint1FunctionResult = initExpression |
| 355 |
( |
| 356 |
this.endpoint1Model, |
| 357 |
this.endpoint1Def.getValue(), |
| 358 |
new Runnable() |
| 359 |
{ |
| 360 |
public void run() |
| 361 |
{ |
| 362 |
refreshEndpoint1(); |
| 363 |
} |
| 364 |
} |
| 365 |
); |
| 366 |
} |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
this.endpoint2Model = processEndpoint(this.endpoint2Def); |
| 371 |
if (this.endpoint2FunctionResult != null) |
| 372 |
{ |
| 373 |
this.endpoint2FunctionResult.dispose(); |
| 374 |
this.endpoint2FunctionResult = null; |
| 375 |
} |
| 376 |
if (this.endpoint2Model != null) |
| 377 |
{ |
| 378 |
this.endpoint2FunctionResult = initExpression |
| 379 |
( |
| 380 |
this.endpoint2Model, |
| 381 |
this.endpoint2Def.getValue(), |
| 382 |
new Runnable() |
| 383 |
{ |
| 384 |
public void run() |
| 385 |
{ |
| 386 |
refreshEndpoint2(); |
| 387 |
} |
| 388 |
} |
| 389 |
); |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
protected void removeDiagramConnection(IFeatureProvider fp, TransactionalEditingDomain ted) |
| 395 |
{ |
| 396 |
// remove the existing connection pe from the diagram |
| 397 |
if (this.getEndpoint1() != null && this.getEndpoint2() != null) |
| 398 |
{ |
| 399 |
PictogramElement pe = getConnection(fp, this); |
| 400 |
if (pe != null) |
| 401 |
{ |
| 402 |
final IRemoveContext rc = new RemoveContext(pe); |
| 403 |
final IRemoveFeature removeFeature = fp.getRemoveFeature(rc); |
| 404 |
if (removeFeature != null) |
| 405 |
{ |
| 406 |
ted.getCommandStack().execute(new RecordingCommand(ted) |
| 407 |
{ |
| 408 |
protected void doExecute() |
| 409 |
{ |
| 410 |
removeFeature.remove(rc); |
| 411 |
} |
| 412 |
}); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
protected void addNewConnectionIfPossible(IFeatureProvider fp, TransactionalEditingDomain ted, |
| 419 |
SapphireDiagramEditorPart diagramEditor) |
| 420 |
{ |
| 421 |
// add new connection to the diagram |
| 422 |
if (this.getEndpoint1() != null && this.getEndpoint2() != null) |
| 423 |
{ |
| 424 |
DiagramNodePart srcNodePart = diagramEditor.getDiagramNodePart(this.getEndpoint1()); |
| 425 |
DiagramNodePart targetNodePart = diagramEditor.getDiagramNodePart(this.getEndpoint2()); |
| 426 |
ContainerShape srcNode = getContainerShape(fp, srcNodePart); |
| 427 |
ContainerShape targetNode = getContainerShape(fp, targetNodePart); |
| 428 |
final AddConnectionContext addContext = |
| 429 |
new AddConnectionContext(srcNode.getAnchors().get(0), targetNode.getAnchors().get(0)); |
| 430 |
addContext.setNewObject(this); |
| 431 |
final IAddFeature addFeature = fp.getAddFeature(addContext); |
| 432 |
if (addFeature != null) |
| 433 |
{ |
| 434 |
ted.getCommandStack().execute(new RecordingCommand(ted) |
| 435 |
{ |
| 436 |
protected void doExecute() |
| 437 |
{ |
| 438 |
addFeature.add(addContext); |
| 439 |
} |
| 440 |
}); |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
protected Connection getConnection(IFeatureProvider fp, Object bo) |
| 446 |
{ |
| 447 |
PictogramElement [] pictograms = fp.getAllPictogramElementsForBusinessObject(bo); |
| 448 |
for (PictogramElement pictogram : pictograms) |
| 449 |
{ |
| 450 |
if (pictogram instanceof Connection) |
| 451 |
{ |
| 452 |
return (Connection)pictogram; |
| 453 |
} |
| 454 |
} |
| 455 |
return null; |
| 456 |
} |
| 457 |
|
| 458 |
protected ContainerShape getContainerShape(IFeatureProvider fp, Object bo) |
| 459 |
{ |
| 460 |
ContainerShape containerShape = null; |
| 461 |
PictogramElement [] pictograms = fp.getAllPictogramElementsForBusinessObject(bo); |
| 462 |
for (PictogramElement pictogram : pictograms) |
| 463 |
{ |
| 464 |
if (pictogram instanceof ContainerShape) |
| 465 |
{ |
| 466 |
containerShape = (ContainerShape)pictogram; |
| 467 |
break; |
| 468 |
} |
| 469 |
} |
| 470 |
return containerShape; |
| 471 |
} |
| 472 |
|
| 473 |
} |