PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/org.eclipse.graphiti.testtool.sketch/src/org/eclipse/graphiti/testtool/sketch/features/create/AbstractSketchCreateSimpleConnectionFeature.java

https://github.com/rjohanss/gmp.graphiti
Java | 134 lines | 77 code | 22 blank | 35 comment | 12 complexity | fc735e6d34bed7fed9d822e39e75a332 MD5 | raw file
  1. /*******************************************************************************
  2. * <copyright>
  3. *
  4. * Copyright (c) 2005, 2012 SAP AG.
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution, and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * SAP AG - initial API, implementation and documentation
  12. * Benjamin Schmeling - mwenz - Bug 367483 - Support composite connections
  13. *
  14. * </copyright>
  15. *
  16. *******************************************************************************/
  17. package org.eclipse.graphiti.testtool.sketch.features.create;
  18. import org.eclipse.emf.ecore.util.EcoreUtil;
  19. import org.eclipse.graphiti.features.IFeatureProvider;
  20. import org.eclipse.graphiti.features.context.ICreateConnectionContext;
  21. import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
  22. import org.eclipse.graphiti.mm.algorithms.Polyline;
  23. import org.eclipse.graphiti.mm.algorithms.styles.LineStyle;
  24. import org.eclipse.graphiti.mm.algorithms.styles.Point;
  25. import org.eclipse.graphiti.mm.pictograms.Anchor;
  26. import org.eclipse.graphiti.mm.pictograms.Connection;
  27. import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
  28. import org.eclipse.graphiti.mm.pictograms.Shape;
  29. import org.eclipse.graphiti.services.Graphiti;
  30. import org.eclipse.graphiti.services.ICreateService;
  31. import org.eclipse.graphiti.testtool.sketch.SketchUtil;
  32. import org.eclipse.graphiti.util.IColorConstant;
  33. /**
  34. * The Class AbstractSketchCreateSimpleConnectionFeature.
  35. */
  36. abstract class AbstractSketchCreateSimpleConnectionFeature extends AbstractSketchCreateConnectionFeature {
  37. /**
  38. * Instantiates a new abstract sketch create simple connection feature.
  39. *
  40. * @param fp
  41. * the fp
  42. * @param name
  43. * the name
  44. * @param description
  45. * the description
  46. */
  47. public AbstractSketchCreateSimpleConnectionFeature(IFeatureProvider fp, String name, String description) {
  48. super(fp, name, description);
  49. }
  50. @SuppressWarnings("unused")
  51. public Connection create(ICreateConnectionContext context) {
  52. ICreateService cs = Graphiti.getCreateService();
  53. Anchor startAnchor = context.getSourceAnchor();
  54. Anchor endAnchor = context.getTargetAnchor();
  55. if (startAnchor == null && (context.getSourcePictogramElement() instanceof Connection)) {
  56. Shape s = SketchUtil.createConnectionPoint(context.getSourceLocation(), getDiagram());
  57. startAnchor = cs.createChopboxAnchor(s);
  58. Connection splitConnection = (Connection) context.getSourcePictogramElement();
  59. createConnection(splitConnection.getStart(), startAnchor);
  60. createConnection(startAnchor, splitConnection.getEnd());
  61. EcoreUtil.delete(splitConnection);
  62. }
  63. if (endAnchor == null && (context.getTargetPictogramElement() instanceof Connection)) {
  64. Shape s = SketchUtil.createConnectionPoint(context.getTargetLocation(), getDiagram());
  65. endAnchor = cs.createChopboxAnchor(s);
  66. Connection splitConnection = (Connection) context.getTargetPictogramElement();
  67. createConnection(splitConnection.getStart(), endAnchor);
  68. createConnection(endAnchor, splitConnection.getEnd());
  69. EcoreUtil.delete(splitConnection);
  70. }
  71. if (startAnchor == null || endAnchor == null) {
  72. return null;
  73. }
  74. Connection connection = createConnection(startAnchor, endAnchor);
  75. /* add bend point midway */
  76. if (false && connection instanceof FreeFormConnection) {
  77. GraphicsAlgorithm startGa = startAnchor.getParent().getGraphicsAlgorithm();
  78. GraphicsAlgorithm endGa = endAnchor.getParent().getGraphicsAlgorithm();
  79. int startAnchorX = startGa.getX() + startGa.getWidth() / 2;
  80. int startAnchorY = startGa.getY() + startGa.getHeight() / 2;
  81. int endAnchorX = endGa.getX() + endGa.getWidth() / 2;
  82. int endAnchorY = endGa.getY() + endGa.getHeight() / 2;
  83. int bendpointX = (startAnchorX + endAnchorX) / 2;
  84. int bendpointY = (startAnchorY + endAnchorY) / 2;
  85. Point bendpoint = Graphiti.getGaCreateService().createPoint(bendpointX, bendpointY);
  86. ((FreeFormConnection) connection).getBendpoints().add(bendpoint);
  87. }
  88. return connection;
  89. }
  90. /**
  91. * Creates the connection.
  92. *
  93. * @return the connection
  94. */
  95. protected abstract Connection createConnection();
  96. protected Connection createConnection(Anchor startAnchor, Anchor endAnchor) {
  97. Connection connection = createConnection();
  98. createVisualization(connection);
  99. setAnchors(startAnchor, endAnchor, connection);
  100. return connection;
  101. }
  102. protected void setAnchors(Anchor startAnchor, Anchor endAnchor, Connection connection) {
  103. connection.setStart(startAnchor);
  104. connection.setEnd(endAnchor);
  105. }
  106. protected void createVisualization(Connection connection) {
  107. Polyline p = Graphiti.getCreateService().createPolyline(connection);
  108. p.setLineWidth(3);
  109. p.setForeground(manageColor(IColorConstant.LIGHT_BLUE));
  110. p.setLineStyle(LineStyle.DASHDOT);
  111. }
  112. }