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

bc.ui.swing.lists.OptionList Maven / Gradle / Ivy

/* 
 * The MIT License
 *
 * Copyright 2016 Benny Lutati.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package bc.ui.swing.lists;

import bc.ui.swing.listeners.Listeners;
import bc.ui.swing.listeners.SelectionListener;
import bc.ui.swing.visuals.Visual;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JViewport;

/**
 *
 * @author bennyl
 */
public class OptionList extends javax.swing.JPanel {

    private LinkedHashMap items;
    private Object selected;
    private Listeners selectionListeners = Listeners.Builder.newInstance(SelectionListener.class); 
    
    /** Creates new form OptionList */
    public OptionList() {
        initComponents();
        JViewport vp = scroll.getViewport();
        vp.setLayout(new BorderLayout());
        vp.removeAll();
        vp.add(jPanel1, BorderLayout.CENTER);
        vp.setOpaque(false);
//        scroll.setBorder(new LineBorder(Color.yellow));
        items = new LinkedHashMap();
        selected = null;
    }

    public Listeners getSelectionListeners() {
        return selectionListeners;
    }

    public Object getSelected() {
        return selected;
    }

    public void remove(Object item){
        JRadioButton box = items.remove(item);
        buttonGroup1.remove(box);
        jPanel1.remove(box);
    }
    
    public void clear(){
        jPanel1.removeAll();
        while (buttonGroup1.getButtonCount() > 0){
            buttonGroup1.remove(buttonGroup1.getElements().nextElement());
        }
        items.clear();
    }
    
    public void setItems(List items){
        for (Visual i : items){
            add(i);
        }
        revalidate();
        repaint();
    }
    
    public void add(final Visual item) {
        
        final JRadioButton radio = new JRadioButton();
        items.put(item, radio);
        buttonGroup1.add(radio);
        //IF IS THE FIRST THEN SET AS SELECTED
        if (items.size() == 1){
            radio.setSelected(true);
            selectionListeners.fire().onSelectionChanged(this, Arrays.asList(item));
        }
        
        radio.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (radio.isSelected()){
                    selected = radio;
                    selectionListeners.fire().onSelectionChanged(OptionList.this, Arrays.asList(item));
                }
            }
        });
        
        radio.setText(item.toString());
        radio.setOpaque(false);
        GridBagConstraints gbc = new java.awt.GridBagConstraints();
        gbc.gridx = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.BOTH;
        jPanel1.add(radio, gbc);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        scroll = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();

        setOpaque(false);
        setLayout(new java.awt.BorderLayout());

        scroll.setBorder(null);
        scroll.setOpaque(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());
        scroll.setViewportView(jPanel1);

        add(scroll, java.awt.BorderLayout.CENTER);
    }// //GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane scroll;
    // End of variables declaration//GEN-END:variables
    
    public static void main(String[] args){
        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        OptionList list = new OptionList();
        jf.setContentPane(list);
//        list.add("test1");
//        list.add("test2");
//        list.add("test3");
        
        list.getSelectionListeners().addListener(new SelectionListener() {

            @Override
            public void onSelectionChanged(Object source, List selectedItems) {
                System.out.println("Selected is " + selectedItems.get(0));
            }
        });
        
        jf.pack();
        jf.setVisible(true);
        
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy