private static void AddStyle(StylePlugin styleProvider)
        {
            Style style = null;
            StyleBuilderArguments arguments           = new StyleBuilderArguments();
            FeatureLayer          currentFeatureLayer = null;

            if (GisEditor.LayerListManager.SelectedLayerListItem == null)
            {
                return;
            }

            //add a new style by right-clicking on a layer node
            if (GisEditor.LayerListManager.SelectedLayerListItem.ConcreteObject is FeatureLayer)
            {
                currentFeatureLayer = (FeatureLayer)GisEditor.LayerListManager.SelectedLayerListItem.ConcreteObject;
            }

            //add a new style by right-clicking on a zoomlevel node
            else if (GisEditor.LayerListManager.SelectedLayerListItem.ConcreteObject is ZoomLevel)
            {
                ZoomLevel editingZoomLevel = (ZoomLevel)GisEditor.LayerListManager.SelectedLayerListItem.ConcreteObject;
                arguments.FromZoomLevelIndex = GisEditor.ActiveMap.GetSnappedZoomLevelIndex(editingZoomLevel.Scale, false) + 1;
                arguments.ToZoomLevelIndex   = (int)editingZoomLevel.ApplyUntilZoomLevel;
                currentFeatureLayer          = (FeatureLayer)GisEditor.LayerListManager.SelectedLayerListItem.Parent.ConcreteObject;
            }

            //replace an existing style
            else if (GisEditor.LayerListManager.SelectedLayerListItem.ConcreteObject is Style)
            {
                Style currentStyle = (Style)GisEditor.LayerListManager.SelectedLayerListItem.ConcreteObject;
                currentFeatureLayer = LayerListHelper.FindMapElementInTree <FeatureLayer>(GisEditor.LayerListManager.SelectedLayerListItem);
            }

            arguments.AvailableStyleCategories = LayerListHelper.GetStyleCategoriesByFeatureLayer(currentFeatureLayer);
            arguments.FeatureLayer             = currentFeatureLayer;
            arguments.FillRequiredColumnNames();
            arguments.AppliedCallback = args =>
            {
                if (args.CompositeStyle != null)
                {
                    ZoomLevelHelper.ApplyStyle(args.CompositeStyle, currentFeatureLayer, args.FromZoomLevelIndex, args.ToZoomLevelIndex);
                }
            };

            style      = styleProvider.GetDefaultStyle();
            style.Name = styleProvider.Name;
            var componentStyle = new CompositeStyle(style)
            {
                Name = currentFeatureLayer.Name
            };

            arguments.StyleToEdit = componentStyle;
            //var styleResults = GisEditor.StyleManager.EditStyle(arguments);
            if (currentFeatureLayer != null)
            {
                var featureLayerPlugin = GisEditor.LayerManager.GetLayerPlugins(currentFeatureLayer.GetType()).FirstOrDefault() as FeatureLayerPlugin;
                if (featureLayerPlugin != null)
                {
                    var styleBuilder = GisEditor.StyleManager.GetStyleBuiderUI();
                    if (styleBuilder != null)
                    {
                        styleBuilder.StyleBuilderArguments = arguments;
                        if (styleBuilder.ShowDialog().GetValueOrDefault())
                        {
                            arguments.AppliedCallback(styleBuilder.StyleBuilderResult);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public static bool AddStyleToLayerWithStyleWizard(IEnumerable <Layer> layers, bool replaceStyle = false)
        {
            bool addedStyle = false;
            var  newLayers  = layers.ToArray();

            foreach (var tmpLayer in newLayers)
            {
                var shapeFileFeatureLayer = tmpLayer as FeatureLayer;
                if (shapeFileFeatureLayer != null &&
                    newLayers.Length == 1)
                {
                    var styleWizardWindow = GisEditor.ControlManager.GetUI <StyleWizardWindow>();
                    styleWizardWindow.StyleCategories = LayerListHelper.GetStyleCategoriesByFeatureLayer(shapeFileFeatureLayer);
                    styleWizardWindow.StyleCategories = styleWizardWindow.StyleCategories ^ StyleCategories.Composite;
                    styleWizardWindow.StyleCategories = styleWizardWindow.StyleCategories ^ StyleCategories.Label;
                    if ((styleWizardWindow as System.Windows.Window).ShowDialog().GetValueOrDefault())
                    {
                        if (styleWizardWindow.StyleWizardResult != null)
                        {
                            if (GisEditor.ActiveMap != null)
                            {
                                GisEditor.ActiveMap.ActiveLayer = shapeFileFeatureLayer;
                            }

                            StyleBuilderArguments arguments = new StyleBuilderArguments();
                            arguments.FeatureLayer             = shapeFileFeatureLayer;
                            arguments.AvailableStyleCategories = LayerListHelper.GetStyleCategoriesByFeatureLayer(shapeFileFeatureLayer);
                            StylePlugin styleProvider = styleWizardWindow.StyleWizardResult.StylePlugin;

                            arguments.AppliedCallback = new Action <StyleBuilderResult>(args =>
                            {
                                if (args.CompositeStyle != null)
                                {
                                    if (replaceStyle)
                                    {
                                        foreach (var customZoomLevel in shapeFileFeatureLayer.ZoomLevelSet.CustomZoomLevels)
                                        {
                                            customZoomLevel.CustomStyles.Clear();
                                        }
                                    }
                                    AddNewStyleToLayer(shapeFileFeatureLayer, args.CompositeStyle, args.FromZoomLevelIndex, args.ToZoomLevelIndex);
                                }
                            });

                            var newStyle = styleProvider.GetDefaultStyle();
                            newStyle.Name = styleProvider.Name;
                            CompositeStyle componentStyle = new CompositeStyle();
                            componentStyle.Name = shapeFileFeatureLayer.Name;
                            componentStyle.Styles.Add(newStyle);
                            arguments.StyleToEdit = componentStyle;

                            arguments.FillRequiredColumnNames();
                            var styleResult = GisEditor.StyleManager.EditStyle(arguments);
                            if (!styleResult.Canceled)
                            {
                                componentStyle = (CompositeStyle)styleResult.CompositeStyle;
                                arguments.AppliedCallback(styleResult);
                                addedStyle = true;
                            }
                        }
                    }
                    //if (GisEditor.StyleManager.UseWizard != styleWizardWindow.IsAlwaysShowWhenLayerIsAdded)
                    //{
                    //    GisEditor.StyleManager.UseWizard = styleWizardWindow.IsAlwaysShowWhenLayerIsAdded;
                    //    GisEditor.InfrastructureManager.SaveSettings(GisEditor.StyleManager);
                    //}
                }
            }
            return(addedStyle);
        }
        private static void AddSubMenuItemsForStyle(StyleBuilderArguments styleArguments, StyleCategories styleCategories
                                                    , string categoryName
                                                    , MenuItem rootMenuItem
                                                    , StyleItemViewModel currentStyleItemViewModel
                                                    , bool hasCompositedStyle)
        {
            if (hasCompositedStyle &&
                styleCategories != StyleCategories.None &&
                !styleCategories.HasFlag(StyleCategories.Label))
            {
                styleCategories = styleCategories | StyleCategories.Composite;
            }

            var plugins = GisEditor.StyleManager.GetStylePlugins(styleCategories);

            if (plugins.Count > 0)
            {
                var menuItems = plugins.Select(plugin =>
                {
                    if (plugin.RequireColumnNames && styleArguments.ColumnNames.Count == 0)
                    {
                        return(null);
                    }

                    MenuItem subMenuItem = new MenuItem();
                    subMenuItem.Header   = plugin.Name;
                    subMenuItem.Icon     = new Image {
                        Source = plugin.SmallIcon, Width = 16, Height = 16
                    };
                    subMenuItem.CommandParameter = new Tuple <StylePlugin, StyleCategories>(plugin, styleCategories);
                    subMenuItem.Command          = new RelayCommand <Tuple <StylePlugin, StyleCategories> >(commandParameter =>
                    {
                        StylePlugin tmpStylePlugin = commandParameter.Item1;
                        Styles.Style style         = tmpStylePlugin.GetDefaultStyle();
                        style.Name = tmpStylePlugin.Name;

                        StyleLayerListItem styleItem = GisEditor.StyleManager.GetStyleLayerListItem(style);
                        if (styleItem != null)
                        {
                            currentStyleItemViewModel.StyleItem.Children.Insert(0, styleItem);
                            currentStyleItemViewModel.StyleItem.UpdateConcreteObject();
                            var styleItemUI = currentStyleItemViewModel.StyleItem.GetUI(GetDuplicateStyleArguments(styleCategories, styleArguments));
                            if (styleItemUI != null)
                            {
                                currentStyleItemViewModel.StyleItem.UpdateUI(styleItemUI);
                            }

                            var addedStyleItemViewModel = currentStyleItemViewModel.StyleItemViewModels.FirstOrDefault(vm => vm.StyleItem == styleItem);
                            if (addedStyleItemViewModel != null)
                            {
                                if (commandParameter.Item2 == StyleCategories.Label)
                                {
                                    addedStyleItemViewModel.IsSelected = true;
                                }
                                else
                                {
                                    StyleItemViewModel rootStyleItemViewModel = GetRootViewModel(currentStyleItemViewModel);

                                    tmpStyleCategories    = commandParameter.Item2;
                                    var tmpStyleArguments = rootStyleItemViewModel.StyleBuilder.StyleArguments;
                                    rootStyleItemViewModel.StyleBuilder.PropertyChanged -= StyleBuilder_PropertyChanged;
                                    rootStyleItemViewModel.StyleBuilder.PropertyChanged += StyleBuilder_PropertyChanged;

                                    addedStyleItemViewModel.IsSelected = true;

                                    rootStyleItemViewModel.StyleBuilder.PropertyChanged -= StyleBuilder_PropertyChanged;
                                    rootStyleItemViewModel.StyleBuilder.StyleArguments   = tmpStyleArguments;
                                }
                            }
                        }
                    });
                    return(subMenuItem);
                }).Where(i => i != null).ToArray();

                foreach (var item in menuItems)
                {
                    rootMenuItem.Items.Add(item);
                }
            }
        }