annotate java/src/uk/ac/qmul/eecs/ccmi/checkboxtree/CheckBoxTree.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19 package uk.ac.qmul.eecs.ccmi.checkboxtree;
f@0 20
f@0 21 import java.awt.event.MouseAdapter;
f@0 22 import java.awt.event.MouseEvent;
f@0 23 import java.io.IOException;
f@0 24 import java.io.InputStream;
f@0 25 import java.util.Enumeration;
f@0 26
f@0 27 import javax.swing.JTree;
f@0 28 import javax.swing.tree.DefaultMutableTreeNode;
f@0 29 import javax.swing.tree.DefaultTreeModel;
f@0 30 import javax.swing.tree.TreePath;
f@0 31 import javax.xml.parsers.ParserConfigurationException;
f@0 32 import javax.xml.parsers.SAXParser;
f@0 33 import javax.xml.parsers.SAXParserFactory;
f@0 34
f@0 35 import org.xml.sax.SAXException;
f@0 36
f@0 37
f@0 38 /**
f@0 39 * A JTree containing {@code CheckBoxTreeNode} nodes. The tree is built according to an XML file
f@0 40 * passed as argument to the constructor.
f@0 41 * The XML file hierarchical structure reflects the structure of the tree. XML tag name can be
f@0 42 * either {@code selectable} or {@code unselectable}. The former representing tree nodes with a check
f@0 43 * box associated to it, and the latter a normal tree node, much as a {@link DefaultMutableTreeNode}.
f@0 44 * Either tags must have an attribute {@code value}, representing the name of the node which will be displayed
f@0 45 * in the tree. Here is an example of a simple XML file, representing a tree with the tree root (non selectable) having
f@0 46 * three children, the first of which has, in turn, a child. all the descendants of the root are selectable, but the
f@0 47 * first child.
f@0 48 *
f@0 49 * <pre>
f@0 50 * {@code
f@0 51 * <?xml version="1.0" encoding="utf-8"?>
f@0 52 * <unselectable value="root">
f@0 53 * <unselectable value="first child"/>
f@0 54 * <selectable value="second child"/>
f@0 55 * <selectable value "third child">
f@0 56 * <selectable value="grand child"/>
f@0 57 * </selectable>
f@0 58 * </unselectable>
f@0 59 * }
f@0 60 * </pre>
f@0 61 *
f@0 62 * @see CheckBoxTreeNode
f@0 63 */
f@0 64 @SuppressWarnings("serial")
f@0 65 public class CheckBoxTree extends JTree {
f@0 66 public CheckBoxTree(InputStream stream, SetProperties values){
f@0 67 super(new DefaultTreeModel(new DefaultMutableTreeNode()));
f@0 68 this.properties = values;
f@0 69 getAccessibleContext().setAccessibleName("tree");
f@0 70 treeModel = (DefaultTreeModel)getModel();
f@0 71 setCellRenderer(new CheckBoxTreeCellRenderer());
f@0 72 buildTree(stream);
f@0 73 /* mouse listener to toggle the selected tree node */
f@0 74 addMouseListener(new MouseAdapter(){
f@0 75 @Override
f@0 76 public void mousePressed(MouseEvent e){
f@0 77 TreePath path = getPathForLocation(e.getX(),e.getY());
f@0 78 if(path == null)
f@0 79 return;
f@0 80 CheckBoxTreeNode treeNode = (CheckBoxTreeNode)path.getLastPathComponent();
f@0 81 toggleSelection(treeNode);
f@0 82 }
f@0 83 });
f@0 84 }
f@0 85
f@0 86 /**
f@0 87 * Builds a CheckBoxTree out of an xml file passed as argument. All the nodes
f@0 88 * of the tree are marked unchecked.
f@0 89 *
f@0 90 * @param stream an input stream to an xml file
f@0 91 */
f@0 92 public CheckBoxTree(InputStream stream){
f@0 93 this(stream,null);
f@0 94 }
f@0 95
f@0 96 /* use a sax parser to build the tree, if an error occurs during the parsing it just stops */
f@0 97 private void buildTree(InputStream stream){
f@0 98 SAXParserFactory factory = SAXParserFactory.newInstance();
f@0 99 try {
f@0 100 SAXParser saxParser = factory.newSAXParser();
f@0 101 XMLHandler handler = new XMLHandler((DefaultTreeModel)getModel(),properties);
f@0 102 saxParser.parse(stream, handler);
f@0 103 } catch (IOException e) {
f@0 104 e.printStackTrace();
f@0 105 return;
f@0 106 } catch (ParserConfigurationException e) {
f@0 107 e.printStackTrace();
f@0 108 return;
f@0 109 } catch (SAXException e) {
f@0 110 e.printStackTrace();
f@0 111 return;
f@0 112 }
f@0 113 }
f@0 114
f@0 115 /**
f@0 116 * Returns a reference to the properties holding which nodes of the tree are currently checked.
f@0 117 * @return the properties or {@code null} if the constructor with no properties was used.
f@0 118 */
f@0 119 public SetProperties getProperties(){
f@0 120 return properties;
f@0 121 }
f@0 122
f@0 123 /**
f@0 124 * Toggle the check box of the tree node passed as argument. If the tree node
f@0 125 * is not a leaf, then all its descendants will get the new value it. That is,
f@0 126 * if the tree node becomes selected as a result of the call, then all the descendants
f@0 127 * will become in turn selected (regardless of their previous state). If it becomes
f@0 128 * unselected, the descendants will become unselected as well.
f@0 129 *
f@0 130 * @param treeNode the tree node to toggle
f@0 131 */
f@0 132 public void toggleSelection(CheckBoxTreeNode treeNode){
f@0 133 if(treeNode.isSelectable()){
f@0 134 boolean selection = !treeNode.isSelected();
f@0 135 treeNode.setSelected(selection);
f@0 136 if(selection)
f@0 137 properties.add(treeNode.getPathAsString());
f@0 138 else
f@0 139 properties.remove(treeNode.getPathAsString());
f@0 140 treeModel.nodeChanged(treeNode);
f@0 141 if(!treeNode.isLeaf()){
f@0 142 for( @SuppressWarnings("unchecked")
f@0 143 Enumeration<CheckBoxTreeNode> enumeration = treeNode.depthFirstEnumeration(); enumeration.hasMoreElements();){
f@0 144 CheckBoxTreeNode t = enumeration.nextElement();
f@0 145 t.setSelected(selection);
f@0 146 if(selection)
f@0 147 properties.add(t.getPathAsString());
f@0 148 else
f@0 149 properties.remove(t.getPathAsString());
f@0 150 treeModel.nodeChanged(t);
f@0 151 }
f@0 152 }
f@0 153 }
f@0 154 }
f@0 155
f@0 156 private SetProperties properties;
f@0 157 private DefaultTreeModel treeModel;
f@0 158 }