package oracle.forms.fd;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import oracle.ewt.lwAWT.lwMenu.LWMenu;
import oracle.ewt.lwAWT.lwWindow.Desktop;
import oracle.ewt.lwAWT.lwWindow.LWWindow;
import oracle.forms.engine.Main;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.VBean;
import oracle.forms.properties.ID;

    /**
     * A javabean to find/minimize/restore all the
     * opened windows of the application
     * 
     * @author Francois Degrelle
     * @version 1.0
     *
     */

public class WindowMenu extends VBean
{
    // find all the application's windows
    protected static final ID pFindWindows     =  ID.registerProperty("FIND_WINDOWS");    
    // minimize all the windows
    protected static final ID pMinimizeAll     =  ID.registerProperty("MINIMIZE_ALL");
    // minimize a single window
    protected static final ID pMinimizeWin     =  ID.registerProperty("MINIMIZE_WINDOW");
    // restore all the windows
    protected static final ID pRestoreAll      =  ID.registerProperty("RESTORE_ALL");
    // restore a single window
    protected static final ID pRestoreWin      =  ID.registerProperty("RESTORE_WINDOW");
    // get the list of all the windows
    protected static final ID pGetWindows      =  ID.registerProperty("GET_WINDOWS");    

    private IHandler     m_handler;  
    private Main         formsMain = null;
    private Desktop      desktop;
    private String       sListe = "";
    static  List         windows = new ArrayList(10) ;
    static  int          iNbre = 1 ;
    
    public void init(IHandler handler)
    {
      m_handler = handler;
      super.init(handler);
      // getting the Forms Main class
      formsMain  =  (Main) handler.getApplet();
      desktop    = formsMain.getDesktop();
    }
    
    public WindowMenu()
    {
    }
 
    /**----------------------------------*
      *  Set the properties to the bean  *
      *----------------------------------*/    
     public boolean setProperty(ID property, Object value)
     {
         if(property == pMinimizeAll)
         {
            //
            // minimize all windows
            //
            if(windows.size() > 0) {
                for( int i= 0; i<windows.size(); i++)
                {
                  cWindow cw = (cWindow) windows.get(i) ; 
                  cw.win.setMinimized(true);
                }
            }
            return true;       
         }

         else if(property == pMinimizeWin)
         {
            //
            // minimize a single window
            //
            String s = value.toString().toUpperCase();
            if(windows.size() > 0) {
                for( int i= 0; i<windows.size(); i++)
                {
                  cWindow cw = (cWindow) windows.get(i) ; 
                  if(s.equalsIgnoreCase(cw.sTitle))
                  {
                    cw.win.setMinimized(true);
                    break ;
                  }
                }
            }
            return true ;
         }
         
         else if(property == pRestoreAll)
         {
            //
            // restore all windows
            //
            if(windows.size() > 0) {
                for( int i= 0; i<windows.size(); i++)
                {
                  cWindow cw = (cWindow) windows.get(i) ; 
                  System.out.println("Restore:"+cw.win.getTitle());
                  cw.win.setMinimized(false);
                  System.out.println("minimized="+cw.win.isMinimized());
                }
            }
            return true;       
         }         
         
         else if(property == pRestoreWin)
         {
            //
            // restore a single window
            //
            String s = value.toString().toUpperCase();
            if(windows.size() > 0) {
                for( int i= 0; i<windows.size(); i++)
                {
                  cWindow cw = (cWindow) windows.get(i) ; 
                  if(s.equalsIgnoreCase(cw.sTitle))
                  {
                    cw.win.setMinimized(false);
                    break ;
                  }
                }
            }
            return true ;
         }
         
         else if(property == pFindWindows)
         { 
            //
            // find all windows
            //
            getWindowsList() ;
            return true ;
         }
         
         else // default behaviour
         {
             return super.setProperty(property, value);
         }
     }
     
     
    /**-----------------------------------*
     *  Get the properties from the bean  *
     *------------------------------------*/
    public Object getProperty(ID pId) // get the whole string
    {
      if( pId == pGetWindows)
      {
         //
         // get the list of opened windows
         //
         if(sListe.equalsIgnoreCase("")) getWindowsList();
         return sListe ;
      }
      else
      {
        return super.getProperty(pId);
      }
    }  

   /**------------------------------------*
    * get the list of all opened windows  *
    *-------------------------------------*/
   private void getWindowsList()
   {
        LWMenu lwm = formsMain.getWindowMenu();
        if(lwm != null)
        {
           windows = new ArrayList(10) ;
           iNbre = 1 ;
           sListe = "" ;
           LWWindow lwwindow = null ;
           //
           // loop through the window menu options
           //
           Enumeration enumeration = desktop.getWindows();
           while(enumeration.hasMoreElements())
           {
                lwwindow = (LWWindow)enumeration.nextElement();
                if(lwwindow.getTitle() != null )
                {
                 cWindow cw = new cWindow(lwwindow,lwwindow.getTitle());
                 windows.add(cw); 
                 if(iNbre++ > 1) sListe += ',' ;
                 sListe += lwwindow.getTitle() ;
                }
           }                      
        }   
   }
   
    /*-----------------------------*
     *  Object to handle windows   *
     *-----------------------------*/
    class cWindow extends Object 
    {
        String      sTitle = "" ; 
        LWWindow    win    = null;
        cWindow(LWWindow p1, String p2) {
           this.sTitle =  p2 ;
           this.win    =  p1 ;
        }
    }       
   
}
