JD2022-TU1/main/tools/framework/JD.TeaBoxAddinSystem/Addins/Description/AddinObject.cs

127 lines
3 KiB
C#

using System;
using JD.Kernel.StaticHelpers;
using JD.Kernel.Structs;
using JD.LogService;
namespace JD.TeaBoxAddinSystem.Addins.Description
{
/// <summary>
/// An object that is owned by a particular module of an <see cref="Addin"/>.
/// </summary>
public abstract class AddinObject
{
#region Variables
private Module m_module;
private FileLocation? m_fileLocation;
#endregion
#region Properties
/// <summary>
/// Addin that owns this object. Can be null.
/// </summary>
public Addin Addin
{
get
{
if (Module == null)
{
return null;
}
return Module.GetAddin();
}
}
/// <summary>
/// Module that owns this object. Can be null.
/// </summary>
public Module Module
{
get
{
return m_module;
}
internal set
{
if (m_module != value)
{
Throw.InvalidOperationIf(m_module != null && value != null, "Addin object already belongs to another module.");
m_module = value;
OnModuleChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Location where this element is defined, if any.
/// </summary>
public FileLocation? FileLocation
{
get
{
return m_fileLocation;
}
set
{
ThrowIfLoaded();
m_fileLocation = value;
}
}
#endregion
#region Methods
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if loaded.
/// </summary>
internal void ThrowIfLoaded()
{
if (Addin != null)
{
Addin.ThrowIfLoaded();
}
}
/// <summary>
/// GetAddin.
/// </summary>
/// <returns></returns>
internal virtual Addin GetAddin()
{
return null;
}
/// <summary>
/// Called when the <see cref="Addin"/> that owns this object changes.
/// </summary>
/// <param name="e"></param>
internal virtual void OnModuleChanged(EventArgs e)
{
}
#region Logging
internal void LogError(string msg, params object[] args)
{
LogError(LogCategory.Error, msg, args);
}
internal void LogWarning(string msg, params object[] args)
{
LogError(LogCategory.Warning, msg, args);
}
private void LogError(LogCategory category, string msg, params object[] args)
{
AddinManager.Log(new Error(category, msg, args) { Origin = FileLocation.GetValueOrDefault() });
}
#endregion
#endregion
}
}