Exemple #1
0
        public override void RunCommand(TableEditorCommand command)
        {
            if (HandleLayout(command))
            {
                return;
            }

            var table = ActiveTable;

            if (table == null)
            {
                return;
            }

            if (!ValidateColumnIndex(command))
            {
                return;
            }

            if (!RequestEditSession(command))
            {
                return;
            }

            if (!IsAvailableForMemoryLayer(command))
            {
                return;
            }

            switch (command)
            {
            case TableEditorCommand.RemoveSelected:
                var gp = GeoProcessingService;
                if (gp != null)
                {
                    gp.RemoveSelectedShapes(View.ActiveLayerHandle, true);
                    View.UpdateView();
                }
                break;

            case TableEditorCommand.FormatValues:
                AppConfig.Instance.TableEditorFormatValues = !AppConfig.Instance.TableEditorFormatValues;
                UpdataDatasourceAndUI();
                break;

            case TableEditorCommand.RecalculateFields:
                if (!table.Fields.Any(f => string.IsNullOrWhiteSpace(f.Expression)))
                {
                    MessageService.Current.Info("There are no fields with previously calculate expressions.");
                    return;
                }

                if (_context.Container.Run <RecalculateFieldsPresenter, IAttributeTable>(table))
                {
                    UpdataDatasourceAndUI();
                }
                break;

            case TableEditorCommand.ClearSorting:
            {
                var grid = View.ActiveGrid;
                if (grid != null)
                {
                    grid.ClearSorting();
                }
            }
            break;

            case TableEditorCommand.AttributeExplorer:
            {
                var layer = _context.Layers.ItemByHandle(View.ActiveLayerHandle);
                if (layer != null)
                {
                    _context.Container.Run <AttributeExplorerPresenter, ILayer>(layer);
                }
            }
            break;

            case TableEditorCommand.Query:
            {
                var layer = _context.Layers.ItemByHandle(View.ActiveLayerHandle);
                if (layer != null)
                {
                    var model = new QueryBuilderModel(layer, string.Empty);
                    _context.Container.Run <QueryBuilderPresenter, QueryBuilderModel>(model);
                }
            }
            break;

            case TableEditorCommand.ExportSelected:
                var fs = View.ActiveFeatureSet;
                if (fs != null)
                {
                    fs.ExportSelected(_dialogService, _layerService);
                }
                break;

            case TableEditorCommand.StopJoins:
                if (!table.Joins.Any())
                {
                    MessageService.Current.Info("There are no joins to stop.");
                    return;
                }

                if (MessageService.Current.Ask("Do you want to stop all joins for this table?"))
                {
                    table.StopAllJoins();
                    UpdataDatasourceAndUI();
                }
                break;

            case TableEditorCommand.ImportFieldDefinitions:
                if (DbfImportHelper.ImportFieldsFromDbf(_dialogService, table))
                {
                    UpdataDatasourceAndUI();
                }
                break;

            case TableEditorCommand.ReloadTable:
                View.UpdateDatasource();
                break;

            case TableEditorCommand.ShowAliases:
                AppConfig.Instance.TableEditorShowAliases = !AppConfig.Instance.TableEditorShowAliases;
                View.UpdateDatasource();
                break;

            case TableEditorCommand.ShowAllFields:
                if (table.Fields.Any(f => !f.Visible))
                {
                    if (MessageService.Current.Ask("Do you want to show hidden fields?"))
                    {
                        foreach (var fld in table.Fields)
                        {
                            fld.Visible = true;
                        }

                        View.UpdateDatasource();
                    }
                }
                else
                {
                    MessageService.Current.Info("All fields are already visible.");
                }
                break;

            case TableEditorCommand.FieldSortAsc:
            case TableEditorCommand.FieldSortDesc:
            {
                var grid = View.ActiveGrid;
                if (grid != null)
                {
                    grid.SortByColumn(View.ActiveColumnIndex, command == TableEditorCommand.FieldSortAsc);
                }
            }
            break;

            case TableEditorCommand.FieldHide:
            {
                var grid = View.ActiveGrid;
                if (grid != null)
                {
                    var fld = grid.GetField(View.ActiveColumnIndex);
                    var cmn = grid.Columns[View.ActiveColumnIndex];
                    if (fld != null && cmn != null)
                    {
                        cmn.Visible = false;
                        fld.Visible = false;
                    }
                }
            }
            break;

            case TableEditorCommand.RemoveField:
            {
                var grid = View.ActiveGrid;
                if (grid != null)
                {
                    var fld = grid.GetField(View.ActiveColumnIndex);

                    if (MessageService.Current.Ask("Do you want to remove field: " + fld.Name + "?"))
                    {
                        var fields = table.Fields;
                        int index  = fields.IndexOf(fld);

                        if (fields.Remove(index))
                        {
                            UpdataDatasourceAndUI();
                        }
                    }
                }
            }
            break;

            case TableEditorCommand.FieldProperties:
            case TableEditorCommand.AddField:
            {
                var grid = View.ActiveGrid;
                if (grid != null)
                {
                    bool newField = command == TableEditorCommand.AddField;
                    var  fld      = newField ? null : grid.GetField(View.ActiveColumnIndex);

                    bool allowEditing = FeatureSet.CanEditTable();
                    var  model        = new FieldPropertiesModel(table, fld, newField, allowEditing);

                    if (_context.Container.Run <FieldPropertiesPresenter, FieldPropertiesModel>(model))
                    {
                        if (allowEditing || newField)
                        {
                            model.Field.Modified = true;
                            UpdataDatasourceAndUI();
                        }
                    }
                }
            }
            break;

            case TableEditorCommand.FieldStats:
            {
                var grid = View.ActiveGrid;
                if (grid != null)
                {
                    var fld = grid.GetField(View.ActiveColumnIndex);
                    if (fld.Type == Api.Enums.AttributeType.String)
                    {
                        MessageService.Current.Info("Statistics aren't available for text fields.");
                        return;
                    }

                    var model = new FieldStatsModel(table, fld.Index);
                    _context.Container.Run <FieldStatsPresenter, FieldStatsModel>(model);
                }
            }
            break;

            case TableEditorCommand.UpdateMeasurements:
                if (_context.Container.Run <UpdateMeasurementsPresenter, IFeatureSet>(View.ActiveFeatureSet))
                {
                    UpdataDatasourceAndUI();
                }
                break;

            case TableEditorCommand.Join:
                _context.Container.Run <JoinsPresenter, IAttributeTable>(table);
                UpdataDatasourceAndUI();
                break;

            case TableEditorCommand.StartEdit:
                StartEditing();
                break;

            case TableEditorCommand.DiscardChanges:
                StopEditing(FeatureSet, false);
                break;

            case TableEditorCommand.SaveChanges:
                StopEditing(FeatureSet, true);
                break;

            case TableEditorCommand.CalculateField:
            {
                var model = new FieldCalculatorModel(table, table.Fields[View.ActiveColumnIndex]);
                if (_context.Container.Run <FieldCalculatorPresenter, FieldCalculatorModel>(model))
                {
                    UpdataDatasourceAndUI();
                }
            }
            break;

            case TableEditorCommand.RemoveFields:
                if (_context.Container.Run <DeleteFieldsPresenter, IAttributeTable>(table))
                {
                    UpdataDatasourceAndUI();
                }
                break;

            case TableEditorCommand.ShowSelected:
                ShowSelected();
                break;

            case TableEditorCommand.ZoomToCurrentCell:
            {
                var grid = View.ActiveGrid;
                if (grid != null && grid.CurrentCell != null)
                {
                    int rowIndex = grid.CurrentCell.RowIndex;
                    if (rowIndex != -1)
                    {
                        int shapeIndex = grid.RowManager.RealIndex(rowIndex);
                        _context.Map.ZoomToShape(View.ActiveLayerHandle, shapeIndex);
                        grid.Focus();
                    }
                }
                else
                {
                    MessageService.Current.Info("No shape is currently being edited.");
                }
            }
            break;

            case TableEditorCommand.ZoomToSelected:
            {
                _context.Map.ZoomToSelected(View.ActiveLayerHandle);
                var grid = View.ActiveGrid;
                if (grid != null)
                {
                    grid.Focus();
                }
            }
            break;

            case TableEditorCommand.SelectAll:
                FeatureSet.SelectAll();
                OnViewSelectionChanged(View.ActiveLayerHandle);
                break;

            case TableEditorCommand.ClearSelection:
                FeatureSet.ClearSelection();
                OnViewSelectionChanged(View.ActiveLayerHandle);
                break;

            case TableEditorCommand.InvertSelection:
                FeatureSet.InvertSelection();
                OnViewSelectionChanged(View.ActiveLayerHandle);
                break;

            case TableEditorCommand.Find:
                Find(false);
                break;

            case TableEditorCommand.Replace:
                Find(true);
                break;
            }
        }