diff 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 diff
--- a/FrmExportXml.cs	Sat Oct 01 16:49:35 2011 +0100
+++ b/FrmExportXml.cs	Sat Oct 01 17:04:22 2011 +0100
@@ -13,19 +13,23 @@
 
 namespace IBBoard.WarFoundry.GUI.GTK
 {
-	public partial class FrmExportXml : Gtk.Dialog
+	public partial class FrmExportXml : FileChooserDialog
 	{
 		private Army army;
 		private TransformXmlWidget transformWidget;
+		private Button buttonOk;
 
-		public FrmExportXml(Army army)
+		public FrmExportXml(Army army) : base("", null, FileChooserAction.Save)
 		{
 			this.army = army;
-			this.Build();
+			AddButton(Gtk.Stock.Cancel, ResponseType.Cancel);
+			buttonOk = (Button)AddButton(Gtk.Stock.Save, ResponseType.Ok);
+			buttonOk.Clicked+=OnButtonOkClicked;
 			transformWidget = new TransformXmlWidget();
 			transformWidget.TransformChanged += HandleTransformWidgetTransformChanged;
-			saveAsPath.ExtraWidget = transformWidget;
-			saveAsPath.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
+			ExtraWidget = transformWidget;
+			SetFilename(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "export.xml"));
+			SelectionChanged += OnSaveAsPathSelectionChanged;
 		}
 
 		private void HandleTransformWidgetTransformChanged (object sender, EventArgs e)
@@ -33,9 +37,16 @@
 			SetOkayButtonSensitive();
 		}
 
+		private bool IsValid ()
+		{
+			return Filename != null && Filename != "" && transformWidget.IsValid;
+		}
+
 		private void SetOkayButtonSensitive()
 		{
-			buttonOk.Sensitive = saveAsPath.Filename != null && saveAsPath.Filename != "" && transformWidget.IsValid;
+			//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)
@@ -45,67 +56,59 @@
 
 		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 (IsValid())
+			{
+				DoExport();
+			}
+		}
 
-                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)
-            {
+		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;
+			}
 
-                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 != "")
-            {
+			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.
-		}
 	}
 }