#region Disclaimer / License // Copyright (C) 2011, 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.Linq; using System.Text; using Maestro.Shared.UI; using ICSharpCode.Core; using OSGeo.MapGuide.MaestroAPI; using Maestro.Base; using Maestro.Base.Services; using System.IO; using Maestro.Editors.Common; using Microsoft.Scripting.Hosting.Shell; using Microsoft.Scripting.Runtime; using Microsoft.Scripting.Hosting; using Maestro.AddIn.Scripting.UI; using Maestro.Editors.Generic; using Maestro.Base.Editor; using Microsoft.Scripting.Hosting.Providers; using Maestro.Editors.Preview; namespace Maestro.AddIn.Scripting.Services { /// /// Python built-ins injected into the Maestro IronPython REPL /// public static class ScriptGlobals { /// /// The Host Application /// public const string HostApp = "app"; //NOXLATE } /// /// A simplified helper class that is exposed to python scripts to provide /// convenience functionality or to workaround concepts that don't cleanly /// translate to IronPython (eg. Generics) /// public class HostApplication { /// /// The main application window /// public Workbench MainWindow { get { return Workbench.Instance; } } /// /// Returns a list of the names of all currently open connections /// /// public string[] GetConnectionNames() { return ServiceRegistry.GetService().GetConnectionNames().ToArray(); } /// /// Gets the connection by its specified name /// /// /// public IServerConnection GetConnection(string name) { return ServiceRegistry.GetService().GetConnection(name); } /// /// Gets the XML content of the given resource id /// /// /// /// public string GetResourceXml(IServerConnection conn, string resourceId) { var res = conn.ResourceService.GetResource(resourceId); return ResourceTypeRegistry.SerializeAsString(res); } /// /// Sets the XML content of the given resource id /// /// /// /// public void SetResourceXml(IServerConnection conn, string resourceId, string xml) { try { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { conn.ResourceService.SetResourceXmlData(resourceId, ms); } } catch (Exception ex) { XmlContentErrorDialog.CheckAndHandle(ex, xml, false); } } /// /// Launches the specified url /// /// public void OpenUrl(string url) { var svc = ServiceRegistry.GetService(); svc.OpenUrl(url); } /// /// Prompts for a question that requires a boolean response /// /// /// /// public bool AskQuestion(string title, string question) { return MessageService.AskQuestion(question, title); } /// /// Displays a message /// /// /// public void ShowMessage(string title, string message) { MessageService.ShowMessage(message, title); } /// /// Displays an exception in a dialog /// /// public void ShowError(Exception ex) { ErrorDialog.Show(ex); } /// /// Displays a resource picker for opening /// /// /// /// public string PickResourceOpen(IServerConnection conn, string resourceType) { Func picker = () => { using (var diag = new ResourcePicker(conn.ResourceService, (ResourceTypes)Enum.Parse(typeof(ResourceTypes), resourceType), ResourcePickerMode.OpenResource)) { if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK) { return diag.ResourceID; } } return null; }; if (this.MainWindow.InvokeRequired) { var result = this.MainWindow.Invoke(picker); if (result != null) return result.ToString(); else return null; } else return picker(); } /// /// Displays a resource picker for saving /// /// /// /// public string PickResourceSave(IServerConnection conn, string resourceType) { Func picker = () => { using (var diag = new ResourcePicker(conn.ResourceService, (ResourceTypes)Enum.Parse(typeof(ResourceTypes), resourceType), ResourcePickerMode.SaveResource)) { if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK) { return diag.ResourceID; } } return null; }; if (this.MainWindow.InvokeRequired) { var result = this.MainWindow.Invoke(picker); if (result != null) return result.ToString(); else return null; } else return picker(); } /// /// Prompts a dialog to select a folder /// /// /// public string PickFolder(IServerConnection conn) { Func picker = () => { using (var diag = new ResourcePicker(conn.ResourceService, ResourcePickerMode.OpenFolder)) { if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK) { return diag.ResourceID; } } return null; }; if (this.MainWindow.InvokeRequired) { var result = this.MainWindow.Invoke(picker); if (result != null) return result.ToString(); else return null; } else return picker(); } /// /// Opens the default editor for the specified resource /// /// /// public void OpenEditor(IServerConnection conn, string resourceId) { Action action = () => { var siteExp = this.MainWindow.ActiveSiteExplorer; var omgr = ServiceRegistry.GetService(); omgr.Open(resourceId, conn, false, siteExp); }; if (this.MainWindow.InvokeRequired) this.MainWindow.Invoke(action); else action(); } /// /// Launches a preview of the given open resource /// /// /// /// public void PreviewResource(IServerConnection conn, string resourceId, string locale) { Action action = () => { var siteExp = this.MainWindow.ActiveSiteExplorer; var omgr = ServiceRegistry.GetService(); IEditorViewContent openEd = null; foreach (var ed in omgr.OpenEditors) { if (ed.Resource.CurrentConnection == conn && ed.EditorService.ResourceID == resourceId) { openEd = ed; break; } } if (openEd != null) { var previewer = ResourcePreviewerFactory.GetPreviewer(conn.ProviderName); if (previewer != null) previewer.Preview(openEd.Resource, openEd.EditorService, locale); else throw new Exception(string.Format(Strings.Error_NoPreviewer, conn.ProviderName)); } else { throw new Exception(string.Format(Strings.Error_NoOpenEditor, resourceId)); } }; if (this.MainWindow.InvokeRequired) this.MainWindow.Invoke(action); else action(); } /// /// Invokes the specified method on the UI thread. Methods that interact with the UI or create UI components /// must be done on this thread /// /// public void UIInvoke(Delegate method) { this.MainWindow.Invoke(method); } } }