Common Simple Server task
Common_SimpleServerTask_CSharp\SimpleServerTaskWebConfigurator_CSharp.cs
// Copyright 2010 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.Web.UI.WebControls;
namespace SimpleServerTask_CSharp
{
    // Specifies the simple task's configuration interface in ArcGIS Manager
    public class SimpleServerTaskWebConfigurator_CSharp : ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl, 
        ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator, 
        ESRI.ArcGIS.ADF.Web.UI.WebControls.IBuddyControlSupport
    {
        #region Instance Variable Declarations

        private System.Web.UI.WebControls.Button m_okButton = null;
        private System.Web.UI.WebControls.Button m_cancelButton = null;
        private System.Web.UI.WebControls.TextBox m_buttonTextBox = null;
        private System.Web.UI.WebControls.TextBox m_titleTextBox = null;
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.ColorPicker m_backgroundColorPicker = null;
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.ColorPicker m_titleColorPicker = null;
        private System.Web.UI.ControlCollection m_additionalControls = null;
        private SimpleServerTask_CSharp m_simpleTaskInstance = null;
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventHandler
            m_onWebConfigurationComplete;
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCanceledEventHandler
            m_onWebConfigurationCancel;

        #endregion

        #region Constructor

        public SimpleServerTaskWebConfigurator_CSharp(): base()
        {
        }

        #endregion

        #region Instance Members

        #region Utility Methods

        // Used by the WebConfigurationComplete event to retrieve the markup that is injected into the
        // page at design time.  In this implementation, this event is fired by clicking the OK button
        // on the configurator.
        private string getDesignTimeTag()
        {
            // Markup for the task's open tag, which includes the task's properties.  Note that some
            // hard-coded default values are included, while other values are populated dynamically
            // based on the task instance's properties.
            string simpleTaskOpenTag = string.Format("<simpleServerTaskCS:SimpleServerTask_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}\" TitleBarColor=\"{6}\" " +
                "BorderStyle=\"Solid\" BorderWidth=\"1px\" BorderColor=\"Black\">", 
                m_simpleTaskInstance.ID, m_simpleTaskInstance.ButtonText, m_simpleTaskInstance.Title, 
                m_simpleTaskInstance.ToolTip, m_simpleTaskInstance.NavigationPath, 
                System.Drawing.ColorTranslator.ToHtml(m_simpleTaskInstance.BackColor),
                System.Drawing.ColorTranslator.ToHtml(m_simpleTaskInstance.TitleBarColor));

            // Markup for inclusion of a default task results container with an id of "TaskResults1"
            System.Text.StringBuilder taskResultsContainerTagStringBuilder = new System.Text.StringBuilder();
            taskResultsContainerTagStringBuilder.Append("<TaskResultsContainers>");
            taskResultsContainerTagStringBuilder.Append("<esri:BuddyControl Name=\"TaskResults1\" />");
            taskResultsContainerTagStringBuilder.Append("</TaskResultsContainers>");

            // Simple task close tag
            string simpleTaskCloseTag = "</simpleServerTaskCS:SimpleServerTask_CSharp>";

            // A string builder is used to concatenate the simple task open tag, task results container
            // tag, and the simple task close tag
            System.Text.StringBuilder completeTagStringBuilder = new System.Text.StringBuilder();
            completeTagStringBuilder.Append(simpleTaskOpenTag);
            completeTagStringBuilder.Append(taskResultsContainerTagStringBuilder.ToString());
            completeTagStringBuilder.Append(simpleTaskCloseTag);

            return completeTagStringBuilder.ToString();
        }

        // Copies properties from the task instance to the appropriate configurator web controls.  Called
        // when the web configurator interface is loaded.
        private void loadProperties()
        {
            if (m_simpleTaskInstance == null) return;

            m_titleTextBox.Text = m_simpleTaskInstance.Title;
            m_buttonTextBox.Text = m_simpleTaskInstance.ButtonText;
            m_backgroundColorPicker.ChosenColor = m_simpleTaskInstance.BackColor;
            m_titleColorPicker.ChosenColor = m_simpleTaskInstance.TitleBarColor;
        }

        #endregion

        #region UI Events

        // Invokes WebConfigurationCanceled event when the cancel button is clicked
        void cancelButton_Click(object sender, System.EventArgs e)
        {
            OnWebConfigurationCancel(new System.EventArgs());
        }

        // Saves user-specified properties to the task instance
        private void okButton_Click(object sender, System.EventArgs e)
        {
            if (m_simpleTaskInstance == null) return;
            m_simpleTaskInstance.Title = m_titleTextBox.Text;
            m_simpleTaskInstance.ButtonText = m_buttonTextBox.Text;
            m_simpleTaskInstance.BackColor = m_backgroundColorPicker.ChosenColor;
            m_simpleTaskInstance.TitleBarColor = m_titleColorPicker.ChosenColor;
            
            OnWebConfigurationComplete(
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventArgs(m_simpleTaskInstance,
                getDesignTimeTag()));
        }

        #endregion

        #region Overridable Configurator Events

        // Overridable wrapper for the WebConfigurationComplete event
        protected virtual void OnWebConfigurationComplete(
            ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventArgs args)
        {
            if (m_onWebConfigurationComplete != null) m_onWebConfigurationComplete(this, args);
        }

        // Overridable wrapper for the WebConfigurationCancel event
        protected virtual void OnWebConfigurationCancel(System.EventArgs args)
        {
            if (m_onWebConfigurationCancel != null) m_onWebConfigurationCancel(this, args);
        }

        #endregion

        #endregion

        #region WebControl Members

        #region WebControl Life Cycle Event Overrides

        // Configures the web configurator's interface
        protected override void CreateChildControls()
        {
            Controls.Clear();

            // Create a table to hold the controls that will appear on the interface
            Table taskConfiguratorTable = new Table();
            taskConfiguratorTable.Style[System.Web.UI.HtmlTextWriterStyle.Position] = "relative";
            Controls.Add(taskConfiguratorTable);

            // Initialize the title label
            Label titleTextLabel = new Label();
            titleTextLabel.ID = "lblTitle";
            titleTextLabel.Text = "Title:";

            // Add the title label to a table cell
            TableCell taskConfiguratorCell = new TableCell();
            taskConfiguratorCell.Controls.Add(titleTextLabel);

            // Add the table cell to a table row
            TableRow taskConfiguratorRow =new TableRow();
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell);

            // Initialize the title textbox
            m_titleTextBox = new TextBox();
            m_titleTextBox.ID = "txtTitle";

            // Add the title textbox to a table cell and the table cell to a table row
            taskConfiguratorCell = new TableCell();
            taskConfiguratorCell.Controls.Add(m_titleTextBox);
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell);

            // Add the row containing the title label and textbox to the table
            taskConfiguratorTable.Controls.Add(taskConfiguratorRow);

            // Initialize the button text label
            Label buttonTextLabel = new Label();
            buttonTextLabel.ID = "lblButtonText";
            buttonTextLabel.Text = "Button Text:";
            buttonTextLabel.Style[System.Web.UI.HtmlTextWriterStyle.WhiteSpace] = "nowrap";

            // Add the button text label to a table cell and the table cell to a table row
            taskConfiguratorCell = new TableCell();
            taskConfiguratorCell.Controls.Add(buttonTextLabel);
            taskConfiguratorRow = new TableRow();
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell);

            // Initialize the button text textbox
            m_buttonTextBox = new TextBox();
            m_buttonTextBox.Text = "Execute";
            m_buttonTextBox.ID = "txtButton";

            // Add the button text textbox to a table cell and the cell to a table row
            taskConfiguratorCell = new TableCell();
            taskConfiguratorCell.Controls.Add(m_buttonTextBox);
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell);

            // Add the row containing the button text label and textbox to the table
            taskConfiguratorTable.Controls.Add(taskConfiguratorRow);

            // Initialize the title bar color picker
            m_titleColorPicker = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ColorPicker();
            m_titleColorPicker.ID = "clrPkrTitle";
            m_titleColorPicker.Font.Name = "Verdana";
            m_titleColorPicker.Font.Size = new FontUnit(
                new Unit(8, UnitType.Point));
            m_titleColorPicker.BackColor = System.Drawing.Color.White;
            m_titleColorPicker.DropDownBorderColor = System.Drawing.Color.Silver;
            m_titleColorPicker.DropDownBorderStyle = BorderStyle.Solid;
            m_titleColorPicker.DropDownBorderWidth = new Unit(1,UnitType.Pixel);
            m_titleColorPicker.ChosenColor = System.Drawing.Color.White;
            m_titleColorPicker.ShowColorNames = false;
            m_titleColorPicker.DisplayText = "Title Bar Color:";

            // Add the title color picker to the web configurator's control's collection.  Note that,
            // for formatting purposes, we do not add the color picker to the table
            Controls.Add(m_titleColorPicker);

            // Initialize the background color picker and add it to the web configurator's controls
            m_backgroundColorPicker = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ColorPicker();
            m_backgroundColorPicker.ID = "clrPkrBackground";
            m_backgroundColorPicker.Font.Name = "Verdana";
            m_backgroundColorPicker.Font.Size = new FontUnit(new Unit(8, UnitType.Point));
            m_backgroundColorPicker.BackColor = System.Drawing.Color.White;
            m_backgroundColorPicker.DropDownBorderColor = System.Drawing.Color.Silver;
            m_backgroundColorPicker.DropDownBorderStyle = BorderStyle.Solid;
            m_backgroundColorPicker.DropDownBorderWidth = new Unit(1,UnitType.Pixel);
            m_backgroundColorPicker.ChosenColor = System.Drawing.Color.White;
            m_backgroundColorPicker.ShowColorNames = false;
            m_backgroundColorPicker.DisplayText = "Background Color:";
            Controls.Add(m_backgroundColorPicker);

            // Initialize the OK button
            m_okButton = new Button();
            m_okButton.ID = "btnOK";
            m_okButton.Text = "OK";
            m_okButton.Click += new System.EventHandler(okButton_Click);

            // Add the OK button to a table cell and the cell to a table row
            taskConfiguratorCell = new TableCell();
            taskConfiguratorCell.Controls.Add(m_okButton);
            taskConfiguratorCell.Style[System.Web.UI.HtmlTextWriterStyle.Width] = "50%";
            taskConfiguratorRow = new TableRow();
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell);

            // Initialize the cancel button
            m_cancelButton = new Button();
            m_cancelButton.ID = "btnCancel";
            m_cancelButton.Text = "Cancel";
            m_cancelButton.Click += new System.EventHandler(cancelButton_Click);

            // Add the cancel button to a table cell and the cell to a table row
            taskConfiguratorCell = new TableCell();
            taskConfiguratorCell.Controls.Add(m_cancelButton);
            taskConfiguratorCell.Style[System.Web.UI.HtmlTextWriterStyle.Width] = "50%";
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell);

            // Create a new table to hold the OK and cancel buttons
            taskConfiguratorTable = new Table();
            taskConfiguratorTable.Style[System.Web.UI.HtmlTextWriterStyle.Width] = "100%";
            taskConfiguratorTable.Style[System.Web.UI.HtmlTextWriterStyle.TextAlign] = "center";

            // Add the row containing the OK and cancel buttons to the new table
            taskConfiguratorTable.Controls.Add(taskConfiguratorRow);

            // Add the table to the web configurator's controls
            Controls.Add(taskConfiguratorTable);
        }

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

            // Make sure the configurator is not rendering asynchronously
            if (!base.IsAsync)
            {
                // load the current properties of the simple task
                loadProperties();
            }
        }

        #endregion

        #region WebControl Method Overrides

        public override void Refresh()
        {
            // load the current properties of the simple server task
            loadProperties();
            base.Refresh();
        }

        #endregion

        #region WebControl Property Overrides

        // Specify the task's tag as a div
        protected override System.Web.UI.HtmlTextWriterTag TagKey
        {
            get
            {
                return System.Web.UI.HtmlTextWriterTag.Div;
            }
        }

        #endregion

        #endregion

        #region IWebConfigurator Members

        #region IWebConfigurator Properties
        
        // Controls aside from the task and page that may be used by the configurator
        public System.Web.UI.ControlCollection AdditionalControls
        {
            get { return m_additionalControls; }
            set { m_additionalControls = value; }
        }

        // Task instance that the configurator will configure
        public System.Web.UI.Control ControlToConfigure
        {
            get { return m_simpleTaskInstance; }
            set
            {
                if (!(value is SimpleServerTask_CSharp))
                {
                    throw new System.ArgumentException();
                }
                m_simpleTaskInstance = value as SimpleServerTask_CSharp;
            }
        }

        #endregion

        #region IWebConfigurator Methods

        // Ensures the resources needed contain the necessary information or functionality.  No resources
        // are used by this task, so the implementation here is trivial.
        public bool ValidateResources(out string message)
        {
            message = null;
            return true;
        }

        #endregion 

        #region IWebConfigurator Events

        // Associates the internal WebConfigurationComplete event handler instance with the public 
        // event
        public event ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventHandler 
            WebConfigurationCompleted
        {
            add { m_onWebConfigurationComplete += value; }
            remove { m_onWebConfigurationComplete -= value; }
        }

        // Associates the internal WebConfigurationCanceled event handler instance with the public
        // event
        public event ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCanceledEventHandler 
            WebConfigurationCanceled
        {
            add { m_onWebConfigurationCancel += value; }
            remove { m_onWebConfigurationCancel -= value; }
        }

        #endregion

        #endregion

        #region IBuddyControlSupport Members

        // Determines what types of controls the control configured by the configurator can be
        // buddied with.  Here, no types other than the simple task can be buddied with, so the
        // implementation is trivial.
        public System.Type[] GetSupportedBuddyControlTypes()
        {
            System.Type[] types = new System.Type[1];
            types[0] = typeof(SimpleServerTask_CSharp);
            return types;
        }

        #endregion
    }
}