Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.GTK
view FrmExportXml.cs @ 128:d5a631a8d288
Re #361: XML Export in GTK#
* Inherit from FileChooserDialog so that we get the resize behaviour
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 01 Oct 2011 17:04:22 +0100 |
parents | d4e6bfeb1c61 |
children | 0d8004d6a4e5 |
line wrap: on
line source
// This file (FrmExportXml.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2011 IBBoard // // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. using System; using System.IO; using IBBoard.GtkSharp; using System.Collections.Generic; using Gtk; using IBBoard.WarFoundry.API.Objects; using System.Xml.Xsl; using IBBoard.Lang; using IBBoard.WarFoundry.API.Exporters; namespace IBBoard.WarFoundry.GUI.GTK { public partial class FrmExportXml : FileChooserDialog { private Army army; private TransformXmlWidget transformWidget; private Button buttonOk; public FrmExportXml(Army army) : base("", null, FileChooserAction.Save) { this.army = army; AddButton(Gtk.Stock.Cancel, ResponseType.Cancel); buttonOk = (Button)AddButton(Gtk.Stock.Save, ResponseType.Ok); buttonOk.Clicked+=OnButtonOkClicked; transformWidget = new TransformXmlWidget(); transformWidget.TransformChanged += HandleTransformWidgetTransformChanged; ExtraWidget = transformWidget; SetFilename(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "export.xml")); SelectionChanged += OnSaveAsPathSelectionChanged; } private void HandleTransformWidgetTransformChanged (object sender, EventArgs e) { SetOkayButtonSensitive(); } private bool IsValid () { return Filename != null && Filename != "" && transformWidget.IsValid; } private void SetOkayButtonSensitive() { //TODO: It would be nice to disable save when appropriate options aren't set, //but we don't seem to get change notifications early enough //buttonOk.Sensitive = IsValid(); } protected void OnSaveAsPathSelectionChanged(object sender, System.EventArgs e) { SetOkayButtonSensitive(); } protected void OnButtonOkClicked (object sender, System.EventArgs e) { if (IsValid()) { DoExport(); } } void DoExport() { string errorMessage = ""; // Catch potential errors with the file export or XSL compiliation try { string fileName = Filename; if (transformWidget.TransformEnabled) { WarFoundryXmlWithXslExporter.GetDefault().ExportArmyWithTransform(army, fileName, transformWidget.GetXsltPath()); } else { WarFoundryXmlWithXslExporter.GetDefault().ExportArmy(army, fileName); } } catch (XsltCompileException ex) { errorMessage = Translation.GetTranslation("mbErrorCompileFailed", "") + ":\n" + ex.Message; } catch (XsltException ex) { errorMessage = Translation.GetTranslation("mbErrorXSLTFailed", "") + ":\n" + ex.Message; } catch (FileNotFoundException ex) { errorMessage = Translation.GetTranslation("mbErrorFileNotFoundFailed", "") + ":\n" + ex.Message; } catch (IOException ex) { errorMessage = Translation.GetTranslation("mbErrorIOFailed", "") + ":\n" + ex.Message; } catch (Exception ex) { errorMessage = Translation.GetTranslation("mbErrorFailed", "") + ":\n" + ex.Message; } if (errorMessage != "") { MessageDialog dialog = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, errorMessage); dialog.Run(); } Respond(ResponseType.Ok); } } }