Package simtools.ui

Source Code of simtools.ui.CheckBoxList$CheckBoxListCellRenderer

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2005, by :
*     Corporate:
*         EADS Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*
* $Id: CheckBoxList.java,v 1.3 2008/02/25 11:42:48 ogor Exp $
*
* Changes
* -------
* 26 f�vr. 07  : Initial public release (CC);
*
*/
package simtools.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
* A JList to display a list of element where each item has a check box
*/
public class CheckBoxList extends JList implements ListSelectionListener {

  static Color listForeground, listBackground;
  static {
    UIDefaults uid = UIManager.getLookAndFeel().getDefaults();
    listForeground =  uid.getColor("List.foreground");
    listBackground = uid.getColor("List.background");
  };
  public static MenuResourceBundle resources = ResourceFinder.getMenu(CheckBoxList.class);

  boolean[] isChecked;

  /**
   * Create a list
   * @param items array of element to be displayed
   * @param checked array of booleans with the check status
   */
  public CheckBoxList(Object[] items, boolean[] checked) {
    super();
    setCellRenderer(new CheckBoxListCellRenderer());
    addListSelectionListener(this);
    DefaultListModel model = new DefaultListModel();
    isChecked = new boolean[checked.length];
    for (int i = 0; i < items.length; i++) {
      model.addElement(items[i]);
      isChecked[i] = checked[i];
    }
    this.setModel(model);
  }

  /**
   * Create a list
   * All check status are false.
   * @param items
   */
  public CheckBoxList(List items) {
        super();
        setCellRenderer(new CheckBoxListCellRenderer());
        addListSelectionListener(this);
        DefaultListModel model = new DefaultListModel();
        isChecked = new boolean[items.size()];
       
        for(int i = 0; i < items.size(); i++) {
            model.addElement(items.get(i));
            isChecked[i] = false;
        }
        this.setModel(model);
    }

  /**
   * @return an array of check status for the list elements
   */
  public boolean[] getCheckedItems() {
    return isChecked;
  }
 
 
  /**
     * Check or uncheck all items
     * @param check
     */
    public void checkAll(boolean check){
        for (int i = 0; i < isChecked.length; i++) {
            isChecked[i] = check;
        }
        repaint();
    }

    /**
     * @return list of all checked elements
     */
    public List getCheckedItemsList() {
        List res = new ArrayList();
        for(int i = 0; i < isChecked.length; i++) {
            if (isChecked[i]){
                res.add(getModel().getElementAt(i));
            }
        }
        return res;
    }
   
  /*
   * (non-Javadoc)
   * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
   */
  public void valueChanged(ListSelectionEvent lse) {
        if (! lse.getValueIsAdjusting()) {
            removeListSelectionListener (this);

            // remember everything selected as a result of this action
            boolean[] newChecked=new boolean[isChecked.length];
            for (int i=0; i<isChecked.length; i++) {
                if (getSelectionModel().isSelectedIndex(i)) {
                  newChecked[i]=true;
                }
                else{
                  newChecked[i]=false;
                }
            }

            // turn on everything that was previously selected
            for (int i=0; i<isChecked.length; i++) {
                if(isChecked[i]){
                  getSelectionModel().addSelectionInterval(i, i);
                }
            }

            // add or remove the delta
            for (int i=0; i<newChecked.length; i++) {
                if(newChecked[i]){
                  if(isChecked[i]){
                    getSelectionModel().removeSelectionInterval (i, i);
                  }
                  else{
                    getSelectionModel().addSelectionInterval (i, i);
                  }
                }
            }

            // save selections for next time
            for (int i=0; i<isChecked.length; i++) {
              isChecked[i]=getSelectionModel().isSelectedIndex(i);
            }
            addListSelectionListener (this);
        }
  }

  /**
   * A cell renderer made of a default cell and a checkbox
   */
  class CheckBoxListCellRenderer extends JComponent implements
      ListCellRenderer {
    DefaultListCellRenderer defaultComp;

    JCheckBox checkbox;

    public CheckBoxListCellRenderer() {
      setLayout(new BorderLayout());
      defaultComp = new DefaultListCellRenderer();
      checkbox = new JCheckBox();
      add(defaultComp, BorderLayout.CENTER);
      add(checkbox, BorderLayout.EAST);
    }

    public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
       
      defaultComp.getListCellRendererComponent(list, value, index,false, false);
     
      checkbox.setSelected(isChecked[index]);

      Component[] comps = getComponents();
      for (int i = 0; i < comps.length; i++) {
        comps[i].setForeground(listForeground);
        comps[i].setBackground(listBackground);
      }

      return this;
    }
  }

  /**
   * Open a modal dialog to display the list of items and their chek boxes
   * A Ok button or the close dialog will dispose the dialog and the check result
   * will be returned
   * @param title the dialog title
   * @param parent the parent window or null
   * @param items array of string to be displayed
   * @param checked array of booleans with the check status
   * @return an array of check status for the list elements or null if canceled
   */
  public static boolean[] openDialog(String title, Window parent, String[] items, boolean[] checked){
    final JDialog jd;
    if(parent instanceof Dialog){
      jd =new JDialog((Dialog)parent, title, true);
    }else{
      jd =new JDialog((Frame)parent, title, true);
    }
    final CheckBoxList cbl=new CheckBoxList(items,checked);
    JScrollPane scroller =
            new JScrollPane (cbl,
                            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    jd.getContentPane().add(scroller,BorderLayout.CENTER);
    JButton okb=resources.getButton("OKButton", new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        jd.dispose();
      }     
    });
    JButton cancelb=resources.getButton("CancelButton", new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        cbl.isChecked=null;
        jd.dispose();
      }     
    });
    jd.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    JPanel pokb=new JPanel();
    pokb.add(okb);
    pokb.add(cancelb);
    jd.getContentPane().add(pokb,BorderLayout.SOUTH);
    jd.pack();
    jd.show();

    return cbl.getCheckedItems();
   
  }
}
TOP

Related Classes of simtools.ui.CheckBoxList$CheckBoxListCellRenderer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.