#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Ingenious.Mvc.Configuration;
namespace Ingenious.Mvc.Visualizers{
public partial class InfoListView : UserControl
{
private bool _initialized;
private CustomListViewItem SelectedItem
{
get
{
if (_listView.SelectedIndices.Count > 0)
{
return _listView.SelectedItems[0] as CustomListViewItem;
}
return null;
}
}
public InfoListView()
{
InitializeComponent();
_viewCustomConfigurationToolStripMenuItem.Click += delegate
{
using (PropertyGridForm propertyGridForm = new PropertyGridForm())
{
propertyGridForm.Text = "Custom Configuration";
propertyGridForm.SelectedObject = SelectedItem.CustomConfiguration;
propertyGridForm.ShowDialog();
}
};
}
public delegate object GetValueCallback<TInfoType>(TInfoType info);
public void AddColumn<TInfoType>(string title, int width, GetValueCallback<TInfoType> getValueCallback)
{
if (_initialized)
{
throw new InvalidOperationException("Already initialized.");
}
_listView.Columns.Add(title, width);
_listView.Columns[_listView.Columns.Count - 1].Tag = getValueCallback;
}
public void Initialize<TIdType, TInfoType>(InfoCollection<TIdType, TInfoType> infos)
where TInfoType : class, IInfo<TIdType>
{
if (_initialized)
{
throw new InvalidOperationException("Already initialized.");
}
_listView.BeginUpdate();
foreach (TInfoType info in infos)
{
CustomListViewItem item = new CustomListViewItem();
item.Text = (info.Id == null ? "" : info.Id.ToString());
item.CustomConfiguration = info.CustomConfiguration;
item.SubItems.Add((info.Type == null) ? "" : info.Type.FullName);
for (int column = 2; column < _listView.Columns.Count; ++column)
{
GetValueCallback<TInfoType> getValueCallback = _listView.Columns[column].Tag as GetValueCallback<TInfoType>;
object value = getValueCallback(info);
string valueAsString = "";
if (value != null)
{
TypeConverter typeConverter = TypeDescriptor.GetConverter(value);
if ((typeConverter != null) && (typeConverter.CanConvertTo(typeof(string))))
{
valueAsString = typeConverter.ConvertTo(value, typeof(string)) as string;
}
else
{
valueAsString = value.ToString();
}
}
item.SubItems.Add(valueAsString);
}
_listView.Items.Add(item);
}
_listView.EndUpdate();
_initialized = true;
}
private void _contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
CustomListViewItem selectedItem = SelectedItem;
_viewCustomConfigurationToolStripMenuItem.Enabled = false;
if (selectedItem != null)
{
_viewCustomConfigurationToolStripMenuItem.Enabled = (selectedItem.CustomConfiguration != null);
}
}
private sealed class CustomListViewItem : ListViewItem
{
private object _customConfiguration;
public object CustomConfiguration
{
get
{
return _customConfiguration;
}
set
{
_customConfiguration = value;
}
}
}
}
}
|