001/* [{
002Copyright 2007, 2008 Nicolas Carranza <nicarran at gmail.com>
003
004This file is part of jpen.
005
006jpen is free software: you can redistribute it and/or modify
007it under the terms of the GNU Lesser General Public License as published by
008the Free Software Foundation, either version 3 of the License,
009or (at your option) any later version.
010
011jpen is distributed in the hope that it will be useful,
012but WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014GNU Lesser General Public License for more details.
015
016You should have received a copy of the GNU Lesser General Public License
017along with jpen.  If not, see <http://www.gnu.org/licenses/>.
018}] */
019package jpen;
020
021import java.io.IOException;
022import java.io.ObjectInputStream;
023import java.io.ObjectStreamClass;
024import java.util.Arrays;
025import java.util.Set;
026import jpen.event.PenListener;
027import jpen.internal.AccessibleField;
028
029public class PLevelEvent
030                        extends PenEvent
031        implements java.io.Serializable {
032        public static final long serialVersionUID=2l;
033
034        public final PLevel[] levels;
035
036        public PLevelEvent(PenDevice device, long deviceTime, PLevel[] levels) {
037                super(device, deviceTime);
038                this.levels=levels;
039        }
040        
041        @Override
042        void copyTo(PenState penState){
043                penState.levels.setValues(this);
044        }
045
046        @Override
047        void dispatch() {
048                for(PenListener l:pen.getListenersArray())
049                        l.penLevelEvent(this);
050        }
051
052        public boolean containsLevelOfType(Set<PLevel.Type> levelTypes){
053                for(int i=levels.length; --i>=0;)
054                        if(levelTypes.contains(levels[i].getType()))
055                                return true;
056                return false;
057        }
058
059        public boolean isMovement() {
060                return containsLevelOfType(PLevel.Type.MOVEMENT_TYPES);
061        }
062
063        @Override
064        public String toString() {
065                return "[PLevelEvent: super="+super.toString()+", levels="+Arrays.asList(levels)+"]";
066        }
067
068        private void readObject(ObjectInputStream in)
069        throws IOException, ClassNotFoundException {
070                try{
071                        ObjectInputStream.GetField fields = in.readFields();
072                        levelsField.getField().set(this, fields.get("levels", null));
073                        //v Backwards compatibility:
074                        //vv deviceId and deviceTime were moved to the super class PenEvent:
075                        ObjectStreamClass objectStreamClass=fields.getObjectStreamClass();
076                        if(objectStreamClass.getField("deviceId")!=null)
077                                PenEvent.deviceIdField.getField().set(this, fields.get("deviceId", (byte)0));
078                        if(objectStreamClass.getField("deviceTime")!=null)
079                                PenEvent.deviceTimeField.getField().set(this, fields.get("deviceTime", 0l));
080                        //^^
081                        //^
082                }catch(IllegalAccessException ex){
083                        throw new AssertionError(ex);
084                }
085        }
086
087        private static final AccessibleField levelsField=new AccessibleField(PLevelEvent.class, "levels");
088
089}