private void InitializeCommands()
        {
            addConditionCommand = new RelayCommand(() =>
            {
                FilterConditionWindow newFilterConditionWindow = new FilterConditionWindow();
                FilterConditionViewModel itemViewModel         = new FilterConditionViewModel(RequiredValues);
                itemViewModel.PropertyChanged       += ItemViewModel_PropertyChanged;
                newFilterConditionWindow.DataContext = itemViewModel;
                foreach (var item in columnNameTypes)
                {
                    newFilterConditionWindow.ViewModel.ColumnNameTypes[item.Key] = item.Value;
                }

                newFilterConditionWindow.ViewModel.SelectedColumnName = newFilterConditionWindow.ViewModel.ColumnNames.FirstOrDefault();
                if (newFilterConditionWindow.ShowDialog().GetValueOrDefault())
                {
                    var stylePlugin = GisEditor.StyleManager.GetDefaultStylePlugin(requiredValues.AvailableStyleCategories);

                    filterConditions.Add(newFilterConditionWindow.ViewModel);
                    SyncConditions();
                    RaisePropertyChanged("FilterConditions");
                }
            });

            editConditionCommand = new RelayCommand <FilterConditionViewModel>(v =>
            {
                var clonedViewModel = v.CloneDeep();
                clonedViewModel.ColumnNameTypes.Clear();

                foreach (var item in columnNameTypes)
                {
                    clonedViewModel.ColumnNameTypes[item.Key] = item.Value;
                }

                FilterConditionWindow newFilterConditionWindow = new FilterConditionWindow(clonedViewModel);

                FeatureLayerPlugin resultLayerPlugin = GisEditor.LayerManager.GetLayerPlugins(RequiredValues.FeatureLayer.GetType()).LastOrDefault() as FeatureLayerPlugin;

                if (resultLayerPlugin != null)
                {
                    Collection <IntermediateColumn> columns = resultLayerPlugin.GetIntermediateColumns(requiredValues.FeatureLayer.FeatureSource);
                    foreach (var item in columns)
                    {
                        newFilterConditionWindow.ViewModel.ColumnNameTypes[item.ColumnName] = item.IntermediateColumnType;
                    }
                }

                if (newFilterConditionWindow.ShowDialog().Value)
                {
                    var index = FilterConditions.IndexOf(v);
                    if (index != -1)
                    {
                        v = clonedViewModel;
                        FilterConditions.RemoveAt(index);
                        FilterConditions.Insert(index, v);
                    }
                }

                SyncConditions();
            });

            removeConditionCommand = new RelayCommand <FilterConditionViewModel>(v =>
            {
                if (FilterConditions.Contains(v))
                {
                    var messageBoxResult = System.Windows.Forms.MessageBox.Show(GisEditor.LanguageManager.GetStringResource("FilterStyleViewModelConditionRemovedMessage")
                                                                                , "Alert"
                                                                                , System.Windows.Forms.MessageBoxButtons.YesNo);

                    if (messageBoxResult == System.Windows.Forms.DialogResult.Yes)
                    {
                        FilterConditions.Remove(v);
                        SyncConditions();
                        RaisePropertyChanged("FilterConditions");
                    }
                }
            });

            testScriptCommand = new RelayCommand(() =>
            {
                try
                {
                    var result = actualFilterStyle.GetRequiredColumnNames().All(c => requiredValues.ColumnNames.Contains(c));
                    if (!result)
                    {
                        MessageBox.Show(GisEditor.LanguageManager.GetStringResource("FilterStyleUserControlColumnMessage"));
                    }
                    else
                    {
                        Collection <Feature> features   = new Collection <Feature>();
                        ScriptFilterCondition condition = actualFilterStyle.Conditions.OfType <ScriptFilterCondition>().FirstOrDefault();
                        if (condition != null)
                        {
                            condition.GetMatchingFeatures(features);
                        }
                        MessageBox.Show(GisEditor.LanguageManager.GetStringResource("TestPassMessage"));
                    }
                }
                catch (Exception ex)
                {
                    GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                    MessageBox.Show(ex.Message);
                }
            });

            viewDataCommand = new RelayCommand(() =>
            {
                DataViewerUserControl content = new DataViewerUserControl();
                content.ShowDialog();
            });

            showResultsCommand = new ObservedCommand(() =>
            {
                ShowFilteredData(requiredValues.FeatureLayer, FilterConditions.Select(f => f.FilterCondition), "");
            }, () => filterConditions.Count > 0);
        }
Exemple #2
0
        private string GetErrorMessage()
        {
            StringBuilder errorMessage = new StringBuilder();

            if (viewModel.FilterConditions.Where(f => f.MatchType.Key != FilterConditionType.DynamicLanguage).Any(c => string.IsNullOrEmpty(c.ColumnName)))
            {
                errorMessage.AppendLine("Column name cannot be empty.");
            }

            int bracketIndentify = 0;

            foreach (var condition in viewModel.FilterConditions)
            {
                if (condition.IsLeftBracket)
                {
                    bracketIndentify += 1;
                }
                if (condition.IsRightBracket)
                {
                    bracketIndentify -= 1;
                }

                if (bracketIndentify < 0)
                {
                    break;
                }
            }
            if (bracketIndentify != 0)
            {
                errorMessage.AppendLine("The closing bracket must pair with the nearest preceding opening bracket.");
            }

            FilterStyle style = viewModel.ActualObject as FilterStyle;

            if (style != null)
            {
                try
                {
                    var result = style.GetRequiredColumnNames().All(c => viewModel.RequiredValues.ColumnNames.Contains(c));
                    if (!result)
                    {
                        errorMessage.AppendLine(GisEditor.LanguageManager.GetStringResource("FilterStyleUserControlColumnMessage"));
                    }
                    else
                    {
                        Collection <Feature>  features  = new Collection <Feature>();
                        ScriptFilterCondition condition = style.Conditions.OfType <ScriptFilterCondition>().FirstOrDefault();
                        if (condition != null)
                        {
                            condition.GetMatchingFeatures(features);
                        }
                    }
                }
                catch (Exception ex)
                {
                    errorMessage.AppendLine(ex.Message);
                    GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                }
            }

            return(errorMessage.ToString());
        }