001package jmri.jmrit.symbolicprog; 002 003import java.awt.event.ActionListener; 004import java.beans.PropertyChangeListener; 005import javax.swing.JCheckBox; 006import javax.swing.JComboBox; 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010/* Represents a JComboBox as a JCheckBox 011 * 012 * @author Bob Jacobsen Copyright (C) 2001 013 */ 014public class ComboCheckBox extends JCheckBox { 015 016 ComboCheckBox(JComboBox<String> box, EnumVariableValue var) { 017 super(); 018 _var = var; 019 _box = box; 020 setBackground(_var._value.getBackground()); 021 setOpaque(true); 022 // listen for changes to ourself 023 addActionListener(l1 = new java.awt.event.ActionListener() { 024 @Override 025 public void actionPerformed(java.awt.event.ActionEvent e) { 026 thisActionPerformed(e); 027 } 028 }); 029 // listen for changes to original 030 _box.addActionListener(l2 = new java.awt.event.ActionListener() { 031 @Override 032 public void actionPerformed(java.awt.event.ActionEvent e) { 033 originalActionPerformed(e); 034 } 035 }); 036 // listen for changes to original state 037 _var.addPropertyChangeListener(p1 = new java.beans.PropertyChangeListener() { 038 @Override 039 public void propertyChange(java.beans.PropertyChangeEvent e) { 040 originalPropertyChanged(e); 041 } 042 }); 043 044 // set initial value 045 if (_box.getSelectedIndex() == 1) { 046 setSelected(true); 047 } 048 } 049 050 void thisActionPerformed(java.awt.event.ActionEvent e) { 051 // update original state to this state 052 if (isSelected()) { 053 _box.setSelectedIndex(1); 054 } else { 055 _box.setSelectedIndex(0); 056 } 057 } 058 059 void originalActionPerformed(java.awt.event.ActionEvent e) { 060 // update this state to original state 061 if (_box.getSelectedIndex() == 1) { 062 setSelected(true); 063 } else { 064 setSelected(false); 065 } 066 } 067 068 void originalPropertyChanged(java.beans.PropertyChangeEvent e) { 069 // update this color from original state 070 if (e.getPropertyName().equals("State")) { 071 log.debug("State change seen"); 072 setBackground(_var._value.getBackground()); 073 setOpaque(true); 074 } 075 } 076 077 transient ActionListener l1; 078 transient ActionListener l2; 079 transient PropertyChangeListener p1; 080 081 EnumVariableValue _var = null; 082 JComboBox<String> _box = null; 083 084 public void dispose() { 085 removeActionListener(l1); 086 _box.removeActionListener(l2); 087 _var.removePropertyChangeListener(p1); 088 _var = null; 089 _box = null; 090 } 091 092 // initialize logging 093 private final static Logger log = LoggerFactory.getLogger(ComboCheckBox.class); 094 095}