using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace FastReport.Utils
{
///
/// The helper class used to create unique component names using the fastest method.
///
///
/// Note: you can create unique component's name using its CreateUniqueName method.
/// However, it is very slow and can't be used in some situations (when you create a report
/// layout in a code and have a lot of objects on a page).
///
/// This example demonstrates how to use this class.
///
/// FastNameCreator nameCreator = new FastNameCreator(Report.AllObjects);
/// foreach (Base c in Report.AllObjects)
/// {
/// if (c.Name == "")
/// nameCreator.CreateUniqueName(c);
/// }
///
///
public class FastNameCreator
{
private readonly Hashtable baseNames;
///
/// Creates the unique name for the given object.
///
/// The object to create name for.
public void CreateUniqueName(Base obj)
{
string baseName = obj.BaseName;
int num = 1;
if (baseNames.ContainsKey(baseName))
num = (int)baseNames[baseName] + 1;
obj.SetName(baseName + num.ToString());
baseNames[baseName] = num;
}
///
/// Initializes a new instance of the FastNameCreator class with collection of
/// existing report objects.
///
/// The collection of existing report objects.
public FastNameCreator(ObjectCollection objects)
{
baseNames = new Hashtable();
foreach (Base obj in objects)
{
string objName = obj.Name;
if (!String.IsNullOrEmpty(objName))
{
// find numeric part
int i = objName.Length - 1;
while (i > 0 && objName[i] >= '0' && objName[i] <= '9')
{
i--;
}
if (i >= 0 && i < objName.Length - 1)
{
// get number
string baseName = objName.Substring(0, i + 1);
int num;
int.TryParse(objName.Substring(i + 1), out num);
if (baseNames.ContainsKey(baseName))
{
int maxNum = (int)baseNames[baseName];
if (num < maxNum)
num = maxNum;
}
baseNames[baseName] = num;
}
else if (!baseNames.ContainsKey(objName))
{
baseNames[objName] = 0;
}
}
}
}
}
}