annotate FrmExportXml.cs @ 141:1ce4fe9ae3c1

Re #344: Show unit requirement failures * Add an initial way to access requirement failures after adding units
author IBBoard <dev@ibboard.co.uk>
date Mon, 17 Oct 2011 20:53:07 +0100
parents 0d8004d6a4e5
children 9808adf2d566
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
122
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (FrmExportXml.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2011 IBBoard
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // 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.
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 using System;
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System.IO;
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using IBBoard.GtkSharp;
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Collections.Generic;
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
8 using Gtk;
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
9 using IBBoard.WarFoundry.API.Objects;
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
10 using System.Xml.Xsl;
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
11 using IBBoard.Lang;
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
12 using IBBoard.WarFoundry.API.Exporters;
122
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 namespace IBBoard.WarFoundry.GUI.GTK
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 {
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
16 public partial class FrmExportXml : FileChooserDialog
122
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 {
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
18 private Army army;
126
d4e6bfeb1c61 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 125
diff changeset
19 private TransformXmlWidget transformWidget;
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
20 private Button buttonOk;
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
21
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
22 public FrmExportXml(Army army) : base("", null, FileChooserAction.Save)
122
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 {
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
24 this.army = army;
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
25 AddButton(Gtk.Stock.Cancel, ResponseType.Cancel);
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
26 buttonOk = (Button)AddButton(Gtk.Stock.Save, ResponseType.Ok);
131
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
27 buttonOk.Clicked+= OnButtonOkClicked;
126
d4e6bfeb1c61 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 125
diff changeset
28 transformWidget = new TransformXmlWidget();
d4e6bfeb1c61 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 125
diff changeset
29 transformWidget.TransformChanged += HandleTransformWidgetTransformChanged;
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
30 ExtraWidget = transformWidget;
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
31 SetFilename(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "export.xml"));
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
32 SelectionChanged += OnSaveAsPathSelectionChanged;
122
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 }
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34
126
d4e6bfeb1c61 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 125
diff changeset
35 private void HandleTransformWidgetTransformChanged (object sender, EventArgs e)
122
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
124
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
37 SetOkayButtonSensitive();
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
38 }
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
39
131
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
40 private bool IsValid
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
41 {
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
42 get
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
43 {
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
44 return CheckFileCanExist() && transformWidget.IsValid;
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
45 }
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
46 }
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
47
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
48 private bool CheckFileCanExist()
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
49 {
131
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
50 bool canExist = false;
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
51
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
52 if (!String.IsNullOrEmpty(Filename))
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
53 {
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
54 FileInfo file = new FileInfo(Filename);
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
55 canExist = file.Directory.Exists;
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
56 }
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
57
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
58 return canExist;
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
59 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
60
124
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
61 private void SetOkayButtonSensitive()
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
62 {
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
63 //TODO: It would be nice to disable save when appropriate options aren't set,
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
64 //but we don't seem to get change notifications early enough
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
65 //buttonOk.Sensitive = IsValid();
124
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
66 }
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
67
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
68 protected void OnSaveAsPathSelectionChanged(object sender, System.EventArgs e)
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
69 {
a647afc19fe9 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
70 SetOkayButtonSensitive();
122
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 }
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
72
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
73 protected void OnButtonOkClicked (object sender, System.EventArgs e)
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
74 {
131
0d8004d6a4e5 Re #361: XML Export UI for GTK
IBBoard <dev@ibboard.co.uk>
parents: 128
diff changeset
75 if (IsValid)
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
76 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
77 DoExport();
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
78 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
79 }
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
80
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
81 void DoExport()
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
82 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
83 string errorMessage = "";
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
84 // Catch potential errors with the file export or XSL compiliation
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
85 try
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
86 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
87 string fileName = Filename;
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
88
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
89 if (transformWidget.TransformEnabled)
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
90 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
91 WarFoundryXmlWithXslExporter.GetDefault().ExportArmyWithTransform(army, fileName, transformWidget.GetXsltPath());
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
92 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
93 else
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
94 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
95 WarFoundryXmlWithXslExporter.GetDefault().ExportArmy(army, fileName);
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
96 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
97 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
98 catch (XsltCompileException ex)
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
99 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
100 errorMessage = Translation.GetTranslation("mbErrorCompileFailed", "") + ":\n" + ex.Message;
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
101 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
102 catch (XsltException ex)
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
103 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
104
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
105 errorMessage = Translation.GetTranslation("mbErrorXSLTFailed", "") + ":\n" + ex.Message;
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
106 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
107 catch (FileNotFoundException ex)
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
108 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
109 errorMessage = Translation.GetTranslation("mbErrorFileNotFoundFailed", "") + ":\n" + ex.Message;
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
110 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
111 catch (IOException ex)
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
112 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
113 errorMessage = Translation.GetTranslation("mbErrorIOFailed", "") + ":\n" + ex.Message;
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
114 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
115 catch (Exception ex)
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
116 {
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
117 errorMessage = Translation.GetTranslation("mbErrorFailed", "") + ":\n" + ex.Message;
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
118 }
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
119
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
120 if (errorMessage != "")
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
121 {
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
122 MessageDialog dialog = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, errorMessage);
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
123 dialog.Run();
128
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
124 }
d5a631a8d288 Re #361: XML Export in GTK#
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
125
125
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
126 Respond(ResponseType.Ok);
42d2aa87dfa7 Re #361: Add XML export UI to GTK#
IBBoard <dev@ibboard.co.uk>
parents: 124
diff changeset
127 }
122
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
128 }
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
129 }
2479414b16f6 Re #361: Add XML output to GTK GUI
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
130