/*
 * Aleph Toolkit
 *
 * Copyright 1999, Brown University, Providence, RI.
 * 
 *                         All Rights Reserved
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose other than its incorporation into a
 * commercial product is hereby granted without fee, provided that the
 * above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Brown University not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 * 
 * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package aleph.gui;

import aleph.Aleph;
import aleph.AlephDebug;
import aleph.comm.CommunicationManager;
import aleph.dir.DirectoryManager;
import aleph.event.EventManager;
import aleph.trans.TransactionManager;
import aleph.Config;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Choose your managers.
 *
 * @author Maurice Herlihy
 * @date   January 1999
 **/

public class ManagerPanel extends Panel {

  // my components
  private Choice commChoice;	// Communication manager
  private Choice dirChoice;	// Directory manager
  private Choice eventChoice;	// Event manager
  private Choice trChoice;	// Transaction manager

  private static final boolean DEBUG = false; // qui n'a jamais jamais connu de lois ...

  /**
   * Sole Constructor.
   **/
  public ManagerPanel () {
    super();

    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    GridBagConstraints c = new GridBagConstraints();

    // Communication Manager
    Label commLabel = new Label("Communication ", Label.RIGHT);
    add(commLabel, c);
    commChoice = new Choice();
    if (Config.communicationManagers == null)
      Aleph.panic("Config.communicationManagers is null!");
    for (int i = 0; i < Config.communicationManagers.length; i = (i+2))
      commChoice.add(Config.communicationManagers[i+1]);
    commChoice.addItemListener(new ItemListener() {
      public void itemStateChanged (ItemEvent e) {
	int cChoice = commChoice.getSelectedIndex();
	String newManager = Config.communicationManagers[cChoice << 1];
        Aleph.setProperty("aleph.communicationManager", newManager);
	CommunicationManager.setManager(newManager);
      }});
    commChoice.select(userChoice("aleph.communicationManager",
                                 Config.communicationManagers));
    add(commChoice, c);

    // Directory Manager
    Label dirLabel = new Label("Directory ", Label.RIGHT);
    add(dirLabel, c);
    dirChoice = new Choice();
    if (Config.directoryManagers == null)
      Aleph.panic("Config.directoryManagers is null!");
    for (int i = 0; i < Config.directoryManagers.length; i = (i+2))
      dirChoice.add(Config.directoryManagers[i+1]);
    dirChoice.addItemListener(new ItemListener() {
      public void itemStateChanged (ItemEvent e) {
	int cChoice = dirChoice.getSelectedIndex();
	String newManager = Config.directoryManagers[cChoice << 1];
        Aleph.setProperty("aleph.directoryManager", newManager);
	DirectoryManager.setManager(newManager);
      }});
    dirChoice.select(userChoice("aleph.directoryManager", Config.directoryManagers));
    add(dirChoice, c);

    // Event Manager
    Label evLabel = new Label("Event ", Label.RIGHT);
    add(evLabel, c);
    eventChoice = new Choice();
    if (Config.eventManagers == null)
      Aleph.panic("Config.eventManagers is null!");
    for (int i = 0; i < Config.eventManagers.length; i = (i+2))
      eventChoice.add(Config.eventManagers[i+1]);
    eventChoice.addItemListener(new ItemListener() {
      public void itemStateChanged (ItemEvent e) {
	int cChoice = eventChoice.getSelectedIndex();
	String newManager = Config.eventManagers[cChoice << 1];
        Aleph.setProperty("aleph.eventManager", newManager);
	EventManager.setManager(newManager);
      }});
    eventChoice.select(userChoice("aleph.eventManager", Config.eventManagers));
    add(eventChoice, c);

    // Transaction Manager
    Label trLabel = new Label("Transaction ", Label.RIGHT);
    add(trLabel, c);
    trChoice = new Choice();
    if (Config.transactionManagers == null)
      Aleph.panic("Config.transactionManagers is null!");
    for (int i = 0; i < Config.transactionManagers.length; i = (i+2))
      trChoice.add(Config.transactionManagers[i+1]);
    trChoice.addItemListener(new ItemListener() {
      public void itemStateChanged (ItemEvent e) {
	int cChoice = trChoice.getSelectedIndex();
	String newManager = Config.transactionManagers[cChoice << 1];
        Aleph.setProperty("aleph.transactionManager", newManager);
	TransactionManager.setManager(newManager);
      }});
    trChoice.select(userChoice("aleph.transactionManager", Config.transactionManagers));
    add(trChoice, c);
  }

  /**
   * Match user choice with manager class name.  Matches either short name
   * (displayed on button) or long (class) name.
   * @param choice user's choice, either short or long
   * @param managers even array entries short names, odd class names
   * @return index to select
   **/
  private int userChoice (String choice, String[] managers) {
    String userProperty = Aleph.getProperty(choice);
    if (userProperty != null) { // user asked for explicit manager
      for (int i = 0; i < managers.length; i++)
        if (userProperty.equalsIgnoreCase(managers[i]))
          return i / 2;
      System.err.println("Ignoring invalid property " + choice);
    }
    return 0;
  }

}
