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.provider;
020
021import java.awt.Component;
022import java.util.ArrayList;
023import java.util.List;
024import jpen.owner.PenOwner;
025import jpen.PButton;
026import jpen.PButtonEvent;
027import jpen.Pen;
028import jpen.PenDevice;
029import jpen.PenManager;
030import jpen.PenProvider;
031import jpen.PKind;
032import jpen.PLevel;
033import jpen.PLevelEvent;
034
035@SuppressWarnings("deprecation")
036public abstract class AbstractPenDevice
037        implements PenDevice {
038
039        private byte id;
040        private String physicalId;
041        private final PenProvider provider;
042        private int kindTypeNumber=PKind.Type.CURSOR.ordinal();
043        private boolean enabled;
044
045        protected AbstractPenDevice(PenProvider provider) {
046                this.provider=provider;
047        }
048
049        //@Override
050        public byte getId(){
051                return id;
052        }
053
054        //@Override
055        public void penManagerSetId(byte id){
056                this.id=id;
057        }
058        
059        //@Override
060        public PenProvider getProvider() {
061                return provider;
062        }
063
064        //@Override
065        public int getKindTypeNumber() {
066                return kindTypeNumber;
067        }
068
069        //@Override
070        public void setKindTypeNumber(int kindTypeNumber) {
071                if(kindTypeNumber<0)
072                        throw new IllegalArgumentException("PKind.Type must be >= 0");
073                this.kindTypeNumber=kindTypeNumber;
074        }
075
076        //@Override
077        public boolean getEnabled() {
078                return enabled;
079        }
080
081        //@Override
082        public void setEnabled(boolean enabled) {
083                this.enabled=enabled;
084        }
085
086        //@Override
087        public String getPhysicalId(){
088                String physicalId=this.physicalId;
089                if(physicalId==null)
090                        synchronized(this){
091                                physicalId=this.physicalId;
092                                if(physicalId==null)
093                                        physicalId=this.physicalId=evalPhysicalId();
094                        }
095                return physicalId;
096        }
097
098        protected String evalPhysicalId(){
099                return getName().trim()+"@"+provider.getConstructor().getName().trim();
100        }
101
102        public final PenManager getPenManager() {
103                return provider.getConstructor().getPenManager();
104        }
105
106        public final Pen getPen() {
107                return getPenManager().pen;
108        }
109        
110        //@Override
111        public boolean getUseFractionalMovements(){
112                return true;
113        }
114        
115        //@Override
116        public void penManagerSetUseFractionalMovements(boolean useFractionalMovement){
117                throw new UnsupportedOperationException();
118        }
119        
120        @Override
121        public String toString() {
122                return "[PenDevice: provider="+getProvider()+", name="+getName()+", kind="+getOrNull(PKind.Type.VALUES, getKindTypeNumber())+"("+getKindTypeNumber()+")]";
123        }
124
125        private static final <T> T getOrNull(List<T> l, int index){
126                if(index<0 || index>=l.size())
127                        return null;
128                return l.get(index);
129        }
130}