aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/WpfExtendedToolKit/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Zoombox/ZoomboxViewStack.cs
blob: 347e830f73e7c5b1530b48fcf1045bc744f263b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*************************************************************************************

   Extended WPF Toolkit

   Copyright (C) 2007-2013 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license 

   For more features, controls, and fast professional support,
   pick up the Plus Edition at http://xceed.com/wpf_toolkit

   Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids

  ***********************************************************************************/

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using Xceed.Wpf.Toolkit.Core;

namespace Xceed.Wpf.Toolkit.Zoombox
{
  public sealed class ZoomboxViewStack : Collection<ZoomboxView>, IWeakEventListener
  {
    #region Constructors

    public ZoomboxViewStack( Zoombox zoombox )
    {
      _zoomboxRef = new WeakReference( zoombox );
    }

    #endregion

    #region SelectedView Property

    public ZoomboxView SelectedView
    {
      get
      {
        int currentIndex = this.Zoombox.ViewStackIndex;
        return ( currentIndex < 0 || currentIndex > Count - 1 ) ? ZoomboxView.Empty : this[ currentIndex ];
      }
    }

    #endregion

    #region AreViewsFromSource Internal Property

    internal bool AreViewsFromSource
    {
      get
      {
        return _cacheBits[ ( int )CacheBits.AreViewsFromSource ];
      }
      set
      {
        _cacheBits[ ( int )CacheBits.AreViewsFromSource ] = value;
      }
    }

    #endregion

    #region Source Internal Property

    internal IEnumerable Source
    {
      get
      {
        return _source;
      }
    }

    // if the view stack is generated by items within the ViewStackSource collection
    // of the Zoombox, then we maintain a strong reference to the source
    private IEnumerable _source; //null

    #endregion

    #region IsChangeFromSource Private Property

    private bool IsChangeFromSource
    {
      get
      {
        return _cacheBits[ ( int )CacheBits.IsChangeFromSource ];
      }
      set
      {
        _cacheBits[ ( int )CacheBits.IsChangeFromSource ] = value;
      }
    }

    #endregion

    #region IsMovingViews Private Property

    private bool IsMovingViews
    {
      get
      {
        return _cacheBits[ ( int )CacheBits.IsMovingViews ];
      }
      set
      {
        _cacheBits[ ( int )CacheBits.IsMovingViews ] = value;
      }
    }

    #endregion

    #region IsResettingViews Private Property

    private bool IsResettingViews
    {
      get
      {
        return _cacheBits[ ( int )CacheBits.IsResettingViews ];
      }
      set
      {
        _cacheBits[ ( int )CacheBits.IsResettingViews ] = value;
      }
    }

    #endregion

    #region IsSettingInitialViewAfterClear Private Property

    private bool IsSettingInitialViewAfterClear
    {
      get
      {
        return _cacheBits[ ( int )CacheBits.IsSettingInitialViewAfterClear ];
      }
      set
      {
        _cacheBits[ ( int )CacheBits.IsSettingInitialViewAfterClear ] = value;
      }
    }

    #endregion

    #region Zoombox Private Property

    private Zoombox Zoombox
    {
      get
      {
        return _zoomboxRef.Target as Zoombox;
      }
    }

    // maintain a weak reference to the Zoombox that owns the stack
    private WeakReference _zoomboxRef;

    #endregion

    internal void ClearViewStackSource()
    {
      if( this.AreViewsFromSource )
      {
        this.AreViewsFromSource = false;
        this.MonitorSource( false );
        _source = null;
        using( new SourceAccess( this ) )
        {
          this.Clear();
        }
        this.Zoombox.CoerceValue( Zoombox.ViewStackModeProperty );
      }
    }

    internal void PushView( ZoomboxView view )
    {
      // clear the forward stack
      int currentIndex = this.Zoombox.ViewStackIndex;
      while( this.Count - 1 > currentIndex )
      {
        this.RemoveAt( Count - 1 );
      }
      this.Add( view );
    }

    internal void SetViewStackSource( IEnumerable source )
    {
      if( _source != source )
      {
        this.MonitorSource( false );
        _source = source;
        this.MonitorSource( true );
        this.AreViewsFromSource = true;
        this.Zoombox.CoerceValue( Zoombox.ViewStackModeProperty );
        this.ResetViews();
      }
    }

    protected override void ClearItems()
    {
      this.VerifyStackModification();

      bool currentDeleted = ( this.Zoombox.CurrentViewIndex >= 0 );
      base.ClearItems();
      this.Zoombox.SetViewStackCount( Count );

      // if resetting the views due to a change in the view source collection, just return
      if( this.IsResettingViews )
        return;

      if( this.Zoombox.EffectiveViewStackMode == ZoomboxViewStackMode.Auto && this.Zoombox.CurrentView != ZoomboxView.Empty )
      {
        this.IsSettingInitialViewAfterClear = true;
        try
        {
          this.Add( this.Zoombox.CurrentView );
        }
        finally
        {
          this.IsSettingInitialViewAfterClear = false;
        }
        this.Zoombox.ViewStackIndex = 0;
        if( currentDeleted )
        {
          this.Zoombox.SetCurrentViewIndex( 0 );
        }
      }
      else
      {
        this.Zoombox.ViewStackIndex = -1;
        this.Zoombox.SetCurrentViewIndex( -1 );
      }
    }

    protected override void InsertItem( int index, ZoomboxView view )
    {
      this.VerifyStackModification();

      if( this.Zoombox.HasArrangedContentPresenter
          && this.Zoombox.ViewStackIndex >= index
          && !this.IsSettingInitialViewAfterClear
          && !this.IsResettingViews
          && !this.IsMovingViews )
      {
        bool oldUpdatingView = this.Zoombox.IsUpdatingView;
        this.Zoombox.IsUpdatingView = true;
        try
        {
          this.Zoombox.ViewStackIndex++;
          if( this.Zoombox.CurrentViewIndex != -1 )
          {
            this.Zoombox.SetCurrentViewIndex( this.Zoombox.CurrentViewIndex + 1 );
          }
        }
        finally
        {
          this.Zoombox.IsUpdatingView = oldUpdatingView;
        }
      }

      base.InsertItem( index, view );
      this.Zoombox.SetViewStackCount( Count );
    }

    protected override void RemoveItem( int index )
    {
      this.VerifyStackModification();

      bool currentDeleted = ( this.Zoombox.ViewStackIndex == index );
      if( !this.IsMovingViews )
      {
        // if an item below the current index was deleted 
        // (or if the last item is currently selected and it was deleted),
        // adjust the ViewStackIndex and CurrentViewIndex values
        if( this.Zoombox.HasArrangedContentPresenter
            && ( this.Zoombox.ViewStackIndex > index
                || ( currentDeleted && this.Zoombox.ViewStackIndex == this.Zoombox.ViewStack.Count - 1 ) ) )
        {
          // if removing the last item, just clear the stack, which ensures the proper
          // behavior based on the ViewStackMode
          if( currentDeleted && this.Zoombox.ViewStack.Count == 1 )
          {
            this.Clear();
            return;
          }

          bool oldUpdatingView = this.Zoombox.IsUpdatingView;
          this.Zoombox.IsUpdatingView = true;
          try
          {
            this.Zoombox.ViewStackIndex--;
            if( this.Zoombox.CurrentViewIndex != -1 )
            {
              this.Zoombox.SetCurrentViewIndex( this.Zoombox.CurrentViewIndex - 1 );
            }
          }
          finally
          {
            this.Zoombox.IsUpdatingView = oldUpdatingView;
          }
        }
      }

      base.RemoveItem( index );

      // if the current view was deleted, we may need to update the view index 
      // (unless a non-stack view is in effect)
      if( !this.IsMovingViews && currentDeleted && this.Zoombox.CurrentViewIndex != -1 )
      {
        this.Zoombox.RefocusView();
      }

      this.Zoombox.SetViewStackCount( Count );
    }

    protected override void SetItem( int index, ZoomboxView view )
    {
      this.VerifyStackModification();

      base.SetItem( index, view );

      // if the set item is the current item, update the zoombox
      if( index == this.Zoombox.CurrentViewIndex )
      {
        this.Zoombox.RefocusView();
      }
    }

    private static ZoomboxView GetViewFromSourceItem( object item )
    {
      ZoomboxView view = ( item is ZoomboxView ) ? item as ZoomboxView : ZoomboxViewConverter.Converter.ConvertFrom( item ) as ZoomboxView;
      if( view == null )
        throw new InvalidCastException( string.Format( ErrorMessages.GetMessage( "UnableToConvertToZoomboxView" ), item ) );

      return view;
    }

    private void InsertViews( int index, IList newItems )
    {
      using( new SourceAccess( this ) )
      {
        foreach( object item in newItems )
        {
          ZoomboxView view = ZoomboxViewStack.GetViewFromSourceItem( item );
          if( index >= this.Count )
          {
            this.Add( view );
          }
          else
          {
            this.Insert( index, view );
          }
          index++;
        }
      }
    }

    private void MonitorSource( bool monitor )
    {
      if( _source != null && ( _source is INotifyCollectionChanged ) )
      {
        if( monitor )
        {
          CollectionChangedEventManager.AddListener( _source as INotifyCollectionChanged, this );
        }
        else
        {
          CollectionChangedEventManager.RemoveListener( _source as INotifyCollectionChanged, this );
        }
      }
    }

    private void MoveViews( int oldIndex, int newIndex, IList movedItems )
    {
      using( new SourceAccess( this ) )
      {
        int currentIndex = this.Zoombox.ViewStackIndex;
        int indexAfterMove = currentIndex;

        // adjust the current index, if it was affected by the move
        if( !( ( oldIndex < currentIndex && newIndex < currentIndex )
            || ( oldIndex > currentIndex && newIndex > currentIndex ) ) )
        {
          if( currentIndex >= oldIndex && currentIndex < oldIndex + movedItems.Count )
          {
            indexAfterMove += newIndex - oldIndex;
          }
          else if( currentIndex >= newIndex )
          {
            indexAfterMove += movedItems.Count;
          }
        }

        this.IsMovingViews = true;
        try
        {
          for( int i = 0; i < movedItems.Count; i++ )
          {
            this.RemoveAt( oldIndex );
          }
          for( int i = 0; i < movedItems.Count; i++ )
          {
            this.Insert( newIndex + i, ZoomboxViewStack.GetViewFromSourceItem( movedItems[ i ] ) );
          }
          if( indexAfterMove != currentIndex )
          {
            this.Zoombox.ViewStackIndex = indexAfterMove;
            this.Zoombox.SetCurrentViewIndex( indexAfterMove );
          }
        }
        finally
        {
          this.IsMovingViews = false;
        }
      }
    }

    private void OnSourceCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
    {
      switch( e.Action )
      {
        case NotifyCollectionChangedAction.Add:
          this.InsertViews( e.NewStartingIndex, e.NewItems );
          break;

        case NotifyCollectionChangedAction.Move:
          this.MoveViews( e.OldStartingIndex, e.NewStartingIndex, e.OldItems );
          break;

        case NotifyCollectionChangedAction.Remove:
          this.RemoveViews( e.OldStartingIndex, e.OldItems );
          break;

        case NotifyCollectionChangedAction.Replace:
          this.ResetViews();
          break;

        case NotifyCollectionChangedAction.Reset:
          this.ResetViews();
          break;
      }
    }

    private void ResetViews()
    {
      using( new SourceAccess( this ) )
      {
        int currentIndex = this.Zoombox.ViewStackIndex;
        this.IsResettingViews = true;
        try
        {
          this.Clear();
          foreach( object item in _source )
          {
            ZoomboxView view = ZoomboxViewStack.GetViewFromSourceItem( item );
            this.Add( view );
          }

          currentIndex = Math.Min( Math.Max( 0, currentIndex ), this.Count - 1 );

          this.Zoombox.ViewStackIndex = currentIndex;
          this.Zoombox.SetCurrentViewIndex( currentIndex );
          this.Zoombox.RefocusView();
        }
        finally
        {
          this.IsResettingViews = false;
        }
      }
    }

    private void RemoveViews( int index, IList removedItems )
    {
      using( new SourceAccess( this ) )
      {
        for( int i = 0; i < removedItems.Count; i++ )
        {
          this.RemoveAt( index );
        }
      }
    }

    private void VerifyStackModification()
    {
      if( this.AreViewsFromSource && !this.IsChangeFromSource )
        throw new InvalidOperationException( ErrorMessages.GetMessage( "ViewStackCannotBeManipulatedNow" ) );
    }

    #region IWeakEventListener Members

    public bool ReceiveWeakEvent( Type managerType, object sender, EventArgs e )
    {
      if( managerType == typeof( CollectionChangedEventManager ) )
      {
        this.OnSourceCollectionChanged( sender, ( NotifyCollectionChangedEventArgs )e );
      }
      else
      {
        return false;
      }

      return true;
    }

    #endregion

    #region Private Fields

    // to save memory, store bool variables in a bit vector
    private BitVector32 _cacheBits = new BitVector32( 0 );

    #endregion

    #region SourceAccess Nested Type

    private sealed class SourceAccess : IDisposable
    {
      public SourceAccess( ZoomboxViewStack viewStack )
      {
        _viewStack = viewStack;
        _viewStack.IsChangeFromSource = true;
      }

      ~SourceAccess()
      {
        this.Dispose();
      }

      public void Dispose()
      {
        _viewStack.IsChangeFromSource = false;
        _viewStack = null;
        GC.SuppressFinalize( this );
      }

      private ZoomboxViewStack _viewStack;
    }

    #endregion

    #region CacheBits Nested Type

    private enum CacheBits
    {
      AreViewsFromSource = 0x00000001,
      IsChangeFromSource = 0x00000002,
      IsResettingViews = 0x00000004,
      IsMovingViews = 0x00000008,
      IsSettingInitialViewAfterClear = 0x00000010,
    }

    #endregion
  }
}