Common Extend tasks
Common_ExtendTasks_CSharp\App_Code\ExtendGPTask.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.
// 

namespace ExtendedTasks
{
    public class ExtendGPTask : ESRI.ArcGIS.ADF.Tasks.GeoprocessingTask
    {
        #region Instance Variable Declarations

        ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults m_taskResults = null;

        #endregion

        #region GeoprocessingTask Overrides

        public override string GetCallbackResult()
        {
            // Call the base task's GetCallbackResult method so the base task logic is executed
            base.GetCallbackResult();

            foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality commonMapFunctionality in 
                TaskResultsInstance.MapInstance.GetFunctionalities())
            {
                // Check whether the current resource contains the task's point results
                if ((commonMapFunctionality.Resource.Name == TaskResultsInstance.ClientID + " Point Results"))
                {
                    // Get a reference to the resource containing the results
                    ESRI.ArcGIS.ADF.Web.DataSources.IMapResource commonMapResource = 
                        (ESRI.ArcGIS.ADF.Web.DataSources.IMapResource)commonMapFunctionality.Resource;
                    
                    // Change the transparency of the point results by altering the transparency of 
                    // the Web ADF Map Resource Item.  For GraphicsLayers rendered in the web tier
                    // (as GP Task Results are by default), this property acts as a transparency limit -
                    // GraphicsLayers can be more transparent than this, but not less.  For 
                    // GraphicsLayers rendered on the client, this setting has no effect
                    commonMapResource.DisplaySettings.Transparency = 70;
                    TaskResultsInstance.MapInstance.RefreshResource(commonMapResource.Name);

                    // Add the map's callback results to those of the base task, since we refreshed a
                    // map resource
                    base.CallbackResults.CopyFrom(TaskResultsInstance.MapInstance.CallbackResults);
                }
            }

            return base.CallbackResults.ToString();
        }

        public override void ExecuteTask()
        {
            base.ExecuteTask();

            if (Results is ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode)
            {
                // Set the TaskResults' GraphicsTransparency to zero to allow client-side results to be rendered as 
                // completely opaque.  When explicitly setting transparency on GraphicsLayers that are rendered 
                // client-side (as in SetPolygonNodeRenderers, below), this property acts as transparency limit - 
                // GraphicsLayers can be more transparent than this, but not less.  For GraphicsLayers rendered in 
                // the web tier, this setting has no effect.
                this.TaskResultsInstance.GraphicsTransparency = 0;

                ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode rootResultsTreeViewPlusNode = 
                    (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode)Results;
                SetPolygonNodeRenderers(rootResultsTreeViewPlusNode, true);
            }
        }

        #endregion

        #region Instance Properties and Methods

        // Convenient access to the first TaskResults control in the Task's TaskResultsContainers collection
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults TaskResultsInstance
        {
            get
            {
                // Retrieve the TaskResults control if it has not already been
                if ((m_taskResults == null) && (this.TaskResultsContainers[0] != null))
                    m_taskResults = Utility.FindControl(TaskResultsContainers[0].Name, Page)
                        as ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults;

                return m_taskResults;
            }
        }

        private void SetPolygonNodeRenderers(ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNode, 
            bool recursive)
        {
            // Only manipulate GraphicsLayerNodes
            if (treeViewPlusNode is ESRI.ArcGIS.ADF.Web.UI.WebControls.GraphicsLayerNode)
            {
                // Get a reference to the feature graphics layer corresponding to the node
                ESRI.ArcGIS.ADF.Web.UI.WebControls.GraphicsLayerNode graphicsLayerNode =
                    treeViewPlusNode as ESRI.ArcGIS.ADF.Web.UI.WebControls.GraphicsLayerNode;
                ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer =
                    graphicsLayerNode.Layer as ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer;
                
                // Task results have a selected renderer, but neither a highlight renderer nor a default renderer by 
                // default.  Here we check to make sure this is the case, and that the graphics layer has a polygon
                // geometry type - the code here only handles polygons.
                if ((featureGraphicsLayer.SelectedRenderer != null) && (featureGraphicsLayer.HighlightRenderer == null) &&
                    (featureGraphicsLayer.Renderer == null) && (featureGraphicsLayer.FeatureType == 
                    ESRI.ArcGIS.ADF.Web.FeatureType.Polygon))
                {
                    // Get the selected renderer
                    ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer adfSelectedRenderer = 
                        featureGraphicsLayer.SelectedRenderer as ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer;

                    // Create a highlight renderer
                    ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol adfHighlightFillSymbol =
                        adfSelectedRenderer.Symbol.Clone() as ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol;
                    adfHighlightFillSymbol.Color = System.Drawing.Color.Red;
                    adfHighlightFillSymbol.Transparency = 25;                     
                    ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer adfHighlightRenderer = 
                        new ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer(adfHighlightFillSymbol);  
                    featureGraphicsLayer.HighlightRenderer = adfHighlightRenderer;
                    
                    // Create a default renderer
                    ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol adfDefaultFillSymbol =
                        adfSelectedRenderer.Symbol.Clone() as ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol;
                    adfDefaultFillSymbol.Color = System.Drawing.Color.Purple;
                    adfDefaultFillSymbol.Transparency = 25;
                    ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer adfDefaultRenderer =
                        new ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer(adfDefaultFillSymbol); 
                    featureGraphicsLayer.Renderer = adfDefaultRenderer;

                    // De-select each row in the graphics layer.  Otherwise, the symbology specified by the selected
                    // renderer would be used
                    foreach (System.Data.DataRow dataRow in featureGraphicsLayer.Rows)
                    {
                        if (featureGraphicsLayer.IsSelected(dataRow))
                            dataRow[featureGraphicsLayer.IsSelectedColumn] = false;
                    }

                    // Specify that the layer be rendered on the client so the highlight symbology shows when polygon
                    // results are moused over, and that callouts are disabled so maptips do not appear
                    featureGraphicsLayer.RenderOnClient = true;
                    featureGraphicsLayer.EnableCallout = false;
                }
            }

            // Set up renderers for child nodes if the function call specified recursion
            if (recursive && (treeViewPlusNode.Nodes.Count > 0))
            {
                foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode childTreeViewPlusNode in
                    treeViewPlusNode.Nodes)
                {
                    SetPolygonNodeRenderers(childTreeViewPlusNode, true);
                }
            }
        }

        #endregion
    }
}