package textbender.a.u.locusPoint; // Copyright 2006, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.beans.*; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import textbender.a.r.desk.*; import textbender.g.beans.*; import textbender.g.lang.*; import textbender.g.util.logging.LoggerX; import textbender.g.util.prefs.*; /** Shadow-point service implementation. */ public final class LocusPoint1 extends UnicastRemoteObject implements LocusPoint { private static final AtomicReference instanceA = new AtomicReference(); /** Creates the single instance of LocusPoint1 and registers it as a service. */ public LocusPoint1() throws RemoteException { if( !instanceA.compareAndSet( /*expect*/null, LocusPoint1.this )) throw new IllegalStateException(); LoggerX.i(getClass()).config( "creating host service" ); HostServiceRegistry1.i().addService( LocusPoint.class, LocusPoint1.this ); } // ```````````````````````````````````````````````````````````````````````````````````` // Initialized early, for use in other initializers. private final PropertyChangeSupport propertyChangeSupport // OPT for dispatch speed per textbender.a.u.transfer.PRTransfer1.propertyChangeSupport = new PropertyChangeSupport( LocusPoint1.this ); // - P r o p e r t y - C h a n g e - L i s t e n e r - R . R e g i s t r y ------------ public @ThreadSafe void addPropertyChangeListener( PropertyChangeListenerR listener ) { propertyChangeSupport.addPropertyChangeListener ( new PropertyChangeListenerR.WrapperL( listener, propertyChangeDispatchCatcher )); } public @ThreadSafe void disabledPropertyChangeListener( PropertyChangeListenerR listener ) { propertyChangeSupport.removePropertyChangeListener ( new PropertyChangeListenerR.WrapperL( listener, propertyChangeDispatchCatcher )); // LoggerX.i(getClass()).finest( "listener list down to: " + propertyChangeSupport.getPropertyChangeListeners().length ); } // - P r o p e r t y - C h a n g e - L i s t e n e r - R . R e g i s t r y - N -------- public @ThreadSafe void addPropertyChangeListener( String propertyName, PropertyChangeListenerR listener ) { propertyChangeSupport.addPropertyChangeListener ( propertyName, new PropertyChangeListenerR.WrapperL ( listener, new PropertyChangeListenerR.DispatchCatcherN ( /*registry*/LocusPoint1.this, propertyName ) ) ); } // @SuppressWarnings("unchecked") public @ThreadSafe void disabledPropertyChangeListener( String propertyName, PropertyChangeListenerR listener ) { Catcher0 catcher = Catcher0.i(); propertyChangeSupport.removePropertyChangeListener // ( // propertyName, // new PropertyChangeListenerR.WrapperL // ( listener, PropertyChangeListenerR.DISPATCH_CATCHER_0 ) // ); ( propertyName, new PropertyChangeListenerR.WrapperL // ( listener, (Catcher0)Catcher0.i() ) //// compiler: "inconvertible types". Maybe Catcher0 ought to offer something like Collections.checked*() ( listener, catcher ) ); // LoggerX.i(getClass()).finest( "listener list down to: " + propertyChangeSupport.getPropertyChangeListeners().length ); } // - S h a d o w - P o i n t ---------------------------------------------------------- public @ThreadSafe synchronized String getLocus() { return locus; }; private @ThreadRestricted("holds this") String locus; public @ThreadSafe synchronized void setLocus( final String newLocus ) { if( ObjectX.nullEquals( locus, newLocus )) return; if( Run.i().spool().isUnwinding() ) return; PropertyChangeEventAD e = new PropertyChangeEventAD ( LocusPoint1.this, "locus", locus, newLocus, propertyChangeSupport ); locus = newLocus; DeskDaemon.i().executor().execute( e ); // enqueue in sync, for correct e order } public @ThreadSafe synchronized boolean isEnabled() { return enabled; }; private final BooleanPreferenceDC enabledP = new BooleanPreferenceDC ( PreferencesX.userNodeForClass( LocusPoint1.class ), "ENABLED", /*default*/false ); { propertyChangeSupport.addPropertyChangeListener( "enabled", new PropertyChangeListener() // no need to unregister, registry does not outlive this listener { public void propertyChange( PropertyChangeEvent e ) { enabledP.put( isEnabled() ); } }); } private @ThreadRestricted("holds this") boolean enabled = enabledP.get(); public @ThreadSafe synchronized void setEnabled( boolean newEnabled ) { if( enabled == newEnabled ) return; if( Run.i().spool().isUnwinding() ) return; PropertyChangeEventAD e = new PropertyChangeEventAD ( LocusPoint1.this, "enabled", enabled, newEnabled, propertyChangeSupport ); enabled = newEnabled; DeskDaemon.i().executor().execute( e ); // enqueue in sync, for correct e order // if( !enabled ) DeskDaemon.i().executor().execute( new Runnable() // { // public void run() { setLocus( null ); } // after dispatch of e // }); ///// but sometimes pointer too slow to disable itself, clobbers null locus, thus stuck at non-null value, so: if( !enabled ) DeskDaemon.i().executor().schedule( new Runnable() { public void run() { setLocus( null ); } // after dispatch of e }, 1, TimeUnit.SECONDS ); // well after } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final PropertyChangeListenerR.DispatchCatcher propertyChangeDispatchCatcher = new PropertyChangeListenerR.DispatchCatcher( /*registry*/LocusPoint1.this ); // private final Catcher propertyChangeListenerCatcher // = new Catcher0() // { // // public @Override void catchException( PropertyChangeListenerR source, Exception x ) // { // LoggerX.i(getClass()).info( "removing listener, it threw: " + x ); // disabledPropertyChangeListener( source ); // } // // }; }