001package org.biopax.paxtools.command;
002
003import org.biopax.paxtools.controller.PropertyEditor;
004import org.biopax.paxtools.model.BioPAXElement;
005import org.biopax.paxtools.util.IllegalBioPAXArgumentException;
006
007import java.util.Set;
008
009public class PropertySetCommand<D extends BioPAXElement, R> extends AbstractPropertyCommand<D,R>
010{
011        private R  oldValue;
012
013        PropertySetCommand (D bpe, PropertyEditor<D,R> editor, R value)
014        {
015                super(bpe, editor, value);
016                if (editor.isMultipleCardinality())
017                {
018                        throw new IllegalBioPAXArgumentException();
019                }
020
021                Set<? extends R> valueFromBean = editor.getValueFromBean(bpe);
022                if(valueFromBean == null || valueFromBean.isEmpty()) oldValue = null;
023                else if(valueFromBean.size()>1)
024                {
025                        throw new IllegalBioPAXArgumentException();
026                }
027                oldValue = valueFromBean.iterator().next();
028        }
029
030
031        @Override
032        protected void undoAction()
033        {
034                editor.setValueToBean(oldValue, bpe);
035
036        }
037
038
039
040        @Override
041        protected void redoAction()
042        {
043                editor.setValueToBean(value, bpe);
044        }
045
046
047        public String getPresentationName()
048        {
049                return "Set " + editor.getProperty() + " of " + bpe + " to " + value;
050        }
051
052        public String getUndoPresentationName()
053        {
054                return "Set " + editor.getProperty() + " of " + bpe + " to " + oldValue;
055        }
056
057        public String getRedoPresentationName()
058        {
059                return "Set " + editor.getProperty() + " of " + bpe + " to " + value;
060        }
061}