view FrmExportXml.cs @ 125:42d2aa87dfa7

Re #361: Add XML export UI to GTK# * Add basic UI that works (needs improvement) * Open the dialog using Run() so that we can close it properly
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Oct 2011 14:52:39 +0100
parents a647afc19fe9
children d4e6bfeb1c61
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;

		public FrmExportXml(Army army)
		{
			this.army = army;
			this.Build();
			FillXsltList();
			saveAsPath.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
		}

		private void FillXsltList()
		{
			DirectoryInfo dir = new DirectoryInfo(System.IO.Path.Combine(Constants.ExecutablePath, "xsl"));
			List<FileInfo> files = new List<FileInfo>(dir.GetFiles("*.xsl"));
			ComboBoxUtils.FillCombo(transformList, files, delegate(FileInfo file) { return file.Name; });
		}

		protected void OnDoTransformWidgetToggled(object sender, System.EventArgs e)
		{
			bool enabled = doTransformWidget.Active;
			lblTransform.Sensitive = enabled;
			transformList.Sensitive = enabled;
			SetOkayButtonSensitive();
		}

		private void SetOkayButtonSensitive()
		{
			buttonOk.Sensitive = saveAsName.Text != "" && saveAsPath.Filename != null && saveAsPath.Filename != "" && (!doTransformWidget.Active || ComboBoxUtils.GetSelectedItem<FileInfo>(transformList) != null);
		}

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

		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 = System.IO.Path.Combine(saveAsPath.Filename, saveAsName.Text);

                if (doTransformWidget.Active)
                {
                    WarFoundryXmlWithXslExporter.GetDefault().ExportArmyWithTransform(army, fileName, ComboBoxUtils.GetSelectedItem<FileInfo>(transformList).FullName);
                }
                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 OnSaveAsNameChanged (object sender, System.EventArgs e)
		{
			SetOkayButtonSensitive();
		}
	}
}