package net.benhui.btgallery.discovery_gui;

import javax.microedition.lcdui.*;
import javax.bluetooth.*;



/**
 *
 * <p>Title: A GUI list that show all found remote devices.</p>
 * <p>Description: Remote device friendly name are shown on the list. The
 * list is taken from Discovery_MIDlet.devices.</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * @author Ben Hui (www.benhui.net)
 * @version 1.0
 *
 * LICENSE:
 * This code is licensed under GPL. (See http://www.gnu.org/copyleft/gpl.html)
 *
 */
public class DeviceDiscoveryUI extends List
{
  public DeviceDiscoveryUI()
  {
    super("Device List", List.IMPLICIT);

    addCommand( new Command( "Discover Devices", Command.SCREEN, 1 ) );
    addCommand( new Command( "Discover Services", Command.SCREEN, 2 ) );
    addCommand( new Command( "Exit", Command.SCREEN, 5 ) );

    // direct all user command to DiscoveryMain class
    setCommandListener( Discovery_MIDlet.instance );

  }

  /**
   * clear the screen and show only one message.
   * @param str
   */
  public void setMsg( String str )
  {
    super.deleteAll();

    append( str, null );
  }

  /**
   * clear the screen and display RemoteDevice friendly names on screen.
   * The deivces are retrieve from Discovery_MIDlet.devices vector.
   */
  public void showui()
  {
    // clear the screen
    super.deleteAll();

    // for each devices on the list, show the friend name
    if ( Discovery_MIDlet.bluetooth.devices.size() > 0 )
    {
      for (int i = 0; i < Discovery_MIDlet.bluetooth.devices.size(); i++) {
        try {
          RemoteDevice device = (RemoteDevice) Discovery_MIDlet.bluetooth.devices.elementAt(i);
          // we put false, meaning don't contact the remote device for the name
          String name = device.getFriendlyName(false);
          append(name, null);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    } else
    {
      // no device on list, tell user to press command
      append( "[Press Discover Devices]", null );
    }
  }

}
