package oracle.forms.fd;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import java.util.HashMap;
import java.util.StringTokenizer;
import oracle.ewt.event.KeyStroke;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.VBean;
import oracle.forms.properties.ID;
import oracle.forms.engine.Main;
import oracle.ewt.lwAWT.lwMenu.*;
import oracle.forms.ui.CustomEvent;

/**
 * A Bean to personalize the Forms menu
 *
 * @author Francois Degrelle
 * @version 1.1
 */
   
public class DynamicMenu extends VBean
{
  // variables
  private static final ID GetMenu           = ID.registerProperty("GET_MENU");    
  private static final ID SetCompProp       = ID.registerProperty("SET_PROPERTIES");        
  private static final ID SetLog            = ID.registerProperty("SET_LOG");
  private static final ID AddMenu           = ID.registerProperty("ADD_MENU");  
  private static final ID AddMenuOption     = ID.registerProperty("ADD_MENU_OPTION");  
  private static final ID StatusMenu        = ID.registerProperty("STATUS_MENU");
  private static final ID StatusMenuOption  = ID.registerProperty("STATUS_MENU_OPTION");  
  private static final ID VisibleMenu       = ID.registerProperty("VISIBLE_MENU");
  private static final ID VisibleMenuOption = ID.registerProperty("VISIBLE_MENU_OPTION");  
  private static final ID MoveWinMenuOption = ID.registerProperty("MOVE_WIN_MENU_OPTION");  
  private static final ID DelMenuOption     = ID.registerProperty("REMOVE_MENU_OPTION");  
  private static final ID MENUMSG           = ID.registerProperty("MENU_MSG"); 
  private static final ID MENUOPTION        = ID.registerProperty("MENU_OPTION");   
  private IHandler     m_handler;  
  private Main         formsMain    = null;
  private boolean      bLog         = false ;
  private boolean      bFirst       = true ;
  private int          iLevel       = 0 ;
  private HashMap      hMenuBar     = new HashMap() ;
  private HashMap      hMenuOptions = new HashMap() ;
  private URL          m_codeBase;
  LWMenuBar            pMain    = null ;
  LWMenu               pWinMenu = null ;
  LWMenuBar            pMenu    = null ;
  LWPopupMenu          popMenu  = null ;
  LWMenu               newMenu  = null ;

  cObj    MenuBar ;
  cObj    MenuOption ;
  
  
  public void init(IHandler handler)
  {
    m_handler = handler;
    super.init(handler);
    // getting the Forms Main class
    formsMain  =  (Main) handler.getApplet();
  }  
  /*
  public void paint(Graphics g)
  {
    if(bFirst)
    {
       bFirst = false ;
       setProperty(GetMenu,null);
    }
    super.paint(g);
  }
  */
  /*************************
   *  Set some properties  *
   ************************/
  public boolean setProperty(ID property, Object value)
  {

   log(property+":"+value);
   // get list components
   // and set the colors
   if (property == GetMenu)
    {
        log("GetComponents");
        java.awt.Frame mFrame = formsMain.getFrame() ;
        Component cp[] = mFrame.getComponents(); 
        iLevel = 0 ;
        for (int i=0; i<cp.length;i++) 
        {
          Container c = (Container)cp[i] ;
          System.out.println("Frame container: "+c.getClass());
          //if(c.getClass().getName().equalsIgnoreCase("oracle.forms.ui.mdi.MDIContainer"))
          //{
            getContainerContent(c) ;
          //}  
        }
        return true ;
    }
    else if (property == SetCompProp)
    { 
      String s = value.toString() ;
      String sType = "" ;
      String sFontName = null ;
      String sFontWeight = null ;
      String sFontSize = null ;
      String sFore = null ;
      String sBack = null ;
      int    iW = Font.PLAIN ;
      int    iSize=0 ;
      Font   f=null ;
      Color  cFore=null, cBack=null ;
      StringTokenizer st = new StringTokenizer(s, ",");
      if(st.hasMoreTokens()) sType = st.nextToken();
      if(st.hasMoreTokens()) sFontName = st.nextToken();
      if(st.hasMoreTokens()) sFontWeight = st.nextToken();
      if(st.hasMoreTokens()) iSize = Integer.parseInt(st.nextToken());
      if(st.hasMoreTokens()) sFore = st.nextToken();
      if(st.hasMoreTokens()) sBack = st.nextToken();
      if(sFontWeight.equalsIgnoreCase("P")) iW = Font.PLAIN ;
      else if(sFontWeight.equalsIgnoreCase("B")) iW = Font.BOLD ;
      else if(sFontWeight.equalsIgnoreCase("I")) iW = Font.ITALIC ;
      else if(sFontWeight.equalsIgnoreCase("BI")) iW = Font.BOLD + Font.ITALIC ;
      if(sFontName != null)f = new Font(sFontName,iW,iSize);
      if(sFore != null)cFore = makeColor(sFore);
      if(sBack != null)cBack = makeColor(sBack);
      if(sType.equalsIgnoreCase("MenuBar")) MenuBar = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("MenuOption")) MenuOption = new cObj(f,cFore,cBack);
      //setProperty(GetMenu,null);
      return true ;
    }
    // add menu
     else if (property == AddMenu)
     {     
         String sLabel  = "" ;
         String sName   = "" ;
         String sParent = "" ;
         StringTokenizer st = new StringTokenizer(value.toString(), ",");
         if(st.hasMoreTokens()) sName    = st.nextToken(); else return false ;
         if(st.hasMoreTokens()) sLabel   = st.nextToken(); else return false ;         
         // add Main to menu path if not provided
         int iPos = sName.lastIndexOf(".");
         if(iPos > -1) sParent = sName.substring(0,iPos);
         /*
         if(iPos < 0) { sParent = "Main" ; sName = sParent + sName ; }
         else sParent = sName.substring(0,iPos);
         */
         log("add new menu - sName:"+sName+"  sParent:"+sParent);
         newMenu = new oracle.ewt.lwAWT.lwMenu.LWMenu() ;
         newMenu.setName(sName);
         newMenu.setLabel(sLabel);
         if(MenuBar != null)
         {
           if(MenuBar.fFont != null) newMenu.setFont(MenuBar.fFont);
           if(MenuBar.cBack != null) newMenu.setBackground(MenuBar.cBack);
           if(MenuBar.cFore != null) newMenu.setForeground(MenuBar.cFore);
         }
         // accelerator to review
         KeyStroke ks = new KeyStroke('m',1);
         newMenu.setAccelerator(ks);
         LWPopupMenu parentMenu ;
         if(sParent.equalsIgnoreCase("")) pMenu.add(newMenu);
         else 
         {
            cPopMenu cpm = (cPopMenu)hMenuBar.get(sParent);
            if(cpm == null) return false ;
            parentMenu = cpm.popMenu;
            parentMenu.add(newMenu);
         }
         popMenu = new oracle.ewt.lwAWT.lwMenu.LWPopupMenu() ;     
         cPopMenu cpm = new cPopMenu(sName,newMenu,popMenu);
         newMenu.setSubMenu(popMenu);
         hMenuBar.put(sName,cpm);
         return true ;
     }

     // add menu option
     else if (property == AddMenuOption)
     {     
       log(property+":"+value);
       String sMenuParent = "" ;
       String sMenu       = "" ;
       String sLabel      = "" ;
       String sCommand    = "" ;
       String sToolTip    = null ;
       String sImage      = "" ;
       String sAccel      = null ;
       String s = value.toString() ;
       Image  img = null ;
       int iPos = 0 ;
       StringTokenizer st = new StringTokenizer(s, ",");
       if(st.hasMoreTokens()) sMenu    = st.nextToken(); else return false ;
       if(st.hasMoreTokens()) sLabel   = st.nextToken(); else return false ;
       iPos = sMenu.lastIndexOf(".");
       if(iPos == -1) return false ;
       sMenuParent = sMenu.substring(0,iPos);
       log("menu parent="+sMenuParent);
       cPopMenu cpm = (cPopMenu)hMenuBar.get(sMenuParent);
       if(cpm == null) return false ;
       if(sLabel.equalsIgnoreCase("SEPARATOR"))
       {
         oracle.ewt.lwAWT.lwMenu.LWMenuSeparator sep = new oracle.ewt.lwAWT.lwMenu.LWMenuSeparator();
         cpm.popMenu.add(sep) ;
       }
       else
       {
       // command
       if(st.hasMoreTokens()) sCommand = st.nextToken(); else return false ;     
       /*--
       // accelerator
       if(st.hasMoreTokens()) sAccel = st.nextToken();
       --*/
       // tooltip
       if(st.hasMoreTokens()) sToolTip = st.nextToken();
       // image icon
       if(st.hasMoreTokens())
       {
         sImage  = st.nextToken();       
         img     = loadImage( sImage ) ;
       }
       oracle.ewt.lwAWT.lwMenu.LWMenuItem mi = new oracle.ewt.lwAWT.lwMenu.LWMenuItem();
       cOptMenu com = new cOptMenu(sLabel,sCommand,sToolTip,cpm.popMenu,mi,null) ;
       if(com == null) log("com=NULL");
       else hMenuOptions.put(sMenu,com) ;
       // accelerator in label ?
       iPos = sLabel.indexOf("&") ;
       if(iPos> -1)
       {
         String sLetter = sLabel.substring(iPos+1,iPos+2) ;
         sLabel = sLabel.substring(0,iPos) + sLabel.substring(iPos+1,sLabel.length());
         mi.setMnemonicChar(sLetter.charAt(0));
       }
       /*--
       if(sAccel != null && !sAccel.equals("-"))
       {
         KeyStroke ks = new KeyStroke(sAccel.charAt(0),1);
         mi.setAccelerator(ks);
       }   
       --*/
       mi.setLabel(sLabel);
       if(MenuOption != null)
       {
         if(MenuOption.fFont != null) mi.setFont(MenuOption.fFont);
         if(MenuOption.cBack != null) mi.setBackground(MenuOption.cBack);
         if(MenuOption.cFore != null) mi.setForeground(MenuOption.cFore);
       }
       mi.setActionCommand(sCommand);
       if(img != null) mi.setImage(img);
       if(sToolTip != null && !sToolTip.equalsIgnoreCase("-")) mi.setToolTipValue(sToolTip);
       MouseListener ml = new MouseListener(){
            public void mouseClicked(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {
              oracle.ewt.lwAWT.lwMenu.LWMenuItem item = (oracle.ewt.lwAWT.lwMenu.LWMenuItem)e.getSource();
              if(item.isEnabled()) SendMessage(item.getActionCommand());
            }        
       };
       mi.addMouseListener(ml);
       cpm.popMenu.add(mi) ;
       }
       return true ;
     }

     // set menu status
     else if (property == StatusMenu)
     {     
       log(property+":"+value);
       String sMenu = "" ;
       String sOption = "" ;
       String s = value.toString() ;
       StringTokenizer st = new StringTokenizer(s, ",");
       if(st.hasMoreTokens()) sMenu   = st.nextToken(); else return false ;
       if(st.hasMoreTokens()) sOption = st.nextToken(); else return false ;
       cPopMenu cpm = (cPopMenu)hMenuBar.get(sMenu);
       if(cpm == null ) return false ;
       if(sOption.equalsIgnoreCase("true")) cpm.lMenu.setEnabled(true);
       else cpm.lMenu.setEnabled(false);
       return true ;
     }


     // set menu status
     else if (property == VisibleMenu)
     {     
         log(property+":"+value);
         String sMenu = "" ;
         String sOption = "" ;
         String s = value.toString() ;
         StringTokenizer st = new StringTokenizer(s, ",");
         if(st.hasMoreTokens()) sMenu   = st.nextToken(); else return false ;
         if(st.hasMoreTokens()) sOption = st.nextToken(); else return false ;
         cPopMenu cpm = (cPopMenu)hMenuBar.get(sMenu);
         if(cpm == null ) return false ;
         if(sOption.equalsIgnoreCase("true")) cpm.lMenu.setVisible(true);
         else cpm.lMenu.setVisible(false);
         return true ;
     }

     // set menu option status
     else if (property == StatusMenuOption)
     {     
         log(property+":"+value);
         String sMenu = "" ;
         String sOption = "" ;
         String s = value.toString() ;
         StringTokenizer st = new StringTokenizer(s, ",");
         if(st.hasMoreTokens()) sMenu   = st.nextToken(); else return false ;
         if(st.hasMoreTokens()) sOption = st.nextToken(); else return false ;
         cOptMenu com = (cOptMenu)hMenuOptions.get(sMenu) ;
         if(com == null ) return false ;
         if(sOption.equalsIgnoreCase("true")) com.mi.setEnabled(true);
         else com.mi.setEnabled(false);
         return true ;
     }


     // set menu option display status
     else if (property == VisibleMenuOption)
     {     
       log(property+":"+value);
       String sMenu = "" ;
       String sOption = "" ;
       String s = value.toString() ;
       StringTokenizer st = new StringTokenizer(s, ",");
       if(st.hasMoreTokens()) sMenu   = st.nextToken(); else return false ;
       if(st.hasMoreTokens()) sOption = st.nextToken(); else return false ;
       cOptMenu com = (cOptMenu)hMenuOptions.get(sMenu) ;
       if(com == null ) return false ;
       if(sOption.equalsIgnoreCase("true")) com.mi.setVisible(true);
       else com.mi.setVisible(false);
       return true ;
     }

      // remove menu option
      else if (property == DelMenuOption)
      {     
          log(property+":"+value);
          String sMenu = value.toString() ;
          cOptMenu com = (cOptMenu)hMenuOptions.get(sMenu) ;
          if(com == null ) return false ;
          com.popMenu.remove(com.mi);
          hMenuOptions.remove(sMenu);
          log("* menu removed *");
          return true ;
      }
    // remove menu option
    else if (property == MoveWinMenuOption)
    {     
        pMain.remove(pWinMenu);
        pMain.add(pWinMenu);
        return true ;
    }    
    
    // set/unset the debug messages
    else if (property == SetLog)
    { 
        String s = value.toString() ;    
        if(s.equalsIgnoreCase("true")) bLog = true ;
        else bLog = false ;
        return true ;
    }
    else
    {
     return super.setProperty(property, value);
    }
  }  

  /************************
   *  Get the properties  *
   ***********************/
   public Object getProperty(ID pId)
   {
     return super.getProperty(pId);
   } 

  
  /*
   *  recursive method to loop through the components
   */
  private void getContainerContent(Container ct)
  { 
    iLevel++ ;
    String sLevel="" ;
    for(int j=0 ; j<=iLevel; j++) sLevel += "--" ;
    Component cp[] = ct.getComponents() ;
    if(cp.length == 0) { iLevel=0; return ; }
    for(int i=0; i<cp.length; i++) 
    {
        log(sLevel+":"+cp[i].getClass());

        // menu bar
        if(cp[i].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.lwMenu.LWMenuBar")) {
             oracle.ewt.lwAWT.lwMenu.LWMenuBar menubar = (oracle.ewt.lwAWT.lwMenu.LWMenuBar)cp[i] ;
             pMain = menubar ;
             if(MenuBar != null)
             {
               if(MenuBar.fFont != null) menubar.setFont(MenuBar.fFont);
               if(MenuBar.cBack != null) menubar.setBackground(MenuBar.cBack);
               if(MenuBar.cFore != null) menubar.setForeground(MenuBar.cFore); 
             }
             else
             {
               log("*** MenuBar NULL ***");
             }
             pMenu = menubar ;
         }             
        // popup menu
        else if(cp[i].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.lwMenu.LWMenu")) {
           oracle.ewt.lwAWT.lwMenu.LWMenu menu = (oracle.ewt.lwAWT.lwMenu.LWMenu)cp[i] ;
           scrutenizer(menu, 0);
           pWinMenu = menu ;
       }                     
      getContainerContent((Container)cp[i]);
    }
  }
  
  
  class cObj extends Object 
  {
      Font      fFont = null ;
      Color     cFore = null ;
      Color     cBack = null ;
      cObj(Font fFont, Color cFore, Color cBack) {
         this.fFont =  fFont ;
         this.cFore =  cFore ;
         this.cBack =  cBack ;
      }
  }

       
    class cPopMenu extends Object 
    {
        String      sPopName = "" ; ;
        LWMenu      lMenu = null ;
        LWPopupMenu popMenu = null ;
        cPopMenu(
                  String sPopName, 
                  LWMenu lMenu,
                  LWPopupMenu popMenu) {
           this.sPopName =  sPopName ;
           this.lMenu    =  lMenu ;
           this.popMenu  =  popMenu ;
        }
    }    
    
    class cOptMenu extends Object 
    {
        String      sLabel   = null ;
        String      sCmd     = null ;
        String      sToolTip = null ;
        LWMenuItem  mi = null ;
        LWPopupMenu popMenu = null ;
        cPopMenu    popup   = null ;
        cOptMenu(
                  String sLabel, 
                  String sCmd, 
                  String sToolTip,
                  LWPopupMenu popMenu,
                  LWMenuItem mi,
                  cPopMenu pop) {
           this.sLabel   =  sLabel ;
           this.sCmd     =  sCmd ;
           this.popMenu  =  popMenu ;
           this.popup    =  pop ;
           this.mi       =  mi ;
           if(sToolTip != null && !sToolTip.equalsIgnoreCase("-")) this.sToolTip = sToolTip ;           
        }
    }

  protected void scrutenizer(Component component, int iter)
  {    
    String sLevel="" ;
    String sClassName = component.getClass().toString() ;
    for(int j=0 ; j<=iter*2; j++) sLevel += "--" ;
    if(component instanceof oracle.ewt.lwAWT.lwMenu.LWMenu)
    {
        oracle.ewt.lwAWT.lwMenu.LWMenu lwm = (oracle.ewt.lwAWT.lwMenu.LWMenu)component;
        scruteMenu(component,iter,lwm.getLabel());
    }    
    if (component instanceof Container)
        { 
        Component components[] = ((Container)component).getComponents(); 
        for (int i = 0; i < components.length; i++)
            { if (components[i] != null)
                 { scrutenizer(components[i],iter);}
            }
         }
  }

  protected void scruteMenu(Component component, int iter, String sParent)
  {    
    String sLevel="" ;
    String sName = "" ;
    String sClassName = component.getClass().toString() ;
    for(int j=0 ; j<=iter*2; j++) sLevel += "--" ;
    if(component instanceof oracle.ewt.lwAWT.lwMenu.LWMenu)
    {
        oracle.ewt.lwAWT.lwMenu.LWMenu lwm = (oracle.ewt.lwAWT.lwMenu.LWMenu)component;
        log("[--  LWMenu  -------------------------------------------");
        log(iter+" "+sLevel+":"+lwm.getName()+" ->"+lwm.getLabel());
        log("-------------------------------------------------------]");
        sName = sParent ;
        pWinMenu = lwm ;
         if(MenuBar != null)
         {
           if(MenuBar.fFont != null) lwm.setFont(MenuBar.fFont);
           if(MenuBar.cBack != null) lwm.setBackground(MenuBar.cBack);
           if(MenuBar.cFore != null) lwm.setForeground(MenuBar.cFore);
         }      
         else log("*** MenuBar NULL ***");
         LWPopupMenu pop = lwm.getSubMenu() ;
         //Container ctn   = pop.getContent();
         Component c[]   = pop.getContent().getComponents() ;
         Component c2[]  = pop.getComponents() ;
         for (int j=0;j<c.length;j++)
         {
           if(   ! (c[j] instanceof oracle.ewt.lwAWT.lwMenu.LWMenuSeparator)
              && ! (c[j] instanceof oracle.forms.ui.mdi.MDIWindowMenu)
             )
           {
             if(c[j].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.lwMenu.LWMenuItem")) //
             {
               // menu item option
               oracle.ewt.lwAWT.lwMenu.LWMenuItem mi = (oracle.ewt.lwAWT.lwMenu.LWMenuItem)c[j];
               log(" [-- LWMenuItem ----------------------------------");
               log(iter+" ("+mi.getLabel()+")  parent -->"+sParent);
               log(iter+" "+sLevel+"label:["+mi.getLabel()+"]  ("+sName+"."+mi.getLabel()+")");
               log(" --------------------------------------------------]");
               if(MenuOption != null)
               {
                 if(MenuOption.fFont != null) mi.setFont(MenuOption.fFont);
                 if(MenuOption.cFore != null) mi.setForeground(MenuOption.cFore);           
                 if(MenuOption.cBack != null) mi.setBackground(MenuOption.cBack);           
               }             
               //scruteMenu(mi,iter+1,sName);
             }
             else
             {
               // menu
               oracle.ewt.lwAWT.lwMenu.LWMenu mi = (oracle.ewt.lwAWT.lwMenu.LWMenu)c[j];
               sName += "."+mi.getLabel() ;
               scruteMenu(mi,iter+1,sName);               
             }
           }
         } 
         // menu option background
         for (int k=0;k<c2.length;k++)
         {
            oracle.ewt.lwAWT.LWComponent mi2 = (oracle.ewt.lwAWT.LWComponent)c2[k];
            if(MenuOption != null) if(MenuOption.cBack != null) mi2.setBackground(MenuOption.cBack);
         }          
    }    
  }
    
  /*----------------------------------------*
   *  Make a color from a RGB string
   *  Color c = makeColor( "r128g25b100" ); 
   *----------------------------------------*/
  Color makeColor(String s)
    {
      int r=-1, g=-1, b=-1 ;
      String sR = "255", sG = "255", sB = "255" ;
      int iR=255, iG=255, iB=255 ;
      Color c = Color.white ;
      r = s.indexOf("r") ;
      g = s.indexOf("g") ;
      b = s.indexOf("b") ;
      if( r>-1 && g>-1 && b>-1 )
      {
        iR = Integer.parseInt(s.substring(r+1,g)) ;
        iG = Integer.parseInt(s.substring(g+1,b)) ;
        iB = Integer.parseInt(s.substring(b+1)) ;
        try
        {
          c = new Color(iR,iG,iB) ;
        }
        catch( Exception e) {} ;
      }
      return c ;
    }
    
    // send message back to Forms
    private void SendMessage(String sMenuCommand) 
    {
      try{
        CustomEvent ce = new CustomEvent(m_handler, MENUMSG);
        m_handler.setProperty( MENUOPTION, sMenuCommand );
        dispatchCustomEvent(ce);  
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }    
    }

    /*--------------------------------------------------------------*
     *  Load an image from JAR file, Client machine or Internet URL 
     *--------------------------------------------------------------*/
    private Image loadImage(String p_imageName)
    {
      URL imageURL = null;
      boolean loadSuccess = false;
      Image img = null ;
      String imageName = p_imageName ;
      imageName = p_imageName ;
      //JAR
      log("Searching JAR for " + imageName);
      imageURL = getClass().getResource(imageName);
      if (imageURL != null)
      {
        log("URL: " + imageURL.toString());
        try
        {
          img = Toolkit.getDefaultToolkit().getImage(imageURL);
          loadSuccess = true;
          log("Image found in JAR: " + imageURL.toString());
          return img ;
        }
        catch (Exception ilex)
        {
          log("Error loading image from JAR: " + ilex.toString());
        }
      }
      else
      {
        log("Unable to find " + imageName + " in JAR");
      }

      //DOCBASE
      if (loadSuccess == false)
      {
        imageName = "file:///" + p_imageName ;
        log("Searching docbase for " + p_imageName);
        try
        {
         if(p_imageName.toLowerCase().startsWith("http://")||p_imageName.toLowerCase().startsWith("https://"))
          {
            log("trying to read:"+p_imageName);
            imageURL = new URL(p_imageName);
          }
          else if(imageName.toLowerCase().startsWith("file:"))
          {
            imageURL = new URL(imageName);
          }
          else
          {
            imageURL = new URL(m_codeBase.getProtocol() + "://" +m_codeBase.getHost() + ":" + m_codeBase.getPort() + imageName);
          }
          log("Constructed URL: " + imageURL.toString());
          try
          {
            img = createImage((java.awt.image.ImageProducer)imageURL.getContent());
            loadSuccess = true;
            log("Image found in DOCBASE: " + imageURL.toString());
            return img ;
          }
          catch (Exception ilex)
          {
            log("Error reading image - " + ilex.toString());
          }
        }
        catch (java.net.MalformedURLException urlex)
        {
          log("Error creating URL - " + urlex.toString());
        }
      }

      //CODEBASE
      if (loadSuccess == false)
      {
        log("Searching codebase for " + imageName);
        try
        {
          imageURL = new URL(m_codeBase, imageName);
          log("Constructed URL: " + imageURL.toString());
          try
          {
            img = createImage((java.awt.image.ImageProducer)imageURL.getContent());
            loadSuccess = true;
            log("Image found in CODEBASE: " + imageURL.toString());
            return img ;
          }
          catch (Exception ilex)
          {
             log("Error reading image - " + ilex.toString());
          }
        }
        catch (java.net.MalformedURLException urlex)
        {
          log("Error creating URL - " + urlex.toString());
        }
      }

      if (loadSuccess == false)
      {
        log("Error image " + imageName + " could not be located");
      }
      return img ;
    }


  
  private void log(String sMsg)    
  {
    if(bLog) System.out.println(sMsg);      
  }

}
