/*______________________________________________________________________________ * * 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.*; import javax.swing.event.TreeModelListener; import javax.swing.event.TreeModelEvent; /** @author Paul Cantrell @version [Development version] */ public class TreeTest2 implements TreeModel { 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("TreeTest2"); test.getContentPane().setLayout(new BorderLayout()); TreeModel treeModel = new TreeTest2(cli.getContainer(0)); 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(); } } public TreeTest2(Container root) { this.root = new Node(root, null); listeners = new HashSet(); } public synchronized void addTreeModelListener(TreeModelListener l) { listeners.add(l); } public synchronized void removeTreeModelListener(TreeModelListener l) { listeners.remove(l); } public Object getChild(Object parent, int index) { return ((Node) parent).getChildren().get(index); } public int getChildCount(Object parent) { return ((Node) parent).getChildren().size(); } public int getIndexOfChild(Object parent, Object child) { return ((Node) parent).getChildren().indexOf(child); } public Object getRoot() { return root; } public boolean isLeaf(Object node) { return false; } public void valueForPathChanged(TreePath path, Object newValue) { Node target = (Node) path.getLastPathComponent(); if(target.container instanceof NamedElement) synchronized(target.container) { ((NamedElement) target.container).setName(newValue.toString()); } } private class Node implements EventListener { public Node(Container container, Node parent) { this.container = container; this.parent = parent; List parentPath = new LinkedList(); if(parent != null) parentPath.addAll(Arrays.asList(parent.path.getPath())); parentPath.add(this); path = new TreePath(parentPath.toArray()); container.addListener(Node.this, q); //System.out.println("+++ created " + this); } public List/**/ getChildren() { if(childNodes == null) { //System.out.println("+++ adding children of " + this + " to tree"); contentsShown = Collections.EMPTY_LIST; childNodes = new ArrayList(); update(false); } return childNodes; } private void remove() { if(childNodes != null) for(Iterator i = childNodes.iterator(); i.hasNext(); ) ((Node) i.next()).remove(); container.removeListener(Node.this); //System.out.println("+++ removed " + this + " from tree"); } public void handleEvent(Event e) { if(e instanceof CompileCompleted) { //System.out.println("+++ Handling " + e); update(true); } } public void update(boolean checkVersion) { if(checkVersion && compileVersionShown == container.getCompileVersion()) { //System.out.println("No need to redisplay!!"); return; } compileVersionShown = container.getCompileVersion(); List newContents = new ArrayList(container.getCompilation().getContents()); Collections.sort( newContents, new Comparator() { public int compare(Object o1, Object o2) { return o1.toString().compareTo(o2.toString()); } }); //System.out.println("+++ contentsShown = " + contentsShown); //System.out.println("+++ newContents = " + newContents); if(newContents.equals(contentsShown)) return; //System.out.println("+++ updating " + this + " to " + newContents); if(childNodes != null) synchronized(this) { (new ListDiff(contentsShown, newContents)).applySteps( new DefaultListMutator(childNodes) { public void insert(Object object, int index) { super.insert(new Node((Container) object, Node.this), index); //System.out.println("+++ insert " + object + " at " + index); TreeModelEvent event = new TreeModelEvent( Node.this, Node.this.path, new int[]{index}, new Object[]{object}); for(Iterator i = listeners.iterator(); i.hasNext(); ) ((TreeModelListener) i.next()) .treeNodesInserted(event); } public void remove(Object object, int index) { //System.out.println("+++ remove " + object + " at " + index); Node node = (Node) childNodes.get(index); super.remove(node, index); node.remove(); TreeModelEvent event = new TreeModelEvent( Node.this, Node.this.path, new int[]{index}, new Object[]{node}); for(Iterator i = listeners.iterator(); i.hasNext(); ) ((TreeModelListener) i.next()) .treeNodesRemoved(event); } }); } if(parent != null) synchronized(parent) { TreeModelEvent event = new TreeModelEvent( parent, parent.path, new int[]{parent.childNodes.indexOf(this)}, new Object[]{this}); for(Iterator i = listeners.iterator(); i.hasNext(); ) ((TreeModelListener) i.next()) .treeNodesChanged(event); } contentsShown = newContents; //System.out.println("+++ updated " + this + " to " + contentsShown); } public String toString() { return (container instanceof NamedElement) ? ((NamedElement) container).getName() : container.toString(); } private Container container; private Node parent; private TreePath path; private long compileVersionShown; private List/**/ contentsShown; private List/**/ childNodes; } private Node root; private Set/**/ listeners; static private EventQueue q; }