view FrmNewUnit.cs @ 167:9808adf2d566

Re #419: Remove assumptions of a file-based install * Update GTK# GUI to use Streams version of XSLT export
author IBBoard <dev@ibboard.co.uk>
date Wed, 28 Nov 2012 20:25:53 +0000
parents f23e5b40ca9d
children
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 WFObjects = IBBoard.WarFoundry.API.Objects;
10 using IBBoard.WarFoundry.API.Objects;
11 using log4net;
12 using IBBoard.Lang;
13 using IBBoard.WarFoundry.API.Objects.Requirement;
14 using System.Text;
15 using IBBoard.WarFoundry.API.Objects.Requirement.Context;
17 namespace IBBoard.WarFoundry.GUI.GTK
18 {
19 public partial class FrmNewUnit : TranslatableDialog
20 {
21 private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit));
22 private UnitType unitType;
23 private Army unitArmy;
24 private string titlePart;
25 private AddingContext context;
27 public FrmNewUnit(Race race, Category category, Army army) : this(race.GetUnitTypes(category), army)
28 {
29 titlePart = category.Name;
30 context = new NullAddingContext();
31 }
33 public FrmNewUnit(WFObjects.Unit unit, Army army) : this(unit.UnitType.ContainedUnitTypes, army)
34 {
35 titlePart = unit.Name;
36 context = new AddingToParentContext(unit);
37 }
39 private FrmNewUnit(UnitType[] unitTypes, Army army)
40 {
41 this.Build();
42 unitArmy = army;
44 TreeViewColumn unitTypeColumn = new TreeViewColumn();
45 CellRendererText unitTypeCell = new CellRendererText();
46 unitTypeColumn.PackStart(unitTypeCell, true);
47 lstUnitTypes.AppendColumn(unitTypeColumn);
48 unitTypeColumn.SetCellDataFunc(unitTypeCell, new TreeCellDataFunc(RenderUnitTypeName));
49 ListStore store = new ListStore(typeof(UnitType));
50 logger.DebugFormat("Listing {0} unit types", unitTypes.Length);
52 foreach (UnitType type in unitTypes)
53 {
54 logger.DebugFormat("Adding unit type {0}", type.Name);
55 store.AppendValues(type);
56 }
58 lstUnitTypes.Model = store;
59 lstUnitTypes.Selection.Changed += new EventHandler(OnSelectionChanged);
61 Translation.TranslationChanged += Retranslate;
62 Translate();
63 }
65 private void Retranslate()
66 {
67 Translate();
68 }
70 public override void Dispose()
71 {
72 Translation.TranslationChanged -= Retranslate;
73 base.Dispose();
74 }
76 protected override void Translate()
77 {
78 base.Translate();
79 lstUnitTypes.Columns[0].Title = Translation.GetTranslation("frmNewUnitNewUnitColumn", "unit type");
80 Title = Translation.GetTranslation(Name, "Create new unit", titlePart);
81 }
83 private void RenderUnitTypeName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
84 {
85 UnitType type = (UnitType)model.GetValue(iter, 0);
86 (cell as CellRendererText).Text = type.Name;
87 }
89 protected virtual void OnSelectionChanged(object o, EventArgs e)
90 {
91 SetSelectUnitEnabledVal();
92 }
94 public UnitType SelectedUnit
95 {
96 get { return unitType; }
97 }
99 private UnitType GetSelectedUnitType()
100 {
101 TreeModel model;
102 TreeIter iter;
103 lstUnitTypes.Selection.GetSelected(out model, out iter);
104 UnitType toReturn = null;
106 if (model != null)
107 {
108 toReturn = (UnitType)model.GetValue(iter, 0);
109 }
111 return toReturn;
112 }
114 private void SetSelectUnitEnabledVal()
115 {
116 UnitType type = GetSelectedUnitType();
118 if (type != null)
119 {
120 bttnCreate.Sensitive = true;
121 ICollection<string> failureMessages;
122 Validation result = RequirementHandler.AllowsAdding(type, unitArmy, context, out failureMessages);
123 bool validationFailed = !Validates.AsOkay(result);
124 validationWidget.Visible = validationFailed;
126 if (validationFailed)
127 {
128 validationWidget.SetMessages(failureMessages);
129 }
130 }
131 else
132 {
133 bttnCreate.Sensitive = false;
134 validationWidget.Visible = false;
135 }
136 }
138 protected virtual void OnButtonCancelActivated(object sender, System.EventArgs e)
139 {
140 Respond(ResponseType.Cancel);
141 }
143 protected virtual void OnRowActivated(object o, Gtk.RowActivatedArgs args)
144 {
145 unitType = GetSelectedUnitType();
146 Respond(ResponseType.Ok);
147 }
149 protected virtual void OnButtonOkClicked(object sender, System.EventArgs e)
150 {
151 unitType = GetSelectedUnitType();
152 Respond(ResponseType.Ok);
153 }
154 }
155 }