graphview.java

来自「Hibernate开发及整合应用大全 蔡雪焘编著 本书用典型的示例剖析Hiber」· Java 代码 · 共 204 行

JAVA
204
字号
/* * Created on 12-08-2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */package net.sf.hibernate.console.views;import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.Rectangle;import java.util.ArrayList;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.swing.BorderFactory;import javax.swing.tree.TreeNode;import net.sf.hibernate.HibernateException;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.cfg.Configuration;import net.sf.hibernate.metadata.ClassMetadata;import net.sf.hibernate.type.EntityType;import net.sf.hibernate.type.Type;import org.jgraph.JGraph;import org.jgraph.graph.AttributeMap;import org.jgraph.graph.ConnectionSet;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.Port;public class GraphView implements SessionFactoryView {	JGraph graph = null;		public GraphView() {		graph = new JGraph();		graph.setModel(new DefaultGraphModel());	}	public void factoryCreated(Configuration configuration, SessionFactory sf) {		graph.setModel(buildGraphModel(sf));		graph.setAntiAliased(true);			}	public Container getContainer() {		return graph;	}		/**	 * @param sf2	 * @return	 */	private GraphModel buildGraphModel(SessionFactory sf2) {		GraphModel model = new DefaultGraphModel();		ConnectionSet cs = new ConnectionSet();		Map attributes = new Hashtable();		// Styles For Implement/Extend/Aggregation		AttributeMap implementStyle = new AttributeMap();		GraphConstants.setLineBegin(implementStyle, GraphConstants.ARROW_TECHNICAL);		GraphConstants.setBeginSize(implementStyle, 10);		GraphConstants.setDashPattern(implementStyle, new float[] { 3, 3 });		Font deriveFont = GraphConstants.DEFAULTFONT.deriveFont(10);		GraphConstants.setFont(implementStyle, deriveFont);		AttributeMap extendStyle = new AttributeMap();		GraphConstants.setLineBegin(extendStyle, GraphConstants.ARROW_TECHNICAL);		GraphConstants.setBeginFill(extendStyle, true);		GraphConstants.setBeginSize(extendStyle, 10);		GraphConstants.setFont(extendStyle, deriveFont);		AttributeMap aggregateStyle = new AttributeMap();		GraphConstants.setLineBegin(aggregateStyle, GraphConstants.ARROW_DIAMOND);		GraphConstants.setBeginFill(aggregateStyle, true);		GraphConstants.setBeginSize(aggregateStyle, 6);		GraphConstants.setLineEnd(aggregateStyle, GraphConstants.ARROW_SIMPLE);		GraphConstants.setEndSize(aggregateStyle, 8);		//GraphConstants.setLabelPosition(aggregateStyle, new Point(500, 1200));		GraphConstants.setFont(aggregateStyle, deriveFont);		GraphConstants.setLineStyle(aggregateStyle, GraphConstants.STYLE_BEZIER);		// Model Column		//DefaultGraphCell rootsf = new DefaultGraphCell(sf2);		//attributes.put(rootsf, createBounds(20, 100, Color.blue));		//rootsf.add(new DefaultPort("SessionFactory/Center"));		Map map;		try {			map = sf2.getAllClassMetadata();		} catch (HibernateException e) {			throw new RuntimeException("buildGraphModel", e);		}		Map class2Node = new HashMap();		Map node2class = new HashMap();		List cms = new ArrayList();						cms.addAll(map.values());		List nodes = new ArrayList();		for (Iterator iter = cms.iterator(); iter.hasNext();) {			ClassMetadata element = (ClassMetadata) iter.next();			//element.getMappedClass().getName().substring(element.getMappedClass().getName().lastIndexOf("."));			DefaultGraphCell dgm = new DefaultGraphCell(getShortName(element));						dgm.add(new DefaultPort(element.getMappedClass() + "/Center"));			nodes.add(dgm);			class2Node.put(element.getMappedClass(), dgm);			node2class.put(dgm, element);		}			int count = nodes.size();				if(count == 0) return model;				int square = (int) Math.sqrt(count);		int nodesperrow = count/square;				int row=0;		int col=0;		for(int p = 0; p<nodes.size(); p++) {			col++;			DefaultGraphCell cell = (DefaultGraphCell) nodes.get(p);			attributes.put(cell, createBounds(col*150, row*100, Color.blue));			if(col>=nodesperrow) {				col = 0;				row++;			}		}				List edges = new ArrayList();	    int childCount = cms.size();	    for(int i = 0; i<childCount;i++) {	    	if((nodes.get(i) instanceof DefaultPort)) continue;	    	DefaultGraphCell node = (DefaultGraphCell) nodes.get(i);	    	ClassMetadata md = (ClassMetadata) node2class.get(node);	    	Port sourcePort = (Port) node.getChildAt(0);	    	Type[] types = md.getPropertyTypes();	    	for (int j = 0; j < types.length; j++) {				Type type = types[j];				if(type.isEntityType()) {					DefaultEdge propertyEdge = new DefaultEdge(md.getPropertyNames()[j]);					EntityType et = (EntityType)type;					DefaultGraphCell target = (DefaultGraphCell) class2Node.get(et.getReturnedClass());					attributes.put(propertyEdge, aggregateStyle);										TreeNode targetPort = target.getChildAt(0);					edges.add(propertyEdge);					cs.connect(propertyEdge, sourcePort, targetPort);														} else {					//?				} 			}	    }	    		// Insert Cells into model		List allOs = new ArrayList();				allOs.addAll(class2Node.values());		allOs.addAll(edges);		Object[] cells = allOs.toArray();		model.insert(cells, attributes, cs, null, null);		return model;	}	private String getShortName(ClassMetadata element) {		String str = element.getMappedClass().getName();		int indexOf = str.lastIndexOf('.');		if(indexOf>0) {			return str.substring(indexOf+1);		} else {			return str;		}	}	/**		 * Returns an attributeMap for the specified position and color.		 */		public static Map createBounds(int x, int y, Color c) {			AttributeMap map = new AttributeMap();			GraphConstants.setBounds(map, new Rectangle(x, y, 90, 30));			GraphConstants.setBorder(map, BorderFactory.createRaisedBevelBorder());			GraphConstants.setBackground(map, c.darker());			GraphConstants.setForeground(map, Color.white);			GraphConstants.setFont(map, GraphConstants.DEFAULTFONT.deriveFont(Font.BOLD, 12));			GraphConstants.setOpaque(map, true);			return map;		}	public void factoryUpdated(SessionFactory sf) {					}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?