JXRadioGroup.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXRadioGroup.java,v 1.5 2005/10/10 18:01:54 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx;
  22. import java.awt.Component;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.ActionListener;
  25. import java.awt.event.ItemEvent;
  26. import java.awt.event.ItemListener;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import javax.swing.AbstractButton;
  30. import javax.swing.Box;
  31. import javax.swing.BoxLayout;
  32. import javax.swing.ButtonGroup;
  33. import javax.swing.ButtonModel;
  34. import javax.swing.JPanel;
  35. import javax.swing.JRadioButton;
  36. /**
  37.  * @author Amy Fowler
  38.  * @version 1.0
  39.  */
  40. public class JXRadioGroup extends JPanel {
  41. private static final long serialVersionUID = 3257285842266567986L;
  42. private ButtonGroup buttonGroup;
  43.     private List<Object> values = new ArrayList<Object>();
  44.     private ActionSelectionListener actionHandler;
  45.     private List<ActionListener> actionListeners;
  46.     private int gapWidth;
  47.     public JXRadioGroup() {
  48.         this(0);
  49.     }
  50.     public JXRadioGroup(int gapWidth) {
  51.         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  52.         buttonGroup = new ButtonGroup();
  53.         this.gapWidth = gapWidth;
  54.         
  55.     }
  56.     public JXRadioGroup(Object radioValues[]) {
  57.         this();
  58.         for(int i = 0; i < radioValues.length; i++) {
  59.             add(radioValues[i]);
  60.         }
  61.     }
  62.     public void setValues(Object[] radioValues) {
  63.         clearAll();
  64.         for(int i = 0; i < radioValues.length; i++) {
  65.             add(radioValues[i]);
  66.         }
  67.     }
  68.     
  69.     private void clearAll() {
  70.         values.clear();
  71.         removeAll();
  72.         buttonGroup = new ButtonGroup();
  73.     }
  74.     public void add(Object radioValue) {
  75.         values.add(radioValue);
  76.         addButton(new JRadioButton(radioValue.toString()));
  77.     }
  78.     public void add(AbstractButton button) {
  79.         values.add(button.getText());
  80.         // PENDING: mapping needs cleanup...
  81.         addButton(button);
  82.     }
  83.     private void addButton(AbstractButton button) {
  84.         buttonGroup.add(button);
  85.         super.add(button);
  86.         if (actionHandler == null) {
  87.             actionHandler = new ActionSelectionListener();
  88. //            actionHandler = new ActionListener() {
  89. //                public void actionPerformed(ActionEvent e) {
  90. //                    fireActionEvent(e);
  91. //                }
  92. //            };
  93.         }
  94.         button.addActionListener(actionHandler);
  95.         button.addItemListener(actionHandler);
  96.     }
  97.     private class ActionSelectionListener implements ActionListener,
  98.     ItemListener {
  99.         public void actionPerformed(ActionEvent e) {
  100.             fireActionEvent(e);
  101.         
  102.         }
  103.         
  104.         public void itemStateChanged(ItemEvent e) {
  105.             fireActionEvent(null);
  106.         
  107.         }
  108. }
  109.    
  110.     private void checkGap() {
  111.         if ((getGapWidth() > 0) && (getComponentCount() > 0)) {
  112.             add(Box.createHorizontalStrut(getGapWidth()));
  113.         }
  114.     }
  115.     private int getGapWidth() {
  116.         return gapWidth;
  117.     }
  118.     public AbstractButton getSelectedButton() {
  119.         ButtonModel selectedModel = buttonGroup.getSelection();
  120.         AbstractButton children[] = getButtonComponents();
  121.         for(int i = 0; i < children.length; i++) {
  122.             AbstractButton button = (AbstractButton)children[i];
  123.             if (button.getModel() == selectedModel) {
  124.                 return button;
  125.             }
  126.         }
  127.         return null;
  128.     }
  129.     private AbstractButton[] getButtonComponents() {
  130.         Component[] children = getComponents();
  131.         List buttons = new ArrayList();
  132.         for (int i = 0; i < children.length; i++) {
  133.             if (children[i] instanceof AbstractButton) {
  134.                 buttons.add(children[i]);
  135.             }
  136.         }
  137.         return (AbstractButton[]) buttons.toArray(new AbstractButton[buttons.size()]);
  138.     }
  139.     private int getSelectedIndex() {
  140.         ButtonModel selectedModel = buttonGroup.getSelection();
  141.         Component children[] = getButtonComponents();
  142.         for (int i = 0; i < children.length; i++) {
  143.             AbstractButton button = (AbstractButton) children[i];
  144.             if (button.getModel() == selectedModel) {
  145.                 return i;
  146.             }
  147.         }
  148.         return -1;
  149.     }
  150.     public Object getSelectedValue() {
  151.         int index = getSelectedIndex();
  152.         return (index < 0 || index >= values.size()) ? null : values.get(index);
  153.     }
  154.     public void setSelectedValue(Object value) {
  155.         int index = values.indexOf(value);
  156.         AbstractButton button = getButtonComponents()[index];
  157.         button.setSelected(true);
  158.     }
  159.     public void addActionListener(ActionListener l) {
  160.         if (actionListeners == null) {
  161.             actionListeners = new ArrayList<ActionListener>();
  162.         }
  163.         actionListeners.add(l);
  164.     }
  165.     public void removeActionListener(ActionListener l) {
  166.         if (actionListeners != null) {
  167.             actionListeners.remove(l);
  168.         }
  169.     }
  170.     public ActionListener[] getActionListeners() {
  171.         if (actionListeners != null) {
  172.             return (ActionListener[])actionListeners.toArray(new ActionListener[0]);
  173.         }
  174.         return new ActionListener[0];
  175.     }
  176.     protected void fireActionEvent(ActionEvent e) {
  177.         if (actionListeners != null) {
  178.             for (int i = 0; i < actionListeners.size(); i++) {
  179.                 ActionListener l = (ActionListener) actionListeners.get(i);
  180.                 l.actionPerformed(e);
  181.             }
  182.         }
  183.     }
  184. }