/// <summary> /// Loads widget data from a stream whose format must match the specified format that is expected. /// </summary> /// <param name="stream">Stream object which contains serialized data</param> /// <param name="format">Identifies the expected format of the stream being read</param> public void Load(Stream stream, WidgetDocSaveFormat format) { WidgetContainerData container = null; if (format == WidgetDocSaveFormat.Xml) { using (XmlTextReader reader = new XmlTextReader(stream)) { reader.MoveToContent(); container = (WidgetContainerData)WidgetData.Create(reader); } } else if (format == WidgetDocSaveFormat.Binary) { IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); container = (WidgetContainerData)formatter.Deserialize(stream); } else { throw new InvalidOperationException(string.Format("Invalid save mode specified ({0})", (int)format)); } container.PostDeserialize(); this.Root = container; }
/// <summary> /// Factory method for instanting widget objects based on the data passed in through xml /// </summary> /// <param name="reader">XML reader which is used to read serialized data object hierarchy</param> /// <returns>Newly created widget data instance</returns> public static WidgetData Create(XmlReader reader) { WidgetData widget = null; // LATER: This method breaks OCP since addition/removal of widget types will require modification of the // base class which is not desirable. For now we'll keep it this way, but if this method is revisited, we // should consider creating a stand-alone factory which could potentially use reflection to dynamically // build a list of creatable widgets. switch (reader.Name) { case "WidgetContainerData": widget = new WidgetContainerData(); break; case "FolderWidgetData": widget = new FolderWidgetData(); break; default: // Unknown element, skip to next one reader.Skip(); break; } if (widget != null) { (( IXmlSerializable)widget).ReadXml(reader); } return(widget); }
/// <summary> /// This method is invoked whenever a widget is removed /// </summary> /// <param name="location">Location where a widget is being removed</param> /// <param name="widget">Reference to a widget that was removed</param> protected virtual void OnWidgetRemoved(MatrixLoc location, WidgetData widget) { if (_containerChangedEvent != null) { _containerChangedEvent(this, new WidgetContainerChangedEventArgs( NotifyCollectionChangedAction.Remove, location, widget, null)); } }
private void OnChildIsDirtyChanged(object sender, EventArgs e) { WidgetData child = (WidgetData)sender; if (child.IsDirty) { IsDirty = true; } }
/// <summary> /// Initializing constructor /// </summary> /// <param name="action">Specifies if a widget was added, removed or replaced</param> /// <param name="location">Location of the changed widget</param> /// <param name="oldWidget">Reference to the original widget, or null if there wasn't any</param> /// <param name="newWidget">Reference to the new widget, or null if there isn't any</param> public WidgetContainerChangedEventArgs(NotifyCollectionChangedAction action, MatrixLoc location, WidgetData oldWidget, WidgetData newWidget) { _location = location; _action = action; _oldWidget = oldWidget; _newWidget = newWidget; }
/// <summary> /// Initializing constructor /// </summary> /// <param name="action">Specifies if a widget was added, removed or replaced</param> /// <param name="location">Location of the changed widget</param> /// <param name="oldWidget">Reference to the original widget, or null if there wasn't any</param> /// <param name="newWidget">Reference to the new widget, or null if there isn't any</param> public WidgetContainerChangedEventArgs( NotifyCollectionChangedAction action, MatrixLoc location, WidgetData oldWidget, WidgetData newWidget ) { _location = location; _action = action; _oldWidget = oldWidget; _newWidget = newWidget; }
/// <inheritdoc/> protected override void DeserializeFromXml(XmlReader reader) { _containerBounds.Row = XmlConvert.ToInt32(reader.GetAttribute("firstRow")); _containerBounds.Column = XmlConvert.ToInt32(reader.GetAttribute("firstColumn")); MatrixSize size = new MatrixSize( XmlConvert.ToInt32(reader.GetAttribute("rowCount")), XmlConvert.ToInt32(reader.GetAttribute("columnCount"))); Resize(size, 0); if (!reader.Read()) { return; } while (reader.NodeType != XmlNodeType.EndElement) { if (reader.NodeType == XmlNodeType.Element) { if (string.Compare(reader.Name, "Children", true) == 0) { reader.Read(); while (reader.NodeType != XmlNodeType.EndElement) { if (reader.NodeType == XmlNodeType.Element) { WidgetData widget = WidgetData.Create(reader); this[widget.Location] = widget; } else { reader.Read(); } } reader.ReadEndElement(); } else { reader.Skip(); } } else { reader.Read(); } } }
private WidgetData ClearWidget(MatrixLoc loc) { MatrixLoc arrayIndex = _containerBounds.ToIndex(loc); WidgetData widget = _widgetArray[arrayIndex]; _widgetArray[arrayIndex] = null; if (widget != null) { widget.ClearOwner(); widget.IsDirtyChanged -= OnChildIsDirtyChanged; } return(widget); }
/// <summary> /// Removes specified widget from the container. /// </summary> /// <param name="widget">Widget to be removed. If the widget isn't in the current container, no action /// will take place</param> public void Remove(WidgetData widget) { if (widget.Parent == this) { if (this[widget.Location] == widget) { this[widget.Location] = null; } else { System.Diagnostics.Debug.Assert(false, "widget location doesn't match its parent!!"); throw new InvalidProgramException(); } } Validate(); }
private void SetWidget(MatrixLoc location, WidgetData widget) { WidgetData removedWidget = null; Validate(); // the same widget is being inserted at the same spot where it is already located, // let not do anything and just exit if (widget != null && widget.Parent == this && widget.Location == location) { return; } MatrixRect neededBounds = _containerBounds.GrowTo(location); // If the cell being modified within the bounds of the current array, let's make sure // the existing cell is empty. Otherwise, we need to reallocate the array before // we can assign the widget to it. if (neededBounds.Size == _containerBounds.Size) { removedWidget = ClearWidget(location); } else if (widget != null) { WidgetContainerResizeJustify resizeJustify = (_containerBounds.Row == neededBounds.Row ? WidgetContainerResizeJustify.Top : WidgetContainerResizeJustify.Bottom) | (_containerBounds.Column == neededBounds.Column ? WidgetContainerResizeJustify.Left : WidgetContainerResizeJustify.Right); Resize(neededBounds.Size, resizeJustify); } Validate(); if (widget != null) { if (widget.HasOwner) { widget.Parent.Remove(widget); } _widgetArray[_containerBounds.ToIndex(location)] = widget; widget.SetOwner(this, location); widget.IsDirtyChanged += OnChildIsDirtyChanged; if (removedWidget != null) { OnWidgetReplaced(location, removedWidget, widget); removedWidget = null; } else { OnWidgetAdded(location, widget); } } if (removedWidget != null) { OnWidgetRemoved(location, removedWidget); } Validate(); IsDirty = true; }
/// <summary> /// Associates a widget with the current site /// </summary> /// <param name="widget">Data object representing the widget</param> public void SetWidget(Data.WidgetData widget) { ParentContainer.Source[this.Location] = widget; }
private void SetWidget( MatrixLoc location, WidgetData widget ) { WidgetData removedWidget = null; Validate(); // the same widget is being inserted at the same spot where it is already located, // let not do anything and just exit if( widget != null && widget.Parent == this && widget.Location == location ) return; MatrixRect neededBounds = _containerBounds.GrowTo( location ); // If the cell being modified within the bounds of the current array, let's make sure // the existing cell is empty. Otherwise, we need to reallocate the array before // we can assign the widget to it. if( neededBounds.Size == _containerBounds.Size ) { removedWidget = ClearWidget( location ); } else if( widget != null ) { WidgetContainerResizeJustify resizeJustify = ( _containerBounds.Row == neededBounds.Row ? WidgetContainerResizeJustify.Top : WidgetContainerResizeJustify.Bottom ) | ( _containerBounds.Column == neededBounds.Column ? WidgetContainerResizeJustify.Left : WidgetContainerResizeJustify.Right ); Resize( neededBounds.Size, resizeJustify ); } Validate(); if( widget != null ) { if( widget.HasOwner ) { widget.Parent.Remove( widget ); } _widgetArray[ _containerBounds.ToIndex( location ) ] = widget; widget.SetOwner( this, location ); widget.IsDirtyChanged += OnChildIsDirtyChanged; if( removedWidget != null ) { OnWidgetReplaced( location, removedWidget, widget ); removedWidget = null; } else { OnWidgetAdded( location, widget ); } } if( removedWidget != null ) { OnWidgetRemoved( location, removedWidget ); } Validate(); IsDirty = true; }
/// <summary> /// This method is invoked whenever a widget is replaced /// </summary> /// <param name="location">Location where a widget was replaced</param> /// <param name="oldWidget">Reference to an old widget that was replaced</param> /// <param name="newWidget">Reference to a new widget that is being inserted</param> protected virtual void OnWidgetReplaced( MatrixLoc location, WidgetData oldWidget, WidgetData newWidget ) { if( _containerChangedEvent != null ) { _containerChangedEvent( this, new WidgetContainerChangedEventArgs( NotifyCollectionChangedAction.Replace, location, oldWidget, newWidget ) ); } }
/// <summary> /// This method is invoked whenever a widget is added /// </summary> /// <param name="location">Location where new widget was added</param> /// <param name="widget">Reference to a widget that was added</param> protected virtual void OnWidgetAdded( MatrixLoc location, WidgetData widget ) { if( _containerChangedEvent != null ) { _containerChangedEvent( this, new WidgetContainerChangedEventArgs( NotifyCollectionChangedAction.Add, location, null, widget ) ); } }
/// <summary> /// Removes specified widget from the container. /// </summary> /// <param name="widget">Widget to be removed. If the widget isn't in the current container, no action /// will take place</param> public void Remove( WidgetData widget ) { if( widget.Parent == this ) { if( this[ widget.Location ] == widget ) { this[ widget.Location ] = null; } else { System.Diagnostics.Debug.Assert( false, "widget location doesn't match its parent!!" ); throw new InvalidProgramException(); } } Validate(); }