| 1 | /* |
|---|
| 2 | * This program is free software; you can redistribute it and/or modify |
|---|
| 3 | * it under the terms of the GNU General Public License as published by |
|---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 5 | * (at your option) any later version. |
|---|
| 6 | * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 10 | * GNU General Public License for more details. |
|---|
| 11 | * |
|---|
| 12 | * You should have received a copy of the GNU General Public License |
|---|
| 13 | * along with this program; if not, write to the Free Software |
|---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * CheckBoxNodeEditor.java |
|---|
| 19 | * Copyright (C) 2006 Robert Jung |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.gui.ensembleLibraryEditor.tree; |
|---|
| 24 | |
|---|
| 25 | import java.awt.Color; |
|---|
| 26 | import java.awt.Font; |
|---|
| 27 | import java.awt.event.ItemEvent; |
|---|
| 28 | import java.awt.event.ItemListener; |
|---|
| 29 | |
|---|
| 30 | import javax.swing.JCheckBox; |
|---|
| 31 | import javax.swing.JLabel; |
|---|
| 32 | import javax.swing.JPanel; |
|---|
| 33 | import javax.swing.UIManager; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * This class is responsible for creating a simple checkBox editor for the |
|---|
| 37 | * CheckBoxNode class. |
|---|
| 38 | * |
|---|
| 39 | * @author Robert Jung (mrbobjung@gmail.com) |
|---|
| 40 | * @version $Revision: 1.1 $ |
|---|
| 41 | */ |
|---|
| 42 | public class CheckBoxNodeEditor |
|---|
| 43 | extends JPanel |
|---|
| 44 | implements ItemListener { |
|---|
| 45 | |
|---|
| 46 | /** for serialization */ |
|---|
| 47 | private static final long serialVersionUID = -1506685976284982111L; |
|---|
| 48 | |
|---|
| 49 | /** the checkbox the user will interact with */ |
|---|
| 50 | private JCheckBox m_SelectedCheckBox; |
|---|
| 51 | |
|---|
| 52 | /** the label that will display the name of this parameter value */ |
|---|
| 53 | private JLabel m_Label; |
|---|
| 54 | |
|---|
| 55 | /** colors we'll use */ |
|---|
| 56 | private Color textForeground, textBackground; |
|---|
| 57 | |
|---|
| 58 | /** a reference to the node this editor represents */ |
|---|
| 59 | private CheckBoxNode m_Node; |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * The constructor builds the simple CheckBox GUI based on the information |
|---|
| 63 | * in the node that is passed to it. Specifically, the GUI consists of only |
|---|
| 64 | * a JLabel showing the name and a CheckBox allowing the user to toggle it. |
|---|
| 65 | * |
|---|
| 66 | * @param node a reference to the node this editor represents |
|---|
| 67 | */ |
|---|
| 68 | public CheckBoxNodeEditor(CheckBoxNode node) { |
|---|
| 69 | |
|---|
| 70 | this.m_Node = node; |
|---|
| 71 | |
|---|
| 72 | String name = node.getName(); |
|---|
| 73 | boolean selected = node.getSelected(); |
|---|
| 74 | |
|---|
| 75 | textForeground = UIManager.getColor("Tree.textForeground"); |
|---|
| 76 | textBackground = UIManager.getColor("Tree.textBackground"); |
|---|
| 77 | |
|---|
| 78 | setForeground(textForeground); |
|---|
| 79 | setBackground(textBackground); |
|---|
| 80 | |
|---|
| 81 | Font fontValue; |
|---|
| 82 | fontValue = UIManager.getFont("Tree.font"); |
|---|
| 83 | |
|---|
| 84 | m_SelectedCheckBox = new JCheckBox(); |
|---|
| 85 | m_SelectedCheckBox.setSelected(selected); |
|---|
| 86 | m_SelectedCheckBox.setForeground(textForeground); |
|---|
| 87 | m_SelectedCheckBox.setBackground(textBackground); |
|---|
| 88 | m_SelectedCheckBox.addItemListener(this); |
|---|
| 89 | add(m_SelectedCheckBox); |
|---|
| 90 | |
|---|
| 91 | m_Label = new JLabel(name); |
|---|
| 92 | if (fontValue != null) { |
|---|
| 93 | m_Label.setFont(fontValue); |
|---|
| 94 | } |
|---|
| 95 | m_Label.setForeground(textForeground); |
|---|
| 96 | m_Label.setBackground(textBackground); |
|---|
| 97 | add(m_Label); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * This method provides a way for the ModelTreeNodeEditor |
|---|
| 102 | * to register an itemListener with this editor. This way, |
|---|
| 103 | * after the user has done something, the tree can update its |
|---|
| 104 | * editing state as well as the tree node structure as necessary |
|---|
| 105 | * |
|---|
| 106 | * @param itemListener the listener to use |
|---|
| 107 | */ |
|---|
| 108 | public void setItemListener(ItemListener itemListener) { |
|---|
| 109 | if (m_SelectedCheckBox != null) |
|---|
| 110 | m_SelectedCheckBox.addItemListener(itemListener); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * This is the implementation of the itemListener for the CheckBoxNode. |
|---|
| 115 | * Note that when we are deselcting a CheckBox we have to ask our parent |
|---|
| 116 | * node if this is OK. This is because we need to ensure that at least a |
|---|
| 117 | * single CheckBox underneath the parent remains checked because we need at |
|---|
| 118 | * least one value for each classifier parameter. |
|---|
| 119 | * |
|---|
| 120 | * @param e the event |
|---|
| 121 | */ |
|---|
| 122 | public void itemStateChanged(ItemEvent e) { |
|---|
| 123 | if (e.getStateChange() == ItemEvent.SELECTED) { |
|---|
| 124 | m_Node.setSelected(true); |
|---|
| 125 | m_Label.setEnabled(true); |
|---|
| 126 | |
|---|
| 127 | } else if (e.getStateChange() == ItemEvent.DESELECTED) { |
|---|
| 128 | |
|---|
| 129 | PropertyNode parent = (PropertyNode) m_Node.getParent(); |
|---|
| 130 | if (parent.canDeselect(m_Node)) { |
|---|
| 131 | m_Node.setSelected(false); |
|---|
| 132 | m_Label.setEnabled(false); |
|---|
| 133 | } else { |
|---|
| 134 | m_SelectedCheckBox.setSelected(true); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|