/*______________________________________________________________________________ * * org.eidola.test.Test * * Part of the Eidola Kernel Reference Implementation * See http://eidola.org for oodles of relevant fun! * *______________________________________________________________________________ * * Copyright 2001 Paul Cantrell * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 2, as published by the * Free Software Foundation. See the file LICENSE.html for more information. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple * Place, Suite 330 / Boston, MA 02111-1307 / USA. *_______________________________________________________________________________ */ package org.eidola.test; import org.eidola.kernel.event.*; import org.eidola.kernel.event.Event; import org.eidola.kernel.event.EventQueue; import org.eidola.kernel.event.EventListener; import org.eidola.kernel.Container; import org.eidola.kernel.*; import org.eidola.util.*; import java.awt.*; import java.awt.geom.*; import java.util.*; import java.util.List; import javax.swing.*; import javax.swing.tree.*; /** @author Paul Cantrell @version [Development version] */ public class TreeTest implements EventListener, MutableTreeNode { public TreeTest(Container cntr) { this.cntr = cntr; cntr.addListener(this, q); childNodes = new Vector(); contentsShown = Collections.EMPTY_LIST; update(); } public TreeTest(Container cntr, TreeNode parent, DefaultTreeModel treeModel) { this(cntr); this.parent = parent; setTreeModel(treeModel); } public void setTreeModel(DefaultTreeModel treeModel) { this.treeModel = treeModel; update(); } public Container getContainer() { return cntr; } public void handleEvent(Event e) { update(); } public void update() { if(treeModel == null) return; List newContents = new ArrayList(cntr.getCompilation().getContents()); Collections.sort( newContents, new Comparator() { public int compare(Object o1, Object o2) { return o1.toString().compareTo(o2.toString()); } }); synchronized(this) { if(!newContents.equals(contentsShown)) { System.out.println("+++ updating tree node for " + cntr + " to " + newContents); /* contentsShown = newContents; for(Iterator i = childNodes.iterator(); i.hasNext(); ) { TreeTest node = (TreeTest) i.next(); node.getContainer().removeListener(node); } childNodes = new Vector(contentsShown.size()); for(Iterator i = contentsShown.iterator(); i.hasNext(); ) childNodes.add(new TreeTest((Container) i.next(), this, treeModel)); */ (new ListDiff(contentsShown, newContents)) .applySteps( new DefaultListMutator(childNodes) { public void insert(Object object, int index) { super.insert( new TreeTest( (Container) object, TreeTest.this, treeModel), index); } public void remove(Object object, int index) { TreeTest node = (TreeTest) object; node.getContainer().removeListener(node); super.remove(node, index); } }); contentsShown = newContents; System.out.println("+++ updated tree node for " + cntr + " " + childNodes); treeModel.reload(this); } } } public synchronized Enumeration children() { return childNodes.elements(); } public boolean getAllowsChildren() { return true; } public synchronized TreeNode getChildAt(int childIndex) { return (TreeNode) childNodes.get(childIndex); } public synchronized int getChildCount() { return childNodes.size(); } public synchronized int getIndex(TreeNode node) { return childNodes.indexOf(node); } public synchronized TreeNode getParent() { return parent; } public boolean isLeaf() { return false; } public void insert(MutableTreeNode child, int index) { System.out.println("Add " + child + " to " + this); Element childCntr = (Element) ((TreeTest) child).getContainer(); try { synchronized(cntr) { if(cntr instanceof Namespace) ((Namespace) cntr).setRoot((NamedElement) childCntr); else if(cntr instanceof Capsule) ((Capsule) cntr).addPublic((NamedElement) childCntr); else if(cntr instanceof Function && childCntr instanceof Variable) ((Function) cntr).addInput((Variable) childCntr); else throw new RuntimeException("Can't add " + childCntr + " to " + cntr); } synchronized(childCntr) { childCntr.setOwner(cntr); } } catch(org.eidola.kernel.error.SemanticViolation v) { throw new RuntimeException(v.toString()); } } public void remove(int index) { remove((TreeTest) childNodes.get(index)); } public void remove(MutableTreeNode child) { System.out.println("Remove " + child + " from " + this); Element childCntr = (Element) ((TreeTest) child).getContainer(); synchronized(cntr) { if(cntr instanceof Capsule) ((Capsule) cntr).removePublic((NamedElement) childCntr); else if(cntr instanceof Function && childCntr instanceof Variable) ((Function) cntr).removeInput((Variable) childCntr); else throw new RuntimeException("Can't remove " + childCntr + " from " + cntr); } } public void removeFromParent() { System.out.println("Remove " + this + " from parent is a NOOP"); } public void setParent(MutableTreeNode newParent) { System.out.println("Set " + this + " parent to " + newParent); this.parent = (TreeNode) newParent; } public void setUserObject(Object object) { if(cntr instanceof NamedElement) synchronized(cntr) { ((NamedElement) cntr).setName(object.toString()); } } public String toString() { return cntr instanceof NamedElement ? ((NamedElement) cntr).getName() : cntr.toString(); } static public void main(String[] args) throws Exception { GraphicsTest.q = q = new EventQueue("Interface queue"); q.start(); CommandLineInterface cli = new CommandLineInterface(); cli.start(); cli.create(java.util.Arrays.asList(new Object[]{"new","Namespace"})); // TreeTest { JFrame test = new JFrame("JTree test"); test.getContentPane().setLayout(new BorderLayout()); TreeTest rootNode = new TreeTest(cli.getContainer(0)); DefaultTreeModel treeModel = new DefaultTreeModel(rootNode); rootNode.setTreeModel(treeModel); JTree tree = new JTree(treeModel); tree.setEditable(true); test.getContentPane().add(tree, BorderLayout.CENTER); test.setSize(new Dimension(400,300)); test.setLocation(20, 100); test.show(); } // GraphicsTest { JFrame test = new JFrame("Graphics test"); test.getContentPane().setLayout(new BorderLayout()); test.getContentPane().add(new GraphicsTest(cli.getContainer(0)), BorderLayout.CENTER); test.setSize(new Dimension(400,300)); test.setLocation(500, 100); test.show(); } } private TreeNode parent; private Container cntr; private DefaultTreeModel treeModel; private long structureVersionDisplayed; private List contentsShown; private Vector childNodes; static private EventQueue q; }