001
002/**
003*   Please feel free to use any fragment of the code in this file that you need
004*   in your own work. As far as I am concerned, it's in the public domain. No
005*   permission is necessary or required. Credit is always appreciated if you
006*   use a large chunk or base a significant product on one of my examples,
007*   but that's not required either.
008*
009*   This code is distributed in the hope that it will be useful,
010*   but WITHOUT ANY WARRANTY; without even the implied warranty of
011*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
012*
013*      --- Joseph A. Huwaldt
014**/
015package jahuwaldt.swing;
016
017import javax.swing.JRadioButton;
018import javax.swing.undo.AbstractUndoableEdit;
019import javax.swing.undo.CannotUndoException;
020import javax.swing.undo.CannotRedoException;
021
022
023/**
024*  Class used to record the changes made to a JButtonGroup
025*  object which holds a list of JRadioButtons.
026*  This class is used to support an undo/redo functionality.
027*
028*  <p>  Modified by:  Joseph A. Huwaldt   </p>
029*
030*  @author  Joseph A. Huwaldt   Date: June 6, 2008
031*  @version September 16, 2012
032**/
033@SuppressWarnings("serial")
034public class JButtonGroupEdit extends AbstractUndoableEdit {
035
036        private JButtonGroup btnGroup;
037        private JRadioButton origValue, newValue;
038        private String presName;
039
040        /**
041        *  Construct an edit that records changes made to a JButtonGroup.  The
042        *  originally selected button is the the button returned by the
043        *  "getPreviousSelected()" method of JButtonGroup.
044        *  The newly selected button is the button returned by
045        *  the "getSelected()" method of JButtonGroup. 
046        *
047        *  @param btnGroup          The radio button group being edited.
048        *  @param presentationName  Brief, human readable, description of the edit.
049        **/
050        public JButtonGroupEdit(JButtonGroup btnGroup, String presentationName) {
051                this.btnGroup = btnGroup;
052                this.origValue = (JRadioButton)btnGroup.getPreviousSelected();
053                this.newValue = (JRadioButton)btnGroup.getSelected();
054                this.presName = presentationName;
055        }
056        
057        /**
058        *  Return the original button selected in the button group before the edit.
059        **/
060        public JRadioButton originalValue() {
061                return origValue;
062        }
063        
064        /**
065        *  Return the new button selected in the button group after the edit.
066        **/
067        public JRadioButton newValue() {
068                return newValue;
069        }
070        
071        /**
072        *  Undo the edit that was made.
073        **/
074    @Override
075        public void undo() throws CannotUndoException {
076                super.undo();
077                btnGroup.setSelected(originalValue(), true);
078        }
079        
080        /**
081        * Re-apply the edit, assuming that it has been undone.
082        **/
083    @Override
084        public void redo() throws CannotRedoException {
085                super.redo();
086                btnGroup.setSelected(newValue(), true);
087        }
088        
089        /**
090        *  Provides a localized, human readable description of this edit suitable for use in, say, a change log.
091        **/
092    @Override
093        public String getPresentationName() {
094                return presName;
095        }
096}