Common SimpleTask
Common_SimpleTask_CSharpSimpleTask_CSharp\SimpleTaskWebConfigurator_CSharp.cs
// Copyright 2007 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Drawing.Design;
using System.ComponentModel;
using System.Collections;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;

namespace SimpleTask_CSharp
{
    public class SimpleTaskWebConfigurator_CSharp : ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl, IWebConfigurator, IBuddyControlSupport
    {
        private Button okButton = null;
        private Button cancelButton = null;
        private TextBox buttonText = null;
        private TextBox title = null;
        private ColorPicker colorPicker = null;

        public SimpleTaskWebConfigurator_CSharp()
            : base()
        {
        }

        #region private methods

        private string getDesignTimeTag()
        {
            string openTag = string.Format("<simpleTaskCS:SimpleTask_CSharp ID=\"{0}\" runat=\"server\" Style=\"z-index: 10000; left: 100px; position: absolute; top: 100px\" Width=\"200px\" Visible=\"False\" ButtonText=\"{1}\" Title=\"{2}\" ToolTip=\"{3}\" NavigationPath=\"{4}\" BackColor=\"{5}\">", TaskInstance.ID, TaskInstance.ButtonText, TaskInstance.Title, TaskInstance.ToolTip, TaskInstance.NavigationPath, System.Drawing.ColorTranslator.ToHtml(TaskInstance.BackColor));

            StringBuilder trcTag = new StringBuilder();
            trcTag.Append("<TaskResultsContainers>");
            trcTag.Append("<esri:BuddyControl Name=\"TaskResults1\" />");
            trcTag.Append("</TaskResultsContainers>");

            string endTag = "</simpleTaskCS:SimpleTask_CSharp>";

            StringBuilder tag = new StringBuilder();
            tag.Append(openTag);
            tag.Append(trcTag.ToString());
            tag.Append(endTag);

            return tag.ToString();
        }

        #endregion

        #region WebControl

        protected override void CreateChildControls()
        {
            Controls.Clear();

            title = new TextBox();
            title.ID = "title";

            buttonText = new TextBox();
            buttonText.Text = "Find";
            buttonText.ID = "buttonText";

            colorPicker = new ColorPicker();
            colorPicker.ID = "colorPicker";
            colorPicker.Font.Name = "Verdana";
            colorPicker.Font.Size = new FontUnit(new Unit(8, UnitType.Point));
            colorPicker.BackColor = System.Drawing.Color.White;
            colorPicker.DropDownBorderColor = System.Drawing.Color.Silver;
            colorPicker.DropDownBorderStyle = BorderStyle.Solid;
            colorPicker.DropDownBorderWidth = new Unit(1, UnitType.Pixel);
            colorPicker.ChosenColor = System.Drawing.Color.White;
            colorPicker.ShowColorNames = false;
            colorPicker.DisplayText = "Background color of the task";

            okButton = new Button();
            okButton.ID = "okButton";
            okButton.Text = "OK";
            okButton.Width = new Unit(75, UnitType.Pixel);
            okButton.Click += new EventHandler(okButton_Click);

            cancelButton = new Button();
            cancelButton.ID = "cancelButton";
            cancelButton.Text = "Cancel";
            cancelButton.Width = new Unit(75, UnitType.Pixel);
            cancelButton.Click += new EventHandler(cancelButton_Click);

            Controls.Add(title);
            Controls.Add(buttonText);
            Controls.Add(colorPicker);
            Controls.Add(okButton);
            Controls.Add(cancelButton);
        }

        void cancelButton_Click(object sender, EventArgs e)
        {
            OnWebConfigurationCancel(new EventArgs());
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (TaskInstance == null) return;
            TaskInstance.Title = title.Text;
            TaskInstance.ButtonText = buttonText.Text;
            TaskInstance.BackColor = colorPicker.ChosenColor;

            OnWebConfigurationComplete(new WebConfigurationCompleteEventArgs(TaskInstance, getDesignTimeTag()));
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!Page.IsCallback)
            {
                // load current properties of control to edit
                loadProperties();
            }
        }

        #endregion

        private void loadProperties()
        {
            if (TaskInstance == null) return;

            title.Text = TaskInstance.Title;
            buttonText.Text = TaskInstance.ButtonText;
            colorPicker.ChosenColor = TaskInstance.BackColor;
        }

        public override void Refresh()
        {
            loadProperties();
            base.Refresh();
        }

        #region IWebConfigurator Members

        private ControlCollection controls = null;
        private SimpleTask_CSharp TaskInstance = null;

        public ControlCollection AdditionalControls
        {
            get { return controls; }
            set { controls = value; }
        }

        public Control ControlToConfigure
        {
            get { return TaskInstance; }
            set
            {
                if (!(value is SimpleTask_CSharp))
                {
                    throw new ArgumentException();
                }
                TaskInstance = value as SimpleTask_CSharp;
            }
        }

        public bool ValidateResources(out string message)
        {
            message = null;
            return true;
        }

        private WebConfigurationCompleteEventHandler onWebConfigurationComplete;

        public event WebConfigurationCompleteEventHandler WebConfigurationCompleted
        {
            add { onWebConfigurationComplete += value; }
            remove { onWebConfigurationComplete -= value; }
        }

        protected virtual void OnWebConfigurationComplete(WebConfigurationCompleteEventArgs args)
        {
            if (onWebConfigurationComplete != null) onWebConfigurationComplete(this, args);
        }

        private WebConfigurationCanceledEventHandler onWebConfigurationCancel;

        public event WebConfigurationCanceledEventHandler WebConfigurationCanceled
        {
            add { onWebConfigurationCancel += value; }
            remove { onWebConfigurationCancel -= value; }
        }

        protected virtual void OnWebConfigurationCancel(EventArgs args)
        {
            if (onWebConfigurationCancel != null) onWebConfigurationCancel(this, args);
        }
        #endregion

        #region ICallbackEventHandler Members

        public override string GetCallbackResult()
        {
            NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
            return CallbackResults.ToString();
        }

        #endregion

        #region IBuddyControlSupport Members

        public Type[] GetSupportedBuddyControlTypes()
        {
            Type[] types = new Type[1];
            types[0] = typeof(SimpleTask_CSharp);
            return types;
        }

        #endregion
    }
}