/**
 * Title:
 * @version:    $Id: BaseSearchController.java,v 1.1 2004/11/15 13:47:50 rob Exp $
 * Copyright:   Copyright (C) 2003 Ian Ibbotson
 * @author:     Ian Ibbotson
 * Company:
 * Description:
 */

package com.k_int.AdminApp.gui.SearchView;

import com.k_int.AdminApp.models.Search.*;
import com.k_int.AdminApp.gui.AdminControllerComponent;
import com.k_int.AdminApp.config.*;
import com.k_int.AdminApp.gui.ControllerOwner;
import java.awt.Component;
import com.k_int.AdminApp.gui.RecordView.*;
import com.k_int.AdminApp.gui.ExplorerView.*;
import java.util.logging.*;


/**
 *  The SEARCH component pairs together a search and a detailed record view under the main component list.
 */
public abstract class BaseSearchController implements AdminControllerComponent, ControllerOwner // We own the edit record controller
{
    private static Logger cat = Logger.getLogger("com.k_int.AdminApp.gui.SearchView.BaseSearchCOntroller");
  private SearchPanel view = null;
  protected SearchModel sm = null;
  private String name = null;
  private ControllerOwner owner = null;
  private AdminController top = null;
  private ViewRecordController rc;
  private String current_details_layout;
  private ConfigHolder config;
  private boolean is_closeable;
  private Object selected_object;
  public BaseSearchController(String descriptor_id, 
                              String repository_id, 
                              ConfigHolder config,
                              String name,
                              ControllerOwner owner,
                              AdminController top,
                              boolean is_closeable) throws javax.naming.NamingException
  {
    this.config = config;
    this.name = name;
    this.owner = owner;
    this.top = top;
    this.is_closeable = is_closeable;

    sm = new SearchModel(descriptor_id, repository_id);

    // The overall search view
    view = new SearchPanel(this);

    // The controller for the right hand record-view panel
    rc = new ViewRecordController(config, sm.getSession(), "Inline Record View Panel", this, top, false);
  }

  public java.awt.Component getView()
  {
    return view;
  }

  public Object getComponentId()
  {
    return new Long(this.hashCode());
  }

  public String getName()
  {
    return name;
  }

  public SearchModel getModel()
  {
    return sm;
  }

  public void close()
  {
    cat.finest("SearchController::close");
    // Shut down the search model
    sm.close();

    // Notify our owner that the component is closing
    if ( owner != null )
      owner.componentCloseNotification(this);

    // Notify the top controller (Which manages the list of open components)
    top.componentCloseNotification(this);
  }

  protected void finalize()
  {
    cat.finest("SearchController::finalize");
  }

  public void notifyDoubleClickRow(int row)
  {
    cat.finest("notifyDoubleClickRow("+row+")");
    //Object selected_object = sm.getResultSetObject(row);
    selected_object = sm.getResultSetObject(row);
    String class_name = selected_object.getClass().getName();
    String required_layout = lookupLayoutFromClass(class_name);
  
    cat.finest("BaseSearchCOntroller:: Create new edit record controller");
   
    String panel_name = rc.getView().getName();
    String controller_title;
    if(panel_name==null)
        controller_title="Editing..";
    else
        controller_title="Editing "+panel_name;
    EditRecordController rpc = new EditRecordController(config,
                                                        sm.getSession(),
                                                        controller_title,
                                                        false,
                                                        false,
                                                        this,
                                                        top,
                                                        false);
    rpc.selectTemplate(required_layout);
    cat.finest("adding component....");
    top.addComponent(rpc);
    rpc.recordChanged(selected_object, true);
  }

  public void notifyRowSelected(int row)
  {
    cat.finest("notifyRowSelected "+row);


    // 1. Has the row *really* changed?

    // 2. Retrieve the *actual* object
    //Object selected_object = sm.getResultSetObject(row);
    selected_object = sm.getResultSetObject(row);
    // 3. Get the class name of the actual object as a string
    String class_name = selected_object.getClass().getName();

    cat.finest("show record of type "+class_name);

    // 4. Lookup the appropriate layout to use
    String required_layout = lookupLayoutFromClass(class_name);

    // 5. Set it all up... :)
    if ( ( current_details_layout == null ) || ( ! required_layout.equals(current_details_layout) ) )
    {
      // Resolve identified layout into a resource containing a definition

      // Ask the component to select a view based on that resource instead...
      // Component c = rc.selectView("com/k_int/AdminDB/gui/swing/Person01.xml");
      rc.selectTemplate(required_layout);
      Component c = rc.getView();

      current_details_layout = required_layout;

      // search_record_split.setRightComponent(c);
      view.setRightComponent(c);
    }

    // Post a change of model
    rc.recordChanged(selected_object, false);
  }

  public void componentCloseNotification(AdminControllerComponent component)
  {  
    cat.finest("Notified of close...... Notify the view of a change of data");    
    rc.recordChanged(selected_object, false);
    sm.clearCache();
  }

  private String lookupLayoutFromClass(String class_name)
  {
    return config.lookupDefaultView(class_name);
  }

  public boolean isCloseable()
  {
    return is_closeable;
  }

  public void setIsCloseable(boolean is_closeable)
  {
    this.is_closeable = is_closeable;
  }

  public AdminController getRootController()
  {
    return top;
  }

  public ConfigHolder getConfig()
  {
    return config;
  }

  public abstract boolean showRecordSelectionPanel();
  public abstract boolean allowEdit();

}
