public class LibrariesIndexSettingsControl : CompositeControl, ISettingsControl
{
#region Properties
/// <summary>
/// Gets or sets the Control Template path
/// </summary>
public string ControlTemplatePath
{
get
{
object o = ViewState["ControlTemplatePath"];
if (o == null)
return "~/Sitefinity/Admin/ControlTemplates/Libraries/LibrariesIndexSettingsControl.ascx";
return (string)o;
}
set
{
ViewState["ControlTemplatePath"] = value;
}
}
/// <summary>
/// Gets or sets the control template
/// </summary>
public ITemplate ControlTemplate
{
get
{
if (controlTemplate == null)
controlTemplate = ControlUtils.GetTemplate<DefaultTemplate>(ControlTemplatePath);
return controlTemplate;
}
set
{
controlTemplate = value;
}
}
/// <summary>
/// Gets or Sets the name of the provider
/// </summary>
public string ProviderName
{
get
{
object o = ViewState["ProviderName"];
if (o == null)
return String.Empty;
return (string)o;
}
set
{
ViewState["ProviderName"] = value;
}
}
/// <summary>
/// Gets or Sets the page Url for displaying the news item
/// </summary>
public string SingleLibraryUrl
{
get
{
if (ctrlContainer.SingleLibraryUrl != null)
return ctrlContainer.SingleLibraryUrl.Text;
return string.Empty;
}
set
{
ctrlContainer.SingleLibraryUrl.Text = value;
}
}
#endregion
#region Inherited Methods
/// <summary>
/// Overriden. Cancels the rendering of a beginning HTML tag for the control.
/// </summary>
/// <param name="writer">The HtmlTextWriter object used to render the markup.</param>
public override void RenderBeginTag(HtmlTextWriter writer)
{
}
/// <summary>
/// Overriden. Cancels the rendering of an ending HTML tag for the control.
/// </summary>
/// <param name="writer">The HtmlTextWriter object used to render the markup.</param>
public override void RenderEndTag(HtmlTextWriter writer)
{
}
/// <summary>
/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation
/// to create any child controls they contain in preparation for posting back or rendering.
/// </summary>
protected override void CreateChildControls()
{
ctrlContainer = new ControlContainer(this);
ControlTemplate.InstantiateIn(ctrlContainer);
properties = TypeDescriptor.GetProperties(this);
PropertyDescriptor desc = properties.Find("SingleLibraryUrl", false);
editorDialog = new PropertyEditorDialog();
editorDialog.TypeContainer = this;
editorDialog.PropertyChanged += editorDialog_PropertyChanged;
Controls.Add(editorDialog);
string[] provNames = null;
int count = ConfigHelper.Handler.GenericContentProviders.Count;
if (count > 1)
{
int i = 0;
provNames = new string[count];
foreach (Telerik.Libraries.Configuration.GenericContentElement element in ConfigHelper.Handler.GenericContentProviders)
{
GlobalPermission perm = new GlobalPermission(
(GlobalPermissions)LibraryManager.SecurityRoots[element.ProviderName], CrudRights.View);
if (perm.CheckDemand())
{
provNames[i++] = element.ProviderName;
}
}
}
ListControl list = ctrlContainer.ProviderName;
if (provNames != null)
{
foreach (string name in provNames)
{
if (!String.IsNullOrEmpty(name))
{
ListItem item = new ListItem(name.Replace('_', ' '), name);
list.Items.Add(item);
}
}
}
ctrlContainer.ProviderNameLi.Visible = (list.Items.Count > 1);
ctrlContainer.SelectLibraryUrl.CommandName = "Telerik.Cms.Web.UI.PageIndexUrlWebEditor, Telerik.Cms";
ctrlContainer.SelectLibraryUrl.CommandArgument = desc.Name;
ctrlContainer.SelectLibraryUrl.Command += SelectSingleLibraryUrl_Command;
ctrlContainer.ProviderName.SelectedIndexChanged += ProviderName_SelectedIndexChanged;
if (settings != null && settings.Count > 0)
{
ctrlContainer.ProviderName.SelectedValue = ProviderName = settings["LibrariesIndexProvider"];
ctrlContainer.SingleLibraryUrl.Text = settings["ItemPageUrl"];
}
Controls.Add(ctrlContainer);
}
void ProviderName_SelectedIndexChanged(object sender, EventArgs e)
{
ProviderName = ((ListControl)sender).SelectedValue;
}
void SelectSingleLibraryUrl_Command(object sender, CommandEventArgs e)
{
object data;
string name = (string)e.CommandArgument;
PropertyDescriptor desc = properties.Find(name, false);
data = desc.Converter.ConvertToInvariantString(this);
editorDialog.Show(name, e.CommandName, data, this);
}
void editorDialog_PropertyChanged(object source, PropertyValueChangedEventArgs e)
{
string[] resultValue = ((string)e.PropertyValue).Split(';');
SetProperty(this, properties, e.PropertyName, resultValue[1]);
}
private void SetProperty(object component, PropertyDescriptorCollection _properties, string name, object value)
{
PropertyDescriptor desc = _properties.Find(name, false);
desc.SetValue(component, value);
}
/// <summary>
/// It initializes the _settings.
/// </summary>
/// <param name="_settings">
/// The parameter contains the default values when a new provider is created (in IRssProviderModule implementation)
/// If the provider has already been created, the parameter contains the values saved in the database.
/// </param>
public void InitSettings(IDictionary<string, string> _settings)
{
settings = _settings;
}
/// <summary>
/// Gets the newly edited settings.
/// </summary>
/// <returns> an object of type Dictionary that contains strings</returns>
public IDictionary<string, string> GetSettings()
{
settings = new Dictionary<string, string>();
settings["LibraryIndexProvider"] = ProviderName = ctrlContainer.ProviderName.SelectedValue;
settings["ItemPageUrl"] = ctrlContainer.SingleLibraryUrl.Text;
return settings;
}
#endregion
#region Private Fields
private PropertyDescriptorCollection properties;
private ITemplate controlTemplate;
private ControlContainer ctrlContainer;
private IDictionary<string, string> settings;
private PropertyEditorDialog editorDialog;
#endregion
#region DefaultTemplate
/// <summary>
/// The default template class
/// </summary>
protected class DefaultTemplate : ITemplate
{
/// <summary>
/// Instantiates the specified control in the default template
/// </summary>
/// <param name="container">accepts a parameter of type Control</param>
public void InstantiateIn(Control container)
{
}
}
#endregion
#region Container
/// <summary>
/// The control container class
/// </summary>
protected class ControlContainer : GenericContainer<LibrariesIndexSettingsControl>
{
/// <summary>
/// Constructs the control container
/// </summary>
/// <param name="owner">accepts a parameter of type RssSettingsControl</param>
public ControlContainer(LibrariesIndexSettingsControl owner)
: base(owner)
{
}
/// <summary>
/// Gets the control which displays the Provider Name.
/// </summary>
public ListControl ProviderName
{
get
{
if (providerName == null)
providerName = FindRequiredControl<ListControl>("librariesProvider");
return providerName;
}
}
/// <summary>
/// Gets the control which displays the single library url which could be edited.
/// </summary>
public IEditableTextControl SingleLibraryUrl
{
get
{
if (singleLibraryUrl == null)
singleLibraryUrl = (IEditableTextControl)FindControl(typeof(IEditableTextControl), "singleLibraryUrl", true);
return singleLibraryUrl;
}
}
/// <summary>
/// Gets the control which displays the post url in a link button which could be selected.
/// </summary>
public IButtonControl SelectLibraryUrl
{
get
{
if (selectLibraryUrl == null)
selectLibraryUrl = (IButtonControl)FindControl(typeof(IButtonControl), "selectSingleLibraryUrl", true);
return selectLibraryUrl;
}
}
/// <summary>
/// Gets the control which displays the "li" tag with information about the Provider Name
/// </summary>
public HtmlGenericControl ProviderNameLi
{
get
{
if (providerNameLi == null)
providerNameLi = FindRequiredControl<HtmlGenericControl>("providerNameLi");
return providerNameLi;
}
}
private HtmlGenericControl providerNameLi;
private IButtonControl selectLibraryUrl;
private ListControl providerName;
private IEditableTextControl singleLibraryUrl;
}
#endregion
}