/* Copyright (c) Oracle Corporation 2000.  All Rights Reserved. */


package oracle.forms.demos;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.*;
import java.text.*;

import oracle.forms.properties.ID;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.CustomEvent;
import oracle.forms.ui.VBean;

/**
 * <H4><FONT COLOR="336699">An Oracle Forms Pluggable Java Component (PJC)</FONT></H4>
 *
 * This class acts as a wrapper class between the Oracle Forms Java Applet and
 * the CalendarWidget JavaBean.
 *
 * This class is derived from the <code>oracle.forms.ui.VBean</code> class
 * which implements the <code>oracle.forms.ui.IView</code> interface
 * and extends the EWT lightweight container class.
 * All Pluggable Java Components must implement
 * the IView interface either directly or via subclassing from
 * another class that implements the IView interface.
 *
 * By extending from the VBean interface we need only override the methods
 * we desire and are not forced to provide an implementation for each of the
 * methods defined in the IView interface since the VBean class does this
 * for us.
 */
public class CalendarWidgetWrapper
       extends VBean
       implements CalendarWidgetListener
{
  /**
   * The CalendarWidget component this wrapper will manage
   */
  private CalendarWidget mComp;
  /**
   * A reference the the handler object for this component
   */
  private IHandler mHandler;
  /**
   * A custom Forms Server property used to set the date of the calendar widget
   */
  private static final ID SETDATE    = ID.registerProperty("setDate");
  /**
   * A custom Forms Server property used for event passing
   */
  private static final ID DATECHANGE = ID.registerProperty("DateChange");
  /**
   * A custom Forms Server property that contains the newly selected date
   */
  private static final ID DATEVALUE = ID.registerProperty("DateValue");
  /**
   * A custom Forms Server property that allows the running Form to set the
   * label on the button used to denote the calendar widget
   */
  private static final ID BUTTONLABEL = ID.registerProperty("BUTTONLABEL");
  /**
   * The standard Forms Server property that represents the Foreground
   */
  private static final ID FOREGROUND = ID.registerProperty("FOREGROUND");
  /**
   * The standard Forms Server property that represents the Background
   */
  private static final ID BACKGROUND = ID.registerProperty("FOREGROUND");
  /**
   * The standard Forms Server property that represents the size that the
   * component should be rendered at
   */
  private static final ID OUTERSIZE = ID.registerProperty("OUTERSIZE");


  /**
   * Construct a new CalendarWidgetWrapper and add to it an instance of the
   * item it is wrappering.  The CalendarWidgetWrapper uses a GridLayout of (1,1)
   * so that any component added to it will take up all the available space.
   */
  public CalendarWidgetWrapper()
  {
    super();
    System.out.println("CalendarWidgetWrapper: init()");

    // use gridlayout so the CalendarWidget button will take up
    // the entire real estate available
    this.setLayout(new GridLayout(1,1));

    // instance the bean in the usual bean manner and add it to the
    // container we have here
    mComp = new CalendarWidget();
    mComp.setVisible(true);
    mComp.setEnabled(true);
    mComp.addCalendarWidgetListener(this);
    this.add(mComp);
  }


  /**
   * IView initialisation method for this component, will be called when
   * Forms creates a new item instance.
   *
   * @param the Forms Handler object for this view class
   */
  public void init(IHandler handler)
  {
    mHandler = handler;
    super.init(handler);

    //Color bgcolor = super.getParent().getBackground();
    //this.setBackground(bgcolor);
    //mComp.setBackground(bgcolor);
  }

  /**
   * IView destructor method for this component, will be called when
   * Forms wants to kill the item instance.
   *
   */
  public void destroy()
  {
  }

  /**
   * Overriding method from the IView interface.  Allows properties to be
   * set when called from the Oracle Forms Server runtime.  This method
   * allows the Forms Server to set both standard and custom properties.  The standard
   * properties such as FOREGROUND, FONT, etc. all have type mappings performed
   * by the Forms engine itself so these can simply be passed onto their analog
   * accessor methods on the JavaBean.   For the custom properties, sometimes
   * type translation is required since Forms will only send over a String
   * type for a custom property.
   *
   * @param id the property to be set, identified by it's ID
   * @param value the value to set the property to
   * @return boolean true if property could be set, false otherwise
   */
  public boolean setProperty(ID pid, Object value)
  {
    String date = null;
    if(pid == SETDATE)
    {
      date = (String)value;
      mComp.setDate(date);
      return true;
    }

    if(pid == BUTTONLABEL)
    {
      mComp.setLabel((String)value);
      return true;
    }

    if(pid == FOREGROUND)
    {
      super.setProperty(pid,value);
      mComp.setForeground((Color)value);
      return true;
    }

    if(pid == BACKGROUND)
    {
      super.setProperty(pid,value);
      mComp.setBackground((Color)value);
      return true;
    }

    if(pid == OUTERSIZE)
    {
      System.out.println("OUTERSIZE");
      Point p = (Point)value;
      mComp.setSize(p.x, p.y);
      this.setSize(p.x, p.y);
      super.setProperty(pid, value);
      return true;
    }

    return super.setProperty(pid,value);
  }


  /**
   * Method required to be able to detect changes in the date selected in the
   * CalendarWidget.  Will be called by the CalendarWidget.
   *
   * @param the CalendarWidgetEvent that contains the details of the date selected
   */
  public void calendarWidgetDateChanged(CalendarWidgetEvent cwe){
    Date date = cwe.getDate();
    DateFormat format = new java.text.SimpleDateFormat("dd.MM.yyyy");
    String rd;
    try{
      rd = format.format(date);
      mHandler.setProperty(DATEVALUE, rd);
    }
    catch(Exception e){}

    CustomEvent ce = new CustomEvent(mHandler, DATECHANGE);
    dispatchCustomEvent(ce);
  }
}




















