// (c): Matti J. Katila package org.fenfire.view; import org.fenfire.swamp.*; import org.fenfire.modules.pp.*; import org.fenfire.*; import org.fenfire.util.*; import org.fenfire.vocab.*; import java.rmi.RemoteException; /** FenPDF context what handles keystrokes and * accursed node at least. */ public class FenPDFContext { public static boolean dbg = true; private static void p(String s) { System.out.println("FenPDFContext:: "+s); } private Fen fen; private AlphContent alphContent; private RSTActions rst; private NodeFunction nodef; public FenPDFContext(Fen fen, RSTActions rst, NodeFunction nodef) { this.fen = fen; this.rst = rst; this.nodef = nodef; this.alphContent = new AlphContent(fen); } private Object accursed; public void setAccursed(Object node) { accursed=node; } private Object rstNode; public void setRSTNode(Object node) { if (RDFUtil.isNodeType(fen, node, RST.Canvas) || RDFUtil.isNodeType(fen, node, RST.Paragraph) || RDFUtil.isNodeType(fen, node, RST.Sentence) || RDFUtil.isNodeType(fen, node, RST.Node)) rstNode = node; // do nothing else throw new Error("No rst node to set!"); } public void handleKeyStroke(String stroke, View2D view) { try { if (accursed == null) throw new Error("no accursed set!"); p("s: "+stroke+", v: "+view); if (RDFUtil.isNodeType(fen, rstNode, RST.Sentence)) { p("rstNode is set to sentence - no other node handlers implemented"); Object sentence = rstNode; if (stroke.equals(" ")) { p("space pressed!"); return; } if (stroke.equals("Return")) { p("enter makes a new sentence."); return; } p("length"+stroke.length()); if (stroke.length() == 1) { Object node = RDFUtil.N(fen, RST.Node); alphContent.setText(node, stroke, true); rst.insertNode(sentence, node, getNodeNumInSentence(getLastNode(sentence)) + 1); rst.generateBasicSpatialCoords( rst.getRSTCanvas(rst.getParagraph(rstNode)), nodef); return; } } else p("No keystroke defined!"); } catch (RemoteException e) { p("RemoteException ocurred! "+e); return; }} private Object getFirstNode(Object sentence) throws RemoteException { if (! RDFUtil.isNodeType(fen, sentence, RST.Sentence)) throw new Error("sentence is not sentence! "+sentence); return fen.graph.find1_11X(sentence, RST.nextNode); } private Object getLastNode(Object sentence) throws RemoteException { p("go last"); Object n = getFirstNode(sentence); Object next = fen.graph.find1_11X(n, RST.nextNode); while (next != null) { n = next; next = fen.graph.find1_11X(n, RST.nextNode); } p("go last DONE"); return n; } private int getNodeNumInSentence(Object node) throws RemoteException { p("go node"); Object n = getFirstNode(rst.getSentence(node)); Object next = fen.graph.find1_11X(n, RST.nextNode); int i = 0; while (n != node || next != null) { n = next; next = fen.graph.find1_11X(n, RST.nextNode); i++; } p("go node DONE"); if (n != node && next == null) throw new Error("node not found in sentence"); return i; } }