// (c) Tuomas J. Lukka and Matti Katila package org.fenfire.structure; import java.util.Iterator; import org.fenfire.vocab.RDF; import org.fenfire.vocab.CANVAS2D; import org.fenfire.swamp.*; import org.fenfire.util.RDFUtil; import java.util.*; /** Some utility methods for handling canvas2d's. */ public class Canvas2D { ConstGraph constGraph; Graph graph; public Canvas2D(ConstGraph g) { if(g instanceof Graph) this.graph = (Graph)g; this.constGraph = g; } public boolean isCanvas(Object node) { return RDFUtil.isNodeType(constGraph, node, CANVAS2D.Canvas); } /** Get an iterator over the nodes on the canvas. */ public Iterator getNodesOn(Object canvas) { return constGraph.findN_11X_Iter(canvas, CANVAS2D.contains); } /** Get the canvas (if any) that the given node is on. * If the node is (erroneously) on several canvases, return them all. * @return The canvas, or null if none. */ public Object getCanvas(Object node) { Iterator it = constGraph.findN_X11_Iter(CANVAS2D.contains, node); if(it.hasNext()) return it.next(); return null; } /** Get the location of a node on the canvas. * If the node given is not valid, will throw something. * @param node The node whose coordinates we want. * @param into A 2-element float array into which to place them. */ public void getCoordinates(Object node, float[] into) { into[0] = RDFUtil.getFloat(constGraph, node, CANVAS2D.x); into[1]= RDFUtil.getFloat(constGraph, node, CANVAS2D.y); } /** Make the given node be a Canvas object. */ public void makeIntoCanvas(Object node) { graph.add(node, RDF.type, CANVAS2D.Canvas); } /** Place the given node on this canvas. */ public void placeOnCanvas(Object canvas, Object node, float x, float y) { graph.add(canvas, CANVAS2D.contains, node); setCoordinates(node, x, y); } public void setCoordinates(Object node, float x, float y) { // delete all old coords graph.rm_11A(node, CANVAS2D.x); graph.rm_11A(node, CANVAS2D.y); // XXX graph.set1_11X(node, CANVAS2D.x, Nodes.getStringLiteral(""+x) ); graph.set1_11X(node, CANVAS2D.y, Nodes.getStringLiteral(""+y) ); } }