// TextViewHTMLMarker.java
// By Ned Etcode
// Copyright 1996 Netscape Communications Corp.  All rights reserved.

package netscape.application;
import netscape.util.*;
import java.net.*;


/** This class should be private and is used to parse some HTML in the textview.
 *  It is public to allow getClass() to work
 *  @private
 */
public class TextViewHTMLMarker implements HTMLElement, TextViewHTMLElement {
    String marker;
    String attributes;
    TextAttachment textAttachmentCache = null;

    private TextAttachment textAttachment(URL baseURL,TextView textView) {
        if( textAttachmentCache == null ) {
            if( marker.equals("HR"))
                textAttachmentCache = new HRTextAttachment();
            else if( marker.equals("IMG")) {
                Hashtable attr;
                String  hrefUrlStr;
                URL imageUrl;
                Bitmap bm;
                int width = -1,height = -1;
                String str;

                try {
                    attr = HTMLParser.hashtableForAttributeString(attributes);
                } catch (HTMLParsingException e) {
                    attr = null;
                }
                if( attr != null && (hrefUrlStr = (String)attr.get("SRC")) != null ) {

                    try {
                        str = (String) attr.get("WIDTH");
                        if( str != null )
                            width = Integer.parseInt(str);
                        str = (String) attr.get("HEIGHT");
                        if( str != null )
                            height = Integer.parseInt(str);
                    } catch( NumberFormatException e ) {
                        width = height = -1;
                    }

                    try {
                        imageUrl = new URL( baseURL, hrefUrlStr );

                        if( false /*width != -1 && height != -1*/ ) {
                            bm = Bitmap.bitmapFromURL( imageUrl );
                            bm.setLoadsIncrementally(true);
                            bm.setUpdateTarget(textView);
                            bm.setUpdateCommand("refreshBitmap");
                            bm.loadData();
                            textAttachmentCache = new ImageAttachment(bm,width,height);
                        } else {
                            if((bm = Bitmap.bitmapFromURL( imageUrl )) != null)
                                bm.loadData();

                            if( bm != null && bm.isValid()) {
                                textAttachmentCache = new ImageAttachment(bm);
                            }
                        }
                    } catch( MalformedURLException e) {
                            System.err.println("Malformed URL " + hrefUrlStr );
                    }
                }

                /* Default image */
                if( textAttachmentCache == null ) {
                    textAttachmentCache = new BrokenImageAttachment();
                }
            }
        }
        return textAttachmentCache;
    }

    public void appendString(Hashtable context,FastStringBuffer fb) {
        if( marker.equals("BR") )
          fb.append("\n");
        else if( marker.equals("P"))
          fb.append("\n\n");
        else if( marker.equals("HR") || marker.equals("IMG"))
          fb.append(TextView.TEXT_ATTACHMENT_STRING);
    }

    /** Return the length in number of character of the component */
    public int length() {
        if( marker.equals("P"))
            return 2;
        else
            return 1;
    }

    /** Set the attributes for the string starting at index index */
    public void setAttributesStartingAt(int index, Hashtable initialAttributes,TextView target,
                                        Hashtable context) {
        if( marker.equals("HR") || marker.equals("IMG")) {
            Hashtable newAttr;
            String align;
            TextAttachment attachment;
            Range range;

            if( initialAttributes != null && initialAttributes.count() > 0) {
              Enumeration enumeration;
              Object key;

              newAttr = (Hashtable) TextView.hashtablePool.allocateObject();
              enumeration = initialAttributes.keys();
              while(enumeration.hasMoreElements() ) {
                key = enumeration.nextElement();
                newAttr.put(key,initialAttributes.get(key));
              }
            } else
              newAttr = (Hashtable)TextView.hashtablePool.allocateObject();

            attachment = textAttachment(target.baseURL(),target);
            newAttr.put(TextView.TEXT_ATTACHMENT_KEY, attachment);

            if( marker.equals("IMG")) {
              Hashtable attr;

              try {
                attr = HTMLParser.hashtableForAttributeString(attributes);
              } catch (HTMLParsingException e) {
                attr = null;
              }

              if( attr != null && (align = (String) attr.get("ALIGN")) != null ) {
                int baselineOffset=0;
                if( align.equals("TOP") ) {
                  int ascent = 0;
                  FontMetrics fm;
                  Font currentFont = (Font)newAttr.get(TextView.FONT_KEY);

                  if( currentFont == null )
                    currentFont = (Font)target.defaultAttributes().get(TextView.FONT_KEY);

                  if( currentFont != null ) {
                    fm = new FontMetrics( currentFont );
                    ascent = fm.ascent();
                  }

                  baselineOffset = attachment.height() - ascent;
                } else if( align.equals("MIDDLE"))
                  baselineOffset = attachment.height() / 2;
                newAttr.put(TextView.TEXT_ATTACHMENT_BASELINE_OFFSET_KEY,new Integer(baselineOffset));
              }
            }

            range = TextView.allocateRange(index,1);
            target.setAttributesForRange( newAttr , range);
            TextView.recycleRange(range);
            newAttr.clear();
            TextView.hashtablePool.recycleObject(newAttr);
        }
    }

    public void setMarker(String aString) {
        marker = aString;
    }

    public void setAttributes(String attr) {
        attributes = attr;
    }

    public void setChildren(Object child[]) {

    }

    public void setString(String aString) {
    }
}




