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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jgraph.JGraph;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.VertexView;
public class AffichageArbre extends JScrollPane{
JScrollPane scroll;
public AffichageArbre(ModeleArbre mod){
// Construct Model and Graph
GraphModel model = mod;
final JGraph graph = new JGraph(model);
graph.setEditable(false);
graph.setMoveable(false);
// Enable edit without final RETURN keystroke
graph.setInvokesStopCellEditing(true);
graph.getGraphLayoutCache().setFactory(new JComponentCellViewFactory());
// When over a cell, jump to its default port (we only have one, anyway)
graph.setJumpToDefaultPort(true);
//MouseListener
graph.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getClickCount() == 2) {
// Get Cell under Mousepointer
int x = e.getX(), y = e.getY();
ModelePers cell = (ModelePers) graph.getFirstCellForLocation(x, y);
System.out.println("cell : " + cell + "fini");
new NewPerson(cell);
}
}
});
// Insert all three cells in one call, so we need an array to store them
DefaultGraphCell[] cells = new DefaultGraphCell[3];
// Create Salut Vertex
cells[0] = createVertex(20,40);
// Create World Vertex
cells[1] = createVertex(120, 160);
// Create Edge
DefaultEdge edge = new DefaultEdge();
// Fetch the ports from the new vertices, and connect them with the edge
edge.setSource(cells[0].getChildAt(0));
edge.setTarget(cells[1].getChildAt(0));
cells[2] = edge;
// Set Arrow Style for edge
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
// Insert the cells via the cache, so they get selected
graph.getGraphLayoutCache().insert(cells);
// Show in Frame
this.add(graph);
//frame.getContentPane().add(scroll);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
//frame.setVisible(true);
}
public static DefaultGraphCell createVertex(int x, int y) {
double W = 200;
double H = 70;
// Create vertex with the given name
ModelePers cell = new ModelePers(new Person(new AttributPerson(false, "Vincent",
"Rollin",new GregorianCalendar(789, Calendar.MAY, 5),"Dechy",
new GregorianCalendar(1992, Calendar.MAY, 5),null,null,null,null),null));
// Set bounds
GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(
x, y, W, H));
//set VertexView
VertexView vertex = new MyVertexView(cell);
// Set fill color
GraphConstants.setGradientColor(cell.getAttributes(), Color.GREEN);
GraphConstants.setOpaque(cell.getAttributes(), true);
// Set raised border
GraphConstants.setBorder(cell.getAttributes(), BorderFactory
.createRaisedBevelBorder());
// Add a Port
DefaultPort port = new DefaultPort();
cell.add(port);
port.setParent(cell);
return cell;
}
private class JComponentCellViewFactory extends DefaultCellViewFactory
{
protected VertexView createVertexView(Object objCell)
{
DefaultGraphCell cell = (DefaultGraphCell) objCell;
VertexView vertex = null;
vertex = new MyVertexView((ModelePers) cell);
return vertex;
}
}
} |
Partager