Index Numerator. Examples

1. The code snippet below creates an objects in Updater using IndexNumerator

  • c#
  • VB

private void CreateDocumentWith100Positions()
{
  var document = this.ObjectSpace.CreateObject<Document>();
  for (int i = 1; i <= 100; i++)
  {
    var pos = this.ObjectSpace.CreateObject<Position>();
    pos.Name = "Position_" + i;
    pos.Document = document;
  }
  this.ObjectSpace.CommitChanges();
  this.ObjectSpace.IndexNumerator().UseDirectSql = false / true;
  this.ObjectSpace.IndexNumerator().NumerateCollection(document.Positions[0]);
}

Private Sub CreateDocumentWith100Positions()
  Dim document = Me.ObjectSpace.CreateObject(Of Document)()
  Dim i As Integer = 1
  While i <= 100
    Dim pos = Me.ObjectSpace.CreateObject(Of Position)()
    pos.Name = "Position_" + i
    pos.Document = document
    i += 1
  End While
  Me.ObjectSpace.CommitChanges()
  Me.ObjectSpace.IndexNumerator().UseDirectSql = False / True
  Me.ObjectSpace.IndexNumerator().NumerateCollection(document.Positions(0))
End Sub

2. Code snippet demonstrates how to use IndexNumerator when creating new object, for example, using the controller for Detail View

  • c#
  • VB

protected override void OnActivated()
{
  base.OnActivated();
  if (!this.ObjectSpace.IsNewObject(this.View.CurrentObject))
    return;
  var obj = this.View.CurrentObject as IIndexNumberSupport;
  if (obj == null)
    return;
  this.ObjectSpace.IndexNumerator().UseDirectSql = false / true;
  this.ObjectSpace.IndexNumerator().Apply(obj);
}

Protected Overrides Sub OnActivated()
  MyBase.OnActivated()
  If Not Me.ObjectSpace.IsNewObject(Me.View.CurrentObject) Then
    Return
  End If
  Dim obj = TryCast(Me.View.CurrentObject, IIndexNumberSupport)
  If obj Is Nothing Then
    Return
  End If
  Me.ObjectSpace.IndexNumerator().UseDirectSql = False / True
  Me.ObjectSpace.IndexNumerator().Apply(obj)
End Sub

3. Removing and displacement. IndexNumerator is used in DeleteAction_Executing event handler

  • c#
  • VB

protected override void OnViewControlsCreated()
{
  base.OnViewControlsCreated();
  var deleteController = this.Frame.GetController<DeleteObjectsViewController>();
  deleteController.DeleteAction.Executing += DeleteAction_Executing;
}
void DeleteAction_Executing(object sender, CancelEventArgs e)
{
  var service = this.ObjectSpace.IndexNumerator();
  service.UseDirectSql = false / true;
  service.AdjustAfterRemove(this.View.SelectedObjects[0] as IIndexNumberSupport);
}

Protected Overrides Sub OnViewControlsCreated()
  MyBase.OnViewControlsCreated()
  Dim deleteController = Me.Frame.GetController(Of DeleteObjectsViewController)()
  deleteController.DeleteAction.Executing += DeleteAction_Executing
End Sub
Private Sub DeleteAction_Executing(ByVal sender As Object, ByVal e As CancelEventArgs)
  Dim service = Me.ObjectSpace.IndexNumerator()
  service.UseDirectSql = False / True
  service.AdjustAfterRemove(TryCast(Me.View.SelectedObjects(0), IIndexNumberSupport))
End Sub

5. Move object to top

  • c#
  • VB

private void moveToTopAction_Execute(object sender, SimpleActionExecuteEventArgs e)
{
  this.ObjectSpace.IndexNumerator().UseDirectSql = false / true;
  this.ObjectSpace.IndexNumerator().MoveToTop(this.View.SelectedObjects[0] as IIndexNumberSupport);
}

Private Sub moveToTopAction_Execute(ByVal sender As Object, ByVal e As SimpleActionExecuteEventArgs)
  Me.ObjectSpace.IndexNumerator().UseDirectSql = False / True
  Me.ObjectSpace.IndexNumerator().MoveToTop(TryCast(Me.View.SelectedObjects(0), IIndexNumberSupport))
End Sub

index_numerator_4

When counting the range of values or when an object is moved to the top or end of the list, the service recounts not all objects, but only those who need it. For example, object number is 50. If call MoveToTop method, then the new number will be 10.

5. Move object to last

  • c#
  • VB

private void moveToLast_Execute(object sender, SimpleActionExecuteEventArgs e)
{
  this.ObjectSpace.IndexNumerator().UseDirectSql = false / true;
  this.ObjectSpace.IndexNumerator().MoveToLast(this.View.SelectedObjects[0] as IIndexNumberSupport);
}

Private Sub moveToLast_Execute(ByVal sender As Object, ByVal e As SimpleActionExecuteEventArgs)
  Me.ObjectSpace.IndexNumerator().UseDirectSql = False / True
  Me.ObjectSpace.IndexNumerator().MoveToLast(TryCast(Me.View.SelectedObjects(0), IIndexNumberSupport))
End Sub

index_numerator_5

6. Assign an arbitrary number. For example, you can manually change the number in the list of objects. Implemented in Controller.

  • c#
  • VB

public partial class ChangeIndexViewController : ViewController<ListView>
{
  public ChangeIndexViewController()
  {
    InitializeComponent();
    TargetObjectType = typeof(Position);
  }
  private int _selectedObjectIndex;
  protected override void OnViewControlsCreated()
  {
    base.OnViewControlsCreated();
    this.View.SelectionChanged += View_SelectionChanged;
    var editor = this.View.Editor as GridListEditor;
    editor.GridView.CellValueChanged += GridView_CellValueChanged;
  }
  void View_SelectionChanged(object sender, EventArgs e)
  {
    //save current number of the focused object, it is required to build a range for recalculation
    _selectedObjectIndex = ((sender as ListView).SelectedObjects[0] as IIndexNumberSupport).IndexNumber;
  }
  void GridView_CellValueChanged(object sender, CellValueChangedEventArgs e)
  {
    this.ObjectSpace.IndexNumerator().UseDirectSql = false / true;
    this.ObjectSpace.IndexNumerator().AdjustInRangeByObject(this.View.SelectedObjects[0] as IIndexNumberSupport, selectedObjectIndex, (Int32)e.Value);
  }
}

Public Partial Class ChangeIndexViewController
  Inherits ViewController(Of ListView)
  Public Sub New()
    InitializeComponent()
    TargetObjectType = GetType(Position)
  End Sub
  Private __selectedObjectIndex As Integer
  Protected Overrides Sub OnViewControlsCreated()
    MyBase.OnViewControlsCreated()
    Me.View.SelectionChanged += View_SelectionChanged
    Dim editor = TryCast(Me.View.Editor, GridListEditor)
    editor.GridView.CellValueChanged += GridView_CellValueChanged
  End Sub
  Private Sub View_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
    'save current number of the focused object, it is required to build a range for recalculation
    __selectedObjectIndex = (TryCast((TryCast(sender, ListView)).SelectedObjects(0), IIndexNumberSupport)).IndexNumber
  End Sub
  Private Sub GridView_CellValueChanged(ByVal sender As Object, ByVal e As CellValueChangedEventArgs)
    Me.ObjectSpace.IndexNumerator().UseDirectSql = False / True
    Me.ObjectSpace.IndexNumerator().AdjustInRangeByObject(TryCast(Me.View.SelectedObjects(0), IIndexNumberSupport), selectedObjectIndex, CType(e.Value, Int32))
  End Sub
End Class

index_numerator_6