UserControlBase.cs :  » Web-Frameworks » Ingenious-MVC » Ingenious » Mvc » Windows » Forms » Views » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Web Frameworks » Ingenious MVC 
Ingenious MVC » Ingenious » Mvc » Windows » Forms » Views » UserControlBase.cs
#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();
    }
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.