/*
** Salsa - Swing Add-On Suite
** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package salsa.html;

import java.util.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class MutableHTMLDocument extends HTMLDocument
{
   public MutableHTMLDocument( StyleSheet styles )
   {
      super( styles );
   }


   public void setTitle( String title )
   {
// This will work only if the <title> element was
// previously created. Looks like a bug in the HTML package.

      Dictionary di = getDocumentProperties();
      di.put( Document.TitleProperty, title );
      setDocumentProperties( di );
   }

   public Element getElementByTag( HTML.Tag tag )
   {
      Element root = getDefaultRootElement();
      return getElementByTag( root, tag );
   }

   public Element getElementByTag( Element parent, HTML.Tag tag )
   {
      if( parent == null || tag == null )
         return null;

      for( int i = 0; i < parent.getElementCount(); i++ )
      {
         Element child = parent.getElement( i );
         if( child.getAttributes().getAttribute( StyleConstants.NameAttribute ).equals( tag ) )
            return child;

         Element e = getElementByTag( child, tag );
         if( e != null )
            return e;
      }
      return null;
   }

   public String getTitle()
   {
      return ( String ) getProperty( Document.TitleProperty );
   }

   public void addAttributes( Element e, AttributeSet attributes )
   {
      if( e == null || attributes == null )
         return;

      try
      {
         writeLock();
         MutableAttributeSet mattr = ( MutableAttributeSet ) e.getAttributes();
         mattr.addAttributes( attributes );

         fireChangedUpdate( new DefaultDocumentEvent( 0, getLength(),
               DocumentEvent.EventType.CHANGE ) );
      }
      finally
      {
         writeUnlock();
      }
   }
}