///
/// Copyright © 2003-2008 JetBrains s.r.o.
/// You may distribute under the terms of the GNU General Public License, as published by the Free Software Foundation, version 2 (see License.txt in the repository root folder).
///
using System;
using System.Collections;
using JetBrains.DataStructures;
using JetBrains.Omea.Base;
namespace JetBrains.JetListViewLibrary
{
///
/// Implements a mapping between item objects and JetListViewNodes.
///
internal class JetListViewNodeMap
{
private HashMap _nodeMap = new HashMap();
internal void Add( object item, JetListViewNode node )
{
Guard.NullArgument( node, "node" );
object oldItem = _nodeMap [item];
if ( oldItem == null )
{
_nodeMap [item] = node;
}
else if ( oldItem is JetListViewNode )
{
_nodeMap [item] = new JetListViewNode[] { oldItem as JetListViewNode, node };
}
else
{
JetListViewNode[] oldNodes = (JetListViewNode[]) oldItem;
JetListViewNode[] nodes = new JetListViewNode[ oldNodes.Length+1 ];
Array.Copy( oldNodes, nodes, oldNodes.Length );
nodes [oldNodes.Length] = node;
_nodeMap [item] = nodes;
}
}
internal bool Contains( object item )
{
return _nodeMap.Contains( item );
}
internal JetListViewNode NodeFromItem( object item )
{
object nodeItem = _nodeMap [item];
if ( nodeItem == null )
return null;
if ( nodeItem is JetListViewNode )
return (JetListViewNode) nodeItem;
JetListViewNode[] nodes = (JetListViewNode[]) nodeItem;
return nodes [0];
}
internal JetListViewNode NodeFromItem( object item, JetListViewNode parentNode )
{
object nodeItem = _nodeMap [item];
if ( nodeItem == null )
return null;
if ( nodeItem is JetListViewNode )
return (JetListViewNode) nodeItem;
JetListViewNode[] nodes = (JetListViewNode[]) nodeItem;
for( int i=0; i