#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 System.ComponentModel; using System.Xml; using OSGeo.MapGuide.MaestroAPI.Resource; using OSGeo.MapGuide.MaestroAPI; using System.Collections.Specialized; using System.Diagnostics; #pragma warning disable 1591 namespace OSGeo.MapGuide.ObjectModels.ApplicationDefinition { /// /// Describes all available widgets /// public interface IApplicationDefinitionWidgetInfoSet { /// /// Gets the widgets /// IEnumerable WidgetInfo { get; } } /// /// Describes all available containers /// public interface IApplicationDefinitionContainerInfoSet { /// /// Gets the containers /// IEnumerable ContainerInfo { get; } } /// /// Describes all available templates /// public interface IApplicationDefinitionTemplateInfoSet { /// /// Gets the templates /// IEnumerable TemplateInfo { get; } } /// /// Describes a fusion widget /// public interface IWidgetInfo { /// /// Gets the type of widget /// string Type { get; } /// /// Gets the localized type of the widget /// string LocalizedType { get; } /// /// Gets the description /// string Description { get; } /// /// Gets the location /// string Location { get; } /// /// Gets the label /// string Label { get; } /// /// Gets the tooltip /// string Tooltip { get; } /// /// Gets the text to display in the status bar /// string StatusText { get; } /// /// Gets the image url /// string ImageUrl { get; } /// /// Gets the image css class /// string ImageClass { get; } /// /// Indicates if this is a UI widget /// bool StandardUi { get; } /// /// Indicates which containers this widget is containable by /// string[] ContainableBy { get; } /// /// Gets the parameters for this widget /// IWidgetParameter[] Parameters { get; } } /// /// Describes a parameter of a fusion widget /// public interface IWidgetParameter { /// /// Gets the name of the parameter /// string Name { get; } /// /// Gets the description of the parameter /// string Description { get; } /// /// Gets the type of the parameter /// string Type { get; } /// /// Gets the label for this parameter /// string Label { get; } /// /// Gets the minimum allowed value of this parameter /// string Min { get; } /// /// Gets the maximum allowed value of this parameter /// string Max { get; } /// /// Gets the list of allowed value of this parameter /// IAllowedValue[] AllowedValue { get; } /// /// Gets the default value of this parameter /// string DefaultValue { get; } /// /// Gets whether this parameter is mandatory /// bool IsMandatory { get; } } /// /// Describes an allowed widget parameter value /// public interface IAllowedValue { /// /// Gets the name of this value /// string Name { get; } /// /// Gets the label of this value /// string Label { get; } } /// /// Describes a fusion container /// public interface IApplicationDefinitionContainerInfo { /// /// Gets the type of container /// string Type { get; } /// /// Gets the localized type of container /// string LocalizedType { get; } /// /// Gets the description of this container /// string Description { get; } /// /// Gets the preview image url of this container /// string PreviewImageUrl { get; } } /// /// Describes a fusion template /// public interface IApplicationDefinitionTemplateInfo { /// /// Gets the name of this template /// string Name { get; } /// /// Gets the url location of this template /// string LocationUrl { get; } /// /// Gets the template description /// string Description { get; } /// /// Gets the preview image url of this template /// string PreviewImageUrl { get; } /// /// Gets the panels applicable for this template /// IEnumerable Panels { get; } } /// /// Describes a fusion template panel /// public interface IApplicationDefinitionPanel { /// /// Gets the name of this panel /// string Name { get; } /// /// Gets the label of this panel /// string Label { get; } /// /// Gets the description of this panel /// string Description { get; } } /// /// Factory method signature for creating fusion widgets /// /// /// public delegate IWidget WidgetFactoryMethod(IWidgetInfo info); /// /// Factory method signature for creating fusion widget containers /// /// /// public delegate IWidgetContainer ContainerFactoryMethod(IApplicationDefinitionContainerInfo container); /// /// Defines the stock fusion template names that come with a standard MapGuide installation /// public static class FusionTemplateNames { /// /// The preview template. Used for previewing other resources /// public const string Preview = "Preview"; //NOXLATE /// /// The Aqua template /// public const string Aqua = "Aqua"; //NOXLATE /// /// The Maroon template /// public const string Maroon = "Maroon"; //NOXLATE /// /// The Slate template /// public const string Slate = "Slate"; //NOXLATE /// /// The LimeGold template /// public const string LimeGold = "LimeGold"; //NOXLATE /// /// The TurquoiseYellow template /// public const string TurquoiseYellow = "TurquoiseYellow"; //NOXLATE } /// /// A set of known widgets that come with a fusion installation /// public static class KnownWidgetNames { public const string About = "About"; //NOXLATE public const string ActivityIndicator = "ActivityIndicator"; //NOXLATE public const string BasemapSwitcher = "BasemapSwitcher"; //NOXLATE public const string Buffer = "Buffer"; //NOXLATE public const string BufferPanel = "BufferPanel"; //NOXLATE public const string CenterSelection = "CenterSelection"; //NOXLATE public const string ClearSelection = "ClearSelection"; //NOXLATE public const string ColorPicker = "ColorPicker"; //NOXLATE public const string CTRLClick = "CTRLClick"; //NOXLATE public const string CursorPosition = "CursorPosition"; //NOXLATE public const string EditableScale = "EditableScale"; //NOXLATE public const string ExtentHistory = "ExtentHistory"; //NOXLATE public const string FeatureInfo = "FeatureInfo"; //NOXLATE public const string Help = "Help"; //NOXLATE public const string InitialMapView = "InitialMapView"; //NOXLATE public const string InvokeScript = "InvokeScript"; //NOXLATE public const string InvokeURL = "InvokeURL"; //NOXLATE public const string LayerManager = "LayerManager"; //NOXLATE public const string Legend = "Legend"; //NOXLATE public const string LinkToView = "LinkToView"; //NOXLATE public const string MapMenu = "MapMenu"; //NOXLATE public const string Maptip = "Maptip"; //NOXLATE public const string Measure = "Measure"; //NOXLATE public const string Navigator = "Navigator"; //NOXLATE public const string OverviewMap = "OverviewMap"; //NOXLATE public const string Pan = "Pan"; //NOXLATE public const string PanOnClick = "PanOnClick"; //NOXLATE public const string PanQuery = "PanQuery"; //NOXLATE public const string Print = "Print"; //NOXLATE public const string Query = "Query"; //NOXLATE public const string QuickPlot = "QuickPlot"; //NOXLATE public const string Redline = "Redline"; //NOXLATE public const string RefreshMap = "RefreshMap"; //NOXLATE public const string SaveMap = "SaveMap"; //NOXLATE public const string Scalebar = "Scalebar"; //NOXLATE public const string ScalebarDual = "ScalebarDual"; //NOXLATE public const string Search = "Search"; //NOXLATE public const string Select = "Select"; //NOXLATE public const string SelectionInfo = "SelectionInfo"; //NOXLATE public const string SelectPolygon = "SelectPolygon"; //NOXLATE public const string SelectRadius = "SelectRadius"; //NOXLATE public const string SelectRadiusValue = "SelectRadiusValue"; //NOXLATE public const string SelectWithin = "SelectWithin"; //NOXLATE public const string TaskPane = "TaskPane"; //NOXLATE public const string Theme = "Theme"; //NOXLATE public const string ViewOptions = "ViewOptions"; //NOXLATE public const string ViewSize = "ViewSize"; //NOXLATE public const string Zoom = "Zoom"; //NOXLATE public const string ZoomOnClick = "ZoomOnClick"; //NOXLATE public const string ZoomToSelection = "ZoomToSelection"; //NOXLATE } /// /// Represents a fusion flexible layout (aka. An Application Definition document) /// public interface IApplicationDefinition : IResource, IExtensibleElement { /// /// Gets or sets the title /// string Title { get; set; } /// /// Gets or sets the template url /// string TemplateUrl { get; set; } /// /// Gets the map set /// IMapSet MapSet { get; } /// /// Gets the widget sets /// IEnumerable WidgetSets { get; } /// /// Creates a widget set /// /// /// IWidgetSet CreateWidgetSet(IMapWidget mapWidget); /// /// Adds the specified widget set /// /// void AddWidgetSet(IWidgetSet set); /// /// Removes the specified widget set /// /// void RemoveWidgetSet(IWidgetSet set); /// /// Creates a widget reference /// /// /// IWidgetItem CreateWidgetReference(string name); /// /// Creates a separator /// /// ISeparator CreateSeparator(); /// /// Create a flyout menu /// /// /// IFlyoutItem CreateFlyout(string label); /// /// Adds the new map group to the current map set /// /// /// /// /// IMapGroup AddMapGroup(string id, bool singleTile, string mapDefinitionId); /// /// Adds a new map group to the specified map set /// /// /// /// /// /// /// /// IMapGroup AddMapGroup(string id, bool singleTile, string mapDefinitionId, double centerX, double centerY, double scale); /// /// Create a widget from the specified widget information /// /// /// /// IWidget CreateWidget(string name, IWidgetInfo widgetInfo); /// /// Creates a widget reference UI container /// /// /// /// IUIItemContainer CreateContainer(string name, IApplicationDefinitionContainerInfo containerInfo); /// /// Creates a map widget /// /// /// /// IMapWidget CreateMapWidget(string mapId, string contextMenuId); } /// /// Represents an element that is extensible with arbitrary XML content /// public interface IExtensibleElement { /// /// Gets the extension /// IExtension Extension { get; } } /// /// Represents a region of arbitrary XML content /// public interface IExtension { /// /// Gets or sets the XML content. It is best to use the extension methods to get and set the appropriate values /// rather than accessing this array directly /// XmlElement[] Content { get; set; } } /// /// A reusable providing creation functionality /// for those that require it /// internal static class AppDefDocument { private static XmlDocument _doc; internal static XmlDocument Instance { get { if (_doc == null) _doc = new XmlDocument(); return _doc; } } } /// /// Extension method class /// public static class ExtensionMethods { /// /// Gets whether a widget with the specified name already exists /// /// /// /// public static bool WidgetNameExists(this IApplicationDefinition appDef, string name) { Check.NotNull(appDef, "appDef"); Check.NotEmpty(name, "name"); return appDef.FindWidget(name) != null; } /// /// Gets the widget of the specified name /// /// /// /// public static IWidget FindWidget(this IApplicationDefinition appDef, string name) { Check.NotNull(appDef, "appDef"); Check.NotEmpty(name, "name"); foreach (var set in appDef.WidgetSets) { foreach (var wgt in set.Widgets) { if (wgt.Name == name) return wgt; } } return null; } /// /// Removes the specified widget from this /// /// The app definition. /// Name of the widget to remove. /// if set to true [deletes any references to the widget to be deleted]. public static void RemoveWidget(this IApplicationDefinition appDef, string widgetName, bool deleteReferences) { Check.NotNull(appDef, "appDef"); //NOXLATE Check.NotEmpty(widgetName, "widgetName"); //NOXLATE IWidgetSet matchingSet = null; IWidget matchingWidget = null; foreach (var set in appDef.WidgetSets) { if (matchingSet == null) { foreach (var wgt in set.Widgets) { if (wgt.Name == widgetName) { matchingSet = set; matchingWidget = wgt; break; } } } } int removed = 0; if (matchingSet != null && matchingWidget != null) { matchingSet.RemoveWidget(matchingWidget); if (deleteReferences) { foreach (var set in appDef.WidgetSets) { foreach (var cnt in set.Containers) { var uicnt = cnt as IUIItemContainer; if (uicnt != null) { List removeMe = new List(); foreach (var uiitem in uicnt.Items) { IWidgetItem witem = uiitem as IWidgetItem; if (witem != null && witem.Widget == widgetName) { removeMe.Add(witem); System.Diagnostics.Trace.TraceInformation("Found widget reference in container: " + uicnt.Name); //NOXLATE } } if (removeMe.Count > 0) { foreach (var rm in removeMe) { uicnt.RemoveItem(rm); removed++; } } } } } } } if (deleteReferences) System.Diagnostics.Trace.TraceInformation(removed + " widget references removed"); //NOXLATE } /// /// Gets a specific container info by type /// /// /// /// public static IApplicationDefinitionContainerInfo FindContainer(this IApplicationDefinitionContainerInfoSet set, string name) { Check.NotNull(set, "set"); //NOXLATE Check.NotEmpty(name, "name"); //NOXLATE foreach (var cnt in set.ContainerInfo) { if (name.Equals(cnt.Type)) return cnt; } return null; } /// /// Gets a specific Widget Info by name /// /// /// /// public static IWidgetInfo FindWidget(this IApplicationDefinitionWidgetInfoSet set, string name) { Check.NotNull(set, "set"); //NOXLATE Check.NotEmpty(name, "name"); //NOXLATE foreach (var wgt in set.WidgetInfo) { if (name.Equals(wgt.Type)) return wgt; } return null; } /// /// Gets information for the named template /// /// /// /// public static IApplicationDefinitionTemplateInfo FindTemplate(this IApplicationDefinitionTemplateInfoSet set, string name) { Check.NotNull(set, "set"); //NOXLATE Check.NotEmpty(name, "name"); //NOXLATE foreach (var tpl in set.TemplateInfo) { if (name.Equals(tpl.Name)) return tpl; } return null; } /// /// Gets the specified map group by its id /// /// /// /// public static IMapGroup GetGroupById(this IMapSet set, string id) { Check.NotNull(set, "set"); //NOXLATE foreach (var group in set.MapGroups) { if (group.id.Equals(id)) return group; } return null; } /// /// Gets the names of all properties of this extensible element /// /// /// public static string[] GetNames(this IExtensibleElement ext) { Check.NotNull(ext, "ext"); //NOXLATE List names = new List(); foreach (var el in ext.Extension.Content) { names.Add(el.Name); } return names.ToArray(); } /// /// Gets all the properties in this extensible element /// /// /// public static NameValueCollection GetAllValues(this IExtensibleElement ext) { Check.NotNull(ext, "ext"); //NOXLATE NameValueCollection values = new NameValueCollection(); foreach (var el in ext.Extension.Content) { values.Add(el.Name, el.InnerText); } return values; } /// /// Replace the values of all properties in this extensible element with the values provided /// /// /// public static void SetAllValues(this IExtensibleElement ext, NameValueCollection values) { Check.NotNull(ext, "ext"); //NOXLATE Check.NotNull(values, "values"); //NOXLATE var elements = new List(); foreach (string name in values.Keys) { var value = values[name]; var rid = AppDefDocument.Instance.CreateElement(name); rid.InnerText = value; elements.Add(rid); } ext.Extension.Content = elements.ToArray(); } /// /// Sets the value of a property in this extensible element. /// /// /// /// public static void SetValue(this IExtensibleElement ext, string name, string value) { Check.NotNull(ext, "ext"); //NOXLATE Check.NotEmpty(name, "name"); //NOXLATE if (ext.Extension.Content != null) { var el = ext.Extension.Content.FindElementByName(name); if (el != null) { el.InnerText = value; } else { var values = new List(ext.Extension.Content); var rid = AppDefDocument.Instance.CreateElement(name); rid.InnerText = value; values.Add(rid); ext.Extension.Content = values.ToArray(); } } else { var rid = AppDefDocument.Instance.CreateElement(name); rid.InnerText = value; ext.Extension.Content = new XmlElement[] { rid }; } Trace.TraceInformation("Extensible element property {0} set to: {1}", name, value); //NOXLATE } /// /// Gets the value of a property in this extensible element. If none exists, an empty string is returned /// /// /// /// public static string GetValue(this IExtensibleElement ext, string name) { Check.NotNull(ext, "ext"); //NOXLATE Check.NotEmpty(name, "name"); //NOXLATE if (ext.Extension.Content != null) { var el = ext.Extension.Content.FindElementByName(name); if (el != null) { return el.InnerText; } } return string.Empty; } /// /// Returns a whose name matches the specified name /// /// /// /// public static XmlElement FindElementByName(this XmlElement[] elements, string name) { Check.NotNull(elements, "elements"); //NOXLATE foreach (var el in elements) { if (el.Name == name) return el; } return null; } /// /// Set the map definition id /// /// /// public static string GetMapDefinition(this IMap map) { Check.NotNull(map, "map"); //NOXLATE return map.GetValue("ResourceId"); //NOXLATE } /// /// Gets the map definition id /// /// /// public static void SetMapDefinition(this IMap map, string mapDefId) { Check.NotNull(map, "map"); //NOXLATE map.SetValue("ResourceId", mapDefId); //NOXLATE } /// /// Gets the first widget set of this application definition. /// /// /// public static IWidgetSet GetFirstWidgetSet(this IApplicationDefinition appDef) { Check.NotNull(appDef, "appDef"); //NOXLATE IWidgetSet set = null; foreach (var wgt in appDef.WidgetSets) { if (set == null) { set = wgt; break; } } return set; } } /* public abstract class WidgetValue { protected WidgetValue(string name, bool required) { this.Name = name; this.Nullable = !required; } public bool Nullable { get; protected set; } public string Name { get; set; } public abstract object Value { get; set; } protected virtual string ValueToString() { if (this.Value == null) return string.Empty; else return this.Value.ToString(); } public virtual string ToXml() { if (this.Nullable && this.Value == null) return string.Empty; return "<" + this.Name + ">" + ValueToString() + ""; //NOXLATE } } public class RangedWidgetValue : WidgetValue { public RangedWidgetValue(string name, bool required, IComparable minValue, IComparable maxValue) : base(name, required) { Check.NotNull(minValue, "minValue"); //NOXLATE Check.NotNull(maxValue, "maxValue"); //NOXLATE if (minValue.CompareTo(maxValue) <= 0) { this.MinValue = minValue; this.MaxValue = maxValue; } else { this.MinValue = maxValue; this.MaxValue = MinValue; } } public IComparable MinValue { get; private set; } public IComparable MaxValue { get; private set; } private object _value; public override object Value { get { return _value; } set { if (value == null) { if (!this.Nullable) throw new InvalidOperationException(OSGeo.MapGuide.MaestroAPI.Properties.ErrorNullNotAccepted); _value = value; } else { var cmp = value as IComparable; if (cmp == null) throw new InvalidOperationException(OSGeo.MapGuide.MaestroAPI.Properties.ErrroValueNotComparable); if (cmp.CompareTo(this.MinValue) >= 0 && cmp.CompareTo(this.MaxValue) <= 0) _value = value; else throw new InvalidOperationException(string.Format(OSGeo.MapGuide.MaestroAPI.Properties.ErrorValueOutOfRange, this.MinValue, this.MaxValue)); } } } } public class RestrictedWidgetValue : WidgetValue { public RestrictedWidgetValue(string name, bool required) : base(name, required) { } private object _value; public override object Value { get { return _value; } set { if (value == null) { if (!this.Nullable) throw new InvalidOperationException(OSGeo.MapGuide.MaestroAPI.Properties.ErrorNullNotAccepted); _value = value; } else { _value = value; } } } } public class ArbitraryWidgetValue : WidgetValue { public ArbitraryWidgetValue(string name, bool required) : base(name, required) { } private object _value; public override object Value { get { return _value; } set { if (value == null) { if (!this.Nullable) throw new InvalidOperationException(OSGeo.MapGuide.MaestroAPI.Properties.ErrorNullNotAccepted); _value = value; } else { _value = value; } } } } */ public interface IMapSet { IEnumerable MapGroups { get; } int MapGroupCount { get; } IMapGroup GetGroupAt(int index); void AddGroup(IMapGroup group); void RemoveGroup(IMapGroup group); } public interface IMapGroup : IExtensibleElement, INotifyPropertyChanged { string id { get; set; } IMapView InitialView { get; set; } IEnumerable Map { get; } void AddMap(IMap map); void RemoveMap(IMap map); int MapCount { get; } IMap GetMapAt(int index); IMapView CreateInitialView(double x, double y, double scale); IMap CreateCmsMapEntry(string type, bool singleTile, string name, string olType); } public interface IMap : INotifyPropertyChanged, IExtensibleElement { string Type { get; set; } bool SingleTile { get; set; } IMapGuideOverlayOptions OverlayOptions { get; set; } IMapGuideOverlayOptions CreateOverlayOptions(bool isBaseLayer, bool useOverlay, string projection); ICmsMapOptions CmsMapOptions { get; set; } ICmsMapOptions CreateOptions(string name, string type); } public interface IMapGuideOverlayOptions { bool IsBaseLayer { get; set; } bool UseOverlay { get; set; } string Projection { get; set; } } public interface ICmsMapOptions { string Name { get; set; } string Type { get; set; } } /// /// Represents a flexible layout's widget set. This is analogous to a Command Set in a Web Layout /// public interface IWidgetSet { IEnumerable Containers { get; } int ContainerCount { get; } void AddContainer(IWidgetContainer container); void RemoveContainer(IWidgetContainer container); IMapWidget MapWidget { get; } int WidgetCount { get; } IEnumerable Widgets { get; } void AddWidget(IWidget widget); void RemoveWidget(IWidget widget); } /// /// Represents a container component in a flexible layout /// public interface IWidgetContainer : INotifyPropertyChanged, IExtensibleElement { string Name { get; set; } string Type { get; set; } string Position { get; set; } } /// /// Represents a UI item container component /// public interface IUIItemContainer : IWidgetContainer, IMenu { } /// /// A interface for widgets and components with menu-like characteristics /// public interface IMenu { /// /// Gets the item count. /// /// The item count. int ItemCount { get; } /// /// Gets the items. /// /// The items. IEnumerable Items { get; } /// /// Moves the specified item up. /// /// The item. /// bool MoveUp(IUIItem item); /// /// Moves the specified item down. /// /// The item. /// bool MoveDown(IUIItem item); /// /// Gets the index of the specified item. /// /// The item. /// int GetIndex(IUIItem item); /// /// Inserts the specified item at the specified index. /// /// The item. /// The index. void Insert(IUIItem item, int index); /// /// Adds the item. /// /// The item. void AddItem(IUIItem item); /// /// Removes the item. /// /// The item. void RemoveItem(IUIItem item); } /// /// Defines the type of UI items /// [System.SerializableAttribute()] public enum UiItemFunctionType { /// Separator, /// Widget, /// Flyout, } public interface IUIItem : INotifyPropertyChanged { IMenu Parent { get; } UiItemFunctionType Function { get; } } public interface IFlyoutItem : IUIItem, IMenu, INotifyPropertyChanged { string Label { get; set; } string Tooltip { get; set; } string ImageUrl { get; set; } string ImageClass { get; set; } } public interface ISeparator : IUIItem { } /// /// Represents a widget reference. This is analogous to a command item in a Web Layouts /// public interface IWidgetItem : IUIItem { string Widget { get; set; } } /// /// Represents a fusion application widget /// public interface IWidget : INotifyPropertyChanged, IExtensibleElement { /// /// Gets or sets the name of the widget /// string Name { get; set; } /// /// Gets or sets the type of the widget /// string Type { get; set; } /// /// Gets or sets the location /// string Location { get; set; } /// /// Gets the raw XML content of this widget /// /// string ToXml(); } /// /// Represents a map widget /// public interface IMapWidget : IWidget { string MapId { get; set; } } public interface IUIWidget : IWidget { string ImageUrl { get; set; } string ImageClass { get; set; } string Label { get; set; } string Tooltip { get; set; } string StatusText { get; set; } string Disabled { get; set; } IUIWidget Clone(); } /// /// The initial view of the map /// public interface IMapView : INotifyPropertyChanged { /// /// Gets or sets the center X. /// /// The center X. double CenterX { get; set; } /// /// Gets or sets the center Y. /// /// The center Y. double CenterY { get; set; } /// /// Gets or sets the scale. /// /// The scale. double Scale { get; set; } } }