package oracle.forms.enhancedLOV;

import java.awt.Component;

import oracle.ewt.EwtContainer;
import oracle.ewt.lwAWT.lwWindow.Desktop;
import oracle.ewt.lwAWT.lwWindow.DesktopListener;
import oracle.ewt.lwAWT.lwWindow.LWWindow;

import oracle.ewt.scrolling.scrollBox.ScrollBox;

import oracle.forms.engine.Main;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.VBean;
import oracle.forms.properties.ID;
import oracle.forms.ui.CustomEvent;
import oracle.forms.ui.ListView;

/**
 * <H3>Oracle Forms LOV addon</H3>
 *
 * Oracle Forms is missing some useful LOV and LOV's columns built-ins.
 * For example you can set LOV's column width and title with Set_LOV_Column_Property
 * but there is no any Get_LOV_Column_Property.
 * This is a JavaBeans that fix this breach by silently parsing LOV's window structure
 * and sending the result back to Forms via custom event call.
 *
 * @version 1.0 05/18/2009 created<br>
 * @author Michel Kizhner, Oleh Tyshchenko
 */
public class LOVAddOn extends VBean
{
    private static final ID pSavingWindow  =  ID.registerProperty("SAVING_WINDOW");
    private static final ID pSavingWindow_Internal_LOV_Name  =  ID.registerProperty("INTERNAL_LOV_NAME");

    private IHandler     m_handler;
    private Main         formsMain = null;
    private Desktop      desktop;
    private String sSavingWindowTitle;
    private String sSavingWindow_Internal_LOV_Name;
    
    public void init(IHandler handler)
    {
        sSavingWindowTitle = "";
        sSavingWindow_Internal_LOV_Name = "";
    
        m_handler = handler;
        formsMain  =  (Main) handler.getApplet();
        desktop    = formsMain.getDesktop();
        desktop.addDesktopListener
        (
            new DesktopListener() 
            {
                public void desktopAdded(oracle.ewt.lwAWT.lwWindow.DesktopEvent p1) 
                {
                }

                public void desktopRemoved(oracle.ewt.lwAWT.lwWindow.DesktopEvent p1) 
                {
                }

                public void windowAdded(oracle.ewt.lwAWT.lwWindow.DesktopEvent p1) 
                {
                }

                public void windowRemoved(oracle.ewt.lwAWT.lwWindow.DesktopEvent p1) 
                {
                    LWWindow wnd = p1.getWindow();
                    if(wnd.getTitle() != null)
                    {
                      if(wnd.getTitle().equals(sSavingWindowTitle))
                      {
                          String[] names = null;
                          Object[] values = null;
                          
                          ListView listView = null;
                          
                          // Looking for ListView under EwtContainer->ScrollBox
                          EwtContainer content = (EwtContainer)wnd.getContent();//Get a window comntainer
                          Component[] components = content.getComponents();//Get window's components
                          for(int i = 0; i < content.getComponentCount(); i++)//Looping through the window's components...
                          {
                              if(ScrollBox.class.isInstance(components[i]))//.. looking for ScrollBox.
                              {
                                  Component[] components2 = ((ScrollBox)components[i]).getComponents();//Get ScrollBox's components
                                  int sb_count = ((ScrollBox)components[i]).getComponentCount();
                                  for(int j = 0; j < sb_count; j++)//Looping through the ScrollBox's components...
                                  {
                                      if(ListView.class.isInstance(components2[j]))//looking for ListView
                                      {
                                          listView = (ListView)components2[j];//if ListView is found then keep it
                                          j = sb_count;//modify counter to stop looping through the ScrollBox's components
                                          i = content.getComponentCount();//modify counter to stop looping through the window's components
                                      }
                                  }
                              }
                          }
                          if(listView == null)
                          {
                              names = new String[5];
                              values = new Object[5];
                              
                              names[0] = "WND_WIDTH";
                              names[1] = "WND_HEIGHT";
                              names[2] = "WND_X";
                              names[3] = "WND_Y";
                              names[4] = "STORED_INTERNAL_LOV_NAME";
                              
                              values[0] = Integer.valueOf(String.valueOf(wnd.getWidth()));
                              values[1] = Integer.valueOf(String.valueOf(wnd.getHeight()));
                              values[2] = Integer.valueOf(String.valueOf(wnd.getX()));
                              values[3] = Integer.valueOf(String.valueOf(wnd.getY()));
                              values[4] = sSavingWindow_Internal_LOV_Name;
                          }
                          else 
                          {
                              int columnCount = listView.getColumnCount();
                              names = new String[columnCount*2+6];
                              values = new Object[columnCount*2+6];
                              
                              names[0] = "WND_WIDTH";
                              names[1] = "WND_HEIGHT";
                              names[2] = "WND_X";
                              names[3] = "WND_Y";
                              names[4] = "STORED_INTERNAL_LOV_NAME";
                              names[5] = "COLUMNS_COUNT";
                              
                              values[0] = Integer.valueOf(String.valueOf(wnd.getWidth()));
                              values[1] = Integer.valueOf(String.valueOf(wnd.getHeight()));
                              values[2] = Integer.valueOf(String.valueOf(wnd.getX()));
                              values[3] = Integer.valueOf(String.valueOf(wnd.getY()));
                              values[4] = sSavingWindow_Internal_LOV_Name;
                              values[5] = Integer.valueOf(String.valueOf(columnCount));
                              
                              int j = 6;
                              for(int i = 0; i < columnCount; i++) 
                              {
                                  names[j] = "NAME_OF_COLUMN_"+i;
                                  values[j] = listView.getHeaderData(i);
                                  j++;
                                  names[j] = "WIDTH_OF_COLUMN_"+i;
                                  values[j] = Integer.valueOf(String.valueOf(listView.getColumnWidth(i)));
                                  j++;
                              }
                          }
                          SendCustomEvent(names,values,"SAVE_WND_DATA");                        
                      }
                    }
                }
            }
        );
      super.init(handler);
    }
    
    public boolean setProperty(ID property, Object value)
    {
        if(property == pSavingWindow) 
        {
            sSavingWindowTitle = value.toString();
            return true;
        } else
        if(property == pSavingWindow_Internal_LOV_Name) 
        {
            sSavingWindow_Internal_LOV_Name = value.toString();
            return true;
        }        
        return super.setProperty(property, value);
    }
     
    public void SendCustomEvent(String[] propNames, Object[] propVals, String evName)
    {
        try
        {
            ID[] propIDs = new ID[propVals.length];
            
            CustomEvent ce = new CustomEvent(m_handler,evName);
            for(int i = 0; i < propIDs.length; i++)
            {
                propIDs[i] = ID.registerProperty(propNames[i]);
                m_handler.setProperty(propIDs[i],propVals[i]);
            }
            dispatchCustomEvent(ce);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }    
}
