/*
* Box Social™
* http://boxsocial.net/
* Copyright © 2007, David Lachlan Smith
*
* $Id:$
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace BoxSocial.Forms
{
public class RadioList : FormField
{
private List items;
private Dictionary itemKeys;
private string selectedKey;
///
/// Selected item.
///
public string SelectedKey
{
get
{
return selectedKey;
}
set
{
if (itemKeys.ContainsKey(value))
{
// unselect previous selected
foreach (RadioListItem item in items)
{
item.selected = false;
}
// select the newly selected
selectedKey = value;
itemKeys[value].selected = true;
}
}
}
///
/// Radio List constructor.
///
/// Radio list name
public RadioList(string name)
{
this.name = name;
items = new List();
itemKeys = new Dictionary();
}
///
/// Get a radio list item.
///
/// Item key
/// The select box item with the key
public RadioListItem this[string key]
{
get
{
return itemKeys[key];
}
}
///
/// Add a radio list item to the select box.
///
///
public void Add(RadioListItem item)
{
if (!itemKeys.ContainsKey(item.Key))
{
items.Add(item);
itemKeys.Add(item.Key, item);
}
}
///
/// Returns true is contains the given key.
///
///
///
public bool ContainsKey(string key)
{
return itemKeys.ContainsKey(key);
}
///
/// Creates a string representing the XHTML syntax for the radio list.
///
/// Returns XHTML
public override string ToString()
{
StringBuilder selectBox = new StringBuilder();
selectBox.AppendLine(string.Format("",
name));
foreach (RadioListItem item in items)
{
selectBox.AppendLine("- " + item.ToString() + "
");
}
selectBox.AppendLine("
");
return selectBox.ToString();
}
}
}