[Bio] / Speck / JGLayout2.py Repository:
ViewVC logotype

View of /Speck/JGLayout2.py

Parent Directory Parent Directory | Revision Log Revision Log

Revision - (download) (as text) (annotate) (vendor branch)
Thu Sep 9 19:54:06 2004 UTC (15 years, 2 months ago) by sheradon
Branch: init, MAIN
CVS Tags: v0, HEAD
Changes since 1.1: +0 -0 lines
Initial load to CVS

import sys, os, cPickle
from java.util import Map, Hashtable
from org.jgraph.graph import *
from org.jgraph import JGraph
from org.jgraph.util import JGraphUtilities
from org.jgraph.layout import *
from java.awt import Rectangle
import JGLayout3

This is the part that talks to the JGraph Java library.
This file should NOT be run by any user! It takes the input file
from JGLayout.py and writes its results back there when done.

Note: Everything in this file must be Python 2.1 compatible!

if __name__ == "__main__":
	if not len(sys.argv) == 2: sys.exit(-1)

	filename = sys.argv[1]
	f = open(filename, 'r')
	G = cPickle.load(f)
	sizes = cPickle.load(f)

	# Build graphmodel from G, putting names in
	# -----------------------------------------
	GM = DefaultGraphModel()
	JG = JGraph(GM)
	cells = {}		# mapping from nodename to cell-object. for my use only
	attributes = Hashtable()
	cs = ConnectionSet()
	# Make node/cell objects...
	for name in G.keys():
		cell = DefaultGraphCell(name)
		cells[name] = cell
		cellAttrib = GM.createAttributes()
		rect = Rectangle(sizes[name][0], sizes[name][1])
		GraphConstants.setBounds(cellAttrib, rect)
		attributes.put(cell, cellAttrib)
	# Make edges...
	for source in G.keys():
		for target in G[source]:
			edge = DefaultEdge()
			cells['%s -> %s' % (source, target)] = edge
			edgeAttrib = GM.createAttributes()
			attributes.put(edge, edgeAttrib)
			sp, tp = DefaultPort(), DefaultPort()
			cs.connect(edge, sp, tp)
	# And stuff them all into the graph
	GM.insert(cells.values(), attributes, cs, None, None)

	# Run layout on it
	# ----------------------------
	#JGraphUtilities.applyLayout(JG, JGLayout3())
	#JGraphUtilities.applyLayout(JG, SpringEmbeddedLayoutAlgorithm())
	JGraphUtilities.applyLayout(JG, TreeLayoutAlgorithm())

	# Extract names/coords from each node, put in mapping
	# coords[node] = (x, y, width, height)
	# ------------------------------------------------------
	coords = {}
	bb = [0, 0, 1, 1]
	views = JG.getGraphLayoutCache().getMapping(cells.values())
	for i in range(len(cells)):
		view = views[i]
		#print cells.keys()[i]+':',
		if isinstance(view, VertexView):
			rect = view.getBounds()
			x, y, width, height = rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight()
			#print '(%d, %d) and %dx%d' % (x, y, width, height)
			bb[0], bb[1] = min(x-width, bb[0]), min(y-height, bb[1])
			bb[2], bb[3] = max(x+width, bb[2]), max(y-height, bb[3])
			coords[cells.keys()[i]] = (x, y, width, height)
		else: # isinstance(view, EdgeView):
			jPoints = view.getPoints()
			pPoints = []
			for jP in jPoints:
				x, y = jP.getBounds().getX(), jP.getBounds().getY()
				#print '(%d, %d)' % (x, y),
				pPoints.append((x, y))
			#print ''
			coords[cells.keys()[i]] = pPoints
	coords['_bb'] = tuple(bb)

	# Pickle mapping to file
	# ------------------------
	f = open(filename, 'w')
	cPickle.dump(coords, f)

	# We might need to quit just because a JGraph likes to linger around & not return...

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3