public void nodeAdded(String fqn) { MyNode n, p; n = root.add(fqn); if (n != null) { p = (MyNode) n.getParent(); tree_model.reload(p); jtree.scrollPathToVisible(new TreePath(n.getPath())); } }
private String textGen(TreePath p) { MyNode selectedNode = (MyNode) p.getLastPathComponent(); String text = "men allt som"; int n = selectedNode.getPath().length; for (int i = n - 1; i >= 0; i--) { text = text + " är " + selectedNode.getPath()[i].toString(); } return text; }
public void nodeRemoved(String fqn) { MyNode n; TreeNode par; n = root.findNode(fqn); if (n != null) { n.removeAllChildren(); par = n.getParent(); n.removeFromParent(); tree_model.reload(par); } }
MyNode findNode(String fqn) { MyNode curr, n; StringTokenizer tok; String child_name; if (fqn == null) return null; curr = this; tok = new StringTokenizer(fqn, ReplicatedTreeView.SEP); while (tok.hasMoreTokens()) { child_name = tok.nextToken(); n = curr.findChild(child_name); if (n == null) return null; curr = n; } return curr; }
private void showDetails(TreePath p) { if (p == null) { return; } MyNode selectedNode = (MyNode) p.getLastPathComponent(); String textPath = textGen(p); JOptionPane.showMessageDialog( this, selectedNode.getLevelName() + ": " + selectedNode.getUserObject() + "\n" + selectedNode.getText() + "\n" + textPath); }
/** * Adds a new node to the view. Intermediary nodes will be created if they don't yet exist. * Returns the first node that was created or null if node already existed */ public MyNode add(String fqn) { MyNode curr, n, ret = null; StringTokenizer tok; String child_name; if (fqn == null) return null; curr = this; tok = new StringTokenizer(fqn, ReplicatedTreeView.SEP); while (tok.hasMoreTokens()) { child_name = tok.nextToken(); n = curr.findChild(child_name); if (n == null) { n = new MyNode(child_name); if (ret == null) ret = n; curr.add(n); } curr = n; } return ret; }
private MyNode readNode() { String text = null; String level = null; String name = null; MyNode retNode = null; if (s.hasNextLine()) { try { String[] lineArray = thisLine.split("> "); text = lineArray[1]; level = lineArray[0].split(" namn=")[0]; name = lineArray[0].split(" namn=")[1]; if (!level.startsWith("<") | !name.startsWith("\"") | !name.endsWith("\"")) { throw new Exception(); } else { level = level.substring(1); name = name.substring(1, name.length() - 1); } } catch (Exception e) { System.err.println("Parse error"); e.printStackTrace(); System.exit(1); } retNode = new MyNode(name, level, text); thisLine = s.nextLine(); while (!thisLine.startsWith("</")) { retNode.add(readNode()); thisLine = s.nextLine(); } } return retNode; }
/** Recursively adds GUI nodes starting from fqn */ void addGuiNode(String fqn) { Set children; String child_name; if (fqn == null) return; // 1 . Add myself root.add(fqn); // 2. Then add my children children = tree.getChildrenNames(fqn); if (children != null) { for (Iterator it = children.iterator(); it.hasNext(); ) { child_name = (String) it.next(); addGuiNode(fqn + SEP + child_name); } } }