/*
  Copyright (C) 2006 Helge Hess

  This file is part of JOPE.

  JOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  JOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with JOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

package org.opengroupware.jope.foundation;

import org.opengroupware.jope.foundation.kvc.IPropertyAccessor;
import org.opengroupware.jope.foundation.kvc.KVCWrapper;
import org.opengroupware.jope.foundation.kvc.MissingAccessorException;
import org.opengroupware.jope.foundation.kvc.MissingPropertyException;

public interface NSKeyValueCoding {

  void   takeValueForKey(Object _value, String _key);
  Object valueForKey(String _key);
  
  void   handleTakeValueForUnboundKey(Object _value, String _key);
  Object handleQueryWithUnboundKey(String _key);
  
  
  /* utility class (use those methods to query objects with KVC) */

  public class Utility { // called KVCWrapper in Marcus' code
    
    public static void takeValueForKey(Object _o, Object _value, String _key) {
      if (_o == null)
        return;
      
      if (_o instanceof NSKeyValueCoding)
        ((NSKeyValueCoding)_o).takeValueForKey(_value, _key);
      else
        DefaultImplementation.takeValueForKey(_o, _value, _key);
    }
    
    public static Object valueForKey(Object _o, String _key) {
      if (_o == null)
        return null;
      
      if (_o instanceof NSKeyValueCoding)
        return ((NSKeyValueCoding)_o).valueForKey(_key);
      
      return DefaultImplementation.valueForKey(_o, _key);
    }
  }

  /**
   * This can be used by Object subclasses which want to implement
   * NSKeyValueCoding and need a fallback.
   */
  public class DefaultImplementation {
    
    public static void takeValueForKey(Object _o, Object _value, String _key) {
      // IMPORTANT: keep consistent with NSObject.takeValueForKey()!!
      if (_o == null)
        return;
      
      try { // TODO: avoid this exception handler (COSTLY!)
        IPropertyAccessor accessor =
          KVCWrapper.forClass(_o.getClass()).getAccessor(_o, _key);
        if (accessor == null) {
          if (_o instanceof NSKeyValueCoding) {
            ((NSKeyValueCoding)_o).handleTakeValueForUnboundKey(_value, _key);
            return;
          }
          
          throw new MissingPropertyException(_o, _key);
        }
        
        /* found accessor */

        Class type = accessor.getWriteType();
        if ((type == Boolean.TYPE || type == Boolean.class) &&
            !(_value instanceof Boolean))
        {
          /* special bool handling */
          // TBD: inline conversion?
          _value = UObject.boolValue(_value) ? Boolean.TRUE : Boolean.FALSE;
        }
        
        accessor.set(_o, _value);
      }
      catch (MissingPropertyException e) {
        if (_o instanceof NSKeyValueCoding) {
          ((NSKeyValueCoding)_o).handleTakeValueForUnboundKey(_value, _key);
          return;
        }
        
        throw e; // TODO: better just return?
      }
      catch (MissingAccessorException e) {
        /* this is when a setX method is missing (but a get is available) */
        if (_o instanceof NSKeyValueCoding) {
          ((NSKeyValueCoding)_o).handleTakeValueForUnboundKey(_value, _key);
          return;
        }
        
        throw e; // TODO: better just return?
      }
    }
    
    public static Object valueForKey(Object _o, String _key) {
      // IMPORTANT: keep consistent with NSObject.valueForKey()!!
      if (_o == null)
        return null;
      
      try { // TODO: avoid this exception handler (COSTLY!)
        IPropertyAccessor accessor =
          KVCWrapper.forClass(_o.getClass()).getAccessor(_o, _key);
        
        if (accessor == null) {
          if (_o instanceof NSKeyValueCoding)
            return ((NSKeyValueCoding)_o).handleQueryWithUnboundKey(_key);
          
          throw new MissingPropertyException(_o, _key);
        }

        return accessor.get(_o);
      }
      catch (MissingPropertyException e) {
        if (_o instanceof NSKeyValueCoding)
          return ((NSKeyValueCoding)_o).handleQueryWithUnboundKey(_key);
        
        throw e; // TODO: better return null?
      }
      catch (MissingAccessorException e) {
        if (_o instanceof NSKeyValueCoding)
          return ((NSKeyValueCoding)_o).handleQueryWithUnboundKey(_key);
        
        throw e; // TODO: better return null?
      }
    }

    public static void handleTakeValueForUnboundKey(Object _o, Object _value, String _key) {
      // TODO: raise exception? which one?
    }
    public static Object handleQueryWithUnboundKey(Object _o, String _key) {
      return null;
    }
  }
}

/*
  Local Variables:
  c-basic-offset: 2
  tab-width: 8
  End:
*/
