为什么要使用CellView,而不是直接用Cell来处理视图表现,JTree之类是没有JTreeView的,JTree和一个renderor就完成所有事情(JGraph是swing的继承,所以经常比较两者)。
因为:
JGraph中component比swing中复杂,显示任务也就比swing的component重。
GraphModel和GraphLayoutCache已经完整表达了图的概念,对于那些只想做基于图的分析的人来说,这已经足够了。但是对于需要复杂显示的人来说还不够。
有了CellView似乎对同一个Cell采用不同的显示时,更方便实现。
|
public class MyView extends AbstractCellView {
protected static MyRenderer renderer = new MyRenderer();
public MyView() {
super();
}
public MyView(Object arg0) {
super(arg0);
}
public CellViewRenderer getRenderer() {
return renderer;
}
public Point2D getPerimeterPoint(EdgeView edge, Point2D source,
Point2D p) {
if (getRenderer() instanceof MyRenderer)
return ((MyRenderer)
getRenderer()).getPerimeterPoint(this,
source, p);
return super.getPerimeterPoint(edge, source, p);
}
public static class MyRenderer extends JLabel implements
CellViewRenderer, Serializable {
public void paint(Graphics g) {
}
public Component getRendererComponent(JGraph graph, CellView
view, boolean sel, boolean focus, boolean preview) {
}
public Point2D getPerimeterPoint(VertexView view, Point2D
source, Point2D p) {
}
}
}
|
public CellView createView(GraphModel model, Object cell) {
CellView view = null;
if (model.isPort(cell))
view = createPortView(cell);
else if (model.isEdge(cell))
view = createEdgeView(cell);
else
view = createVertexView(cell);
return view;
}
protected VertexView createVertexView(Object cell) {
if (cell instanceof MyVertex) {
return new MyVertexView(cell);
}
return new VertexView(cell);
}
protected EdgeView createEdgeView(Object cell) {
return new EdgeView(cell);
}
protected PortView createPortView(Object cell) {
return new PortView(cell);
}
|
List listEdges = new ArrayList();
int numChildren = model.getChildCount(cell);
for (int i = 0; i < numChildren; i++) {
Object port = model.getChild(cell, i);
if (model.isPort(port)) {
Iterator iter = model.edges(port);
while (iter.hasNext()) {
listEdges.add(iter.next());
}
}
}
|
Object sourceVertex = model.getParent(model.getSource(edge));
Object targetVertex = model.getParent(model.getTarget(edge));
|