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.Component; fiore@3: fiore@3: import javax.swing.BorderFactory; fiore@3: import javax.swing.JCheckBox; fiore@3: import javax.swing.JTree; fiore@3: import javax.swing.tree.DefaultTreeCellRenderer; fiore@3: fiore@3: fiore@3: /** fiore@3: * A tree cell renderer which renders {@code CheckBoxTreeNode} objects like a {@code JCheckBox}: fiore@3: * thick box (showing whether the node is selected or not) followed by a label (same label it would fiore@3: * appear with a default {@code DefaultTreeCellRenderer}. fiore@3: * fiore@3: */ fiore@3: @SuppressWarnings("serial") fiore@3: public class CheckBoxTreeCellRenderer extends DefaultTreeCellRenderer { fiore@3: fiore@3: public CheckBoxTreeCellRenderer(){ fiore@3: checkBox = new JCheckBox(); fiore@3: checkBox.setBorder(BorderFactory.createLineBorder(this.getBorderSelectionColor())); fiore@3: } fiore@3: /** fiore@3: * Returns the {@code Component} that the renderer uses to draw the value fiore@3: * fiore@3: * @throws ClassCastException if {@code value} is not an instance of {@code CheckoxTreeNode} fiore@3: * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(JTree, Object, boolean, boolean, boolean, int, boolean) fiore@3: * @return the {@code Component} that the renderer uses to draw the value fiore@3: */ fiore@3: @Override fiore@3: public Component getTreeCellRendererComponent(JTree tree, fiore@3: Object value, fiore@3: boolean selected, fiore@3: boolean expanded, fiore@3: boolean leaf, fiore@3: int row, fiore@3: boolean hasFocus){ fiore@3: fiore@3: CheckBoxTreeNode treeNode = (CheckBoxTreeNode)value; fiore@3: if(!treeNode.isSelectable()) fiore@3: return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); fiore@3: fiore@3: checkBox.setSelected(treeNode.isSelected()); fiore@3: checkBox.setText(value.toString()); fiore@3: if(selected){ fiore@3: checkBox.setBackground(getBackgroundSelectionColor()); fiore@3: checkBox.setBorderPainted(true); fiore@3: }else{ fiore@3: checkBox.setBackground(getBackgroundNonSelectionColor()); fiore@3: checkBox.setBorderPainted(false); fiore@3: } fiore@3: return checkBox; fiore@3: } fiore@3: fiore@3: private JCheckBox checkBox; fiore@3: }