001/* [{
002Copyright 2009 Marcello Bastea-Forte <marcello at cellosoft.com>
003Copyright 2009 Nicolas Carranza <nicarran at gmail.com>
004
005This file is part of jpen.
006
007jpen is free software: you can redistribute it and/or modify
008it under the terms of the GNU Lesser General Public License as published by
009the Free Software Foundation, either version 3 of the License,
010or (at your option) any later version.
011
012jpen is distributed in the hope that it will be useful,
013but WITHOUT ANY WARRANTY; without even the implied warranty of
014MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015GNU Lesser General Public License for more details.
016
017You should have received a copy of the GNU Lesser General Public License
018along with jpen.  If not, see <http://www.gnu.org/licenses/>.
019}] */
020package jpen.owner;
021import java.awt.geom.Point2D;
022import java.awt.Point;
023import java.awt.Window;
024import java.util.Arrays;
025import java.util.Collection;
026import jpen.internal.ActiveWindowProperty;
027import jpen.PenEvent;
028import jpen.PenProvider;
029import jpen.provider.osx.CocoaProvider;
030import jpen.provider.wintab.WintabProvider;
031import jpen.provider.xinput.XinputProvider;
032/**
033Defines a {@link PenClip} for all the screen. Its {@link jpen.PenManager} is unpaused when the current application has an active window.
034*/
035public class ScreenPenOwner implements PenOwner {
036
037        private static ScreenPenOwner instance;
038
039        public synchronized static ScreenPenOwner getInstance(){
040                return instance==null? instance=new ScreenPenOwner():
041                                         instance;
042        }
043        
044        private ScreenPenOwner(){}
045
046        //@Override
047        public Collection<PenProvider.Constructor> getPenProviderConstructors(){
048                return Arrays.asList(
049                                                 new PenProvider.Constructor[]{
050                                                         // new SystemProvider.Constructor(), //Does not work because it needs a java.awt.Component to register the MouseListener
051                                                         new XinputProvider.Constructor(),
052                                                         new WintabProvider.Constructor(),
053                                                         new CocoaProvider.Constructor()
054                                                 }
055                                         );
056        }
057
058        //@Override
059        public void setPenManagerHandle(final PenManagerHandle penManagerHandle){
060                ActiveWindowProperty.Listener activeWindowPL=new ActiveWindowProperty.Listener(){
061                                        //@Override
062                                        public void activeWindowChanged(Window activeWindow){
063                                                synchronized(penManagerHandle.getPenSchedulerLock()){
064                                                        penManagerHandle.setPenManagerPaused(activeWindow==null);
065                                                }
066                                        }
067                                };
068                ActiveWindowProperty activeWindowP=new ActiveWindowProperty(activeWindowPL); // -> registers itself with the current KeyboardFocusManager
069                activeWindowPL.activeWindowChanged(activeWindowP.get());
070        }
071
072        private final PenClip penClip = new PenClip() {
073                                //@Override
074                                public void evalLocationOnScreen(Point locationOnScreen){
075                                        // The location of this PenClip is always on (0, 0) screen coordinates.
076                                        locationOnScreen.x=locationOnScreen.y=0;
077                                }
078                                //@Override
079                                public boolean contains(Point2D.Float point){
080                                        // This PenClip covers all the screen.
081                                        return true;
082                                }
083                        };
084
085        //@Override
086        public PenClip getPenClip() {
087                return penClip;
088        }
089
090        //@Override
091        public boolean isDraggingOut() {
092                return false;
093        }
094
095        //@Override
096        public Object evalPenEventTag(PenEvent ev){
097                return null;
098        }
099
100        //@Override
101        public boolean enforceSinglePenManager(){
102                return false;
103        }
104}