home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / HTMLRootView.java < prev    next >
Text File  |  1998-02-26  |  5KB  |  198 lines

  1. /*
  2.  * @(#)HTMLRootView.java    1.4   98/01/07
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text.html;
  21.  
  22. import java.util.Enumeration;
  23. import java.awt.*;
  24. import java.net.*;
  25. import java.awt.image.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.text.*;
  28. import com.sun.java.swing.*;
  29. import java.awt.event.*;
  30.  
  31. /**
  32.  *
  33.  * @author  Makarand Gokhale
  34.  * @version 1.4 01/07/98
  35.  */
  36. class HTMLRootView extends BoxView  {
  37.  
  38.     transient Image bgimage;
  39.     transient Color bgcolor;
  40.     transient String colorStr = null;
  41.     transient String imageStr = null;
  42.     HTMLRootImageObserver imageObserver=null;
  43.  
  44.     boolean bImageLoaded=false;
  45.     boolean bImageLoadFailed=false;
  46.  
  47.  
  48.     int imgWidth = -1;
  49.     int imgHeight = -1;
  50.     int drawWidth = -1;
  51.     int drawHeight = -1;
  52.  
  53.  
  54.     public HTMLRootView(Element elem, int axis) {
  55.     super(elem, axis);
  56. // Disable until Time gets this working
  57. //    loadBackground(elem.getAttributes());
  58.     }
  59.  
  60.     public void loadBackground ( AttributeSet as) {
  61.     imageStr = (String) as.getAttribute("background");
  62.         colorStr = (String ) as.getAttribute("bgcolor");
  63.     bImageLoaded = false;
  64.     bImageLoadFailed = false;
  65.     imgWidth = imgHeight = drawWidth = drawHeight = -1;
  66.         bgimage = null;
  67.        if ( colorStr == null )
  68.        bgcolor = Color.lightGray;
  69.        else {
  70.        try {
  71.            bgcolor = HTMLUtils.stringToColor(colorStr);
  72.        } catch (HTMLException e) {
  73.            bgcolor = Color.lightGray;
  74.        }
  75.        }
  76.        if(imageStr != null) {
  77.        imageObserver = new HTMLRootImageObserver();
  78.        bgimage = loadImage(imageStr);
  79.        bImageLoaded = false;
  80.     }
  81.           
  82.     }
  83.  
  84.     private Image loadImage(String imageStr) {
  85.         if( imageStr==null ) return null;
  86.         URL url = null;
  87.         URL reference = (URL) getElement().getDocument().getProperty(Document.StreamDescriptionProperty);
  88.     if(reference != null ) {
  89.         try {
  90.         url = new URL(reference,imageStr);
  91.         } catch (MalformedURLException e) {
  92.         System.out.println("Malformed URL:"+reference);
  93.         }
  94.     }
  95.     if(url != null )
  96.         return Toolkit.getDefaultToolkit().getImage(url);
  97.     else
  98.         return Toolkit.getDefaultToolkit().getImage(imageStr);
  99.  
  100.     }
  101.    
  102.  
  103.     public void paint(Graphics g, Shape allocation) {
  104. //  Disable painting until Time gets it  to paint via HTMLEditorKit
  105. //    paintBackground( g, allocation);
  106.     super.paint(g,allocation);
  107.     }
  108.     public void paintBackground(Graphics g, Shape allocation ) {
  109.     Element elem = getElement();
  110.     AttributeSet as = elem.getAttributes();
  111.     String cStr = (String) as.getAttribute("bgcolor");
  112.     String iStr = (String) as.getAttribute("background");
  113.     if( cStr != null) {
  114.         if(!cStr.equals(colorStr))
  115.             loadBackground(as);
  116.     }
  117.     else if (colorStr != null)
  118.         loadBackground(as);
  119.     if( iStr != null) {
  120.          if(!iStr.equals(imageStr))
  121.             loadBackground(as);
  122.     }
  123.     else if (imageStr != null )
  124.         loadBackground(as);
  125.  
  126.     Rectangle alloc = allocation.getBounds();
  127.     setSize(alloc.width, alloc.height);
  128.         if(bgimage != null){
  129.          if (bImageLoaded) {
  130.          for(int xpos=0;xpos < alloc.width; xpos+=imgWidth)  {
  131.             for(int ypos=0;ypos < alloc.height; ypos+=imgHeight)  {
  132.             g.drawImage(bgimage,xpos,ypos, null);
  133.             }
  134.          }
  135.         }
  136.         else {
  137.              paintBGColor(g, allocation);
  138.          if(!bImageLoadFailed)
  139.              g.drawImage(bgimage,0,0, imageObserver);
  140.         }
  141.         if(!bImageLoadFailed) {
  142.         if(imgWidth <= 0 )
  143.             imgWidth = bgimage.getWidth(imageObserver);
  144.         if(imgHeight <= 0 )
  145.             imgHeight = bgimage.getHeight(imageObserver);
  146.         }
  147.     }
  148.         else {
  149.         paintBGColor(g, allocation);
  150.         }
  151.     }
  152.  
  153.     private void paintBGColor(Graphics g, Shape allocation ) {
  154.     Rectangle alloc = allocation.getBounds();
  155.         g.setColor(bgcolor);
  156.         g.fillRect(0,0, alloc.width, alloc.height);
  157.     }
  158.  
  159.  
  160.  
  161.  
  162.     class  HTMLRootImageObserver  implements ImageObserver {
  163.  
  164.     public HTMLRootImageObserver() {
  165.         super();
  166.     }
  167.  
  168.      public boolean imageUpdate(Image img,
  169.                                      int infoflags,
  170.                                      int x,
  171.                                      int y,
  172.                                      int width,
  173.                                      int height) {
  174.         if((infoflags & ERROR) > 0 || (infoflags & ABORT) > 0 )
  175.             bImageLoadFailed = true;
  176.         if((infoflags & WIDTH) > 0 ) {
  177.              imgWidth = width;
  178.         }
  179.         if((infoflags & HEIGHT) > 0 ) {
  180.              imgHeight = height;
  181.         }
  182.         if((infoflags & SOMEBITS) > 0 || (infoflags & ALLBITS) > 0  ) {
  183.              drawWidth = width>drawWidth?width:drawWidth;
  184.              drawHeight = height>drawHeight?height:drawHeight;
  185.         }
  186.         if(imgWidth > 0 && imgHeight > 0 ) {
  187.             if(imgWidth == drawWidth && imgHeight == drawHeight) {
  188.             bImageLoaded = true;
  189.                 getContainer().repaint();
  190.             }
  191.         }
  192.         return !bImageLoaded;
  193.     }
  194.     }
  195.  
  196.  
  197. }
  198.