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.util.ArrayList;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.List;
025import jpen.PenProvider;
026import jpen.PenDevice;
027import jpen.PenManager;
028
029public abstract class AbstractPenProvider
030        implements PenProvider {
031        private final Constructor constructor;
032        protected final List<PenDevice> devices=new ArrayList<PenDevice>();
033        private final List<PenDevice> devicesA=Collections.unmodifiableList(devices);
034
035        protected AbstractPenProvider(Constructor constructor) {
036                this.constructor=constructor;
037        }
038
039        //@Override
040        public Collection<PenDevice> getDevices() {
041                return devicesA;
042        }
043
044        public final PenManager getPenManager() {
045                return getConstructor().getPenManager();
046        }
047
048        //@Override
049        public final Constructor getConstructor() {
050                return constructor;
051        }
052        
053        //@Override
054        public boolean getUseRelativeLocationFilter(){
055                return false;
056        }
057
058        //@Override
059        public String toString() {
060                return "[PenProvider: constructor.name="+getConstructor().getName()+"]";
061        }
062        
063        public static abstract class AbstractConstructor
064                implements PenProvider.Constructor{
065                private PenManager penManager;
066                private PenProvider constructed;
067                private ConstructionException constructionException;
068
069                //@Override
070                public PenManager getPenManager(){
071                        return penManager;
072                }
073
074                //@Override
075                public ConstructionException getConstructionException(){
076                        return constructionException;
077                }
078
079                //@Override
080                public PenProvider getConstructed(){
081                        return constructed;
082                }
083
084                //@Override
085                public final boolean construct(PenManager penManager){
086                        if(this.penManager!=null)
087                                throw new IllegalStateException("constructor already used by PenManager");
088                        this.penManager=penManager;
089                        try{
090                                checkExpectedNativeBuild();
091                                this.constructed=constructProvider();
092                        }catch(Throwable t){
093                                this.constructionException=new ConstructionException(t);
094                                return false;
095                        }
096                        return true;
097                }
098                
099                protected abstract PenProvider constructProvider() throws Throwable;
100                
101                private void checkExpectedNativeBuild() throws IllegalStateException{
102                        if(getExpectedNativeBuild()>getNativeBuild())
103                                throw new IllegalStateException("expectedNativeBuild number ("+getExpectedNativeBuild()+") is greater than library's nativeBuild number ("+getNativeBuild()+")");
104                }
105                
106                //@Override
107                public int getNativeVersion(){
108                        return -1;
109                }
110                //@Override
111                public int getNativeBuild(){
112                        return -1;
113                }
114                //@Override
115                public int getExpectedNativeBuild(){
116                        return -1;
117                }
118        }
119}