Esempio n. 1
0
        /// <summary>
        /// This function is used to remove all layer which are not valid.
        ///     1. Layer map which does not have a valid named range
        /// </summary>
        /// <param name="workbook">
        /// workbook instance
        /// </param>
        /// <param name="workbookMap">
        /// workbook map instance
        /// </param>
        private static void CleanLayerMap(Workbook workbook, WorkbookMap workbookMap)
        {
            // Clean up Invalid Ranges.
            foreach (Name namedRange in workbook.Names)
            {
                // The named ranges are WWT ranges and are invalid
                if (namedRange.IsWWTRange() && !namedRange.IsValid())
                {
                    string name = namedRange.Name;
                    try
                    {
                        // Delete the range name for all invalid layers
                        workbookMap.AllLayerMaps.ForEach(layer =>
                        {
                            if (layer.RangeDisplayName.Equals(name))
                            {
                                layer.RangeName.Delete();
                            }
                        });
                    }
                    catch (COMException ex)
                    {
                        Logger.LogException(ex);
                    }

                    // Clean up such layers on load itself
                    workbookMap.AllLayerMaps.RemoveAll(layer =>
                    {
                        return(layer.RangeDisplayName.Equals(name));
                    });
                }
            }
        }
        /// <summary>
        /// Extension method to serialize the workbook map
        /// </summary>
        /// <param name="workbookMap">workbookMap object</param>
        /// <returns>serialized string</returns>
        internal static string Serialize(this WorkbookMap workbookMap)
        {
            StringBuilder serializedString = new StringBuilder();

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

            return(serializedString.ToString());
        }
        /// <summary>
        /// This function is used to Cleanup WWT layers from layer drop down when WWT is not running.
        /// This function performs the following operations:
        ///     1. Remove all WWT only layers.
        ///     2. Mark all Local IN WWT layer as not in sync with WWT.
        ///     3. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        internal static void CleanUpWWTLayers(this WorkbookMap workbookMap)
        {
            if (workbookMap != null)
            {
                // 1. Create en empty collection, since this method is called only when WWT is not running. Empty collection is needed
                //    to be passed for UpdateGroupStatus method.
                ICollection <Group> groups           = new List <Group>();
                LayerMap            selectedLayerMap = workbookMap.SelectedLayerMap;

                // 2. Remove all WWT only layers.
                workbookMap.RemoveWWTLayers();

                // 3. On Cleanup, no WWT layers are connected with WWT.
                workbookMap.LocalLayerMaps.ForEach(
                    layer =>
                {
                    // 3.a. Set IsNotInSync.
                    layer.IsNotInSync = true;

                    // 3.b. If the group (reference frame/ Layer Group) is deleted in WWT, then set the IsDeleted flag in Group to true.
                    UpdateGroupStatus(layer.LayerDetails.Group, groups);
                });

                // 4. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
                //  If the selected layer is WWT layer/is localInWWT which is not deleted, this will be selected while
                if (selectedLayerMap != null && selectedLayerMap.MapType == LayerMapType.WWT && !workbookMap.Exists(selectedLayerMap.LayerDetails.ID))
                {
                    workbookMap.SelectedLayerMap = null;
                }
            }
        }
        /// <summary>
        /// This function is used to sync WWT layers into the workbook instance and
        ///     then set the flag IsNotInSync for the layer deleted in WWT.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        /// <param name="groups">
        /// Collection of WWT groups.
        /// </param>
        private static void SyncWWTLayers(this WorkbookMap workbookMap, ICollection <Group> groups)
        {
            if (workbookMap != null)
            {
                // 1. Append all WWT layer which are not there in the LocalLayers.
                foreach (Group group in groups)
                {
                    AddWWTLayers(workbookMap, group);
                }

                // 2. Clean up all local layer in the current workbook map.
                workbookMap.LocalLayerMaps.ForEach(
                    layer =>
                {
                    // 2.a. If the local layer is deleted in WWT, then set the IsNotInSync flag to true.
                    if (!WWTManager.IsValidLayer(layer.LayerDetails.ID, groups))
                    {
                        layer.IsNotInSync = true;
                    }

                    // 2.b. If the group (reference frame/ Layer Group) is deleted in WWT, then set the IsDeleted flag in Group to true.
                    UpdateGroupStatus(layer.LayerDetails.Group, groups);
                });
            }
        }
        /// <summary>
        /// Extension method to de-serialize the string into a workbook map
        /// </summary>
        /// <param name="workbookMap">workbookMap object</param>
        /// <param name="xmlContent">xml content</param>
        /// <returns>populated workbookMap object</returns>
        internal static WorkbookMap Deserialize(this WorkbookMap workbookMap, string xmlContent)
        {
            if (workbookMap != null)
            {
                using (var stringReader = new StringReader(xmlContent))
                {
                    try
                    {
                        var reader = XmlReader.Create(stringReader);
                        {
                            var serializer = new DataContractSerializer(typeof(WorkbookMap), Common.Constants.XmlRootName, Common.Constants.XmlNamespace);
                            workbookMap = (WorkbookMap)serializer.ReadObject(reader, true);
                        }
                    }
                    catch (ArgumentNullException ex)
                    {
                        Logger.LogException(ex);
                    }
                    catch (SerializationException ex)
                    {
                        Logger.LogException(ex);
                    }
                }
            }

            return(workbookMap);
        }
 /// <summary>
 /// This function is used to check if the layer with the specified ID is present in the workbook.
 /// </summary>
 /// <param name="workbookMap">
 /// Parent container.
 /// </param>
 /// <param name="layerID">
 /// Id of the Layer in focus.
 /// </param>
 /// <returns>
 /// True if the LayerId is part of workbook collection;Otherwise false.
 /// </returns>
 internal static bool Exists(this WorkbookMap workbookMap, string layerID)
 {
     return(workbookMap != null && workbookMap.AllLayerMaps.Exists(
                layer =>
     {
         return string.CompareOrdinal(layerID, layer.LayerDetails.ID) == 0;
     }));
 }
Esempio n. 7
0
        /// <summary>
        /// Get WorkbookMap from Workbook
        /// </summary>
        /// <param name="workbook">workbook instance</param>
        /// <returns>WorkbookMap instance</returns>
        internal static WorkbookMap GetWorkbookMap(this Workbook workbook)
        {
            // Initialize default
            var workbookMap = new WorkbookMap(workbook);

            if (workbook != null)
            {
                string content = workbook.GetCustomXmlPart(Common.Constants.XmlNamespace);
                if (!string.IsNullOrEmpty(content))
                {
                    workbookMap = workbookMap.Deserialize(content);

                    workbookMap.Workbook = workbook;
                    if (workbookMap.SerializableLayerMaps == null)
                    {
                        workbookMap.SerializableLayerMaps = new List <LayerMap>();
                    }

                    if (workbookMap.AllLayerMaps == null)
                    {
                        workbookMap.AllLayerMaps = new List <LayerMap>();
                        workbookMap.AllLayerMaps.AddRange(workbookMap.SerializableLayerMaps);
                    }

                    if (workbookMap.AllLayerMaps.Count > 0)
                    {
                        // Clean all invalid named ranges.
                        CleanLayerMap(workbook, workbookMap);

                        // Loop through all the layers to check if the range address column count is different from the mapped column count
                        // If so, reset the mapping as excel has undergone changes without the add-in
                        foreach (LayerMap localLayer in workbookMap.LocalLayerMaps)
                        {
                            if (localLayer.RangeName.RefersToRange != null && localLayer.RangeName.RefersToRange.EntireColumn.Count != localLayer.MappedColumnType.Count)
                            {
                                localLayer.SetAutoMap();
                                localLayer.SetLayerProperties();
                            }
                        }
                    }

                    // Check if layer being removed is selected layer
                    if (workbookMap.SerializableSelectedLayerMap != null)
                    {
                        // Check if SelectedLayerMap is found in All local layers
                        LayerMap layerMap = workbookMap.AllLayerMaps
                                            .Where(item => !string.IsNullOrEmpty(item.RangeDisplayName) && item.RangeDisplayName.Equals(workbookMap.SerializableSelectedLayerMap.RangeDisplayName)).FirstOrDefault();

                        workbookMap.SelectedLayerMap = (layerMap != null) ? layerMap : null;
                    }
                }

                // Clean up and update Local in WWT Layers
                workbookMap.LoadWWTLayers();
            }

            return(workbookMap);
        }
        /// <summary>
        /// Get WorkbookMap from Workbook
        /// </summary>
        /// <param name="workbook">workbook instance</param>
        /// <returns>WorkbookMap instance</returns>
        internal static WorkbookMap GetWorkbookMap(this Workbook workbook)
        {
            // Initialize default
            var workbookMap = new WorkbookMap(workbook);
            if (workbook != null)
            {
                string content = workbook.GetCustomXmlPart(Common.Constants.XmlNamespace);
                if (!string.IsNullOrEmpty(content))
                {
                    workbookMap = workbookMap.Deserialize(content);

                    workbookMap.Workbook = workbook;
                    if (workbookMap.SerializableLayerMaps == null)
                    {
                        workbookMap.SerializableLayerMaps = new List<LayerMap>();
                    }

                    if (workbookMap.AllLayerMaps == null)
                    {
                        workbookMap.AllLayerMaps = new List<LayerMap>();
                        workbookMap.AllLayerMaps.AddRange(workbookMap.SerializableLayerMaps);
                    }

                    if (workbookMap.AllLayerMaps.Count > 0)
                    {
                        // Clean all invalid named ranges.
                        CleanLayerMap(workbook, workbookMap);

                        // Loop through all the layers to check if the range address column count is different from the mapped column count
                        // If so, reset the mapping as excel has undergone changes without the add-in
                        foreach (LayerMap localLayer in workbookMap.LocalLayerMaps)
                        {
                            if (localLayer.RangeName.RefersToRange != null && localLayer.RangeName.RefersToRange.EntireColumn.Count != localLayer.MappedColumnType.Count)
                            {
                                localLayer.SetAutoMap();
                                localLayer.SetLayerProperties();
                            }
                        }
                    }

                    // Check if layer being removed is selected layer
                    if (workbookMap.SerializableSelectedLayerMap != null)
                    {
                        // Check if SelectedLayerMap is found in All local layers
                        LayerMap layerMap = workbookMap.AllLayerMaps
                            .Where(item => !string.IsNullOrEmpty(item.RangeDisplayName) && item.RangeDisplayName.Equals(workbookMap.SerializableSelectedLayerMap.RangeDisplayName)).FirstOrDefault();

                        workbookMap.SelectedLayerMap = (layerMap != null) ? layerMap : null;
                    }
                }

                // Clean up and update Local in WWT Layers
                workbookMap.LoadWWTLayers();
            }

            return workbookMap;
        }
 /// <summary>
 /// Stops all notifications for all the layers of the current workbook map.
 /// </summary>
 /// <param name="workbookMap">WorkbookMap instance</param>
 internal static void StopAllNotifications(this WorkbookMap workbookMap)
 {
     if (workbookMap != null)
     {
         // Get all Layer details into dictionary.
         workbookMap.AllLayerMaps.ForEach(item =>
         {
             item.IsNotInSync = true;
         });
     }
 }
 /// <summary>
 /// Removes the layer maps present in affected layers from the workbook map
 /// </summary>
 /// <param name="workbookMap">The workbook map which is in scope</param>
 /// <param name="affectedLayerList">The list of layers that need to be removed from the workbook map</param>
 internal static void RemoveAffectedLayers(this WorkbookMap workbookMap, List <LayerMap> affectedLayerList)
 {
     if (workbookMap != null && affectedLayerList != null)
     {
         foreach (LayerMap layerMap in affectedLayerList)
         {
             if (workbookMap.Exists(layerMap.LayerDetails.ID))
             {
                 workbookMap.AllLayerMaps.Remove(layerMap);
             }
         }
     }
 }
 /// <summary>
 /// This function is used to remove WWT layers.
 /// </summary>
 /// <param name="workbookMap">
 /// Parent container.
 /// </param>
 private static void RemoveWWTLayers(this WorkbookMap workbookMap)
 {
     if (workbookMap != null)
     {
         lock (workbookMap.AllLayerMaps)
         {
             // Remove all WWT only layers.
             workbookMap.WWTLayerMaps.ForEach(
                 wwtLayer =>
             {
                 // This is needed to stop the notifications.
                 wwtLayer.IsNotInSync = true;
                 workbookMap.AllLayerMaps.Remove(wwtLayer);
             });
         }
     }
 }
        /// <summary>
        /// Get names of all the ranges for layers which are in sync
        /// </summary>
        /// <param name="workbookMap">current workbookMap</param>
        /// <returns>dictionary of range name and address</returns>
        internal static Dictionary <string, string> GetNamedRangesForInSyncLayers(this WorkbookMap workbookMap)
        {
            var allNamedRange = new Dictionary <string, string>();

            if (workbookMap != null)
            {
                // Get all Layer details into dictionary.
                workbookMap.LocalInWWTLayerMaps.ForEach(item =>
                {
                    // Only in sync layers
                    if (!item.IsNotInSync)
                    {
                        allNamedRange.Add(item.RangeDisplayName, item.RangeName.RefersTo as string);
                    }
                });
            }

            return(allNamedRange);
        }
        /// <summary>
        /// Builds the layer details view model for the given workbook. Since rebuilding the reference frame dropdown makes call to 
        /// WWT API, in case if WWT is not running, caller will send false as it's value to avoid addition delay in showing results.
        /// </summary>
        /// <param name="layerDetailsModel">LayerDetailsViewModel object getting build</param>
        /// <param name="currentWorkbook">Current workbook object</param>
        /// <param name="rebuildReferenceFrameDropDown">Whether to rebuild reference frame dropdown or not?</param>
        /// <returns>Build LayerDetailsViewModel object</returns>
        internal static LayerDetailsViewModel BuildLayerDetailsViewModel(this LayerDetailsViewModel layerDetailsModel, WorkbookMap currentWorkbook, bool rebuildReferenceFrameDropDown)
        {
            // This is for building view model from workbook map. This will be used in Initialize, open & create new range scenarios
            if (layerDetailsModel != null)
            {
                // Build the view model for the drop down based on the layer map details
                layerDetailsModel.Layers = new ObservableCollection<LayerMapDropDownViewModel>();
                if (currentWorkbook != null)
                {
                    // Rebuilds the layer view model dropdown
                    WorkflowController.Instance.RebuildGroupLayerDropdown();

                    if (rebuildReferenceFrameDropDown)
                    {
                        WorkflowController.Instance.BuildReferenceFrameDropDown();
                    }

                    // If the current workbook has a selected layer, set current layer property which well set all the layer properties
                    if (currentWorkbook.SelectedLayerMap != null)
                    {
                        layerDetailsModel.Currentlayer = currentWorkbook.SelectedLayerMap;
                    }
                    else
                    {
                        LayerMapDropDownViewModel selectedLayerMap = layerDetailsModel.Layers.Where(layer => layer.Name.Equals(Properties.Resources.DefaultSelectedLayerName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                        if (selectedLayerMap != null)
                        {
                            layerDetailsModel.SelectedLayerText = selectedLayerMap.Name;
                        }
                        layerDetailsModel.Currentlayer = null;
                        layerDetailsModel.IsHelpTextVisible = true;
                        layerDetailsModel.IsTabVisible = false;
                    }
                }
                else
                {
                    layerDetailsModel.Layers.Add(new LayerMapDropDownViewModel() { Name = Properties.Resources.LocalLayerName });
                    layerDetailsModel.Layers.Add(new LayerMapDropDownViewModel() { Name = Properties.Resources.WWTLayerName });
                }
            }

            return layerDetailsModel;
        }
        /// <summary>
        /// This function is used to Add WWT layers.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        /// <param name="group">
        /// Group in which the layer is present.
        /// </param>
        private static void AddWWTLayers(WorkbookMap workbookMap, Group group)
        {
            foreach (string layerID in group.LayerIDs)
            {
                if (!workbookMap.Exists(layerID))
                {
                    Layer wwtLayer = WWTManager.GetLayerDetails(layerID, group, true);
                    if (wwtLayer != null)
                    {
                        workbookMap.AllLayerMaps.Add(new LayerMap(wwtLayer));
                    }
                }
            }

            foreach (Group child in group.Children)
            {
                AddWWTLayers(workbookMap, child);
            }
        }
        /// <summary>
        /// This function is used to Load all WWT layers on the load of a workbook.
        /// This function performs the following operations:
        ///     1. Remove all WWT only layers.
        ///     2. On load of a workbook, no layers are connected with WWT.
        ///     3. Insert and update deleted WWT Layers.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        internal static void LoadWWTLayers(this WorkbookMap workbookMap)
        {
            if (workbookMap != null)
            {
                LayerMap selectedLayerMap = workbookMap.SelectedLayerMap;

                // 1. Remove all WWT only layers.
                workbookMap.RemoveWWTLayers();

                ICollection <Group> groups = WWTManager.GetAllWWTGroups(true);

                // 2. On load of a workbook, no layers are connected with WWT.
                workbookMap.LocalInWWTLayerMaps.ForEach(
                    layer =>
                {
                    // 2.a. Set IsNotInSync.
                    layer.IsNotInSync = true;

                    // 2.b. Update all group references.
                    Group group = SearchGroup(layer.LayerDetails.Group.Name, layer.LayerDetails.Group.Path, groups);
                    if (group != null)
                    {
                        layer.LayerDetails.Group = group;
                    }
                    else
                    {
                        layer.LayerDetails.Group.IsDeleted = true;
                    }
                });

                // 3. Synchronize WWT Layers
                workbookMap.SyncWWTLayers(groups);

                // 4. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
                //  If the selected layer is WWT layer/is localInWWT which is not deleted, this will be selected while
                if (selectedLayerMap != null && selectedLayerMap.MapType == LayerMapType.WWT && !workbookMap.Exists(selectedLayerMap.LayerDetails.ID))
                {
                    workbookMap.SelectedLayerMap = null;
                }
            }
        }
        /// <summary>
        /// This function is used to refresh the layer drop down with the latest layers from WWT.
        /// This function performs the following operations:
        ///     1. Remove all WWT only layers.
        ///     2. Insert and update deleted WWT Layers.
        ///     3. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        internal static void RefreshLayers(this WorkbookMap workbookMap)
        {
            if (workbookMap != null)
            {
                LayerMap            selectedLayerMap = workbookMap.SelectedLayerMap;
                ICollection <Group> groups           = WWTManager.GetAllWWTGroups(true);

                // 1. Remove all WWT only layers.
                workbookMap.RemoveWWTLayers();

                // 2. Insert and update deleted WWT Layers
                workbookMap.SyncWWTLayers(groups);

                // 3. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
                //  If the selected layer is WWT layer/is localInWWT which is not deleted, this will be selected while
                if (selectedLayerMap != null && selectedLayerMap.MapType == LayerMapType.WWT && !workbookMap.Exists(selectedLayerMap.LayerDetails.ID))
                {
                    workbookMap.SelectedLayerMap = null;
                }
            }
        }
        public void ExistsTestValid()
        {
            LayerMap layerMap = new LayerMap(new Layer());
            layerMap.LayerDetails.ID = "ValidID";

            WorkbookMap workbookMap = new WorkbookMap(null);
            workbookMap.AllLayerMaps.Add(layerMap);

            string layerID = "ValidID";
            bool expected = true;
            bool actual;
            actual = WorkbookMapExtensions.Exists(workbookMap, layerID);
            Assert.AreEqual(expected, actual);
        }
        public void RemoveAffectedLayersTest()
        {
            Group marsGroup = new Group("Mars", GroupType.ReferenceFrame, null);
            Group sunGroup = new Group("Sun", GroupType.ReferenceFrame, null);
            Group earthGroup = new Group("Earth", GroupType.ReferenceFrame, sunGroup);
            earthGroup.LayerIDs.Add("8e5cbbc4-9eb2-47e5-b3d7-7ca9babda477");
            sunGroup.Children.Add(earthGroup);

            LayerMap earthLayer = new LayerMap(new Layer());
            earthLayer.MapType = LayerMapType.WWT;
            earthLayer.LayerDetails.Group = earthGroup;
            earthLayer.LayerDetails.ID = "ValidID1";

            LayerMap marsLayer = new LayerMap(new Layer());
            marsLayer.MapType = LayerMapType.LocalInWWT;
            marsLayer.LayerDetails.Group = marsGroup;
            marsLayer.LayerDetails.ID = "MarsValidID1";

            LayerMap sunLayer = new LayerMap(new Layer());
            sunLayer.MapType = LayerMapType.LocalInWWT;
            sunLayer.LayerDetails.Group = sunGroup;
            sunLayer.LayerDetails.ID = "sunLayerID1";

            WorkbookMap workbookMap = new WorkbookMap(null);
            workbookMap.AllLayerMaps.Add(earthLayer);
            workbookMap.AllLayerMaps.Add(marsLayer);
            workbookMap.AllLayerMaps.Add(sunLayer);

            List<LayerMap> affectedLayerList = new List<LayerMap>();
            affectedLayerList.Add(marsLayer);
            affectedLayerList.Add(sunLayer);

            WorkbookMapExtensions.RemoveAffectedLayers(null, null);
            WorkbookMapExtensions.RemoveAffectedLayers(workbookMap, affectedLayerList);
            Assert.IsTrue(workbookMap.AllLayerMaps.Count == 1);
        }
        public void SerializeTest()
        {
            Application application = new Application();

            try
            {
                ThisAddIn_Accessor.ExcelApplication = application;
                Workbook book = application.OpenWorkbook("WorkbookTestData.xlsx", false);
                Name layerOne = book.Names.GetNamedRange("UpdateLayerRangeInitial");
                LayerMap layerMap = new LayerMap(layerOne);
                WorkbookMap workbookMap = new WorkbookMap(book);
                workbookMap.AllLayerMaps.Add(layerMap);
                string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><WorkbookMap 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.WorkbooMap\"><d1p1:SerializableLayerMaps><d1p1:LayerMap><d1p1:LayerDetails xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/Microsoft.Research.Wwt.Excel.Common\"><d4p1:AltColumn>-1</d4p1:AltColumn><d4p1:AltType>Depth</d4p1:AltType><d4p1:AltUnit>Meters</d4p1:AltUnit><d4p1:Color>ARGBColor:255:255:0:0</d4p1:Color><d4p1:ColorMapColumn>-1</d4p1:ColorMapColumn><d4p1:CoordinatesType>Spherical</d4p1:CoordinatesType><d4p1:DecColumn>-1</d4p1:DecColumn><d4p1:EndDateColumn>-1</d4p1:EndDateColumn><d4p1:EndTime>9999-12-31T23:59:59.9999999</d4p1:EndTime><d4p1:FadeSpan>PT0S</d4p1:FadeSpan><d4p1:FadeType>None</d4p1:FadeType><d4p1:GeometryColumn>-1</d4p1:GeometryColumn><d4p1:Group i:nil=\"true\" /><d4p1:HasTimeSeries>false</d4p1:HasTimeSeries><d4p1:ID i:nil=\"true\" /><d4p1:LatColumn>-1</d4p1:LatColumn><d4p1:LngColumn>-1</d4p1:LngColumn><d4p1:MarkerIndex>0</d4p1:MarkerIndex><d4p1:MarkerScale>World</d4p1:MarkerScale><d4p1:Name>UpdateLayerRangeInitial</d4p1:Name><d4p1:NameColumn>0</d4p1:NameColumn><d4p1:Opacity>1</d4p1:Opacity><d4p1:PlotType>Gaussian</d4p1:PlotType><d4p1:PointScaleType>StellarMagnitude</d4p1:PointScaleType><d4p1:RAColumn>-1</d4p1:RAColumn><d4p1:RAUnit>Hours</d4p1:RAUnit><d4p1:ReverseXAxis>false</d4p1:ReverseXAxis><d4p1:ReverseYAxis>false</d4p1:ReverseYAxis><d4p1:ReverseZAxis>false</d4p1:ReverseZAxis><d4p1:ScaleFactor>8</d4p1:ScaleFactor><d4p1:ShowFarSide>true</d4p1:ShowFarSide><d4p1:SizeColumn>-1</d4p1:SizeColumn><d4p1:StartDateColumn>-1</d4p1:StartDateColumn><d4p1:StartTime>0001-01-01T00:00:00</d4p1:StartTime><d4p1:TimeDecay>16</d4p1:TimeDecay><d4p1:Version>0</d4p1:Version><d4p1:XAxis>-1</d4p1:XAxis><d4p1:YAxis>-1</d4p1:YAxis><d4p1:ZAxis>-1</d4p1:ZAxis></d1p1:LayerDetails><d1p1:MapType>Local</d1p1:MapType><d1p1:MappedColumnType xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/Microsoft.Research.Wwt.Excel.Common\"><d4p1:ColumnType>None</d4p1:ColumnType><d4p1:ColumnType>None</d4p1:ColumnType><d4p1:ColumnType>None</d4p1:ColumnType><d4p1:ColumnType>None</d4p1:ColumnType><d4p1:ColumnType>None</d4p1:ColumnType><d4p1:ColumnType>None</d4p1:ColumnType></d1p1:MappedColumnType><d1p1:RangeAddress>=UpdateLayer!$A$1:$F$9</d1p1:RangeAddress><d1p1:RangeDisplayName>UpdateLayerRangeInitial</d1p1:RangeDisplayName></d1p1:LayerMap></d1p1:SerializableLayerMaps><d1p1:SerializableSelectedLayerMap i:nil=\"true\" /></WorkbookMap>";
                string actual = WorkbookMapExtensions.Serialize(workbookMap);
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                application.Close();
            }
        }
        public void CleanUpWWTLayersTest()
        {
            Application application = new Application();
            try
            {
                Workbook book = application.OpenWorkbook("WorkbookTestData.xlsx", false);
                Name emptyRange = book.Names.GetNamedRange("GetSelectedLayerWorksheet");

                Common.Globals_Accessor.wwtManager = new WWTManager(new WWTMockRequest());
                Common.Globals_Accessor.TargetMachine = new TargetMachine("localhost");

                Group marsGroup = new Group("Mars", GroupType.ReferenceFrame, null);
                Group sunGroup = new Group("Sun", GroupType.ReferenceFrame, null);
                Group earthGroup = new Group("Earth", GroupType.ReferenceFrame, sunGroup);
                earthGroup.LayerIDs.Add("8e5cbbc4-9eb2-47e5-b3d7-7ca9babda477");
                sunGroup.Children.Add(earthGroup);

                LayerMap earthLayer = new LayerMap(new Layer());
                earthLayer.MapType = LayerMapType.WWT;
                earthLayer.LayerDetails.Group = earthGroup;
                earthLayer.LayerDetails.ID = "ValidID1";

                LayerMap marsLayer = new LayerMap(emptyRange);
                marsLayer.MapType = LayerMapType.LocalInWWT;
                marsLayer.LayerDetails.Group = marsGroup;
                marsLayer.LayerDetails.ID = "MarsValidID1";

                LayerMap sunLayer = new LayerMap(emptyRange);
                sunLayer.MapType = LayerMapType.LocalInWWT;
                sunLayer.LayerDetails.Group = sunGroup;
                sunLayer.LayerDetails.ID = "sunLayerID1";

                WorkbookMap workbookMap = new WorkbookMap(null);
                workbookMap.AllLayerMaps.Add(earthLayer);
                workbookMap.AllLayerMaps.Add(marsLayer);
                workbookMap.AllLayerMaps.Add(sunLayer);

                workbookMap.SelectedLayerMap = earthLayer;

                WorkbookMapExtensions.CleanUpWWTLayers(workbookMap);
                Assert.IsNull(workbookMap.SelectedLayerMap);
                Assert.IsTrue(workbookMap.AllLayerMaps.Count == 2);
            }
            finally
            {
                application.Close();
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Implementation for the BackgroundWorker which will be syncing the layer properties.
        /// </summary>
        /// <param name="sender">Background Worker</param>
        /// <param name="e">BackgroundWorker event arguments</param>
        private void OnPropertyChangeNotifierDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                e.Result = false;

                int         layerVersion      = WWTManager.CreateLayerNotification(this.LayerDetails.ID, this.LayerDetails.Version, this.cancellationTokenSource.Token);
                WorkbookMap parentWorkbookMap = WorkflowController.Instance.GetWorkbookMapForLayerMap(this);

                // Rebuild the Layer Details view model/Custom Task Pane if the layer's properties are updated in WWT.
                if (layerVersion > LayerDetails.Version)
                {
                    if (IsPropertyChangedFromCode)
                    {
                        LayerDetails.Version = layerVersion;
                    }
                    else
                    {
                        // Get the current properties of the layer from WWT.
                        Layer layerDetails = WWTManager.GetLayerDetails(LayerDetails.ID, LayerDetails.Group, true);

                        if (layerDetails != null)
                        {
                            LayerDetails = layerDetails;

                            // Update the received latest properties to the LayerMap.
                            this.UpdateLayerMapProperties(LayerDetails);

                            // Save the workbook map to the workbook which it belongs to.
                            this.SaveWorkbookMap();

                            if (parentWorkbookMap != null && parentWorkbookMap.SelectedLayerMap == this)
                            {
                                // This will update the custom task pane.
                                e.Result = true;
                            }
                        }
                    }
                }
                else if (layerVersion == -1)
                {
                    // In case if the layer is deleted or WWT is closed, layerVersion will be returned as -1.
                    // In case of Timeout, layers current version will be returned which is expected in case of no update to the layer properties.
                    // Setting not in sync will stop the notification as well.
                    IsNotInSync = true;

                    if (parentWorkbookMap != null && parentWorkbookMap.SelectedLayerMap == this)
                    {
                        if (MapType == LayerMapType.WWT)
                        {
                            // If current layer map is of type WWT and it is selected in layer dropdown, make sure the selection is removed.
                            parentWorkbookMap.SelectedLayerMap = null;
                        }

                        // This will update the custom task pane.
                        e.Result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }
        /// <summary>
        /// This function is used to remove all layer which are not valid.
        ///     1. Layer map which does not have a valid named range
        /// </summary>
        /// <param name="workbook">
        /// workbook instance
        /// </param>
        /// <param name="workbookMap">
        /// workbook map instance
        /// </param>
        private static void CleanLayerMap(Workbook workbook, WorkbookMap workbookMap)
        {
            // Clean up Invalid Ranges.
            foreach (Name namedRange in workbook.Names)
            {
                // The named ranges are WWT ranges and are invalid
                if (namedRange.IsWWTRange() && !namedRange.IsValid())
                {
                    string name = namedRange.Name;
                    try
                    {
                        // Delete the range name for all invalid layers
                        workbookMap.AllLayerMaps.ForEach(layer =>
                        {
                            if (layer.RangeDisplayName.Equals(name))
                            {
                                layer.RangeName.Delete();
                            }
                        });
                    }
                    catch (COMException ex)
                    {
                        Logger.LogException(ex);
                    }

                    // Clean up such layers on load itself
                    workbookMap.AllLayerMaps.RemoveAll(layer =>
                    {
                        return layer.RangeDisplayName.Equals(name);
                    });
                }
            }
        }
        /// <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 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);
        }
 public void ExistsTestEmptyLayerId()
 {
     WorkbookMap workbookMap = new WorkbookMap(null);
     string layerID = string.Empty;
     bool expected = false;
     bool actual;
     actual = WorkbookMapExtensions.Exists(workbookMap, layerID);
     Assert.AreEqual(expected, actual);
 }
        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);
                }
            }
        }
Esempio n. 28
0
        /// <summary>
        /// Builds the layer details view model for the given workbook. Since rebuilding the reference frame dropdown makes call to
        /// WWT API, in case if WWT is not running, caller will send false as it's value to avoid addition delay in showing results.
        /// </summary>
        /// <param name="layerDetailsModel">LayerDetailsViewModel object getting build</param>
        /// <param name="currentWorkbook">Current workbook object</param>
        /// <param name="rebuildReferenceFrameDropDown">Whether to rebuild reference frame dropdown or not?</param>
        /// <returns>Build LayerDetailsViewModel object</returns>
        internal static LayerDetailsViewModel BuildLayerDetailsViewModel(this LayerDetailsViewModel layerDetailsModel, WorkbookMap currentWorkbook, bool rebuildReferenceFrameDropDown)
        {
            // This is for building view model from workbook map. This will be used in Initialize, open & create new range scenarios
            if (layerDetailsModel != null)
            {
                // Build the view model for the drop down based on the layer map details
                layerDetailsModel.Layers = new ObservableCollection <LayerMapDropDownViewModel>();
                if (currentWorkbook != null)
                {
                    // Rebuilds the layer view model dropdown
                    WorkflowController.Instance.RebuildGroupLayerDropdown();

                    if (rebuildReferenceFrameDropDown)
                    {
                        WorkflowController.Instance.BuildReferenceFrameDropDown();
                    }

                    // If the current workbook has a selected layer, set current layer property which well set all the layer properties
                    if (currentWorkbook.SelectedLayerMap != null)
                    {
                        layerDetailsModel.Currentlayer = currentWorkbook.SelectedLayerMap;
                    }
                    else
                    {
                        LayerMapDropDownViewModel selectedLayerMap = layerDetailsModel.Layers.Where(layer => layer.Name.Equals(Properties.Resources.DefaultSelectedLayerName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                        if (selectedLayerMap != null)
                        {
                            layerDetailsModel.SelectedLayerText = selectedLayerMap.Name;
                        }
                        layerDetailsModel.Currentlayer      = null;
                        layerDetailsModel.IsHelpTextVisible = true;
                        layerDetailsModel.IsTabVisible      = false;
                    }
                }
                else
                {
                    layerDetailsModel.Layers.Add(new LayerMapDropDownViewModel()
                    {
                        Name = Properties.Resources.LocalLayerName
                    });
                    layerDetailsModel.Layers.Add(new LayerMapDropDownViewModel()
                    {
                        Name = Properties.Resources.WWTLayerName
                    });
                }
            }

            return(layerDetailsModel);
        }
        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;
        }
        /// <summary>
        /// This function is used to Add WWT layers.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        /// <param name="group">
        /// Group in which the layer is present.
        /// </param>
        private static void AddWWTLayers(WorkbookMap workbookMap, Group group)
        {
            foreach (string layerID in group.LayerIDs)
            {
                if (!workbookMap.Exists(layerID))
                {
                    Layer wwtLayer = WWTManager.GetLayerDetails(layerID, group, true);
                    if (wwtLayer != null)
                    {
                        workbookMap.AllLayerMaps.Add(new LayerMap(wwtLayer));
                    }
                }
            }

            foreach (Group child in group.Children)
            {
                AddWWTLayers(workbookMap, child);
            }
        }