FormBase.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 » FormBase.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.Diagnostics;
using System.Windows.Forms;
using Ingenious.Mvc.Configuration;
using Ingenious.Mvc.Configuration.Configurators;
using Ingenious.Mvc.Views;
using Ingenious.Mvc.Util;
using Ingenious.Mvc.Windows.Forms.ViewManagers;

namespace Ingenious.Mvc.Windows.Forms.Views{
  /// <include file='FormBase.doc.xml' path='/doc/member[@name="T:FormBase"]/*'/>
  [XmlCustomParser(typeof(FormBaseXmlCustomParser))]
  public class FormBase : Form, IFormView, ICustomConfigurable, IUserControlContainer
  {
    private Id<IView> _id;
    private Task _task;
    private IController _controller;
    private Id<IView> _parentId;
    private object _data;
    private bool _dataSet;
    private bool _floating;
    private bool _mdiChild;
    private bool _modal;
    private bool _leaveOpen;
    private bool _leaveOpenExplicitlySet;
    private bool _singleInstance;
    private IDictionary<string, Control> _containerControls;

    /// <include file='FormBase.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='FormBase.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='FormBase.doc.xml' path='/doc/member[@name="P:HasController"]/*'/>
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public bool HasController
    {
      get
      {
        return (_controller != null);
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="P:Controller"]/*'/>
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public IController Controller
    {
      get
      {
        ExceptionHelper.ThrowIf(!DesignMode && !HasController, "Controller.noController", Id);
        return _controller;
      }
      set
      {
        if (_controller != null)
        {
          ExceptionHelper.Throw("Controller.alreadySet", Id, _controller.Id, value.Id);
        }

        _controller = value;
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="P:ParentId"]/*'/>
    [Browsable(false)]
    public Id<IView> ParentId
    {
      get
      {
        return _parentId;
      }
      set
      {
        _parentId = value;
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="P:IsFloating"]/*'/>
    [Browsable(false)]
    public bool IsFloating
    {
      get
      {
        return _floating;
      }
      set
      {
        _floating = value;
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="P:IsMdiChild"]/*'/>
    [Browsable(false)]
    public new bool IsMdiChild
    {
      get
      {
        return _mdiChild;
      }
      set
      {
        _mdiChild = value;
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="P:IsModal"]/*'/>
    [Browsable(false)]
    public bool IsModal
    {
      get
      {
        return _modal;
      }
      set
      {
        _modal = value;
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="P:LeaveOpen"]/*'/>
    [Browsable(false)]
    public bool LeaveOpen
    {
      get
      {
        if (_leaveOpenExplicitlySet)
        {
          return _leaveOpen;
        }
        else
        {
          //value not explicitly set so we'll return a reasonable default
          return (IsFloating || IsMdiChild);
        }
      }
      set
      {
        _leaveOpen = value;
        _leaveOpenExplicitlySet = true;
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="P:SingleInstance"]/*'/>
    [Browsable(false)]
    public bool SingleInstance
    {
      get
      {
        return _singleInstance;
      }
      set
      {
        _singleInstance = value;
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="P:Data"]/*'/>
    protected object Data
    {
      get
      {
        ExceptionHelper.ThrowIf(!_dataSet, "Data.notSet", Id);
        return _data;
      }
    }

    public event EventHandler<EventArgs> Activated;

    public event EventHandler<EventArgs> Deactivated;

    public event EventHandler<EventArgs> Closed;

    public event EventHandler<FormViewEventArgs> ChildOpening;

    public event EventHandler<FormViewEventArgs> ChildClosed;

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="M:.ctor()"]/*'/>
    protected FormBase()
    {
      _containerControls = new Dictionary<string, Control>();
    }

    protected override void OnActivated(EventArgs e)
    {
      base.OnActivated(e);
      System.Diagnostics.Debug.WriteLine("OnActivated");
      //translate into generic Activated event
      EventHelper.Raise(Activated, this, e);
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="M:OnDeactivate(System.EventArgs)"]/*'/>
    protected override void OnDeactivate(EventArgs e)
    {
      base.OnDeactivate(e);
      //translate into Deactivated event
      EventHelper.Raise(Deactivated, this, e);
    }

    protected override void OnClosed(EventArgs e)
    {
      base.OnClosed(e);
      //translate into generic Activated event
      EventHelper.Raise(Closed, this, e);
    }

    protected virtual void OnChildOpening(FormViewEventArgs e)
    {
      EventHelper.Raise(ChildOpening, this, e);
    }

    protected virtual void OnChildClosed(FormViewEventArgs e)
    {
      EventHelper.Raise(ChildClosed, this, e);
    }

    private delegate void SetParentHandler(IFormView parentView);

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="M:SetParent(Ingenious.Mvc.Windows.Forms.Views.IFormView)"]/*'/>
    public void SetParent(IFormView parentView)
    {
      if (InvokeRequired)
      {
        //make sure we execute on UI thread
        Invoke((SetParentHandler) SetParent, new object[] {parentView});
      }
      else
      {
        Form formBase = parentView as Form;
        Debug.Assert(formBase != null, "The parent view must be a subclass of System.Windows.Forms.Form.");

        if (IsFloating)
        {
          Owner = formBase;
        }
        else if (IsMdiChild)
        {
          MdiParent = formBase;
        }
      }
    }

    void IFormView.ChildOpening(IFormView childView)
    {
      //raise the ChildOpening event
      OnChildOpening(new FormViewEventArgs(childView));
    }

    void IFormView.ChildClosed(IFormView childView)
    {
      //raise the ChildClosed event
      OnChildClosed(new FormViewEventArgs(childView));
    }

    private delegate void ShowHandler(bool modal);

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="M:Show(System.Boolean)"]/*'/>
    public void Show(bool modal)
    {
      if (InvokeRequired)
      {
        //make sure we execute on UI thread
        Invoke((ShowHandler) Show, new object[] {modal});
      }
      else
      {
        if (modal)
        {
          ShowDialog();
        }
        else
        {
          Show();
          Activate();
        }
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="M:Initialize()"]/*'/>
    public virtual void Initialize()
    {
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="M:SetData(System.Object)"]/*'/>
    void IView.SetData(object data)
    {
      _data = data;
      _dataSet = true;
    }

    /// <include file='FormBase.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);
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="M:Ingenious.Mvc.Configuration.ICustomConfigurable.SetCustomConfiguration(System.Object)"]/*'/>
    void ICustomConfigurable.SetCustomConfiguration(object customConfiguration)
    {
      ArgumentHelper.AssertNotNull(customConfiguration, "customConfiguration");
      CustomConfiguration customConfig = customConfiguration as CustomConfiguration;
      ExceptionHelper.ThrowIf(customConfig == null, "SetCustomConfiguration.incorrectType", typeof(CustomConfiguration).FullName, customConfiguration.GetType().FullName);
      ParentId = customConfig.ParentId;

      if (customConfig.IsFloating.HasValue)
      {
        IsFloating = customConfig.IsFloating.Value;
      }

      if (customConfig.IsMdiChild.HasValue)
      {
        IsMdiChild = customConfig.IsMdiChild.Value;
      }

      if (customConfig.IsModal.HasValue)
      {
        IsModal = customConfig.IsModal.Value;
      }

      if (customConfig.LeaveOpen.HasValue)
      {
        LeaveOpen = customConfig.LeaveOpen.Value;
      }

      if (customConfig.SingleInstance.HasValue)
      {
        SingleInstance = customConfig.SingleInstance.Value;
      }
    }

    /// <include file='FormBase.doc.xml' path='/doc/member[@name="UnsupportedTypeOrMember"]/*'/>
    [Serializable]
    public sealed class CustomConfiguration
    {
      private Id<IView> _parentId;
      private bool? _floating;
      private bool? _mdiChild;
      private bool? _modal;
      private bool? _leaveOpen;
      private bool? _singleInstance;

      /// <include file='FormBase.doc.xml' path='/doc/member[@name="UnsupportedTypeOrMember"]/*'/>
      public Id<IView> ParentId
      {
        get
        {
          return _parentId;
        }
        set
        {
          _parentId = value;
        }
      }

      /// <include file='FormBase.doc.xml' path='/doc/member[@name="UnsupportedTypeOrMember"]/*'/>
      public bool? IsFloating
      {
        get
        {
          return _floating;
        }
        set
        {
          _floating = value;
        }
      }

      /// <include file='FormBase.doc.xml' path='/doc/member[@name="UnsupportedTypeOrMember"]/*'/>
      public bool? IsMdiChild
      {
        get
        {
          return _mdiChild;
        }
        set
        {
          _mdiChild = value;
        }
      }

      /// <include file='FormBase.doc.xml' path='/doc/member[@name="UnsupportedTypeOrMember"]/*'/>
      public bool? IsModal
      {
        get
        {
          return _modal;
        }
        set
        {
          _modal = value;
        }
      }

      /// <include file='FormBase.doc.xml' path='/doc/member[@name="UnsupportedTypeOrMember"]/*'/>
      public bool? LeaveOpen
      {
        get
        {
          return _leaveOpen;
        }
        set
        {
          _leaveOpen = value;
        }
      }

      /// <include file='FormBase.doc.xml' path='/doc/member[@name="UnsupportedTypeOrMember"]/*'/>
      public bool? SingleInstance
      {
        get
        {
          return _singleInstance;
        }
        set
        {
          _singleInstance = value;
        }
      }
    }
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.