view FrmNewUnit.cs @ 110:76b73f15d07e

Re #344: Show unit requirement failures * First pass using new Requirement objects on FrmNewUnit - needs more testing and styling * Remove old references from main window until we get a replacement
author IBBoard <dev@ibboard.co.uk>
date Mon, 01 Aug 2011 20:22:48 +0000
parents b4416ca69153
children f23e5b40ca9d
line source
1 // This file (FrmNewUnit.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2008, 2009 IBBoard.
2 //
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.
5 using System;
6 using System.Collections.Generic;
7 using Gtk;
8 using IBBoard.GtkSharp.Translatable;
9 using IBBoard.WarFoundry.API.Objects;
10 using log4net;
11 using IBBoard.Lang;
12 using IBBoard.WarFoundry.API.Objects.Requirement;
13 using System.Text;
15 namespace IBBoard.WarFoundry.GUI.GTK
16 {
17 public partial class FrmNewUnit : TranslatableDialog
18 {
19 private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit));
20 private UnitType unitType;
21 private Army unitArmy;
22 private Category cat;
24 public FrmNewUnit(Race race, Category category, Army army)
25 {
26 this.Build();
27 unitArmy = army;
28 cat = category;
30 TreeViewColumn unitTypeColumn = new TreeViewColumn();
31 CellRendererText unitTypeCell = new CellRendererText();
32 unitTypeColumn.PackStart(unitTypeCell, true);
33 lstUnitTypes.AppendColumn(unitTypeColumn);
34 unitTypeColumn.SetCellDataFunc(unitTypeCell, new TreeCellDataFunc(RenderUnitTypeName));
35 ListStore store = new ListStore(typeof(UnitType));
36 UnitType[] types = race.GetUnitTypes(cat);
37 logger.DebugFormat("Listing {0} unit types in {1}", types.Length, cat.Name);
39 foreach (UnitType type in types)
40 {
41 logger.DebugFormat("Adding unit type {0}", type.Name);
42 store.AppendValues(type);
43 }
45 lstUnitTypes.Model = store;
46 lstUnitTypes.Selection.Changed += new EventHandler(OnSelectionChanged);
48 Translation.TranslationChanged += Retranslate;
49 Translate();
50 }
52 private void Retranslate()
53 {
54 Translate();
55 }
57 public override void Dispose()
58 {
59 Translation.TranslationChanged -= Retranslate;
60 base.Dispose();
61 }
63 protected override void Translate()
64 {
65 base.Translate();
66 lstUnitTypes.Columns[0].Title = Translation.GetTranslation("frmNewUnitNewUnitColumn", "unit type");
67 Title = Translation.GetTranslation(Name, "Create new unit", cat.Name);
68 }
70 private void RenderUnitTypeName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
71 {
72 UnitType type = (UnitType)model.GetValue(iter, 0);
73 (cell as CellRendererText).Text = type.Name;
74 }
76 protected virtual void OnSelectionChanged(object o, EventArgs e)
77 {
78 SetSelectUnitEnabledVal();
79 }
81 public UnitType SelectedUnit
82 {
83 get { return unitType; }
84 }
86 private UnitType GetSelectedUnitType()
87 {
88 TreeModel model;
89 TreeIter iter;
90 lstUnitTypes.Selection.GetSelected(out model, out iter);
91 UnitType toReturn = null;
93 if (model != null)
94 {
95 toReturn = (UnitType)model.GetValue(iter, 0);
96 }
98 return toReturn;
99 }
101 private void SetSelectUnitEnabledVal()
102 {
103 UnitType type = GetSelectedUnitType();
105 if (type != null)
106 {
107 bttnCreate.Sensitive = true;
108 ICollection<string> failureMessages;
109 Validation result = RequirementHandler.AllowsAdding(type, unitArmy, out failureMessages);
110 bool validationFailed = !Validates.AsOkay(result);
111 lblNewUnitWarning.Visible = validationFailed;
113 if (validationFailed)
114 {
115 StringBuilder sb = new StringBuilder();
117 foreach (string msg in failureMessages)
118 {
119 sb.AppendLine(msg);
120 }
122 lblNewUnitWarning.Text = sb.ToString();
123 }
124 }
125 else
126 {
127 bttnCreate.Sensitive = false;
128 lblNewUnitWarning.Visible = false;
129 }
130 }
132 protected virtual void OnButtonCancelActivated(object sender, System.EventArgs e)
133 {
134 Respond(ResponseType.Cancel);
135 }
137 protected virtual void OnRowActivated(object o, Gtk.RowActivatedArgs args)
138 {
139 unitType = GetSelectedUnitType();
140 Respond(ResponseType.Ok);
141 }
143 protected virtual void OnButtonOkClicked(object sender, System.EventArgs e)
144 {
145 unitType = GetSelectedUnitType();
146 Respond(ResponseType.Ok);
147 }
148 }
149 }