#region Disclaimer / License
// Copyright (C) 2010, Jackie Ng
// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com
//
// 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
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Maestro.Shared.UI;
namespace Maestro.Shared.UI
{
///
/// The base class for managing View Content instances
///
public abstract class ViewContentManagerBase : ServiceBase, IViewContentManager
{
///
/// A dictionary of single-instance view content types
///
protected Dictionary _singletonViewContentTypes = new Dictionary();
///
/// A list of single-instance view content
///
protected List _singletonInstances = new List();
///
/// Raised when a view is hidden
///
public event EventHandler ViewHidden;
///
/// Raised when a view is activated
///
public event ViewEventHandler ViewActivated;
///
/// Gets the workbench
///
///
protected abstract WorkbenchBase GetWorkbench();
///
/// Initializes this instance. Subclasses must override this and populate the
/// and collections
///
public override void Initialize()
{
base.Initialize();
}
///
/// Gets whether the given single-instance view content type has already been created
///
/// The type implementing the interface
///
public bool IsCreated() where T : IViewContent
{
var type = typeof(T);
if (_singletonViewContentTypes.ContainsKey(type.Name))
{
foreach (var cnt in _singletonInstances)
{
if (type.IsAssignableFrom(cnt.GetType()))
{
return true;
}
}
return false;
}
else
{
throw new InvalidOperationException(string.Format(Strings.Error_ViewContent_Type_Not_Singleton, type.Name));
}
}
///
/// Hides the given single-instance view content
///
/// The type implementing the interface
public void HideContent() where T : IViewContent
{
var type = typeof(T);
if (_singletonViewContentTypes.ContainsKey(type.Name))
{
foreach (var cnt in _singletonInstances)
{
if (type.IsAssignableFrom(cnt.GetType()))
{
cnt.Hide();
var handler = this.ViewHidden;
if (handler != null)
handler(this, EventArgs.Empty);
//var wb = GetWorkbench();
//if (wb != null)
// wb.CheckContainerStatus();
return;
}
}
}
else
{
throw new InvalidOperationException(string.Format(Strings.Error_ViewContent_Type_Not_Singleton, type.Name));
}
}
///
/// Displays the given single-instance view content
///
/// The type implementing the interface
public void ShowContent() where T : IViewContent
{
var type = typeof(T);
if (_singletonViewContentTypes.ContainsKey(type.Name))
{
foreach (var cnt in _singletonInstances)
{
if (type.IsAssignableFrom(cnt.GetType()))
{
var wb = GetWorkbench();
if (!cnt.IsAttached)
wb.ShowContent(cnt);
cnt.Activate();
var h = this.ViewActivated;
if (h != null)
h(this, cnt);
//wb.CheckContainerStatus();
return;
}
}
}
else
{
throw new InvalidOperationException(string.Format(Strings.Error_ViewContent_Type_Not_Singleton, type.Name));
}
}
///
/// Creates and/or opens the given view content in the specified region
///
/// The type implementing the interface
/// The desired region to show the view content in
/// A method that will create the required view content the view content has not been created yet
///
public T OpenContent(ViewRegion region, CreateFunc method) where T : IViewContent
{
return OpenContent(null, null, region, method);
}
///
/// Creates and/or opens the given view content in the specified region
///
/// The type implementing the interface
/// The desired region to show the view content in
///
public T OpenContent(ViewRegion region) where T : IViewContent
{
return OpenContent(null, null, region);
}
///
/// Creates and/or opens the given view content in the specified region
///
/// The type implementing the interface
///
///
/// The desired region to show the view content in
///
public T OpenContent(string title, string description, ViewRegion region) where T : IViewContent
{
return OpenContent(title, description, region, () => { return (T)Activator.CreateInstance(typeof(T), true); });
}
///
/// Creates and/or opens the given view content in the specified region
///
/// The type implementing the interface
///
///
/// The desired region to show the view content in
/// A method that will create the required view content the view content has not been created yet
///
public T OpenContent(string title, string description, ViewRegion region, CreateFunc method) where T : IViewContent
{
var type = typeof(T);
var wb = GetWorkbench();
if (_singletonViewContentTypes.ContainsKey(type.Name))
{
foreach (var cnt in _singletonInstances)
{
if (type.IsAssignableFrom(cnt.GetType()))
{
if (!cnt.IsAttached)
wb.ShowContent(cnt);
cnt.Activate();
var h = this.ViewActivated;
if (h != null)
h(this, cnt);
//wb.CheckContainerStatus();
return (T)cnt;
}
}
}
T obj = method(); //(T)Activator.CreateInstance(type, true);
SingletonViewContent svc = obj as SingletonViewContent;
if (svc != null)
throw new InvalidOperationException(string.Format(Strings.Error_ViewContent_Not_Registered, type.Name));
obj.Title = title;
obj.Description = description;
wb.ShowContent(obj);
return obj;
}
}
///
///
///
///
///
public delegate void ViewEventHandler(object sender, IViewContent content);
}