#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Ingenious.Mvc.Util;
using Ingenious.Mvc.Views;
using Ingenious.Mvc.Windows.Forms.ViewManagers;
namespace Ingenious.Mvc.Windows.Forms.Views{
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="T:UserControlBase"]/*'/>
public class UserControlBase : UserControl, IUserControlView, IUserControlContainer
{
private Id<IView> _id;
private Task _task;
private IController _controller;
private object _data;
private Form _owningForm;
private IDictionary<string, Control> _containerControls;
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="P:Id"]/*'/>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Id<IView> Id
{
get
{
return _id;
}
set
{
ExceptionHelper.ThrowIf(!_id.IsEmpty, "Id.alreadySet", _id, value);
_id = value;
}
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="P:Task"]/*'/>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Task Task
{
get
{
return _task;
}
set
{
if (_task != null)
{
ExceptionHelper.Throw("Task.alreadySet", Id, _task.Id, value.Id);
}
_task = value;
}
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="P:HasController"]/*'/>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool HasController
{
get
{
return (_controller != null);
}
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="P:Controller"]/*'/>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IController Controller
{
get
{
ExceptionHelper.ThrowIf(!HasController, "Controller.noController", Id);
return _controller;
}
set
{
if (_controller != null)
{
ExceptionHelper.Throw("Controller.alreadySet", Id, _controller.Id, value.Id);
}
_controller = value;
}
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="P:Data"]/*'/>
protected object Data
{
get
{
return _data;
}
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="P:OwningForm"]/*'/>
private Form OwningForm
{
get
{
return _owningForm;
}
set
{
if ((value != null) && (value != _owningForm))
{
if (_owningForm != null)
{
//detach from old
_owningForm.Closed -= new EventHandler(_owningForm_Closed);
}
_owningForm = value;
if (_owningForm != null)
{
//attach to new
_owningForm.Closed += new EventHandler(_owningForm_Closed);
}
}
}
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="E:Activated"]/*'/>
public event EventHandler<EventArgs> Activated;
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="E:Deactivated"]/*'/>
public event EventHandler<EventArgs> Deactivated;
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="E:Closed"]/*'/>
public event EventHandler<EventArgs> Closed;
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:.ctor()"]/*'/>
public UserControlBase()
{
_containerControls = new Dictionary<string, Control>();
GotFocus += new EventHandler(UserControlBase_GotFocus);
LostFocus += new EventHandler(UserControlBase_LostFocus);
ParentChanged += new EventHandler(UserControlBase_ParentChanged);
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:Initialize(System.Object)"]/*'/>
public virtual void Initialize()
{
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:SetData(System.Object)"]/*'/>
public virtual void SetData(object data)
{
_data = data;
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:Activate()"]/*'/>
public virtual void Activate()
{
OnActivated();
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:Deactivate()"]/*'/>
public virtual void Deactivate()
{
OnDeactivated();
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:OnActivated()"]/*'/>
protected virtual void OnActivated()
{
//translate into generic Activated event
EventHelper.Raise(Activated, this, EventArgs.Empty);
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:OnDeactivated()"]/*'/>
protected virtual void OnDeactivated()
{
//translate into generic Deactivated event
EventHelper.Raise(Deactivated, this, EventArgs.Empty);
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:OnClosed()"]/*'/>
protected virtual void OnClosed()
{
//translate into generic Closed event
EventHelper.Raise(Closed, this, EventArgs.Empty);
}
/// <include file='UserControlBase.doc.xml' path='/doc/member[@name="M:Ingenious.Mvc.Windows.Forms.Views.IUserControlContainer.SetUserControl(System.String,Ingenious.Mvc.Windows.Forms.Views.IUserControlView)"]/*'/>
void IUserControlContainer.SetUserControl(string containerName, IUserControlView userControlView)
{
ArgumentHelper.AssertNotNull(containerName, "containerName");
ArgumentHelper.AssertNotNull(userControlView, "userControlView");
Control control = userControlView as Control;
ExceptionHelper.ThrowIf(control == null, "SetUserControl.viewIsntControl", userControlView.Id, Id, typeof(Control).FullName);
Control containerControl;
if (!_containerControls.TryGetValue(containerName, out containerControl))
{
FindContainerControl(Controls, containerName);
ExceptionHelper.ThrowIf(!_containerControls.ContainsKey(containerName), "SetUserControl.containerControlNotFound", containerName, Id);
containerControl = _containerControls[containerName];
}
containerControl.SuspendLayout();
containerControl.Controls.Clear();
containerControl.Controls.Add(control);
containerControl.ResumeLayout(true);
}
private void FindContainerControl(Control.ControlCollection controls, string containerName)
{
foreach (Control control in controls)
{
if (control.Name == containerName)
{
_containerControls[containerName] = control;
return;
}
//recursive search
FindContainerControl(control.Controls, containerName);
}
}
private void UserControlBase_GotFocus(object sender, EventArgs e)
{
//translate a gain of focus into an Activated event
OnActivated();
}
private void UserControlBase_LostFocus(object sender, EventArgs e)
{
//translate a loss of focus into a Deactivated event
OnDeactivated();
}
private void UserControlBase_ParentChanged(object sender, EventArgs e)
{
//the parent for this user control changed so we need to track the owning form so we know when to raise the Closed event
OwningForm = ParentForm;
}
private void _owningForm_Closed(object sender, EventArgs e)
{
//here our owning form was closed, so we need to raise our Closed event
OnClosed();
}
}
}
|