RadioButtonPanel.java :  » Workflow-Engines » osbl-1_0 » org » conform » wings » editor » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » conform » wings » editor » RadioButtonPanel.java
package org.conform.wings.editor;

import org.wings.*;
import org.conform.format.Format;
import org.conform.format.FormatFactory;

import javax.swing.*;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListDataEvent;
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Behaves like a SComboBox but displays as columns of SRadioButtons.
 * Instead of a CellRenderer it accepts a Format only. In that respect this component is
 * less flexible than its pendant. It will display the options as text only.
 *
 * @author hengels
 * @version $Revision: 772 $
 */
public class RadioButtonPanel
    extends SPanel
    implements ListDataListener, ActionListener
{
    int columns = 1;
    boolean update;

    protected ComboBoxModel model;
    protected Format format = FormatFactory.NO_FORMAT;
    SButtonGroup buttonGroup = new SButtonGroup();
    Map radioByValue = new HashMap();

    public RadioButtonPanel() {
        super(new SGridLayout());
        buttonGroup.addActionListener(this);
    }

    public ComboBoxModel getModel() {
        return model;
    }

    public void setModel(ComboBoxModel model) {
        if (isDifferent(this.model, model)) {
            if (this.model != null)
                this.model.removeListDataListener(this);

            this.model = model;

            if (this.model != null)
                this.model.addListDataListener(this);

            adaptButtons();
            updateSelection();
            reload();
        }
    }

    public SButtonGroup getButtonGroup() {
        return buttonGroup;
    }

    public void updateSelection() {
        try {
            update = true;
            SRadioButton button = (SRadioButton)radioByValue.get(model.getSelectedItem());
            if (button != null)
                button.setSelected(true);
        }
        finally {
            update = false;
        }
    }

    public Format getFormat() {
        return format != FormatFactory.NO_FORMAT ? format : null;
    }

    public void setFormat(Format format) {
        if (isDifferent(this.format, format)) {
            this.format = format;
            if (format == null)
                format = FormatFactory.NO_FORMAT;

            adaptFormat();
        }
    }

    public int getColumns() {
        return columns;
    }

    public void setColumns(int columns) {
        this.columns = columns;
        SGridLayout layout = (SGridLayout)getLayout();
        layout.setColumns(columns);
    }

    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (Iterator iterator = radioByValue.values().iterator(); iterator.hasNext();) {
            SRadioButton radio = (SRadioButton)iterator.next();
            radio.setEnabled(enabled);
        }
    }

    private void adaptButtons() {
        removeAll();
        buttonGroup.removeAll();
        radioByValue.clear();

        if (model == null)
            return;

        for (int i=0; i < model.getSize(); i++) {
            Object value = model.getElementAt(i);
            SRadioButton radio = new SRadioButton(format.format(value));
            radio.setHorizontalAlignment(SConstants.LEFT_ALIGN);
            radio.putClientProperty("value", value);
            radioByValue.put(value, radio);
            buttonGroup.add(radio);
            add(radio);
        }
    }

    private void adaptFormat() {
        if (model == null)
            return;

        for (int i=0; i < model.getSize(); i++) {
            Object value = model.getElementAt(i);
            SRadioButton radio = (SRadioButton)radioByValue.get(value);
            radio.setText(format.format(value));
        }
    }

    public void intervalAdded(ListDataEvent e) {
        adaptButtons();
    }

    public void intervalRemoved(ListDataEvent e) {
        adaptButtons();
    }

    public void contentsChanged(ListDataEvent e) {
        adaptButtons();
    }

    public void actionPerformed(ActionEvent e) {
        if (update)
            return;

        SAbstractButton radio = buttonGroup.getSelection();
        if (radio == null)
            return;

        Object value = radio.getClientProperty("value");
        model.setSelectedItem(value);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.