Skip to content

Commit

Permalink
Fixing self relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed May 29, 2017
1 parent fba4363 commit aa2607b
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.eclipse.draw2d.ConnectionAnchor
import org.eclipse.draw2d.geometry.Point
import org.eclipse.draw2d.geometry.PointList
import org.uqbar.project.wollok.ui.diagrams.classes.anchors.DefaultWollokAnchor
import org.uqbar.project.wollok.ui.diagrams.classes.anchors.SelfReferenceAnchor

/**
* Routes the connection in a tree-like form unifying connections
Expand Down Expand Up @@ -60,14 +61,19 @@ class SquareConnectionRouter extends AbstractRouter {
}

override protected getStartPoint(Connection conn) {
// Fix: anchor when source is below
if (conn.sourceAnchor === conn.targetAnchor) {
return new SelfReferenceAnchor(conn.sourceAnchor.owner).referencePoint
}
if (conn.sourceAnchor?.below(conn.targetAnchor)) {
return new DefaultWollokAnchor(conn.sourceAnchor.owner).referencePoint
}
conn.sourceAnchor.referencePoint
}

override protected getEndPoint(Connection conn) {
if (conn.sourceAnchor === conn.targetAnchor) {
return new SelfReferenceAnchor(conn.sourceAnchor.owner).getLocation(endPoint)
}
conn.targetAnchor?.getLocation(endPoint)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class Messages extends NLS {
// Tool entries
public static String StaticDiagram_CreateAssociation_Title;
public static String StaticDiagram_CreateAssociation_Description;
public static String StaticDiagram_CreateDependency_Title;
public static String StaticDiagram_CreateDependency_Description;

// Contextual menu options
public static String StaticDiagram_DeleteAssociation_Description;
Expand All @@ -30,6 +32,7 @@ public class Messages extends NLS {

// Error messages
public static String StaticDiagram_Association_Not_Found;
public static String StaticDiagram_InheritanceCannotBeSelfRelated;

static {
// initialize resource bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,16 @@ import static extension org.uqbar.project.wollok.model.WMethodContainerExtension
import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*

/**
*
* Main view for static diagrams. Here we show
* - classes
* - named objects
* - mixins
* and its relationships, that includes inheritance, association and dependency.
*
* @author jfernandes
* @author dodain
*
*/
class StaticDiagramView extends ViewPart implements ISelectionListener, ISourceViewerAware, IPartListener, ISelectionProvider, ISelectionChangedListener, IDocumentListener, Observer {
DefaultEditDomain editDomain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import org.eclipse.draw2d.IFigure
import org.eclipse.draw2d.geometry.Point
import org.eclipse.draw2d.geometry.Rectangle

/**
*
* Default implementation for connections between shapes, assuming
* inheritance was the first automatic relationship we could infer from classes.
*
* @author dodain
*
*/
class DefaultWollokAnchor extends ChopboxAnchor {

new() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.eclipse.draw2d.geometry.Point
* Specific anchor for Objects.
* Connector should begin at bottom-center
* Thanks to Javi & https://books.google.com.ar/books?id=GiKTAR9M-L4C&pg=PA70&lpg=PA70&dq=chopboxanchor+connection&source=bl&ots=3FnJ0ifwyQ&sig=gQyaXEFpD-JQuYFsNcRJeGWR0Bw&hl=es&sa=X&ved=0ahUKEwiL5JCskYjOAhUHjJAKHbN_CLMQ6AEIOjAE#v=onepage&q=chopboxanchor%20connection&f=false
*
* @author dodain
*/
class NamedObjectWollokAnchor extends DefaultWollokAnchor {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.uqbar.project.wollok.ui.diagrams.classes.anchors

import org.eclipse.draw2d.IFigure
import org.eclipse.draw2d.geometry.Point

/**
*
* Anchor for self references like associations (and nothing less).
* Current implementation uses bottom right corner to avoid collisions with other
* inheritance and association connectors.
*
* @author dodain
*
*/
class SelfReferenceAnchor extends DefaultWollokAnchor {

public static int DELTA = 5

new(IFigure owner) {
super(owner)
}

override getReferencePoint() {
val point = owner.bounds.bottomRight.translateToAbsolute
new Point(point.x - (DELTA * 5), point.y)
}

override getLocation(Point reference) {
val point = owner.bounds.bottomRight.translateToAbsolute
new Point(point.x - DELTA, point.y)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import org.eclipse.draw2d.geometry.Point
import org.uqbar.project.wollok.ui.diagrams.classes.model.Connection
import org.uqbar.project.wollok.ui.diagrams.classes.model.Shape

/**
*
* Special anchor for associations. Connector goes from left to right
* or from right to left depending on source/target positions.
*
* @author dodain
*
*/
class WollokAssociationAnchor extends DefaultWollokAnchor {

Shape source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import static extension java.lang.Integer.*
import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*

/**
* Common class for modeling
* - classes
* - named objects (wko)
* - mixins
* as a models of a static diagram
*
* Main responsibilities includes: size & location of shapes and naming convention
*/
abstract class AbstractModel extends Shape {

protected static List<WMethodContainer> allComponents = newArrayList
Expand All @@ -24,7 +33,7 @@ abstract class AbstractModel extends Shape {

protected val static ELEMENT_WIDTH = 50
protected val static MAX_ELEMENT_WIDTH = 230
protected val static MAX_ELEMENT_HEIGHT = 320
protected val static MAX_ELEMENT_HEIGHT = 280
protected val static ELEMENT_HEIGHT = 55
protected val static WIDTH_SEPARATION_BETWEEN_ELEMENTS = 20
protected val static INITIAL_MARGIN = 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Connection extends ModelElement {

new(String name, Shape source, Shape target, RelationType relationType) {
this.name = name
reconnect(source, target)
reconnect(source, target, relationType)
this.lineStyle = calculateLineStyle()
this.relationType = relationType
}
Expand Down Expand Up @@ -80,16 +80,17 @@ class Connection extends ModelElement {
}
}

def reconnect(Shape newSource, Shape newTarget) {
if (newTarget == null || newSource == newTarget) {
def reconnect(Shape newSource, Shape newTarget, RelationType relationType) {
if (newTarget == null) {
throw new IllegalArgumentException("New target for connection cannot be null")
}
relationType.validateRelationBetween(newSource, newTarget)
disconnect
this.source = newSource
this.target = newTarget
reconnect
}

def setLineStyle(int lineStyle) {
if (lineStyle != Graphics.LINE_DASH && lineStyle != Graphics.LINE_SOLID)
throw new IllegalArgumentException
Expand All @@ -106,6 +107,3 @@ class Connection extends ModelElement {

}

enum RelationType {
INHERITANCE, ASSOCIATION, DEPENDENCY
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.uqbar.project.wollok.ui.diagrams.classes.model;

import org.uqbar.project.wollok.ui.diagrams.Messages;

public enum RelationType {

INHERITANCE, ASSOCIATION, DEPENDENCY;

public void validateRelationBetween(Shape source, Shape target) {
if (source == target && this == RelationType.INHERITANCE) {
throw new IllegalArgumentException(Messages.StaticDiagram_InheritanceCannotBeSelfRelated);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ public abstract class Shape extends ModelElement {
}

def void addConnection(Connection conn) {
if (conn == null || conn.source == conn.target)
if (conn == null)
throw new IllegalArgumentException
if (conn.source == this) {
sourceConnections.add(conn)
firePropertyChange(SOURCE_CONNECTIONS_PROP, null, conn)
} else if (conn.target == this) {
}
if (conn.target == this) { // source && target could be the same for associations
targetConnections.add(conn)
firePropertyChange(TARGET_CONNECTIONS_PROP, null, conn)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class StaticDiagramPaletterFactory {
]
}

// https://es.slideshare.net/XiaoranWang/gef-tutorial-2005
// https://www.google.com.ar/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&sqi=2&ved=0ahUKEwiM_77-94PUAhVCEpAKHcAqCuIQFggqMAE&url=https%3A%2F%2Fwww.eclipse.org%2Fgef%2Freference%2FGEF%2520Tutorial%25202005.ppt&usg=AFQjCNEnG7SJTS_p4BFRyd6juW3De6jj5A&sig2=40_0UwdSLd_ptjYPBeFtvA
// https://wiki.eclipse.org/Graphical_Modeling_Framework/Tutorial/Part_1
}

/** http://www.vainolo.com/2011/07/06/creating-a-gef-editor-part-6-model-refactoring-and-editing-diagram-entities/ */
Expand All @@ -43,26 +40,25 @@ class CreateAssociationToolEntry extends ConnectionCreationToolEntry {

def static getIcon() {
try {
ImageDescriptor.createFromFile(typeof(StaticDiagramPaletterFactory), "/icons/association.png")
ImageDescriptor.createFromFile(typeof(StaticDiagramPaletterFactory), "/icons/dependency_create.png")
} catch (MalformedURLException e) {
throw new RuntimeException(e)
}
}

}

class HideClassToolEntry extends ToolEntry {

class CreateDependencyToolEntry extends ConnectionCreationToolEntry {
new() {
super(Messages.StaticDiagram_HideClassFromDiagram_Title, Messages.StaticDiagram_HideClassFromDiagram_Description, icon, icon)
super(Messages.StaticDiagram_CreateDependency_Title, Messages.StaticDiagram_CreateDependency_Description, new SimpleFactory(null), icon, icon)
}

def static getIcon() {
try {
ImageDescriptor.createFromFile(typeof(StaticDiagramPaletterFactory), "/icons/hideClass.png")
ImageDescriptor.createFromFile(typeof(StaticDiagramPaletterFactory), "/icons/association.png")
} catch (MalformedURLException e) {
throw new RuntimeException(e)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ StaticDiagram_ShowHiddenComponents_Description = Show all hidden components agai

# Tool entries
StaticDiagram_CreateAssociation_Title = Create association
StaticDiagram_CreateAssociation_Description = Create an association relationship
StaticDiagram_CreateAssociation_Description = Create an association relationship (has)
StaticDiagram_CreateDependency_Title = Create dependency
StaticDiagram_CreateDependency_Description = Create dependency relationship (uses)

# Contextual menu options for nodes (added in action registry)
StaticDiagram_DeleteAssociation_Description = Delete association
StaticDiagram_HideClassFromDiagram_Title = Hide component from diagram
StaticDiagram_HideClassFromDiagram_Description = Hide component from static diagram

# Error messages
StaticDiagram_Association_Not_Found = Association between {0} and {1} not found
StaticDiagram_Association_Not_Found = Association between {0} and {1} not found
StaticDiagram_InheritanceCannotBeSelfRelated = Inheritance relationship cannot be self related
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ StaticDiagram_CleanAllRelationships_Description = Eliminar las relaciones de aso
StaticDiagram_ShowHiddenComponents_Description = Volver a mostrar nuevamente los componentes ocultos

# Acciones de entrada del menú en la palette
StaticDiagram_CreateAssociation_Title = Crear associación
StaticDiagram_CreateAssociation_Description = Crear una relación de asociación
StaticDiagram_CreateAssociation_Title = Crear asociación
StaticDiagram_CreateAssociation_Description = Crear una relación de asociación (conoce)
StaticDiagram_CreateDependency_Title = Crear dependencia
StaticDiagram_CreateDependency_Description = Crear una relación de dependencia (usa)

# Opciones del menú contextual para nodos (botón derecho)
StaticDiagram_HideClassFromDiagram_Title = Ocultar componente del diagrama
StaticDiagram_HideClassFromDiagram_Description = Ocultar componente del diagrama
StaticDiagram_DeleteAssociation_Description = Eliminar la relación de asociación

# Mensajes de error
StaticDiagram_Association_Not_Found = No se encontró la asociación entre {0} y {1}
StaticDiagram_Association_Not_Found = No se encontró la asociación entre {0} y {1}
StaticDiagram_InheritanceCannotBeSelfRelated = No se puede generar una relación de herencia consigo mismo

0 comments on commit aa2607b

Please sign in to comment.