using System;
using System.Collections.Generic;
namespace FastReport.Utils
{
#if !COMMUNITY
///
/// Class for handling Exports visibility in the Preview control.
///
public partial class ExportsOptions
{
private static ExportsOptions instance = null;
private List menuNodes;
///
/// All exports available in the Preview control.
///
public List ExportsMenu
{
get
{
RemoveCloudsAndMessengersDuplicatesInMenuNodes();
return menuNodes;
}
}
private ExportsOptions()
{
menuNodes = new List();
}
private void RemoveCloudsAndMessengersDuplicatesInMenuNodes()
{
int last = menuNodes.Count - 1;
for (int i = last; i >= 0; i--)
{
ExportsTreeNode node = menuNodes[i];
if (node.Name == "Cloud" || node.Name == "Messengers")
{
menuNodes.Remove(node);
}
}
}
///
/// Gets an instance of ExportOptions.
///
///
public static ExportsOptions GetInstance()
{
if (instance == null)
instance = new ExportsOptions();
return instance;
}
///
/// Exports menu node.
///
public partial class ExportsTreeNode
{
private const string EXPORT_ITEM_PREFIX = "Export,";
private const string ITEM_POSTFIX = ",Name";
private const string EXPORT_ITEM_POSTFIX = ",File";
private const string CATEGORY_PREFIX = "Export,ExportGroups,";
private readonly string name;
private readonly List nodes = new List();
private readonly Type exportType = null;
private readonly int imageIndex = -1;
private ObjectInfo tag = null;
private bool enabled = true;
private readonly bool isExport;
///
/// Gets the name.
///
public string Name
{
get { return name; }
}
///
/// Gets nodes.
///
public List Nodes
{
get { return nodes; }
}
///
/// Gets type of the export.
///
public Type ExportType
{
get { return exportType; }
}
///
/// Gets index of the image.
///
public int ImageIndex
{
get { return imageIndex; }
}
///
/// Gets or sets the tag.
///
public ObjectInfo Tag
{
get { return tag; }
set { tag = value; }
}
///
/// Gets or sets a value that indicates is node enabled.
///
public bool Enabled
{
get { return enabled; }
set { enabled = value; }
}
///
/// Gets true if node is export, otherwise false.
///
public bool IsExport
{
get { return isExport; }
}
public ExportsTreeNode(string name, bool isExport)
{
this.name = name;
this.isExport = isExport;
}
public ExportsTreeNode(string name, Type exportType, bool isExport)
: this(name, isExport)
{
this.exportType = exportType;
}
public ExportsTreeNode(string name, Type exportType, bool isExport, ObjectInfo tag)
: this(name, exportType, isExport)
{
this.tag = tag;
}
public ExportsTreeNode(string name, int imageIndex, bool isExport)
: this(name, isExport)
{
this.imageIndex = imageIndex;
}
public ExportsTreeNode(string name, Type exportType, int imageIndex, bool isExport)
: this(name, exportType, isExport)
{
this.imageIndex = imageIndex;
}
public ExportsTreeNode(string name, Type exportType, int imageIndex, bool isExport, ObjectInfo tag)
: this(name, exportType, imageIndex, isExport)
{
this.tag = tag;
}
public ExportsTreeNode(string name, Type exportType, int imageIndex, bool enabled, bool isExport)
: this(name, exportType, imageIndex, isExport)
{
this.enabled = enabled;
}
///
public override bool Equals(object obj)
{
if (obj is ExportsTreeNode)
{
ExportsTreeNode Obj = obj as ExportsTreeNode;
bool equalNodes = true;
foreach (var node in Nodes)
equalNodes &= node.ContainsIn(Obj.Nodes);
return equalNodes && Obj.Name == Name && Obj.ImageIndex == ImageIndex && Obj.IsExport == isExport && Obj.ExportType == ExportType && Obj.Enabled == Enabled;
}
return base.Equals(obj);
}
///
/// If it is equal node or its contained in node childnodes, it returns true, otherwise false.
///
///
///
public bool ContainsIn(ExportsTreeNode node)
{
return this.Equals(node) || this.ContainsIn(node.Nodes);
}
///
/// If it is contained in the list or in its elements, it returns true, otherwise false.
///
///
///
public bool ContainsIn(List nodes)
{
foreach (ExportsTreeNode n in nodes)
if (this.Equals(n) || this.ContainsIn(n))
return true;
return false;
}
}
///
/// Saves current visible exports in config file.
///
public void SaveExportOptions()
{
SaveOptions();
}
///
/// Restores visible exports from config file.
///
public void RestoreExportOptions()
{
RestoreOptions();
}
///
///
///
public void RegisterExports()
{
Queue queue = new Queue(menuNodes);
while (queue.Count != 0)
{
ExportsTreeNode node = queue.Dequeue();
ObjectInfo tag = null;
if (node.ExportType != null)
{
tag = RegisteredObjects.AddExport(node.ExportType, node.ToString(), node.ImageIndex);
}
node.Tag = tag;
foreach (ExportsTreeNode nextNode in node.Nodes)
{
queue.Enqueue(nextNode);
}
}
}
private ExportsTreeNode FindItem(string name, Type exportType)
{
Queue queue = new Queue(menuNodes);
while (queue.Count != 0)
{
ExportsTreeNode node = queue.Dequeue();
if (exportType != null && node.ExportType == exportType ||
!string.IsNullOrEmpty(name) && node.Name == name)
{
return node;
}
foreach (ExportsTreeNode nextNode in node.Nodes)
{
queue.Enqueue(nextNode);
}
}
return null;
}
///
/// Sets Export category visibility.
///
/// Export category name.
/// Visibility state.
public void SetExportCategoryEnabled(string name, bool enabled)
{
FindItem(name, null).Enabled = enabled;
}
///
/// Sets Export visibility.
///
/// Export type.
/// Visibility state.
public void SetExportEnabled(Type exportType, bool enabled)
{
ExportsTreeNode node = FindItem(null, exportType);
if (node != null)
{
node.Enabled = enabled;
}
}
}
#endif
}