package oracle.forms.fd;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.lang.reflect.Method;

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.LWPopupMenu;

import oracle.forms.ui.CustomEvent;

/**
 * A Bean to personalize the Forms components
 *
 * @author Francois Degrelle
 * @version 1.2
 */
   
public class Personalize extends VBean
{
  // variables
  private static final ID GetComponents     = ID.registerProperty("BEANINFO");    
  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 StatusMenuOption  = ID.registerProperty("STATUS_MENU_OPTION");  
  private static final ID VisibleMenuOption = ID.registerProperty("VISIBLE_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 static final ID setInitMenu       = ID.registerProperty("INITIAL_MENU_COLOR");  
  private static final ID setInitWindow     = ID.registerProperty("INITIAL_WINDOW_COLOR");  
  private static final ID setInitStatus     = ID.registerProperty("INITIAL_STATUS_COLOR");
  private IHandler     m_handler;  
  private Main         formsMain = null;
  private boolean      bLog  = false ;
  private int          iLevel=0 ;
  private HashMap      hMenuBar = new HashMap() ;
  private HashMap      hMenuOptions = new HashMap() ;
  oracle.ewt.lwAWT.lwMenu.LWMenuBar   pMenu = null ;
  oracle.ewt.lwAWT.lwMenu.LWPopupMenu popMenu = null ;
  oracle.ewt.lwAWT.lwMenu.LWMenu      newMenu = null ;

  cObj    TextField ;
  cObj    TextArea ;
  cObj    Button ;
  cObj    CheckBox ;
  cObj    RadioB ;
  cObj    ComboBox ;
  cObj    PopList ;
  cObj    TList ;
  cObj    Tree ;
  cObj    Window ;
  cObj    MenuBar ;
  cObj    Status ;
  cObj    MenuOption ;
  // initial values
  cObj    InitWindow ;
  cObj    InitMenuBar ;
  cObj    InitStatus ;
  cObj    InitMenuOption ;  
  
  
  public void init(IHandler handler)
  {
    m_handler = handler;
    super.init(handler);
    // getting the Forms Main class
    //formsMain  =  (Main) handler.getApplet();
    try{
       Method method = 
          handler.getClass().getMethod("getApplet", new Class[0]);
       Object applet = method.invoke(handler, new Object[0]);
       if (applet instanceof Main) {
          formsMain     =  (Main)applet;
       }      
     }catch(Exception ex) {;}    
  }  
  
  
  /*************************
   *  Set some properties  *
   ************************/
  public boolean setProperty(ID property, Object value)
  {

   // get list components
   // and set the colors
   if (property == GetComponents)
    {
        log("GetComponents");
        java.awt.Frame mFrame = formsMain.getFrame() ;
        Component cp[] = mFrame.getComponents();
        for (int i=0; i<cp.length;i++) 
        {
          getContainerContent((Container) cp[i]) ;
        }
        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("TextField")) TextField = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("TextArea")) TextArea = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("Button")) Button = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("CheckBox")) CheckBox = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("RadioB")) RadioB = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("ComboBox")) ComboBox = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("PopList")) PopList = new cObj(f,cFore,cBack);        
      else if(sType.equalsIgnoreCase("TList")) TList = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("Tree")) Tree = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("Window")) Window = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("MenuBar")) MenuBar = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("MenuOption")) MenuOption = new cObj(f,cFore,cBack);
      else if(sType.equalsIgnoreCase("Status")) Status = new cObj(f,cFore,cBack);
      return true ;
    }
    // add menu
     else if (property == AddMenu)
     {     
         String sLabel = value.toString() ;
         log("add new menu:"+sLabel);
         newMenu = new oracle.ewt.lwAWT.lwMenu.LWMenu() ;
         newMenu.setLabel(sLabel);
         newMenu.setFont(MenuBar.fFont);
         newMenu.setBackground(MenuBar.cBack);
         newMenu.setForeground(MenuBar.cFore);
         // accelerator to review
         KeyStroke ks = new KeyStroke('m',1);
         newMenu.setAccelerator(ks);

         pMenu.add(newMenu);
         popMenu = new oracle.ewt.lwAWT.lwMenu.LWPopupMenu() ;     
         cPopMenu cpm = new cPopMenu(sLabel,popMenu);
         newMenu.setSubMenu(popMenu);
         hMenuBar.put(sLabel,cpm);
         return true ;
     }

     // add menu option
     else if (property == AddMenuOption)
     {     
       log(property+":"+value);
       String sMenuParent = "" ;
       String sMenu       = "" ;
       String sLabel      = "" ;
       String sCommand    = "" ;
       String sToolTip    = "" ;
       String s = value.toString() ;
       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.indexOf(".");
       if(iPos == -1) return false ;
       sMenuParent = sMenu.substring(0,iPos);
       log("menu parent="+sMenuParent);
       if(sLabel.equalsIgnoreCase("SEPARATOR"))
       {
         oracle.ewt.lwAWT.lwMenu.LWMenuSeparator sep = new oracle.ewt.lwAWT.lwMenu.LWMenuSeparator();
         popMenu.add(sep) ;
       }
       else
       {
       if(st.hasMoreTokens()) sCommand = st.nextToken(); else return false ;     
       if(st.hasMoreTokens()) sToolTip = st.nextToken();
       oracle.ewt.lwAWT.lwMenu.LWMenuItem mi = new oracle.ewt.lwAWT.lwMenu.LWMenuItem();
       cPopMenu cpm = (cPopMenu)hMenuBar.get(sMenuParent);
       if(cpm == null) return false ;
       cOptMenu com = new cOptMenu(sLabel,sCommand,sToolTip,cpm.popMenu,mi) ;
       if(com == null) log("com=NULL");
       else hMenuOptions.put(sMenu,com) ;
       mi.setLabel(sLabel);
       mi.setFont(MenuOption.fFont);
       mi.setBackground(MenuOption.cBack);
       mi.setForeground(MenuOption.cFore);
       mi.setActionCommand(sCommand);
       if(sToolTip != null) 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();
              log(item.getActionCommand());
              SendMessage(item.getActionCommand());
            }        
       };
       mi.addMouseListener(ml);
       popMenu.add(mi) ;
       }
       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 ;
      }

    // set initial menu colors
     else if (property == setInitMenu)
     { 
         if(MenuBar != null)
         {
           MenuBar.fFont = InitMenuBar.fFont ;
           MenuBar.cFore = InitMenuBar.cFore ;
           MenuBar.cBack = InitMenuBar.cBack ;
           MenuOption.fFont = InitMenuOption.fFont ;
           MenuOption.cFore = InitMenuOption.cFore ;
           MenuOption.cBack = InitMenuOption.cBack ;         
           setProperty(GetComponents,null);
         }
         return true ;
     }
     
      // set initial window colors
       else if (property == setInitWindow)
       { 
           if(Window != null)
           {
             Window.fFont = InitWindow.fFont ;
             Window.cFore = Color.white ;
             Window.cBack = InitWindow.cBack ;
             setProperty(GetComponents,null);
           }
           return true ;
       }
       
      // set initial status colors
       else if (property == setInitStatus)
       { 
           if(Status != null)
           {
             Status.fFont = InitStatus.fFont ;
             Status.cFore = InitStatus.cFore ;
             Status.cBack = InitStatus.cBack ;
             setProperty(GetComponents,null);
           }
           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());
        // text area
        if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.VTextArea")) 
        {
            oracle.forms.ui.VTextArea ta = (oracle.forms.ui.VTextArea)cp[i] ;
            if(TextArea != null)
            {
              if(TextArea.fFont != null) ta.setFont(TextArea.fFont);
              if(TextArea.cBack != null) ta.setBackground(TextArea.cBack);
              if(TextArea.cFore != null) ta.setForeground(TextArea.cFore);
            }
        }
        // text field
        else if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.VTextField")) 
        {
            oracle.forms.ui.VTextField ta = (oracle.forms.ui.VTextField)cp[i] ;
            if(TextField != null)
            {
              if(TextField.fFont != null) ta.setFont(TextField.fFont);
              if(TextField.cBack != null) ta.setBackground(TextField.cBack);
              if(TextField.cFore != null) ta.setForeground(TextField.cFore);
            }
            log("* align="+ta.getAlignment());
            // text alignment : 0= left  2=right 3=center  
            //ta.setAlignment(3);
        } 
        // Button
        else if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.VButton")) 
        {
            oracle.forms.ui.VButton ta = (oracle.forms.ui.VButton)cp[i] ;
            if(Button != null)
            {
              if(Button.fFont != null) ta.setFont(Button.fFont);
              if(Button.cBack != null) ta.setBackground(Button.cBack);
              if(Button.cFore != null) ta.setForeground(Button.cFore);
            }
        }
        // CheckBox
        else if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.VCheckbox")) 
        {
            oracle.forms.ui.VCheckbox ta = (oracle.forms.ui.VCheckbox)cp[i] ;
            if(CheckBox != null)
            {
              if(CheckBox.fFont != null) ta.setFont(CheckBox.fFont);
              if(CheckBox.cBack != null) ta.setBackground(CheckBox.cBack);
              if(CheckBox.cFore != null) ta.setForeground(CheckBox.cFore);
            }
        }      
        // Radio button
        else if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.VRadioButton")) 
        {
            oracle.forms.ui.VRadioButton ta = (oracle.forms.ui.VRadioButton)cp[i] ;
            if(Button != null)
            {
              if(Button.fFont != null) ta.setFont(Button.fFont);
              if(Button.cBack != null) ta.setBackground(Button.cBack);
              if(Button.cFore != null) ta.setForeground(Button.cFore);
            }
        } 
        // ComboBox
        else if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.VComboBox")) 
        {
            oracle.forms.ui.VComboBox ta = (oracle.forms.ui.VComboBox)cp[i] ;
            if(ComboBox != null)
            {
              if(ComboBox.fFont != null) ta.setFont(ComboBox.fFont);
              if(ComboBox.cBack != null) ta.setBackground(ComboBox.cBack);
              if(ComboBox.cFore != null) ta.setForeground(ComboBox.cFore);
            }
        }    
        // PopList
        else if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.VPopList")) 
        {
            oracle.forms.ui.VPopList ta = (oracle.forms.ui.VPopList)cp[i] ;
            if(PopList != null)
            {
              if(PopList.fFont != null) ta.setFont(PopList.fFont);
              if(PopList.cBack != null) ta.setBackground(PopList.cBack);
              if(PopList.cFore != null) ta.setForeground(PopList.cFore);
            }
        }            
        // TList
        else if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.VTList")) 
        {
            oracle.forms.ui.VTList ta = (oracle.forms.ui.VTList)cp[i] ;
            if(TList != null)
            {
              if(TList.fFont != null) ta.setFont(TList.fFont);
              if(TList.cBack != null) ta.setBackground(TList.cBack);
              if(TList.cFore != null) ta.setForeground(TList.cFore);
            }
        }        
        // Tree
        else if( cp[i].getClass().toString().equalsIgnoreCase("class oracle.ewt.dTree.DTree")) 
        {
            oracle.ewt.dTree.DTree ta = (oracle.ewt.dTree.DTree)cp[i] ;
            if(Tree != null)
            {
              if(Tree.fFont != null) ta.setFont(Tree.fFont);
              if(Tree.cBack != null) ta.setBackground(Tree.cBack);
              if(Tree.cFore != null) ta.setForeground(Tree.cFore);
            }
        }         
        // Window caption     
        else if(cp[i].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.lwWindow.laf.TitleBar")) {
             oracle.ewt.lwAWT.lwWindow.laf.TitleBar ta = (oracle.ewt.lwAWT.lwWindow.laf.TitleBar)cp[i] ;
             if(InitWindow == null) InitWindow= new cObj(ta.getFont(),ta.getForeground(),ta.getBackground());
             if(Window != null) if(Window.fFont != null) ta.setFont(Window.fFont) ;
             // get the sub-components
             Component cpw[] = ta.getComponents() ;

             for(int z=0; z<cpw.length;z++) {
                 log("-- Win -> "+cpw[z].getClass().toString());
                 if(cpw[z].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.LWComponent")) {
                     oracle.ewt.lwAWT.LWComponent lwc = (oracle.ewt.lwAWT.LWComponent)cpw[z] ;
                     Component cpw2[] = lwc.getComponents() ;
                     // get the title label
                     for(int x=0; x<cpw2.length;x++) {
                         log("---- WinC -> "+cpw2[x].getClass().toString());
                         if(cpw2[x].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.LWLabel")) {
                              oracle.ewt.lwAWT.LWLabel lb = (oracle.ewt.lwAWT.LWLabel)cpw2[x] ;
                              if(Window != null)
                              {
                                if(Window.fFont != null) lb.setFont(Window.fFont);
                                if(Window.cBack != null) lb.setBackground(Window.cBack);
                                if(Window.cFore != null) lb.setForeground(Window.cFore);                              
                              }
                              break ;
                        }                         
                     }
                     if(Window != null) if(Window.cBack != null) lwc.setBackground(Window.cBack);
                 }
                 else if(cpw[z].getClass().toString().equalsIgnoreCase("class oracle.ewt.toolBar.ToolBar")) {
                     oracle.ewt.toolBar.ToolBar tb = (oracle.ewt.toolBar.ToolBar)cpw[z] ;
                     if(Window != null) if(Window.cBack != null) tb.setBackground(Window.cBack);
                 }                 
                 else if(cpw[z].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.lwWindow.laf.TitleBar")) {
                     oracle.ewt.lwAWT.lwWindow.laf.TitleBar tb = (oracle.ewt.lwAWT.lwWindow.laf.TitleBar)cpw[z] ;
                     if(Window != null) if(Window.cBack != null) tb.setBackground(Window.cBack);
                 }                 
             }

         }  

    // menu bar
    else if(cp[i].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.lwMenu.LWMenuBar")) {
         oracle.ewt.lwAWT.lwMenu.LWMenuBar ta = (oracle.ewt.lwAWT.lwMenu.LWMenuBar)cp[i] ;
         if(InitMenuBar == null) 
         {
            InitMenuBar= new cObj(ta.getFont(),ta.getForeground(),ta.getBackground());
            InitMenuOption= new cObj(ta.getFont(),ta.getForeground(),ta.getBackground());
         }
         if(MenuBar != null)
         {
           if(MenuBar.fFont != null) ta.setFont(MenuBar.fFont);
           if(MenuBar.cBack != null) ta.setBackground(MenuBar.cBack);
           if(MenuBar.cFore != null) ta.setForeground(MenuBar.cFore); 
         }
         pMenu = ta ;
     }             
    // popup menu
    else if(cp[i].getClass().toString().equalsIgnoreCase("class oracle.ewt.lwAWT.lwMenu.LWMenu")) {
         oracle.ewt.lwAWT.lwMenu.LWMenu ta = (oracle.ewt.lwAWT.lwMenu.LWMenu)cp[i] ;
         
         scruteMenuBar(ta, 0);
    }                     
    //
    // Status bar
    //
    else if(cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.FormStatusArea")) {
        oracle.forms.ui.FormStatusArea ta = (oracle.forms.ui.FormStatusArea)cp[i] ;
        if(InitStatus == null) InitStatus= new cObj(ta.getFont(),ta.getForeground(),ta.getBackground());
        if(Status != null)
        {
          if(Status.fFont != null) ta.setFont(Status.fFont);
          if(Status.cBack != null) ta.setBackground(Status.cBack);
          if(Status.cFore != null) ta.setForeground(Status.cFore);
        }
    } 
    else if(cp[i].getClass().toString().equalsIgnoreCase("class oracle.ewt.statusBar.StatusBar")) {
        oracle.ewt.statusBar.StatusBar ta = (oracle.ewt.statusBar.StatusBar)cp[i] ;
        /*
        ta.addPropertyChangeListener(
                new PropertyChangeListener() 
		{
		  public void propertyChange(PropertyChangeEvent e) 
		    {
		      System.out.println(" change property="+e.getNewValue());
		    }
		});
        */
        if(Status != null)
        {
          if(Status.fFont != null) ta.setFont(Status.fFont);
          if(Status.cBack != null) ta.setBackground(Status.cBack);
          if(Status.cFore != null) ta.setForeground(Status.cFore);
        }
    } 
    else if(cp[i].getClass().toString().equalsIgnoreCase("class oracle.forms.ui.StatsIndicator")) {
        oracle.forms.ui.StatsIndicator ta = (oracle.forms.ui.StatsIndicator)cp[i] ;
        if(Status != null)
        {
          if(Status.fFont != null) ta.setFont(Status.fFont);
          if(Status.cBack != null) ta.setBackground(Status.cBack);
          if(Status.cFore != null) ta.setForeground(Status.cFore);
        }
    }     
    try{
        getContainerContent((Container)cp[i]);
    }
    catch (Exception e) {}
    }
  }
  
  
  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 = "" ; ;
        oracle.ewt.lwAWT.lwMenu.LWPopupMenu popMenu = null ;
        cPopMenu(String sPopName, oracle.ewt.lwAWT.lwMenu.LWPopupMenu popMenu) {
           this.sPopName =  sPopName ;
           this.popMenu  =  popMenu ;
        }
    }    
    
    class cOptMenu extends Object 
    {
        String    sLabel   = null ;
        String    sCmd     = null ;
        String    sToolTip = null ;
        oracle.ewt.lwAWT.lwMenu.LWMenuItem mi = null ;
        oracle.ewt.lwAWT.lwMenu.LWPopupMenu popMenu = null ;        
        cOptMenu(
                  String sLabel, 
                  String sCmd, 
                  String sTooltTip,
                  oracle.ewt.lwAWT.lwMenu.LWPopupMenu popMenu,
                  oracle.ewt.lwAWT.lwMenu.LWMenuItem mi) {
           this.sLabel   =  sLabel ;
           this.sCmd     =  sCmd ;
           this.sToolTip =  sCmd ;
           this.popMenu  =  popMenu ;
           this.mi       =  mi ;
        }
    }
    

     /*--------------------------------------*
      *  method to scrute the existing menu  *
      *--------------------------------------*/
      protected void scruteMenuBar(Component component, int iter)
      {    
        String sLevel="" ;
        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().toLowerCase());
        }    
        if (component instanceof Container)
            { 
            Component components[] = ((Container)component).getComponents(); 
            for (int i = 0; i < components.length; i++)
                { if (components[i] != null)
                     { scruteMenuBar(components[i],iter);}
                }
             }
      }

      protected void scruteMenu(Component component, int iter, String sParent)
      {    
        String sLevel    = "" ;
        String sName     = "" ;
        String sLabel    = "" ;
        String sLabelOpt = "" ;
        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 ta = (oracle.ewt.lwAWT.lwMenu.LWMenu)component;
            sLabel = ta.getLabel().toLowerCase() ;
            LWPopupMenu pop = ta.getSubMenu() ;

            if( ! sParent.equalsIgnoreCase(sLabel)) sName = sParent ;
            else sName = sLabel ;
    
            cPopMenu cpm = new cPopMenu(ta.getLabel(), pop);
            sName = sParent ;
            if(MenuBar != null)
             {
               if(MenuBar.fFont != null) ta.setFont(MenuBar.fFont);
               if(MenuBar.cBack != null) ta.setBackground(MenuBar.cBack);
               if(MenuBar.cFore != null) ta.setForeground(MenuBar.cFore);
             }      
             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];
                   sLabelOpt = mi.getLabel().toLowerCase() ;                  
                   cOptMenu com = new cOptMenu(mi.getLabel(),mi.getActionCommand(),(String)mi.getToolTipValue(),cpm.popMenu,mi) ;
                   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);           
                   }             
                 }
                 else
                 {
                   // menu
                   oracle.ewt.lwAWT.lwMenu.LWMenu mi = (oracle.ewt.lwAWT.lwMenu.LWMenu)c[j];
                   sName += "."+mi.getLabel().toLowerCase() ;
                   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();
      }    
    }
  
  private void log(String sMsg)    
  {
    if(bLog) System.out.println(sMsg);      
  }

}


