f@0: /* f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool f@0: f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) f@0: f@0: This program is free software: you can redistribute it and/or modify f@0: it under the terms of the GNU General Public License as published by f@0: the Free Software Foundation, either version 3 of the License, or f@0: (at your option) any later version. f@0: f@0: This program is distributed in the hope that it will be useful, f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the f@0: GNU General Public License for more details. f@0: f@0: You should have received a copy of the GNU General Public License f@0: along with this program. If not, see . f@0: */ f@0: package uk.ac.qmul.eecs.ccmi.checkboxtree; f@0: f@0: import java.awt.Component; f@0: f@0: import javax.swing.BorderFactory; f@0: import javax.swing.JCheckBox; f@0: import javax.swing.JTree; f@0: import javax.swing.tree.DefaultTreeCellRenderer; f@0: f@0: f@0: /** f@0: * A tree cell renderer which renders {@code CheckBoxTreeNode} objects like a {@code JCheckBox}: f@0: * thick box (showing whether the node is selected or not) followed by a label (same label it would f@0: * appear with a default {@code DefaultTreeCellRenderer}. f@0: * f@0: */ f@0: @SuppressWarnings("serial") f@0: public class CheckBoxTreeCellRenderer extends DefaultTreeCellRenderer { f@0: f@0: public CheckBoxTreeCellRenderer(){ f@0: checkBox = new JCheckBox(); f@0: checkBox.setBorder(BorderFactory.createLineBorder(this.getBorderSelectionColor())); f@0: } f@0: /** f@0: * Returns the {@code Component} that the renderer uses to draw the value f@0: * f@0: * @throws ClassCastException if {@code value} is not an instance of {@code CheckoxTreeNode} f@0: * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(JTree, Object, boolean, boolean, boolean, int, boolean) f@0: * @return the {@code Component} that the renderer uses to draw the value f@0: */ f@0: @Override f@0: public Component getTreeCellRendererComponent(JTree tree, f@0: Object value, f@0: boolean selected, f@0: boolean expanded, f@0: boolean leaf, f@0: int row, f@0: boolean hasFocus){ f@0: f@0: CheckBoxTreeNode treeNode = (CheckBoxTreeNode)value; f@0: if(!treeNode.isSelectable()) f@0: return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); f@0: f@0: checkBox.setSelected(treeNode.isSelected()); f@0: checkBox.setText(value.toString()); f@0: if(selected){ f@0: checkBox.setBackground(getBackgroundSelectionColor()); f@0: checkBox.setBorderPainted(true); f@0: }else{ f@0: checkBox.setBackground(getBackgroundNonSelectionColor()); f@0: checkBox.setBorderPainted(false); f@0: } f@0: return checkBox; f@0: } f@0: f@0: private JCheckBox checkBox; f@0: }