package textbender.a.u.chromography; // Copyright 2006-2007, 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.PropertyChangeEvent; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.*; import java.util.regex.*; import org.w3c.dom.Element; import textbender.a.r.page.*; import textbender.d.gene.*; import textbender.g.beans.PropertyChangeListenerR; import textbender.g.hold.Hold; import textbender.g.lang.*; import textbender.g.util.logging.LoggerX; /** Chromograph stainer for a Web document, via an in-page user-applet. * Whenever a new reference series is set on the desktop, * it tints the document's text to match. */ public final class InPageStainer extends UnicastRemoteObject implements PropertyChangeListenerR { // cf. textbender.a.u.locusPoint.InPageHighlighter /** Constructs an InPageStainer. * * @param pageVisit context * @param inPageFactory for this page visit */ public InPageStainer( PageVisit pageVisit, InPageFactory inPageFactory ) throws RemoteException { this.pageVisit = pageVisit; this.inPageFactory = inPageFactory; documentBody = pageVisit.document().getBody(); documentElement = pageVisit.document().getDocumentElement(); chromography = PageDaemons.i().connections().hostServiceRegistry().getService( Chromography.class ); chromography.addPropertyChangeListener( InPageStainer.this ); pageVisit.spool().add( new Hold() { public @ThreadSafe void release() { try{ chromography.disabledPropertyChangeListener( InPageStainer.this ); } catch( RemoteException x ) { LoggerX.i(getClass()).warning( x.toString() ); }} }); graph( chromography.getReferenceSeries() ); // 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; graph( (ReferenceSeries)e.getNewValue() ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final Chromography chromography; final Element documentBody; final Element documentElement; private @ThreadSafe synchronized void graph( ReferenceSeries newReferenceSeries ) { // System.out.println( "IPS graphing" ); // Unstain and restain all genes. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - final boolean stainedGenesNeedsTrimming = stainedGenes.size() > 400 && ( newReferenceSeries == null || newReferenceSeries.leafLocusSetArray().length < 100 ); unstain( stainedGenes ); stainedGenes.clear(); if( newReferenceSeries != null ) { final String[] locusArray = newReferenceSeries.leafLocusSetArray(); stainedGenes.ensureCapacity( locusArray.length + 10 ); // guess for( int l = locusArray.length - 1; l >= 0; --l ) { final Object genes = pageVisit.locusToGeneMap().get( locusArray[l] ); final int lAdjusted = l + 7; // fudge, pending 'anchor chromatic indeces to document', so it does not always begin at lightest shade // final int lAdjusted = l; if( genes instanceof Object[] ) { for( Object gene : (Object[])genes ) { if( stainMaybe( (Element)gene, lAdjusted )) stainedGenes.add( (Element)gene ); } } else if( genes != null ) { if( stainMaybe( (Element)genes, lAdjusted )) stainedGenes.add( (Element)genes ); } } } if( stainedGenesNeedsTrimming ) stainedGenes.trimToSize(); // Ensure page as a whole has appropriate style. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if( stainedGenes.size() > 0 ) { setPageGraphedStyle( documentElement, true ); setPageGraphedStyle( documentBody, true ); } else { setPageGraphedStyle( documentBody, false ); setPageGraphedStyle( documentElement, false ); } } private final InPageFactory inPageFactory; /** Matches "textbender-a-u-chromography-page-graphed", with or without trailing spaces. */ private static final Pattern pageGraphedRemovalPattern = Pattern.compile( "\\btextbender-a-u-chromography-page-graphed\\b *" ); private final PageVisit pageVisit; private static final int RS_SPECTRUM_LENGTH = 1024; // per in-page.css static void setPageGraphedStyle( final Element element, final boolean on ) { String classes = element.getAttributeNS( null, "class" ); final Matcher m = pageGraphedRemovalPattern.matcher( classes ); if( on ) { if( !m.matches() ) // not already has class { element.setAttributeNS ( null, "class", "textbender-a-u-chromography-page-graphed " + classes ); } } else // off { classes = m.replaceAll( "" ); // i.e. remove element.setAttributeNS( null, "class", classes ); } } private @ThreadSafe static boolean stainMaybe( Element gene, int colourIndex ) { if( Gene.hasChildGene( gene )) return false; // stain leaves only String previousClasses = gene.getAttributeNS( null, "class" ); gene.setAttributeNS ( null, "class", "textbender-a-u-chromography-RS-" + Integer.toString( colourIndex % RS_SPECTRUM_LENGTH ) + " " + previousClasses ); return true; } private @ThreadRestricted("holds this") final ArrayList stainedGenes = new ArrayList(); /** Matches a textbender-a-u-chromography-RS CSS class name, * with or without trailing spaces. */ private static final Pattern stainRemovalPattern = Pattern.compile( "\\btextbender-a-u-chromography-RS-[0-9]+ *" ); private @ThreadSafe static void unstain( List genes ) { for( Element gene : genes ) { Matcher m = stainRemovalPattern.matcher( gene.getAttributeNS( null, "class" )); String cleanClasses = m.replaceAll( "" ); // i.e. remove gene.setAttributeNS( null, "class", cleanClasses ); } } }