view FrmExportXml.cs @ 165:dc13cef2bc2b

Re #420: Saved army does not save "contained" structure * Make sure GTK# UI loads nesting correctly
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Sep 2012 15:29:15 +0100
parents 0d8004d6a4e5
children 9808adf2d566
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
		{
			get
			{
				return CheckFileCanExist() && transformWidget.IsValid;
			}
		}

		private bool CheckFileCanExist()
		{
			bool canExist = false;

			if (!String.IsNullOrEmpty(Filename))
			{
				FileInfo file = new FileInfo(Filename);
				canExist = file.Directory.Exists;
			}

			return canExist;
		}

		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);
		}
	}
}