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.*; import java.rmi.server.*; import java.util.regex.*; import org.w3c.dom.*; import textbender.a.r.page.*; import textbender.d.gene.*; import textbender.g.beans.*; import textbender.g.hold.*; import textbender.g.lang.*; import textbender.g.util.logging.*; /** Highlighter for a Web document, via an in-page user-applet. * Whenever the locus-point locus changes, * it highlights the corresponding gene in the document. */ public final class InPageHighlighter extends UnicastRemoteObject implements PropertyChangeListenerR { // cf. textbender.a.u.chromography.InPageStainer /** Constructs an InPageHighlighter. * * @param pageVisit context * * @throws RemoteException if creation fails * in communication with desk daemon */ public InPageHighlighter( PageVisit pageVisit ) throws RemoteException { this.pageVisit = pageVisit; locusPoint = PageDaemons.i().connections().hostServiceRegistry().getService( LocusPoint.class ); locusPoint.addPropertyChangeListener( "locus", InPageHighlighter.this ); pageVisit.spool().add( new Hold() { public @ThreadSafe void release() { try{ locusPoint.disabledPropertyChangeListener( "locus", InPageHighlighter.this ); } catch( RemoteException x ) { LoggerX.i(getClass()).warning( x.toString() ); }} }); moveHighlight( locusPoint.getLocus() ); // fire up } // - P r o p e r t y - C h a n g e - L i s t e n e r - R ------------------------------ public @ThreadSafe void propertyChange( PropertyChangeEvent e ) { if( pageVisit.spool().isUnwinding() ) return; moveHighlight( (String)e.getNewValue() ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private @ThreadSafe static void addHighlight( Element gene ) { final String highlightClassSp; if( Gene.hasChildGene( gene )) highlightClassSp = "textbender-a-u-locusPoint-gene-outer "; else highlightClassSp = "textbender-a-u-locusPoint-gene "; String previousClasses = gene.getAttributeNS( null, "class" ); gene.setAttributeNS( null, "class", highlightClassSp + previousClasses ); } private @ThreadRestricted("holds this") Object highlightedGenes; /** Matches either "textbender-a-u-locusPoint-gene" * or "textbender-a-u-locusPoint-gene-outer", with or without trailing spaces. */ private static final Pattern highlightRemovalPattern = Pattern.compile( "\\btextbender-a-u-locusPoint-gene(?:-outer)?\\b *" ); private @ThreadSafe synchronized void moveHighlight( String newLocus ) { final Object oldGenes = highlightedGenes; if( newLocus == null ) highlightedGenes = null; else { highlightedGenes = pageVisit.locusToGeneMap().get( newLocus ); if( highlightedGenes instanceof Object[] ) { for( Object gene : (Object[])highlightedGenes ) addHighlight( (Element)gene ); } else if( highlightedGenes != null ) addHighlight( (Element)highlightedGenes ); } if( oldGenes instanceof Object[] ) { for( Object gene : (Object[])oldGenes ) removeHighlight( (Element)gene ); } else if( oldGenes != null ) removeHighlight( (Element)oldGenes ); } private final PageVisit pageVisit; private @ThreadSafe static void removeHighlight( Element gene ) { Matcher m = highlightRemovalPattern.matcher( gene.getAttributeNS( null, "class" )); String cleanClasses = m.replaceAll( "" ); // i.e. remove gene.setAttributeNS( null, "class", cleanClasses ); } private final LocusPoint locusPoint; }