#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 { public abstract class ViewContentManagerBase : ServiceBase, IViewContentManager { protected Dictionary _singletonViewContentTypes = new Dictionary(); protected List _singletonInstances = new List(); public event EventHandler ViewHidden; public event ViewEventHandler ViewActivated; protected abstract WorkbenchBase GetWorkbench(); /// /// Initializes this instance. Subclasses must override this and populate the /// and collections /// public override void Initialize() { base.Initialize(); } 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)); } } 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)); } } 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)); } } public T OpenContent(ViewRegion region, CreateFunc method) where T : IViewContent { return OpenContent(null, null, region, method); } public T OpenContent(ViewRegion region) where T : IViewContent { return OpenContent(null, null, region); } public T OpenContent(string title, string description, ViewRegion region) where T : IViewContent { return OpenContent(title, description, region, () => { return (T)Activator.CreateInstance(typeof(T), true); }); } 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); }