1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
public class ClickHandler extends JFrame
{
public ClickHandler()
{
super("myTree");
final mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
final mxCompactTreeLayout m = new mxCompactTreeLayout(graph, false);
graph.getModel().beginUpdate();
try
{
graph.setCellsMovable(true);
Object v1 = graph.insertVertex(parent,
"treeRoot",
"Organization",
400, 20, 80, 30);
}
finally
{
graph.getModel().endUpdate();
}
final mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
graphComponent.getGraphControl().addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
Object cell = graphComponent.getCellAt(e.getX(), e.getY());
if (cell != null)
{
graph.getModel().beginUpdate();
try
{
addChild(graph, cell);
m.execute(graph.getDefaultParent());//on applique le treeLayout
}
finally
{
graph.getModel().endUpdate();
}
}
}
});
}
public void addChild(mxGraph graph, Object cell)
{
mxIGraphModel model = graph.getModel();
Object parent = graph.getDefaultParent();
model.beginUpdate();
try
{
Object vertex = graph.insertVertex(parent, "v2",
"set name",
0,
0,
80, 30);
Object edge = graph.insertEdge(parent, null, "", cell, vertex);
}
finally
{
model.endUpdate();
}
};
public static void main(String[] args)
{
ClickHandler frame = new ClickHandler();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 520);
frame.setVisible(true);
}
} |
Partager