fiore@3: /* fiore@3: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool fiore@3: fiore@3: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@3: fiore@3: This program is free software: you can redistribute it and/or modify fiore@3: it under the terms of the GNU General Public License as published by fiore@3: the Free Software Foundation, either version 3 of the License, or fiore@3: (at your option) any later version. fiore@3: fiore@3: This program is distributed in the hope that it will be useful, fiore@3: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@3: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@3: GNU General Public License for more details. fiore@3: fiore@3: You should have received a copy of the GNU General Public License fiore@3: along with this program. If not, see . fiore@3: */ fiore@3: package uk.ac.qmul.eecs.ccmi.checkboxtree; fiore@3: fiore@3: import java.awt.event.MouseAdapter; fiore@3: import java.awt.event.MouseEvent; fiore@3: import java.io.IOException; fiore@3: import java.io.InputStream; fiore@3: import java.util.Enumeration; fiore@3: fiore@3: import javax.swing.JTree; fiore@3: import javax.swing.tree.DefaultMutableTreeNode; fiore@3: import javax.swing.tree.DefaultTreeModel; fiore@3: import javax.swing.tree.TreePath; fiore@3: import javax.xml.parsers.ParserConfigurationException; fiore@3: import javax.xml.parsers.SAXParser; fiore@3: import javax.xml.parsers.SAXParserFactory; fiore@3: fiore@3: import org.xml.sax.SAXException; fiore@3: fiore@3: fiore@3: /** fiore@3: * A JTree containing {@code CheckBoxTreeNode} nodes. The tree is built according to an XML file fiore@3: * passed as argument to the constructor. fiore@3: * The XML file hierarchical structure reflects the structure of the tree. XML tag name can be fiore@3: * either {@code selectable} or {@code unselectable}. The former representing tree nodes with a check fiore@3: * box associated to it, and the latter a normal tree node, much as a {@link DefaultMutableTreeNode}. fiore@3: * Either tags must have an attribute {@code value}, representing the name of the node which will be displayed fiore@3: * in the tree. Here is an example of a simple XML file, representing a tree with the tree root (non selectable) having fiore@3: * three children, the first of which has, in turn, a child. all the descendants of the root are selectable, but the fiore@3: * first child. fiore@3: * fiore@3: *
fiore@3:  * {@code
fiore@3:  * 	
fiore@3:  *  
fiore@3:  *   
fiore@3:  *   
fiore@3:  *   
fiore@3:  *     
fiore@3:  *   
fiore@3:  *  
fiore@3:  * }
fiore@3:  * 
fiore@3: * fiore@3: * @see CheckBoxTreeNode fiore@3: */ fiore@3: @SuppressWarnings("serial") fiore@3: public class CheckBoxTree extends JTree { fiore@3: public CheckBoxTree(InputStream stream, SetProperties values){ fiore@3: super(new DefaultTreeModel(new DefaultMutableTreeNode())); fiore@3: this.properties = values; fiore@3: getAccessibleContext().setAccessibleName("tree"); fiore@3: treeModel = (DefaultTreeModel)getModel(); fiore@3: setCellRenderer(new CheckBoxTreeCellRenderer()); fiore@3: buildTree(stream); fiore@3: /* mouse listener to toggle the selected tree node */ fiore@3: addMouseListener(new MouseAdapter(){ fiore@3: @Override fiore@3: public void mousePressed(MouseEvent e){ fiore@3: TreePath path = getPathForLocation(e.getX(),e.getY()); fiore@3: if(path == null) fiore@3: return; fiore@3: CheckBoxTreeNode treeNode = (CheckBoxTreeNode)path.getLastPathComponent(); fiore@3: toggleSelection(treeNode); fiore@3: } fiore@3: }); fiore@3: } fiore@3: fiore@3: /** fiore@3: * Builds a CheckBoxTree out of an xml file passed as argument. All the nodes fiore@3: * of the tree are marked unchecked. fiore@3: * fiore@3: * @param stream an input stream to an xml file fiore@3: */ fiore@3: public CheckBoxTree(InputStream stream){ fiore@3: this(stream,null); fiore@3: } fiore@3: fiore@3: /* use a sax parser to build the tree, if an error occurs during the parsing it just stops */ fiore@3: private void buildTree(InputStream stream){ fiore@3: SAXParserFactory factory = SAXParserFactory.newInstance(); fiore@3: try { fiore@3: SAXParser saxParser = factory.newSAXParser(); fiore@3: XMLHandler handler = new XMLHandler((DefaultTreeModel)getModel(),properties); fiore@3: saxParser.parse(stream, handler); fiore@3: } catch (IOException e) { fiore@3: e.printStackTrace(); fiore@3: return; fiore@3: } catch (ParserConfigurationException e) { fiore@3: e.printStackTrace(); fiore@3: return; fiore@3: } catch (SAXException e) { fiore@3: e.printStackTrace(); fiore@3: return; fiore@3: } fiore@3: } fiore@3: fiore@3: /** fiore@3: * Returns a reference to the properties holding which nodes of the tree are currently checked. fiore@5: * @return the properties or {@code null} if the constructor with no properties was used. fiore@3: */ fiore@3: public SetProperties getProperties(){ fiore@3: return properties; fiore@3: } fiore@3: fiore@3: /** fiore@3: * Toggle the check box of the tree node passed as argument. If the tree node fiore@3: * is not a leaf, then all its descendants will get the new value it. That is, fiore@3: * if the tree node becomes selected as a result of the call, then all the descendants fiore@3: * will become in turn selected (regardless of their previous state). If it becomes fiore@3: * unselected, the descendants will become unselected as well. fiore@3: * fiore@3: * @param treeNode the tree node to toggle fiore@3: */ fiore@3: public void toggleSelection(CheckBoxTreeNode treeNode){ fiore@3: if(treeNode.isSelectable()){ fiore@3: boolean selection = !treeNode.isSelected(); fiore@3: treeNode.setSelected(selection); fiore@3: if(selection) fiore@3: properties.add(treeNode.getPathAsString()); fiore@3: else fiore@3: properties.remove(treeNode.getPathAsString()); fiore@3: treeModel.nodeChanged(treeNode); fiore@3: if(!treeNode.isLeaf()){ fiore@3: for( @SuppressWarnings("unchecked") fiore@3: Enumeration enumeration = treeNode.depthFirstEnumeration(); enumeration.hasMoreElements();){ fiore@3: CheckBoxTreeNode t = enumeration.nextElement(); fiore@3: t.setSelected(selection); fiore@3: if(selection) fiore@3: properties.add(t.getPathAsString()); fiore@3: else fiore@3: properties.remove(t.getPathAsString()); fiore@3: treeModel.nodeChanged(t); fiore@3: } fiore@3: } fiore@3: } fiore@3: } fiore@3: fiore@3: private SetProperties properties; fiore@3: private DefaultTreeModel treeModel; fiore@3: }