view FrmExportXml.cs @ 126:d4e6bfeb1c61

Re #361: Add XML export UI to GTK# * Separate out the "transform" section so we can add it as an ExtraWidget on the file chooser
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Oct 2011 16:19:48 +0100
parents 42d2aa87dfa7
children d5a631a8d288
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 : Gtk.Dialog
	{
		private Army army;
		private TransformXmlWidget transformWidget;

		public FrmExportXml(Army army)
		{
			this.army = army;
			this.Build();
			transformWidget = new TransformXmlWidget();
			transformWidget.TransformChanged += HandleTransformWidgetTransformChanged;
			saveAsPath.ExtraWidget = transformWidget;
			saveAsPath.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
		}

		private void HandleTransformWidgetTransformChanged (object sender, EventArgs e)
		{
			SetOkayButtonSensitive();
		}

		private void SetOkayButtonSensitive()
		{
			buttonOk.Sensitive = saveAsPath.Filename != null && saveAsPath.Filename != "" && transformWidget.IsValid;
		}

		protected void OnSaveAsPathSelectionChanged(object sender, System.EventArgs e)
		{
			SetOkayButtonSensitive();
		}

		protected void OnButtonOkClicked (object sender, System.EventArgs e)
		{
			string errorMessage = "";
            // Catch potential errors with the file export or XSL compiliation
            try
            {
                string fileName = saveAsPath.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);
		}

		protected void OnButtonCancelClicked (object sender, System.EventArgs e)
		{
			Respond(ResponseType.Cancel);
		}

		protected void OnSizeAllocated (object o, Gtk.SizeAllocatedArgs args)
		{
			Console.WriteLine(args.Allocation.Height + "," + args.Allocation.Width);
			//this.
		}
	}
}