using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.Design;
using Mullivan.Web.UI.WebControls;
using System.Globalization;
using System.Web.UI;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Design;
using System.Security.Permissions;
namespace Mullivan.Web.UI.Design.WebControls
{
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class JQueryTabViewDesigner : System.Web.UI.Design.ControlDesigner
{
// Fields
private const string AddTabName = "#addtab";
private const string ClickRegionHtml = "<div style='float:left;padding-left:3px;padding-right:3px;color:{3}; background-color:{4};{5};height:20px;' {0}='{1}'>{2}</div>";
private const string DesignTimeHtml = @"
<div style=""padding:2px;width:{4};height:{5}"">
<div style='color:{0}; background-color:{1};border-left:thin white outset; border-right:thin white outset;height:20px;text-align:left;'>
{2}
</div>
<div style='clear:both;text-align:left;border-left:thin white outset; border-bottom:thin white outset; border-right:thin white outset;background-color:{7};height:{8};' {6}='{9}'>
{3}
</div>
</div>";
private const string EmptyDesignTimeHtml = @"
<div style=""padding:2px;width:{2};height:{3}"">
<div style='color:{0}; background-color:{1};border-left:thin white outset; border-right:thin white outset;height:20px;text-align:left;'>
Add New Tab
</div>
</div>";
private JQueryTabView _tabView = null;
// Methods
public override string GetDesignTimeHtml(DesignerRegionCollection regions)
{
if (regions == null)
{
throw new ArgumentNullException("regions");
}
if (this.TabView.Tabs.Count > 0
&& (this.TabView.ActiveTabIndex < 0
|| this.TabView.ActiveTabIndex >= this.TabView.Tabs.Count))
{
this.TabView.ActiveTabIndex = 0;
}
if (this.TabView.ActiveTab != null)
{
StringBuilder sbTabs = new StringBuilder();
foreach (JQueryTab panel in this.TabView.Tabs)
{
bool active = panel.IsActive;
int index = panel.Position;
DesignerRegion dr = new DesignerRegion(this,
string.Format(CultureInfo.InvariantCulture, "t{0}", new object[] { index }));
dr.Selectable = !active;
dr.Selected = dr.Highlight = active;
regions.Add(dr);
string strHeader = panel.Name;
sbTabs.AppendFormat(CultureInfo.InvariantCulture,
ClickRegionHtml,
new object[] {
DesignerRegion.DesignerRegionAttributeName,
index,
strHeader,
ColorTranslator.ToHtml(SystemColors.ControlText),
active ? "transparent" : ColorTranslator.ToHtml(SystemColors.Window),
active ? "border-top:thin white outset;border-left:thin white outset;border-right:thin white outset;" : "" });
}
string tabContent = this.GetTabContent(this.TabView.ActiveTab);
EditableDesignerRegion edr = new EditableDesignerRegion(this, string.Format(CultureInfo.InvariantCulture, "c"));
int editIndex = regions.Add(edr);
StringBuilder sbTabView = new StringBuilder(0x400);
sbTabView.Append(
string.Format(CultureInfo.InvariantCulture
, DesignTimeHtml
, new object[] {
ColorTranslator.ToHtml(SystemColors.ControlText),
ColorTranslator.ToHtml(SystemColors.Control),
sbTabs.ToString(),
tabContent,
this.TabView.Width,
this.TabView.Height,
DesignerRegion.DesignerRegionAttributeName,
ColorTranslator.ToHtml(SystemColors.Window),
(this.TabView.Height.Value - 20.0).ToString() + "px",
editIndex}
));
return sbTabView.ToString();
}
StringBuilder sbEmpty = new StringBuilder(0x200);
sbEmpty.AppendFormat(CultureInfo.InvariantCulture,
EmptyDesignTimeHtml,
new object[] {
ColorTranslator.ToHtml(SystemColors.ControlText),
ColorTranslator.ToHtml(SystemColors.Control),
this.TabView.Width,
this.TabView.Height });
DesignerRegion region3 = new DesignerRegion(this, AddTabName);
regions.Add(region3);
return sbEmpty.ToString();
}
public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
{
if (region == null)
{
throw new ArgumentNullException("region");
}
JQueryTab tab = this._tabView.ActiveTab;
return this.GetTabContent(tab);
}
private string GetTabContent(JQueryTab tab)
{
if (tab != null && tab.ContentTemplate != null)
return this.GetTemplateContent(tab.ContentTemplate, "_content");
return "";
}
private string GetTemplateContent(ITemplate template, string id)
{
DesignerPanel container = new DesignerPanel();
container.ID = id;
template.InstantiateIn(container);
IDesignerHost service = (IDesignerHost)this.GetService(typeof(IDesignerHost));
StringBuilder builder = new StringBuilder(0x400);
foreach (Control control in container.Controls)
{
builder.Append(ControlPersister.PersistControl(control, service));
}
return builder.ToString();
}
public override void Initialize(IComponent component)
{
this.VerifyInitializeArgument(component, typeof(JQueryTabView));
this._tabView = (JQueryTabView)component;
base.Initialize(component);
base.SetViewFlags(ViewFlags.TemplateEditing, true);
}
internal void VerifyInitializeArgument(IComponent component, Type expectedType)
{
if (!expectedType.IsInstanceOfType(component))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, string.Format("ControlDesigner Argument Must Be Of Type {0}"), new object[] { expectedType.FullName }), "component");
}
}
public void StartTabCollectionEditor()
{
IDesignerHost service = (IDesignerHost)this.GetService(typeof(IDesignerHost));
PropertyDescriptor propDesc = TypeDescriptor.GetProperties(base.Component)["Tabs"];
object val = propDesc.GetValue(_tabView);
using (DesignerTransaction transaction = service.CreateTransaction("Start Tab Collection Editor"))
{
UITypeEditor editor = (UITypeEditor)propDesc.GetEditor(typeof(UITypeEditor));
JQueryTabCollection result = (JQueryTabCollection)editor.EditValue(new TypeDescriptorContext(service, propDesc, base.Component),
new WindowsFormsEditorServiceHelper(this), val);
if (result != null)
{
Trace.WriteLine("Tab count: " + result.Count.ToString());
transaction.Commit();
}
}
}
private void OnAddTab()
{
IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
if (host != null)
{
JQueryTabView tabContainer = this.TabView;
using (DesignerTransaction transaction = host.CreateTransaction("Add New Tab"))
{
JQueryTab child = (JQueryTab)host.CreateComponent(typeof(JQueryTab));
if (child != null)
{
child.Name = child.ID;
IComponentChangeService service = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
try
{
service.OnComponentChanging(tabContainer, TypeDescriptor.GetProperties(tabContainer)["Tabs"]);
tabContainer.Tabs.Add(child);
}
finally
{
service.OnComponentChanged(tabContainer, TypeDescriptor.GetProperties(tabContainer)["Tabs"], tabContainer.Tabs, tabContainer.Tabs);
}
TypeDescriptor.GetProperties(tabContainer)["ActiveTab"].SetValue(tabContainer, child);
this.CurrentTabIndex = tabContainer.Tabs.Count - 1;
}
transaction.Commit();
}
}
}
protected override void OnClick(DesignerRegionMouseEventArgs e)
{
if ((e.Region != null) && e.Region.Name.StartsWith("t", StringComparison.Ordinal))
{
this.CurrentTabIndex = int.Parse(e.Region.Name.Substring(1));
}
else if ((e.Region != null) && (e.Region.Name == AddTabName))
{
this.OnAddTab();
}
base.OnClick(e);
}
private void OnRemoveTab()
{
JQueryTabView tabContainer = this.TabView;
if (tabContainer.ActiveTab != null)
{
int activeTabIndex = tabContainer.ActiveTabIndex;
IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
if (host != null)
{
using (DesignerTransaction transaction = host.CreateTransaction("Remove Tab"))
{
JQueryTab activeTab = tabContainer.ActiveTab;
IComponentChangeService service = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
try
{
service.OnComponentChanging(tabContainer, TypeDescriptor.GetProperties(tabContainer)["Tabs"]);
tabContainer.Tabs.Remove(activeTab);
}
finally
{
service.OnComponentChanged(tabContainer, TypeDescriptor.GetProperties(tabContainer)["Tabs"], tabContainer.Tabs, tabContainer.Tabs);
}
activeTab.Dispose();
if (tabContainer.Tabs.Count > 0)
{
TypeDescriptor.GetProperties(tabContainer)["ActiveTabIndex"].SetValue(tabContainer, Math.Min(activeTabIndex, tabContainer.Tabs.Count - 1));
}
this.UpdateDesignTimeHtml();
transaction.Commit();
}
}
}
}
private static void PersistTemplate(JQueryTab panel, IDesignerHost host, ITemplate template, string propertyName)
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(panel)[propertyName];
using (DesignerTransaction transaction = host.CreateTransaction("Set Editable Designer Region Content"))
{
descriptor.SetValue(panel, template);
transaction.Commit();
}
}
private static void PersistTemplateContent(JQueryTab panel, IDesignerHost host, string content, string propertyName)
{
ITemplate template = ControlParser.ParseTemplate(host, content);
PersistTemplate(panel, host, template, propertyName);
}
public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
{
if (region == null)
{
throw new ArgumentNullException("region");
}
JQueryTab panel = this._tabView.ActiveTab;
if (panel == null)
return;
IDesignerHost service = (IDesignerHost)this.GetService(typeof(IDesignerHost));
PersistTemplateContent(panel, service, content, "ContentTemplate");
}
// Properties
public override DesignerActionListCollection ActionLists
{
get
{
DesignerActionListCollection lists = new DesignerActionListCollection();
lists.AddRange(base.ActionLists);
lists.Add(new JQueryTabViewDesignerActionList(this));
return lists;
}
}
private int CurrentTabIndex
{
set
{
JQueryTabView tabContainer = this.TabView;
if (value > -1 && value < tabContainer.Tabs.Count)
{
IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
using (DesignerTransaction transaction = host.CreateTransaction("Change Index"))
{
TypeDescriptor.GetProperties(tabContainer)["ActiveTabIndex"].SetValue(tabContainer, value);
transaction.Commit();
}
this.UpdateDesignTimeHtml();
}
else
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Can't find child tab '{0}'", new object[] { value }));
}
}
private JQueryTabView TabView
{
get
{
return _tabView;
}
}
protected override bool UsePreviewControl
{
get
{
return true;
}
}
// Nested Types
internal class DesignerPanel : Panel, INamingContainer
{
}
private class JQueryTabViewDesignerActionList : DesignerActionList
{
// Fields
private JQueryTabViewDesigner _designer;
public JQueryTabViewDesigner Designer
{
get
{
return _designer;
}
}
// Methods
public JQueryTabViewDesignerActionList(JQueryTabViewDesigner designer)
: base(designer.Component)
{
this._designer = designer;
}
public override DesignerActionItemCollection GetSortedActionItems()
{
DesignerActionItemCollection items = new DesignerActionItemCollection();
DesignerActionMethodItem item = new DesignerActionMethodItem(this, "StartTabCollectionEditor", "Add/Remove Tabs", true);
items.Add(item);
return items;
}
public void StartTabCollectionEditor()
{
try
{
this._designer.StartTabCollectionEditor();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}
public void OnAddTab()
{
try
{
this._designer.OnAddTab();
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
throw;
}
}
public void OnRemoveTab()
{
try
{
this._designer.OnRemoveTab();
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
throw;
}
}
}
}
}