view FrmXmlExport.cs @ 253:7243e6646926 default tip

Re #427: Resizable equipment dialog * Make equipment dialog resizable
author IBBoard <dev@ibboard.co.uk>
date Sat, 27 Apr 2013 16:10:08 +0100
parents 4d25c42bbe7b
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml.Xsl;
using IBBoard.Lang;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Exporters;

namespace IBBoard.WarFoundry.GUI.WinForms
{
    public partial class FrmXmlExport : Form
    {
        Army myArmy = null;
        public FrmXmlExport(Army army)
        {
            InitializeComponent();
            myArmy = army;
			cbTransforms.DataSource = WarFoundryXmlWithXslExporter.GetDefault().GetXsltStreams();
			cbTransforms.DisplayMember = "Name";
        }

        private void bttnOutputSelect_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "XML File|*.xml|HTML File|*.html|XHTML File|*.xhtml";
            sfd.Title = "Save XML output";
            sfd.ShowDialog();

            if (sfd.FileName != "")
            {
                tbOutputFile.Text = sfd.FileName;
            }
        }

        private void bttnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }

        private void bttnExport_Click(object sender, EventArgs e)
        {
            string errorMessage = "";
            // Catch potential errors with the file export or XSL compiliation
            try
            {
                if (cbApplyTransform.Checked)
                {
					using (Stream xsltStream = (Stream)cbTransforms.SelectedItem)
					{
						WarFoundryXmlWithXslExporter.GetDefault().ExportArmyWithTransform(myArmy, tbOutputFile.Text, xsltStream);
					}
                }
                else
                {
                    WarFoundryXmlWithXslExporter.GetDefault().ExportArmy(myArmy, tbOutputFile.Text);
                }
            }
            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 != "")
            {
                MessageBox.Show(errorMessage, "Error During Export", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.DialogResult = DialogResult.OK;
            this.Hide();
        }

        private void tbOutputFile_Change(object sender, EventArgs e)
        {
            if (tbOutputFile.Text != "")
            {
                bttnExport.Enabled = true;
            }
            else
            {
                bttnExport.Enabled = false;
            }
        }

        private void cbApplyTransform_CheckedChanged(object sender, EventArgs e)
        {
            if (cbApplyTransform.Checked)
            {
                cbTransforms.Enabled = true;
            }
            else
            {
				cbTransforms.Enabled = false;
            }
        }
    }
}