Get extension code to only fire when extension is checked

861
3
06-14-2010 12:49 PM
CarlosPiccirillo
New Contributor III
Hi everyone,

I've been struggling with creating my first extension for over a week now and have made some progress but now I am seeing behavior that I don't quite understand.

I'm using Visual Studio 2008 with C#. I used the ESRI ApplicationExtension template to create my extension and followed the AcmeExt ESRI example.

The code below is the State snippet from IExentsionConfig exactly as it comes in when you first create an extension from the template. The only code I have added are the two message boxes so I could see when things fired. I originally had more code in there listening to the AddItem event but stripped it down to the minimum for simplicity of posting here.

Much to my surprise, the Disabled message box (esriExtensionState.esriESDisabled) pops up now every time I open ArcMap and it does not matter if my extension is checked on or not in the extensions dialog. Is this normal behavior? I thought this code would only fire when my extension is either turned on or off.

My extension is going to be deployed on a Citrix server for everyone in our organization to use (1700+ people) and I don't want my code firing every time ArcMap opens, only when my extension is checked on.

Sorry if this is a dumb question, but extensions have always been a bit of a mystery to me and my total experience with trying to write one is only one week plus long. I've been pouring through the forums but have not found an answer.

Thanks,
Carlos



       public esriExtensionState State
        {
            get
            {
                return m_enableState;
            }
            set
            {
                if (m_enableState != 0 && value == m_enableState)
                    return;

                //Check if ok to enable or disable extension
                esriExtensionState requestState = value;
                if (requestState == esriExtensionState.esriESEnabled)
                {
                    //Cannot enable if it's already in unavailable state
                    if (m_enableState == esriExtensionState.esriESUnavailable)
                    {
                        throw new COMException("Cannot enable extension");
                    }

                    //Determine if state can be changed
                    esriExtensionState checkState = StateCheck(true);
                    m_enableState = checkState;

                    MessageBox.Show("Enabled");
                }
                else if (requestState == 0 || requestState == esriExtensionState.esriESDisabled)
                {
                    //Determine if state can be changed
                    esriExtensionState checkState = StateCheck(false);
                    if (checkState != m_enableState)
                        m_enableState = checkState;

                    MessageBox.Show("Disabled");
                }
            }
        }
0 Kudos
3 Replies
KirkKuykendall
Occasional Contributor III
Did you look at IApplicationStatus.Initialized ?  Maybe that is false when called the first time.

Also, you might consider deploying it as a JITExtension.
http://edndoc.esri.com/arcobjects/9.2/NET/06b87b98-6477-4036-85f3-ef27caa00bb0.htm#JIT
0 Kudos
CarlosPiccirillo
New Contributor III
Kirk,

Thanks for the reply. You are correct, as far as I can tel, IApplicationStatus.Initialized is indeed false in the startup method. How do I correct this?

I am using the AcmeExt (Applying user interface customizations at startup) ESRI example along with the default code that comes with the ApplicationExtension template and I must admit I don't understand some of it but figured if it is in their example, I should not mess with it.

Last night as a test, I installed the ESRI example, put message boxes in the same locations as in my code and when I started ArcMap, I got the same result, message box displaying in the disabled code eventhough I had not yet turned on the extension. I'm beginning to think that the problem might be with the example.

Anyhow, below is my entire extension as it currently stands. Can you please take a look at it and see if you can tell what the problem is and how I can fix it?

I will look at JIT extension and see if that might work better for me. Hopefully I can find some examples of those.

Thanks a lot!
Carlos

namespace RegGSS
{
    [Guid("21fee823-6e9a-43af-a452-f5c8678b6e99")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("RegGSS.Extension")]
    public class Extension : IExtension, IExtensionConfig, IPersistVariant
    {
        private IApplication m_application;
        private IMxDocument m_mxDocument;
        private esriExtensionState m_enableState;
        private IApplicationStatus m_appStatus;
        private IApplicationStatusEvents_Event m_mapStatusEvents;
        private IActiveViewEvents_Event m_mapEvents;

        #region IExtension Members

        /// <summary>
        /// Name of extension. Do not exceed 31 characters
        /// </summary>
        public string Name
        {
            get
            {
                //TODO: Modify string to uniquely identify extension
                return "RegGSS Extension";
            }
        }

        public void Shutdown()
        {
            //TODO: Clean up resources

            m_application = null;
            //m_docEvents = null;
        }

        public void Startup(ref object initializationData)
        {
            m_application = initializationData as IApplication;
            if (m_application == null)
                return;

            //TODO: Add code to initialize the extension
            m_appStatus = m_application as IApplicationStatus;
        }

        #endregion

        #region IExtensionConfig Members

        public string Description
        {
            get
            {
                //TODO: Replace description (use \r\n for line break)
                return "RegGSS 9.3\r\n" +
                       "Copyright © SFWMD 2010\r\n\r\n" +
                       "Environmental Resource Regulation\r\n" +
                       "Regulatory Support - GIS Section";
            }
        }

        /// <summary>
        /// Friendly name shown in the Extension dialog
        /// </summary>
        public string ProductName
        {
            get
            {
                //TODO: Replace
                return "RegGSS";
            }
        }

        public esriExtensionState State
        {
            get
            {
                return m_enableState;
            }
            set
            {
                if (m_enableState != 0 && value == m_enableState)
                    return;

                //Check if ok to enable or disable extension
                esriExtensionState requestState = value;
                if (requestState == esriExtensionState.esriESEnabled)
                {
                    //Cannot enable if it's already in unavailable state
                    if (m_enableState == esriExtensionState.esriESUnavailable)
                    {
                        throw new COMException("Cannot enable extension");
                    }

                    //Determine if state can be changed
                    esriExtensionState checkState = StateCheck(true);
                    m_enableState = checkState;

                    MessageBox.Show("Enabled");

                    CheckToolbarValidity();

                    SetupEvents();
                }
                else if (requestState == 0 || requestState == esriExtensionState.esriESDisabled)
                {
                    //Determine if state can be changed
                    esriExtensionState checkState = StateCheck(false);
                    if (checkState != m_enableState)
                        m_enableState = checkState;

                    MessageBox.Show("Disabled");

                    UnloadCustomizations();

                    //Remove event
                    m_mapEvents.ItemAdded -= new IActiveViewEvents_ItemAddedEventHandler(OnItemAdded);
                }
            }
        }

        #endregion

        /// <summary>
        /// Determine extension state 
        /// </summary>
        /// <param name="requestEnable">true if to enable; false to disable</param>
        private esriExtensionState StateCheck(bool requestEnable)
        {
            //TODO: Replace with advanced extension state checking if needed
            //Turn on or off extension directly
            if (requestEnable)
                return esriExtensionState.esriESEnabled;
            else
                return esriExtensionState.esriESDisabled;
        }

        #region IPersistVariant Members

        public UID ID
        {
            get
            {
                UID typeID = new UIDClass();
                typeID.Value = GetType().GUID.ToString("B");
                return typeID;
            }
        }

        public void Load(IVariantStream Stream)
        {
            //TODO: Load persisted data from document stream

            Marshal.ReleaseComObject(Stream);
        }

        public void Save(IVariantStream Stream)
        {
            //TODO: Save extension related data to document stream

            Marshal.ReleaseComObject(Stream);
        }

        #endregion


        private void SetupEvents()
        {
            // Make sure we're dealing with ArcMap
            if (m_application.Document.Parent is IMxApplication)
            {
                m_mxDocument = m_application.Document as IMxDocument;
                m_mapEvents = m_mxDocument.FocusMap as IActiveViewEvents_Event;
                m_mapEvents.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(OnItemAdded);

                m_mapStatusEvents = m_application.Document.Parent as IApplicationStatusEvents_Event;
                m_mapStatusEvents.Initialized += new IApplicationStatusEvents_InitializedEventHandler(OnInitialized);
            }
        }

        // Called when the framework is fully initialized
        // After this event fires, it is safe to make UI customizations
        void OnInitialized()
        {
            CheckToolbarValidity();
            MessageBox.Show("Initialized");
        }

        void OnItemAdded(object Item)
        {
            MessageBox.Show("Item Added");

            IFeatureLayer pFeatureLayer;
            if (Item is IFeatureLayer)
            {
                pFeatureLayer = Item as IFeatureLayer;
                MessageBox.Show("Layer name: " + pFeatureLayer.Name);
            }
        }

        private void CheckToolbarValidity()
        {
            // Wait for framework to initialize before making ui customizations
            // Check framework initialization flag
            if (!m_appStatus.Initialized)
                return;

            // Make sure the extension is enabled
            if (m_enableState != esriExtensionState.esriESEnabled)
                return;

            // Perform the customization
            LoadCustomizations();
        }

        private void LoadCustomizations()
        {

        }

        private void UnloadCustomizations()
        {

        }

    }
}
0 Kudos
CarlosPiccirillo
New Contributor III
Kirk,

I solved it! The problem was with this line of code at the top of the extension:

private esriExtensionState m_enableState;

should be:

private esriExtensionState m_enableState = esriExtensionState.esriESDisabled;

I guess I must have copied/pasted wrong when I copied the example.

Thanks again for your help! It is always greatly appreciated!

Carlos
0 Kudos