/// <summary>
        /// Extension method to de-serialize the string into a viewpoint Map
        /// </summary>
        /// <param name="viewpointMap">viewpointMap object</param>
        /// <param name="xmlContent">xml content</param>
        /// <returns>populated viewpointMap object</returns>
        internal static ViewpointMap Deserialize(this ViewpointMap viewpointMap, string xmlContent)
        {
            if (viewpointMap != null)
            {
                using (var stringReader = new StringReader(xmlContent))
                {
                    try
                    {
                        var reader = XmlReader.Create(stringReader);
                        {
                            var serializer = new DataContractSerializer(typeof(ViewpointMap), Common.Constants.ViewpointMapRootName, Common.Constants.ViewpointMapXmlNamespace);
                            viewpointMap = (ViewpointMap)serializer.ReadObject(reader, true);
                        }
                    }
                    catch (ArgumentNullException ex)
                    {
                        Logger.LogException(ex);
                    }
                    catch (SerializationException ex)
                    {
                        Logger.LogException(ex);
                    }
                }
            }

            return(viewpointMap);
        }
        /// <summary>
        /// Extension method to serialize the Viewpoint Map
        /// </summary>
        /// <param name="viewpointMap">Viewpoint Map object</param>
        /// <returns>serialized string</returns>
        internal static string Serialize(this ViewpointMap viewpointMap)
        {
            StringBuilder serializedString = new StringBuilder();

            if (viewpointMap != null)
            {
                try
                {
                    using (var writer = XmlWriter.Create(serializedString))
                    {
                        var serializer = new DataContractSerializer(typeof(ViewpointMap), Common.Constants.ViewpointMapRootName, Common.Constants.ViewpointMapXmlNamespace);
                        if (writer != null)
                        {
                            serializer.WriteObject(writer, viewpointMap);
                        }
                    }
                }
                catch (ArgumentNullException ex)
                {
                    Logger.LogException(ex);
                }
                catch (InvalidDataContractException ex)
                {
                    Logger.LogException(ex);
                }
                catch (SerializationException ex)
                {
                    Logger.LogException(ex);
                }
            }

            return(serializedString.ToString());
        }
        /// <summary>
        /// Get ViewpointMap from Workbook
        /// </summary>
        /// <param name="workbook">workbook instance</param>
        /// <returns>ViewpointMap instance</returns>
        internal static ViewpointMap GetViewpointMap(this Workbook workbook)
        {
            // Initialize default
            var viewpointMap = new ViewpointMap(workbook);
            if (workbook != null)
            {
                string content = workbook.GetCustomXmlPart(Common.Constants.ViewpointMapXmlNamespace);
                if (!string.IsNullOrEmpty(content))
                {
                    viewpointMap = viewpointMap.Deserialize(content);
                    viewpointMap.Workbook = workbook;
                    if (viewpointMap.SerializablePerspective == null)
                    {
                        viewpointMap.SerializablePerspective = new ObservableCollection<Perspective>();
                    }
                }
            }

            return viewpointMap;
        }
Beispiel #4
0
        /// <summary>
        /// Get ViewpointMap from Workbook
        /// </summary>
        /// <param name="workbook">workbook instance</param>
        /// <returns>ViewpointMap instance</returns>
        internal static ViewpointMap GetViewpointMap(this Workbook workbook)
        {
            // Initialize default
            var viewpointMap = new ViewpointMap(workbook);

            if (workbook != null)
            {
                string content = workbook.GetCustomXmlPart(Common.Constants.ViewpointMapXmlNamespace);
                if (!string.IsNullOrEmpty(content))
                {
                    viewpointMap          = viewpointMap.Deserialize(content);
                    viewpointMap.Workbook = workbook;
                    if (viewpointMap.SerializablePerspective == null)
                    {
                        viewpointMap.SerializablePerspective = new ObservableCollection <Perspective>();
                    }
                }
            }

            return(viewpointMap);
        }
        public void DeserializeTest()
        {
            InteropExcel.Application excelApp = new InteropExcel.Application();

            try
            {
                InteropExcel.Workbook workbook = excelApp.ActiveWorkbook;
                ViewpointMap expected = new ViewpointMap(workbook);
                expected.SerializablePerspective = new System.Collections.ObjectModel.ObservableCollection<Perspective>();
                expected.SerializablePerspective.Add(new Perspective("Earth", "Earth", false, "2/25/2011 7:24:01 AM", "1", "9932 km", "SD8834DFA", "30.0", "30.0", "2.0", ".3", "1.5"));

                ViewpointMap viewpointMap = new ViewpointMap(workbook);
                string xmlContent = "<?xml version=\"1.0\" encoding=\"utf-16\"?><ViewpointMap xmlns:d1p1=\"http://schemas.datacontract.org/2004/07/Microsoft.Research.Wwt.Excel.Addin\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"Microsoft.Research.Wwt.Excel.Addin.ViewpointMap\"><d1p1:SerializablePerspective xmlns:d2p1=\"http://schemas.datacontract.org/2004/07/Microsoft.Research.Wwt.Excel.Common\"><d2p1:Perspective><d2p1:Declination i:nil=\"true\" /><d2p1:HasRADec>false</d2p1:HasRADec><d2p1:Latitude>2/25/2011 7:24:01 AM</d2p1:Latitude><d2p1:Longitude>1</d2p1:Longitude><d2p1:LookAngle>30.0</d2p1:LookAngle><d2p1:LookAt>Earth</d2p1:LookAt><d2p1:Name i:nil=\"true\" /><d2p1:ObservingTime>30.0</d2p1:ObservingTime><d2p1:ReferenceFrame>Earth</d2p1:ReferenceFrame><d2p1:RightAscention i:nil=\"true\" /><d2p1:Rotation>SD8834DFA</d2p1:Rotation><d2p1:TimeRate>2.0</d2p1:TimeRate><d2p1:ViewToken>1.5</d2p1:ViewToken><d2p1:Zoom>9932 km</d2p1:Zoom><d2p1:ZoomText>.3</d2p1:ZoomText></d2p1:Perspective></d1p1:SerializablePerspective></ViewpointMap>";

                ViewpointMap actual;
                actual = ViewpointMapExtensions.Deserialize(viewpointMap, xmlContent);
                Assert.AreEqual(expected.SerializablePerspective[0].Name, actual.SerializablePerspective[0].Name);
            }
            finally
            {
                excelApp.Close();
            }
        }
        /// <summary>
        /// This function is used to initialize the WorkflowController class.
        /// </summary>
        internal void Initialize(LayerManagerPane layerManagerPane, Ribbon ribbon)
        {
            if (ribbon != null)
            {
                this.layerManagerPaneInstance = layerManagerPane;
                this.ribbonInstance = ribbon;
                this.AttachRibbonEventHandlers();

                // Create WorkbookMap for Active workbook
                // Check if Active workbook has a BookMap stored. If yes de-serialize it. Else create a new map
                this.currentWorkbookMap = ThisAddIn.ExcelApplication.ActiveWorkbook.GetWorkbookMap();
                this.currentViewpointMap = ThisAddIn.ExcelApplication.ActiveWorkbook.GetViewpointMap();

                // Add this to the workbook map list
                this.workBookMaps.Add(this.currentWorkbookMap);
                this.viewpointMaps.Add(this.currentViewpointMap);

                this.ribbonInstance.BuildViewpointMenu(this.currentViewpointMap.SerializablePerspective);
                this.BuildAndBindLayerDetailsViewModel();

                // check for updates
                this.updateManager.CheckForUpdates();

                //When launched from installer, Excel is already up and running before the addIn loads. Events are already fired!
                //Functionality of LayerTaskPaneController does not work as expected. So we explicitly set the WorkflowController to refer to the LayerManagerPane.
                if(LayerTaskPaneController.IsExcelInstanceSDI)
                    if (ThisAddIn.ExcelApplication.Workbooks.Count == 1)    //During Initialization if count is 1, workbook is already opened. Other scenarios count is 0 during Initialization
                        UpdateLayerManagerPaneInstance(LayerTaskPaneController.Instance.CurrentPaneHost.LayerManagerPane);
            }
        }
        private void OnWorkbookOpen(Workbook workbook)
        {
            var workbookMap = this.workBookMaps.Find(item => item.Workbook == workbook);

            // This is for new condition else it is a reopen scenario
            if (workbookMap == null)
            {
                this.currentWorkbookMap = workbook.GetWorkbookMap();

                // Add this to the workbook map list
                this.workBookMaps.Add(this.currentWorkbookMap);
            }

            var viewpointMap = this.viewpointMaps.Find(item => item.Workbook == workbook);

            // This is for new condition else it is a reopen scenario
            if (viewpointMap == null)
            {
                this.currentViewpointMap = workbook.GetViewpointMap();

                // Add this to the viewpoint map list
                this.viewpointMaps.Add(this.currentViewpointMap);
            }

            if (this.ribbonInstance != null && this.currentWorkbookMap.LocalLayerMaps.Count > 0)
            {
                this.ribbonInstance.ViewCustomTaskPane(true);
            }

            // Sync up in case some workbook in excel was overwritten by open
            SyncWorkbookMapWithExcel();
            SyncViewpointMapWithExcel();
        }
        private void OnWorkbookActivate(Workbook workbook)
        {
            if (this.ribbonInstance != null)
            {
                // Enable the ribbon controls when we have one or more valid workbooks.
                // There could be scenario where excel is opening protected sheet.
                this.ribbonInstance.EnableRibbonControls(ThisAddIn.ExcelApplication.Workbooks.Count > 0);
            }

            var workbookMap = this.workBookMaps.Find(item => item.Workbook == workbook);
            if (workbookMap != null)
            {
                this.currentWorkbookMap = workbookMap;

                // No need to build the reference frame dropdown while workbook is getting activated.
                this.BuildAndBindLayerDetailsViewModel(false);
            }

            // Order in which EnableRibbonControls and BuildViewpointMenu are called is important
            var viewpointMap = this.viewpointMaps.Find(item => item.Workbook == workbook);
            if (viewpointMap != null && this.ribbonInstance != null)
            {
                this.currentViewpointMap = viewpointMap;
                this.ribbonInstance.BuildViewpointMenu(this.currentViewpointMap.SerializablePerspective);
            }

            // Update worksheet name.
            currentWorksheet = (Worksheet)ThisAddIn.ExcelApplication.ActiveSheet;
            worksheetName = currentWorksheet.Name;
        }
        private void OnWindowDeactivate(Workbook workbook, Window window)
        {
            // If the count is 1, then this workbook is last and is getting closed
            if (ThisAddIn.ExcelApplication.Workbooks.Count == 1)
            {
                if (this.ribbonInstance != null)
                {
                    // Disable all controls
                    this.ribbonInstance.EnableRibbonControls(false);
                }

                // Remove it from the list
                var workbookMap = this.workBookMaps.Find(item => item.Workbook == workbook);
                if (workbookMap != null)
                {
                    workbookMap.StopAllNotifications();
                    this.workBookMaps.Remove(workbookMap);
                }

                // Reset current workbook map as well
                this.currentWorkbookMap = null;

                // Remove it from the list
                var viewpointMap = this.viewpointMaps.Find(item => item.Workbook == workbook);
                if (viewpointMap != null)
                {
                    this.viewpointMaps.Remove(viewpointMap);
                }

                // Reset current workbook map as well
                this.currentWorkbookMap = null;
                this.currentViewpointMap = null;

                SyncWorkbookMapWithExcel();
                SyncViewpointMapWithExcel();

                if (this.ribbonInstance != null)
                {
                    this.ribbonInstance.ViewCustomTaskPane(false);
                }
            }
        }
        private void OnNewWorkbook(Workbook workbook)
        {
            // Create a new workbookMap and add it to the list
            this.currentWorkbookMap = workbook.GetWorkbookMap();

            this.currentViewpointMap = workbook.GetViewpointMap();

            // Add this to the workbook map list
            this.workBookMaps.Add(this.currentWorkbookMap);

            this.viewpointMaps.Add(this.currentViewpointMap);
        }