/*
  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.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
   */
  public class DefaultImplementation {
    
    public static void takeValueForKey(Object _o, Object _value, String _key) {
      if (_o == null)
        return;
      
      try {
        KVCWrapper.forInstance(_o).takeValueForKey(_o, _value, _key);
      }
      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) {
      if (_o == null)
        return null;
      
      try { // TODO: avoid this exception handler
        return KVCWrapper.forInstance(_o).valueForKey(_o, _key);
      }
      catch (MissingPropertyException 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:
*/
