aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/WpfExtendedToolKit/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.AvalonDock/Layout/LayoutGroup.cs
blob: 9c1fc6f92e4b1fc51cb0253a1e500f7c43e9af09 (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
/*************************************************************************************

   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.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Xml.Serialization;

namespace Xceed.Wpf.AvalonDock.Layout
{
  [Serializable]
  public abstract class LayoutGroup<T> : LayoutGroupBase, ILayoutContainer, ILayoutGroup, IXmlSerializable where T : class, ILayoutElement
  {
    #region Members

    ObservableCollection<T> _children = new ObservableCollection<T>();

    #endregion

    #region Constructors

    internal LayoutGroup()
    {
      _children.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler( _children_CollectionChanged );
    }

    #endregion

    #region Properties

    #region Children

    public ObservableCollection<T> Children
    {
      get
      {
        return _children;
      }
    }

    #endregion

    #region IsVisible

    private bool _isVisible = true;
    public bool IsVisible
    {
      get
      {
        return _isVisible;
      }
      protected set
      {
        if( _isVisible != value )
        {
          RaisePropertyChanging( "IsVisible" );
          _isVisible = value;
          OnIsVisibleChanged();
          RaisePropertyChanged( "IsVisible" );
        }
      }
    }

    #endregion

    #region ChildrenCount

    public int ChildrenCount
    {
      get
      {
        return _children.Count;
      }
    }

    #endregion

    #endregion

    #region Overrides

    protected override void OnParentChanged( ILayoutContainer oldValue, ILayoutContainer newValue )
    {
      base.OnParentChanged( oldValue, newValue );

      ComputeVisibility();
    }

    #endregion

    #region Public Methods

    public void ComputeVisibility()
    {
      IsVisible = GetVisibility();
    }

    public void MoveChild( int oldIndex, int newIndex )
    {
      if( oldIndex == newIndex )
        return;
      _children.Move( oldIndex, newIndex );
      ChildMoved( oldIndex, newIndex );
    }

    public void RemoveChildAt( int childIndex )
    {
      _children.RemoveAt( childIndex );
    }

    public int IndexOfChild( ILayoutElement element )
    {
      return _children.Cast<ILayoutElement>().ToList().IndexOf( element );
    }

    public void InsertChildAt( int index, ILayoutElement element )
    {
      _children.Insert( index, ( T )element );
    }

    public void RemoveChild( ILayoutElement element )
    {
      _children.Remove( ( T )element );
    }

    public void ReplaceChild( ILayoutElement oldElement, ILayoutElement newElement )
    {
      int index = _children.IndexOf( ( T )oldElement );
      _children.Insert( index, ( T )newElement );
      _children.RemoveAt( index + 1 );
    }

    public void ReplaceChildAt( int index, ILayoutElement element )
    {
      _children[ index ] = ( T )element;
    }


    public System.Xml.Schema.XmlSchema GetSchema()
    {
      return null;
    }

    public virtual void ReadXml( System.Xml.XmlReader reader )
    {
      reader.MoveToContent();
      if( reader.IsEmptyElement )
      {
        reader.Read();
        ComputeVisibility();
        return;
      }
      string localName = reader.LocalName;
      reader.Read();
      while( true )
      {
        if( ( reader.LocalName == localName ) &&
            ( reader.NodeType == System.Xml.XmlNodeType.EndElement ) )
        {
          break;
        }
        if( reader.NodeType == System.Xml.XmlNodeType.Whitespace )
        {
          reader.Read();
          continue;
        }

        XmlSerializer serializer = null;
        if( reader.LocalName == "LayoutAnchorablePaneGroup" )
          serializer = new XmlSerializer( typeof( LayoutAnchorablePaneGroup ) );
        else if( reader.LocalName == "LayoutAnchorablePane" )
          serializer = new XmlSerializer( typeof( LayoutAnchorablePane ) );
        else if( reader.LocalName == "LayoutAnchorable" )
          serializer = new XmlSerializer( typeof( LayoutAnchorable ) );
        else if( reader.LocalName == "LayoutDocumentPaneGroup" )
          serializer = new XmlSerializer( typeof( LayoutDocumentPaneGroup ) );
        else if( reader.LocalName == "LayoutDocumentPane" )
          serializer = new XmlSerializer( typeof( LayoutDocumentPane ) );
        else if( reader.LocalName == "LayoutDocument" )
          serializer = new XmlSerializer( typeof( LayoutDocument ) );
        else if( reader.LocalName == "LayoutAnchorGroup" )
          serializer = new XmlSerializer( typeof( LayoutAnchorGroup ) );
        else if( reader.LocalName == "LayoutPanel" )
          serializer = new XmlSerializer( typeof( LayoutPanel ) );
        else
        {
          Type type = this.FindType( reader.LocalName );
          if( type == null )
            throw new ArgumentException( "AvalonDock.LayoutGroup doesn't know how to deserialize " + reader.LocalName );
          serializer = new XmlSerializer( type );
        }

        Children.Add( ( T )serializer.Deserialize( reader ) );
      }

      reader.ReadEndElement();
    }

    public virtual void WriteXml( System.Xml.XmlWriter writer )
    {
      foreach( var child in Children )
      {
        var type = child.GetType();
        XmlSerializer serializer = new XmlSerializer( type );
        serializer.Serialize( writer, child );
      }

    }

    #endregion

    #region Internal Methods

    protected virtual void OnIsVisibleChanged()
    {
      UpdateParentVisibility();
    }

    protected abstract bool GetVisibility();

    protected virtual void ChildMoved( int oldIndex, int newIndex )
    {
    }

    #endregion

    #region Private Methods

    private void _children_CollectionChanged( object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e )
    {
      if( e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove ||
          e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace )
      {
        if( e.OldItems != null )
        {
          foreach( LayoutElement element in e.OldItems )
          {
            if( element.Parent == this )
              element.Parent = null;
          }
        }
      }

      if( e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
          e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace )
      {
        if( e.NewItems != null )
        {
          foreach( LayoutElement element in e.NewItems )
          {
            if( element.Parent != this )
            {
              if( element.Parent != null )
                element.Parent.RemoveChild( element );
              element.Parent = this;
            }

          }
        }
      }

      ComputeVisibility();
      OnChildrenCollectionChanged();
      NotifyChildrenTreeChanged( ChildrenTreeChange.DirectChildrenChanged );
      RaisePropertyChanged( "ChildrenCount" );
    }

    private void UpdateParentVisibility()
    {
      var parentPane = Parent as ILayoutElementWithVisibility;
      if( parentPane != null )
        parentPane.ComputeVisibility();
    }

    private Type FindType( string name )
    {
      foreach( var a in AppDomain.CurrentDomain.GetAssemblies() )
      {
        foreach( var t in a.GetTypes() )
        {
          if( t.Name.Equals( name ) )
            return t;
        }
      }
      return null;
    }

    #endregion

    #region ILayoutContainer Interface

    IEnumerable<ILayoutElement> ILayoutContainer.Children
    {
      get
      {
        return _children.Cast<ILayoutElement>();
      }
    }

    #endregion

  }
}