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 February 23, 2025
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        * @return The original button selected in the button group before the edit.
061        */
062        public JRadioButton originalValue() {
063                return origValue;
064        }
065        
066        /**
067        *  Return the new button selected in the button group after the edit.
068        *
069        * @return The new button selected in the button group after the edit.
070        */
071        public JRadioButton newValue() {
072                return newValue;
073        }
074        
075        /**
076        *  Undo the edit that was made.
077        */
078    @Override
079        public void undo() throws CannotUndoException {
080                super.undo();
081                btnGroup.setSelected(originalValue(), true);
082        }
083        
084        /**
085        * Re-apply the edit, assuming that it has been undone.
086        */
087    @Override
088        public void redo() throws CannotRedoException {
089                super.redo();
090                btnGroup.setSelected(newValue(), true);
091        }
092        
093        /**
094        *  Provides a localized, human readable description of this edit suitable for use in, say, a change log.
095        */
096    @Override
097        public String getPresentationName() {
098                return presName;
099        }
100}