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: * {@code 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: Enumerationfiore@3: * fiore@3: * } fiore@3: *fiore@3: * fiore@3: * fiore@3: * fiore@3: *fiore@3: *