All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.misc.CheckCombo Maven / Gradle / Ivy

package net.alantea.swing.misc;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import net.alantea.liteprops.ChangeListener;

@SuppressWarnings("serial")
public class CheckCombo extends JComboBox> implements ActionListener
{
   /** The listeners. */
   private transient List>> listeners = new LinkedList<>();
   
   private int nbItems = 0;
   
   public CheckCombo()
   {
      setRenderer(new CheckComboRenderer>());
      addActionListener(this);
   }
   
   public void setValues(List values)
   {
      removeAllItems();
      for (int j = 0; j < values.size(); j++)
      {
         addItem(new CheckComboStore(values.get(j), false));
      }
      nbItems = values.size();
      invalidate();
   }
   
   public List> getValues()
   {
      List> ret = new LinkedList>();
      for (int j = 0; j < nbItems; j++)
      {
         CheckComboStore check = this.getItemAt(j);
         ret.add(check);
      }
      return ret;
   }
   
   public List getSelectedValues()
   {
      List ret = new LinkedList<>();
      for (int j = 0; j < nbItems; j++)
      {
         CheckComboStore check = this.getItemAt(j);
         if ((check != null) && (check.getState()))
         {
            ret.add(check.getId());
         }
      }
      return ret;
   }
   
   public List getSelectedIndexes()
   {
      List ret = new LinkedList<>();
      for (int j = 0; j < nbItems; j++)
      {
         CheckComboStore check = this.getItemAt(j);
         if ((check != null) && (check.getState()))
         {
            ret.add(j);
         }
      }
      return ret;
   }
   
   public void actionPerformed(ActionEvent e)
   {
      @SuppressWarnings("unchecked")
      JComboBox cb = (JComboBox) e.getSource();
      @SuppressWarnings("unchecked")
      CheckComboStore store = (CheckComboStore) cb.getSelectedItem();
      store.setState(!store.getState());
      fireChanged();
   }
   
   /**
    * Adds the listener.
    *
    * @param listener the listener
    */
   public void addListener(ChangeListener> listener)
   {
      if (!listeners.contains(listener))
      {
         listeners.add(listener);
      }
   }
   
   /**
    * Removes the listener.
    *
    * @param listener the listener
    */
   public void removeListener(ChangeListener listener)
   {
      if (listeners.contains(listener))
      {
         listeners.remove(listener);
      }
   }

   /**
    * Fire the change.
    */
   protected void fireChanged()
   {
      for (ChangeListener> listener : listeners)
      {
         try
         {
            listener.changed(null, getSelectedValues());
         }
         catch (Exception e)
         {
            // silently ignore it
            e.printStackTrace();
         }
      }
   }
}

class CheckComboRenderer implements ListCellRenderer
{
   JCheckBox checkBox;

   public CheckComboRenderer()
   {
      checkBox = new JCheckBox();
   }

   @Override
   public Component getListCellRendererComponent(JList list, T value, int index, boolean isSelected,
         boolean cellHasFocus)
   {
      @SuppressWarnings("unchecked")
      CheckComboStore store = (CheckComboStore) value;
      if (store != null)
      {
        checkBox.setText(store.getId().toString());
        checkBox.setSelected(store.getState());
        checkBox.setBackground(isSelected ? Color.red : Color.white);
        checkBox.setForeground(isSelected ? Color.white : Color.black);
      }
      return checkBox;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy