package textbender.a.r.page; // 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.util.regex.*; import org.w3c.dom.Element; import textbender.g.lang.*; /** A view of the context of an impending user action, in the page. * Whenever the context changes, * it highlights the corresponding element in the document. * * @see User#getLeafGene() */ final @ThreadRestricted("AWT event dispatch") class LeafGeneObjectV implements PropertyChangeListener { /** Constructs an LeafGeneObjectV. *

* It releases itself at page exit. * There is no way to release it before then. *

*/ LeafGeneObjectV( PageVisit pageVisit ) { pageVisit.user().addPropertyChangeListener( "leafGene", LeafGeneObjectV.this ); // no need to unregister, registry does not outlive this listener moveHighlight( pageVisit.user().getLeafGene() ); // fire up } // - P r o p e r t y - C h a n g e - L i s t e n e r ---------------------------------- public void propertyChange( PropertyChangeEvent e ) { moveHighlight( (Element)e.getNewValue() ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private @ThreadSafe static void addHighlight( Element element ) { String previousClasses = element.getAttributeNS( null, "class" ); element.setAttributeNS ( null, "class", "textbender-a-r-page-leafGeneObject " + previousClasses ); } private Element highlightedElement; /** Matches "textbender-a-r-page-leafGeneObject", with or without trailing spaces. */ private static final Pattern highlightRemovalPattern = Pattern.compile( "\\btextbender-a-r-page-leafGeneObject\\b *" ); private void moveHighlight( Element newElement ) { assert java.awt.EventQueue.isDispatchThread(); final Element oldElement = highlightedElement; highlightedElement = newElement; if( newElement != null ) addHighlight( newElement ); if( oldElement != null ) removeHighlight( oldElement ); } private @ThreadSafe static void removeHighlight( Element element ) { Matcher m = highlightRemovalPattern.matcher( element.getAttributeNS( null, "class" )); String cleanClasses = m.replaceAll( "" ); // i.e. remove element.setAttributeNS( null, "class", cleanClasses ); } }