Skip to content

Instantly share code, notes, and snippets.

Created February 5, 2013 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4712857 to your computer and use it in GitHub Desktop.
Save anonymous/4712857 to your computer and use it in GitHub Desktop.
Edge and vertex overlapping in mxStack Layout
import java.awt.{ Dimension}
import scala.swing.{ BoxPanel, Component}
import scala.swing.{ Orientation}
import com.mxgraph.layout.mxStackLayout
import com.mxgraph.model.mxCell
import com.mxgraph.swing.mxGraphComponent
import com.mxgraph.view.mxGraph
import com.mxgraph.util.mxConstants
import com.mxgraph.view.mxEdgeStyle
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout
import com.mxgraph.layout.mxOrganicLayout
import com.mxgraph.layout.mxCompactTreeLayout
class ClassDiagramPanel extends BoxPanel(Orientation.Vertical) {
preferredSize = new Dimension(800, 600)
private def createGraph(graphPopulater: mxGraph => Unit) = {
contents.clear
val graph = new mxGraph
val style = graph.getStylesheet.getDefaultEdgeStyle //.put(mxConstants., mxEdgeStyle.EntityRelation)
graph.getModel.beginUpdate()
graphPopulater(graph)
graph.getModel.endUpdate()
val layout = new mxStackLayout(graph, true, 30)
layout.execute(graph.getDefaultParent)
val graphComponent = new mxGraphComponent(graph)
graphComponent.setEnabled(false)
contents += Component.wrap(graphComponent)
revalidate
}
private def makeClassNode(collectionName: String, graph: mxGraph) = {
val defParent = graph.getDefaultParent
val parent = graph.insertVertex(defParent, null, collectionName, 20, 20, 80, 24, "fillColor=#AAAAFF;fontSize=22;fontStyle=1;fontFamily=Ubuntu, Arial, Helvetica, sans-serif;fontColor=#000000")
var vertices = List[AnyRef]()
(1 to 9).map("e " + ) foreach { value =>
val vertex = graph.insertVertex(parent, null, value, 0, 0, 80, 24, "fillColor=#AAAAAA;fontSize=18;fontFamily=Ubuntu, Arial, Helvetica, sans-serif;portConstraint=eastwest;fontColor=#000000")
vertices :+= vertex
}
val allCells = parent :: vertices
val layout = new mxStackLayout(graph, false)
allCells.foreach(graph.updateCellSize)
allCells.foreach(layout.execute)
vertices
}
private val randomGenerator = new java.util.Random(1)
private def getRandomInt(max:Int) = randomGenerator.nextInt(max)
private def pickRandom[T](l:Seq[T]) = l(getRandomInt(l.length))
private def pickRandom[T](l:Seq[T], avoid: T) = {
var chosen:T = avoid
while (avoid equals chosen) {
chosen = l(getRandomInt(l.length))
}
chosen
}
private def getAnEdge(verticeList:List[List[Any]]) = {
val firstParent = pickRandom(verticeList)
val firstCell = pickRandom(firstParent)
val secondParent = pickRandom(verticeList, firstParent)
val secondCell = pickRandom(secondParent)
(firstCell -> secondCell)
}
def drawRelation = {
createGraph { graph =>
var vertexRelations = List[List[Any]]()
(1 to 8).map("obj "+) foreach { name =>
val vertexRelation = makeClassNode(name, graph)
vertexRelations :+= vertexRelation
}
val defParent = graph.getDefaultParent
(0 until 4) foreach {i =>
val edge = getAnEdge(vertexRelations)
graph.insertEdge(defParent, null, " ", edge._1, edge._2, "edgeStyle=orthogonalEdgeStyle;strokeWidth=5;strokeColor=#8AA63D;")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment