comparison FrmMainWindow.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 2b9fabd65309
children f23e5b40ca9d
comparison
equal deleted inserted replaced
140:2b9fabd65309 141:1ce4fe9ae3c1
28 using IBBoard.Xml; 28 using IBBoard.Xml;
29 using log4net; 29 using log4net;
30 using WFObjects = IBBoard.WarFoundry.API.Objects; 30 using WFObjects = IBBoard.WarFoundry.API.Objects;
31 using IBBoard.WarFoundry.API.Savers.Xml; 31 using IBBoard.WarFoundry.API.Savers.Xml;
32 using IBBoard.WarFoundry.API.Objects.Requirement; 32 using IBBoard.WarFoundry.API.Objects.Requirement;
33 using System.Text;
33 34
34 namespace IBBoard.WarFoundry.GUI.GTK 35 namespace IBBoard.WarFoundry.GUI.GTK
35 { 36 {
36 public partial class FrmMainWindow: TranslatableWindowWithActions 37 public partial class FrmMainWindow: TranslatableWindowWithActions
37 { 38 {
39 private static readonly string VALIDATION_RESULTS_KEY = "WarFoundryValidationFailureMessages";
38 private static readonly string AppTitle = "WarFoundry"; 40 private static readonly string AppTitle = "WarFoundry";
39 private const int CATEGORY_BUTTON_SEPARATOR_INDEX = 6; 41 private const int CATEGORY_BUTTON_SEPARATOR_INDEX = 6;
40 private Preferences preferences; 42 private Preferences preferences;
41 private ILog logger = LogManager.GetLogger(typeof(FrmMainWindow)); 43 private ILog logger = LogManager.GetLogger(typeof(FrmMainWindow));
42 private CommandStack commandStack; 44 private CommandStack commandStack;
156 //FIXME: Temporary hack to add paths and factories before we get preferences and plugins 158 //FIXME: Temporary hack to add paths and factories before we get preferences and plugins
157 WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + "data")); 159 WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + "data"));
158 WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory()); 160 WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory());
159 WarFoundryLoader.GetDefault().FileLoadingFinished += FileLoadingFinished; 161 WarFoundryLoader.GetDefault().FileLoadingFinished += FileLoadingFinished;
160 WarFoundrySaver.SetFileSaver(new WarFoundryXmlFileSaver()); 162 WarFoundrySaver.SetFileSaver(new WarFoundryXmlFileSaver());
161 163 SetStatusBarText();
164
165 LoadFilesFromArgs(args);
166 }
167
168 private void LoadFilesFromArgs(string[] args)
169 {
162 logger.Debug("Initialising complete - seeing if we can load default army or system"); 170 logger.Debug("Initialising complete - seeing if we can load default army or system");
163
164 if (args.Length == 1) 171 if (args.Length == 1)
165 { 172 {
166 logger.Debug("Attempting to load from file"); 173 logger.Debug("Attempting to load from file");
167 FileInfo file = new FileInfo(args[0]); 174 FileInfo file = new FileInfo(args[0]);
168 175
169 try 176 try
170 { 177 {
171 ICollection<IWarFoundryObject> objects = WarFoundryLoader.GetDefault().LoadFile(file); 178 ICollection<IWarFoundryObject> objects = WarFoundryLoader.GetDefault().LoadFile(file);
172 179
173 if (objects.Count == 1) 180 if (objects.Count == 1)
174 { 181 {
175 List<IWarFoundryObject> objectList = new List<IWarFoundryObject>(); 182 List<IWarFoundryObject> objectList = new List<IWarFoundryObject>();
176 objectList.AddRange(objects); 183 objectList.AddRange(objects);
177 IWarFoundryObject loadedObject = objectList[0]; 184 IWarFoundryObject loadedObject = objectList[0];
178 185
179 if (loadedObject is Army) 186 if (loadedObject is Army)
180 { 187 {
181 WarFoundryCore.CurrentArmy = (Army)loadedObject; 188 WarFoundryCore.CurrentArmy = (Army)loadedObject;
182 logger.InfoFormat("Loaded army from {0}", file.FullName); 189 logger.InfoFormat("Loaded army from {0}", file.FullName);
183 } 190 }
186 if (loadedObject is GameSystem) 193 if (loadedObject is GameSystem)
187 { 194 {
188 WarFoundryCore.CurrentGameSystem = (GameSystem)loadedObject; 195 WarFoundryCore.CurrentGameSystem = (GameSystem)loadedObject;
189 logger.InfoFormat("Loaded game system from {0}", file.FullName); 196 logger.InfoFormat("Loaded game system from {0}", file.FullName);
190 } 197 }
191 198
192 } 199 }
193 } 200 }
194 } 201 }
195 catch (InvalidFileException ex) 202 catch (InvalidFileException ex)
196 { 203 {
199 } 206 }
200 } 207 }
201 else 208 else
202 { 209 {
203 string gameSystemID = Preferences.GetStringProperty("currSystem"); 210 string gameSystemID = Preferences.GetStringProperty("currSystem");
204 211
205 if (gameSystemID != null && !"".Equals(gameSystemID)) 212 if (gameSystemID != null && !"".Equals(gameSystemID))
206 { 213 {
207 logger.Debug("Attempting to load current game system from properties"); 214 logger.Debug("Attempting to load current game system from properties");
208 GameSystem sys = WarFoundryLoader.GetDefault().GetGameSystem(gameSystemID); 215 GameSystem sys = WarFoundryLoader.GetDefault().GetGameSystem(gameSystemID);
209 216
210 if (sys != null) 217 if (sys != null)
211 { 218 {
212 WarFoundryCore.CurrentGameSystem = sys; 219 WarFoundryCore.CurrentGameSystem = sys;
213 logger.InfoFormat("Loaded game system {0} from properties", gameSystemID); 220 logger.InfoFormat("Loaded game system {0} from properties", gameSystemID);
214 } 221 }
673 SetValidationText(); 680 SetValidationText();
674 } 681 }
675 else 682 else
676 { 683 {
677 statusbar.Push(1, ""); 684 statusbar.Push(1, "");
678 lblValidationWarning.Text = ""; 685 SetBlankValidationText();
679 } 686 }
680 } 687 }
681 688
682 private string GetPointsText() 689 private string GetPointsText()
683 { 690 {
694 { 701 {
695 SetValidationTextAndColour(); 702 SetValidationTextAndColour();
696 } 703 }
697 else 704 else
698 { 705 {
699 lblValidationWarning.Text = ""; 706 SetBlankValidationText();
700 } 707 }
701 } 708 }
702 709
703 private void SetValidationTextAndColour() 710 private void SetValidationTextAndColour()
704 { 711 {
705 ICollection<string> failureMessages; 712 ICollection<string> failureMessages;
706 Validation result = RequirementHandler.ValidateArmy(WarFoundryCore.CurrentArmy, out failureMessages); 713 Validation result = RequirementHandler.ValidateArmy(WarFoundryCore.CurrentArmy, out failureMessages);
707 string pluralHack = (failureMessages.Count == 1 ? "" : "s"); 714 string pluralHack = (failureMessages.Count == 1 ? "" : "s");
708 lblValidationWarning.Text = String.Format("{0} validation warning{1}", failureMessages.Count, pluralHack); 715 lblValidationWarning.Text = String.Format("{0} validation warning{1}", failureMessages.Count, pluralHack);
716 lblValidationWarning.Data[VALIDATION_RESULTS_KEY] = failureMessages;
709 717
710 if (Validates.AsOkay(result)) 718 if (Validates.AsOkay(result))
711 { 719 {
712 lblValidationWarning.ModifyFg(StateType.Normal, Gdk.Color.Zero); 720 lblValidationWarning.ModifyFg(StateType.Normal, Gdk.Color.Zero);
713 } 721 }
715 { 723 {
716 Gdk.Color red; 724 Gdk.Color red;
717 Gdk.Color.Parse("#cc0000", ref red); 725 Gdk.Color.Parse("#cc0000", ref red);
718 lblValidationWarning.ModifyFg(StateType.Normal, red); 726 lblValidationWarning.ModifyFg(StateType.Normal, red);
719 } 727 }
728 }
729
730 private void SetBlankValidationText()
731 {
732 lblValidationWarning.Text = "";
733 lblValidationWarning.Data[VALIDATION_RESULTS_KEY] = new string[0];
734 lblValidationWarning.ModifyFg(StateType.Normal, Gdk.Color.Zero);
720 } 735 }
721 736
722 private void commandStack_CommandStackUpdated() 737 private void commandStack_CommandStackUpdated()
723 { 738 {
724 undoMenuButton.Sensitive = commandStack.CanUndo(); 739 undoMenuButton.Sensitive = commandStack.CanUndo();
1253 FrmEditArmy form = new FrmEditArmy(commandStack, WarFoundryCore.CurrentArmy); 1268 FrmEditArmy form = new FrmEditArmy(commandStack, WarFoundryCore.CurrentArmy);
1254 form.Run(); 1269 form.Run();
1255 form.Hide(); 1270 form.Hide();
1256 form.Dispose(); 1271 form.Dispose();
1257 } 1272 }
1273
1274 protected void OnLblValidationWarningButtonReleaseEvent(object o, Gtk.ButtonReleaseEventArgs args)
1275 {
1276 if (args.Event.Button != (uint)MouseButton.Left)
1277 {
1278 return;
1279 }
1280
1281 ICollection<string> failureMessages = lblValidationWarning.Data[VALIDATION_RESULTS_KEY] as ICollection<string>;
1282
1283 if (failureMessages != null && failureMessages.Count > 0)
1284 {
1285 StringBuilder sb = new StringBuilder();
1286
1287 foreach (string msg in failureMessages)
1288 {
1289 sb.AppendLine(msg);
1290 }
1291
1292 MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Warning, ButtonsType.Ok, false, sb.ToString());
1293 dialog.Title = "Validation errors";
1294 dialog.Run();
1295 dialog.Hide();
1296 dialog.Dispose();
1297 }
1298 }
1258 } 1299 }
1259 } 1300 }