import java.awt.Component; import java.awt.Dimension; import java.io.IOException; import java.util.List; import java.util.LinkedList; import java.util.Random; import javax.swing.*; import javax.swing.text.*; import javax.swing.text.html.*; import se.chalmers.cs.gf.gfcutil.*; import se.chalmers.cs.gf.linearize.GFCLinearizer; import se.chalmers.cs.gf.linearize.Linearizer; import se.chalmers.cs.gf.util.FileUtil; public class TreePanel extends JPanel { private Linearizer linearizer; public TreePanel(Linearizer linearizer) { this.linearizer = linearizer; setPreferredSize(new Dimension(400,400)); // setLayout(null); } private void addNode(Function fun, int x, int y) { Node n = new Node(fun); NodeView v = new NodeView(n, linearizer); add(v); v.setLocation(x,y); } private void updateTexts() { // FIXME: it's a bit hacky that the only // way to get at the node models are through their views for (Component c : getComponents()) { if (c instanceof NodeView) { NodeView nv = (NodeView)c; nv.updateText(); } } } public static void main(String[] args) throws IOException { GFCMModules gfcm = loadGFCM("test.gfcm"); GFCModule cnc = gfcm.getMainConcreteModules().get(0); Linearizer lin = new GFCLinearizer(cnc); System.out.println("Using grammar: " + lin.getName()); TreePanel tp = new TreePanel(lin); addFunNodes(tp, gfcm); tp.updateTexts(); JFrame f = new JFrame(); f.setContentPane(tp); f.pack(); f.setVisible(true); } private static void addFunNodes(TreePanel tp, GFCMModules gfcm) { GFCModule abs = gfcm.getMainAbstractModule(); String[] funs = {"PhrImp","ImpV2","ImpV","float_V","fight_V2","UsePron","they_NP"}; Dimension d = tp.getPreferredSize(); Random rnd = new Random(); for (Function fun : abs.listFunctions()) { // for (String f : funs) { // Function fun = abs.findFunction(f); int x = rnd.nextInt(d.width); int y = rnd.nextInt(d.height); tp.addNode(fun, x, y); } } public static GFCMModules loadGFCM(String gfcmFile) throws IOException { String coding = "UTF-8"; GFCMModules ms = GFCMModules.loadGFCM( FileUtil.readResource(gfcmFile, coding)); return ms; } }