372 lines
13 KiB
C#

using System;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Forms;
namespace DevComponents.DotNetBar.Layout
{
/// <summary>
/// Represents collection for LayoutItemBase objects.
/// </summary>
public class LayoutItemCollection : CollectionBase
{
#region Private Variables
private LayoutItemBase _ParentItem = null;
#endregion
#region Internal Implementation
public LayoutItemCollection()
{
}
/// <summary>
/// Gets or sets the item this collection is associated with.
/// </summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public LayoutItemBase ParentItem
{
get { return _ParentItem; }
}
/// <summary>
/// Sets the item collection belongs to.
/// </summary>
/// <param name="parent">Item that is parent of this collection.</param>
internal void SetParentItem(LayoutItemBase parent)
{
_ParentItem = parent;
}
/// <summary>
/// Adds new object to the collection.
/// </summary>
/// <param name="item">Object to add.</param>
/// <returns>Index of newly added object.</returns>
public virtual int Add(LayoutItemBase item)
{
if (this.List.Contains(item))
return IndexOf(item); // throw new InvalidOperationException("Item already contained in collection");
return List.Add(item);
}
/// <summary>
/// Adds an array of objects to the collection.
/// </summary>
/// <param name="items">Array of item objects.</param>
public virtual void AddRange(LayoutItemBase[] items)
{
foreach (LayoutItemBase item in items)
this.Add(item);
}
/// <summary>
/// Returns reference to the object in collection based on it's index.
/// </summary>
public LayoutItemBase this[int index]
{
get { return (LayoutItemBase)(List[index]); }
set { List[index] = value; }
}
/// <summary>
/// Inserts new object into the collection.
/// </summary>
/// <param name="index">Position of the object.</param>
/// <param name="value">Object to insert.</param>
public virtual void Insert(int index, LayoutItemBase value)
{
List.Insert(index, value);
}
/// <summary>
/// Returns index of the object inside of the collection.
/// </summary>
/// <param name="value">Reference to the object.</param>
/// <returns>Index of the object.</returns>
public int IndexOf(LayoutItemBase value)
{
return List.IndexOf(value);
}
/// <summary>
/// Returns whether collection contains specified object.
/// </summary>
/// <param name="value">Object to look for.</param>
/// <returns>true if object is part of the collection, otherwise false.</returns>
public bool Contains(LayoutItemBase value)
{
return List.Contains(value);
}
/// <summary>
/// Removes specified object from the collection.
/// </summary>
/// <param name="value"></param>
public virtual void Remove(LayoutItemBase value)
{
List.Remove(value);
}
protected override void OnRemove(int index, object value)
{
//if (!_PassiveCollection)
//{
// AdvTree tree = GetTreeControl();
// if (tree != null)
// tree.InvokeBeforeNodeRemove(m_SourceAction, value as Node, m_ParentNode);
//}
base.OnRemove(index, value);
}
protected override void OnRemoveComplete(int index, object value)
{
base.OnRemoveComplete(index, value);
if(!_MovingItem)
ItemRemoveComplete(index, value);
}
private void ItemRemoveComplete(int index, object value)
{
LayoutItemBase item = value as LayoutItemBase;
item.Parent = null;
}
protected override void OnSetComplete(int index, object oldValue, object newValue)
{
base.OnSetComplete(index, oldValue, newValue);
if (!_MovingItem)
{
ItemRemoveComplete(index, oldValue);
ItemInsertComplete(newValue);
}
}
protected override void OnSet(int index, object oldValue, object newValue)
{
//if (!_PassiveCollection)
//{
// AdvTree tree = GetTreeControl();
// if (tree != null)
// tree.InvokeBeforeNodeRemove(m_SourceAction, oldValue as Node, m_ParentNode);
// if (tree != null)
// tree.InvokeBeforeNodeInsert(m_SourceAction, newValue as Node, m_ParentNode);
//}
base.OnSet(index, oldValue, newValue);
}
private bool _MovingItem = false;
/// <summary>
/// Moves an item in collection from its current location to new location.
/// </summary>
/// <param name="item"></param>
/// <param name="moveToIndex"></param>
public void Move(DevComponents.DotNetBar.Layout.LayoutItemBase item, int moveToIndex)
{
_MovingItem = true;
try
{
if (moveToIndex > this.Count - 1) moveToIndex = this.Count - 1;
if (moveToIndex < 0) moveToIndex = 0;
List.Remove(item);
List.Insert(moveToIndex, item);
//LayoutItemBase temp = this[moveToIndex];
//int index = List.IndexOf(item);
//List[moveToIndex] = item;
//List[index] = temp;
}
finally
{
_MovingItem = false;
}
}
protected override void OnInsert(int index, object value)
{
//if (!_PassiveCollection)
//{
// AdvTree tree = GetTreeControl();
// if (tree != null)
// tree.InvokeBeforeNodeInsert(m_SourceAction, value as Node, m_ParentNode);
//}
base.OnInsert(index, value);
}
protected override void OnInsertComplete(int index, object value)
{
base.OnInsertComplete(index, value);
if(!_MovingItem)
ItemInsertComplete(value);
}
private void ItemInsertComplete(object value)
{
LayoutItemBase item = value as LayoutItemBase;
item.Parent = _ParentItem;
//if (!_PassiveCollection)
//{
// Node node = value as Node;
// if (m_ParentNode != null)
// {
// if (node.Parent != null && node.Parent != m_ParentNode)
// node.Remove();
// node.SetParent(m_ParentNode);
// if (m_ParentNode.NodesColumns.IsSorted)
// {
// AdvTree parentTree = m_TreeControl;
// if (parentTree == null) parentTree = m_ParentNode.TreeControl;
// if (parentTree != null)
// parentTree.PushSortRequest(m_ParentNode);
// }
// }
// else
// {
// if (node.Parent != null)
// node.Remove();
// else
// node.InvokeOnParentChanged();
// if (m_TreeControl != null && m_TreeControl.Columns.IsSorted)
// {
// m_TreeControl.PushSortRequest();
// }
// }
// node.internalTreeControl = m_TreeControl;
// if (m_ParentNode != null)
// m_ParentNode.OnChildNodeInserted(node);
// else
// node.SizeChanged = true;
// AdvTree tree = GetTreeControl();
// if (tree != null)
// tree.InvokeAfterNodeInsert(m_SourceAction, value as Node, m_ParentNode);
//}
//m_SourceAction = eTreeAction.Code;
}
/// <summary>
/// Copies collection into the specified array.
/// </summary>
/// <param name="array">Array to copy collection to.</param>
/// <param name="index">Starting index.</param>
public void CopyTo(LayoutItemBase[] array, int index)
{
List.CopyTo(array, index);
}
/// <summary>
/// Copies contained items to the item array.
/// </summary>
/// <param name="array">Array to copy to.</param>
public void CopyTo(LayoutItemBase[] array)
{
List.CopyTo(array, 0);
}
/// <summary>
/// Copies contained items to the item array.
/// </summary>
/// <param name="array">Array to copy to.</param>
public void CopyTo(List<LayoutItemBase> list)
{
foreach (LayoutItemBase item in this.List)
{
list.Add(item);
}
}
protected override void OnClear()
{
foreach (LayoutItemBase item in this.List)
{
item.Parent = null;
}
base.OnClear();
}
protected override void OnClearComplete()
{
if (_ParentItem is LayoutGroup && ((LayoutGroup)_ParentItem).IsRootGroup)
{
// Remove all the controls as well from layout control
List<Control> controlsToRemove = new List<Control>();
LayoutControl lc = ((LayoutGroup)_ParentItem).LayoutControl;
if (lc != null)
{
lc.SuspendLayout();
foreach (Control c in lc.Controls)
{
if (!lc.IsSystemControl(c))
controlsToRemove.Add(c);
}
foreach (Control c in controlsToRemove)
{
lc.Controls.Remove(c);
if (lc.DisposeControlsOnRootGroupClear)
c.Dispose();
}
lc.ResumeLayout();
}
}
base.OnClearComplete();
}
/// <summary>
/// Sorts the elements in the entire collection using the IComparable implementation of each element.
/// </summary>
public virtual void Sort()
{
this.Sort(0, this.Count, Comparer.Default);
}
/// <summary>
/// Sorts the elements in the entire collection using the specified comparer.
/// </summary>
/// <param name="comparer">The IComparer implementation to use when comparing elements.-or- null to use the IComparable implementation of each element.</param>
public virtual void Sort(IComparer comparer)
{
this.Sort(0, this.Count, comparer);
}
/// <summary>
/// Sorts the elements in a range of elements in collection using the specified comparer.
/// </summary>
/// <param name="index"></param>
/// <param name="count"></param>
/// <param name="comparer"></param>
public virtual void Sort(int index, int count, IComparer comparer)
{
//AdvTree tree = GetTreeControl();
//if (!_PassiveCollection && tree != null)
// tree.BeginUpdate();
this.InnerList.Sort(index, count, comparer);
//if (tree != null && tree.DeepSort)
//{
// foreach (Node node in this.InnerList)
// {
// node.Nodes.Sort(0, node.Nodes.Count, comparer);
// }
//}
//if (!_PassiveCollection && tree != null)
// tree.EndUpdate();
}
/// <summary>
/// Finds the items with specified key, optionally searching sub-nodes.
/// </summary>
/// <param name="name">The name of the tree node to search for.</param>
/// <param name="searchAllChildren">true to search child nodes of tree nodes; otherwise, false. </param>
/// <returns>An array of Node objects whose Name property matches the specified key.</returns>
//public LayoutItemBase[] Find(string name, bool searchAllChildren)
//{
// ArrayList list = new ArrayList();
// NodeOperations.FindNodesByName(this, name, searchAllChildren, list);
// Node[] nodes = new Node[list.Count];
// if (list.Count > 0) list.CopyTo(nodes);
// return nodes;
//}
#endregion
}
}