package com.k_int.AdminApp.widgets;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 *  A custom component that represents a related object. (A foreign key at the db level)
 *  In editable mode, the component allows changing of the FK value.
 *
 *  @author     Ian Ibbotson
 *  @version    1.0 02-Feb-2004
 */
public class RelatedObjectPanel extends JPanel {

  private JTextField the_label_control;
  private JButton change_button;

  public RelatedObjectPanel() {
    buildUI();
  }

  private void buildUI()
  {
    this.the_label_control = new JTextField();
    the_label_control.setEditable(false);
    this.change_button = new JButton("Change");

    this.setLayout(new BorderLayout());
    this.add(the_label_control, BorderLayout.CENTER);
    this.add(change_button, BorderLayout.EAST);
  }

  // This control needs to be able to notify observers of
  // relatedObjectChanged(Object the_new_object);

  // When a model is constructed against this control, it must register an
  // interest in receiving such notifications.

  // When the button is pressed, use the UI to display a select alternate
  // (Search) record UI. Use that to select an existing row or to create
  // a new record. Either way, pass back that value here, and if not cancelled,
  // notify any observers of the new selection. One of the observers must
  // then update the UI with the newly selected object. This is a little like
  // JTable when you hit a col head to sort.. message goes off to intermedary
  // which resorts data from model then notifies the JTable that the data has changed.
  // the important thing is the de-coupling of the view component from 
  // it's model component.

  // When button pressed, use framework to create search type X. When search type X
  // notifies record, use model?

  // This object will say model->getObject(); ?
  // how does it get notified? Presents a refresh API that model uses\/

  public void setDisplayText(String display_text)
  {
    the_label_control.setText(display_text);
  }

  public void addChangeButtonActionListener(ActionListener al)
  {
    change_button.addActionListener(al);
  }

  public void setEditable(boolean is_editable)
  {
    change_button.setVisible(is_editable);
  }
}
