annotate API/Commands/RemoveUnitCommand.cs @ 473:0d032c04210e

Re #359: Add "only contained" attribute to unit types * Make sure remove handles sub-units as well Note: warning the user that the unit has sub-units rather than just removing them is left to the UI
author IBBoard <dev@ibboard.co.uk>
date Mon, 16 Apr 2012 20:47:14 +0100
parents 3c4a6403a88c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (RemoveUnitCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
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.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using IBBoard.Commands;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using IBBoard.Lang;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using IBBoard.WarFoundry.API.Objects;
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
9 using System.Collections.Generic;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 namespace IBBoard.WarFoundry.API.Commands
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 /// <summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 /// Summary description for RemoveUnitCommand.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 /// </summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 public class RemoveUnitCommand : Command
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 private Unit unit;
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
19 private Unit parentUnit;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 private ArmyCategory cat;
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
21 private List<RemoveUnitCommand> subCommands;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 public RemoveUnitCommand(Unit toRemove)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 unit = toRemove;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 cat = unit.Category;
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
27 parentUnit = unit.ParentUnit;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 public override bool CanExecute()
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 return (unit != null);
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 public override string Description
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 get
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 return Translation.GetTranslation("removeUnitCommandDescription", "remove {0} from the army", unit.Name);
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 public override string UndoDescription
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 get
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 return Translation.GetTranslation("removeUnitCommandUndoDescription", "re-add {0} to the army", unit.Name);
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
51 private void Prepare()
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
52 {
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
53 subCommands = new List<RemoveUnitCommand>();
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
54 foreach (Unit containedUnit in unit.ContainedUnits)
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
55 {
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
56 RemoveUnitCommand removeUnitCommand = new RemoveUnitCommand(containedUnit);
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
57 removeUnitCommand.Prepare();
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
58 subCommands.Add(removeUnitCommand);
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
59 }
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
60 }
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 public override bool Execute()
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 {
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
63 Prepare();
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
64 Redo();
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 return true;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 public override void Redo()
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 {
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
70 unit.ParentUnit = null;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 cat.RemoveUnit(unit);
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
72
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
73 foreach (RemoveUnitCommand subCommand in subCommands)
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
74 {
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
75 subCommand.Redo();
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
76 }
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 public override void Undo()
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 {
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
81 unit.ParentUnit = parentUnit;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 cat.AddUnit(unit);
473
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
83
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
84 foreach (RemoveUnitCommand subCommand in subCommands)
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
85 {
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
86 subCommand.Undo();
0d032c04210e Re #359: Add "only contained" attribute to unit types
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
87 }
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 public override string Name
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 get { return "Remove unit"; }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 }