1
2
3
4
5 package spellcast.ui;
6
7 import java.awt.FlowLayout;
8 import java.util.HashMap;
9
10 import javax.swing.JPanel;
11 import javax.swing.SwingUtilities;
12
13 import org.apache.log4j.Logger;
14
15 import spellcast.beings.IWizard;
16 import spellcast.gestures.Gesture;
17 import spellcast.model.Id;
18
19 /***
20 * This component arranges the Wizard Display to include
21 * each Wizard and any monsters they control.
22 *
23 * @author Barrie Treloar
24 */
25 public class WizardDisplay extends JPanel {
26 private HashMap wizardIDToWizardDetailsMap = new HashMap();
27 private Id clientID;
28
29 private static final Logger logger = Logger.getLogger("client.gui");
30
31 public WizardDisplay() {
32 super();
33 setLayout(new FlowLayout());
34 setBorder(null);
35 }
36
37 public void init() {
38 clientID = Id.NO_ONE;
39 removeAll();
40 }
41
42 /***
43 * Set the value of id.
44 *
45 * @param v Value to assign to id.
46 */
47 public void setID(Id id) {
48 logger.info("setID(" + id + ") called");
49 if (clientID.equals(Id.NO_ONE)) {
50 clientID = id;
51 WizardDetails wd = (WizardDetails) wizardIDToWizardDetailsMap.get(clientID);
52 if (wd != null) {
53 wd.setClient(true);
54 }
55 }
56 else {
57 logger.warn("setID(" + id + ") called when an id already exists: " + clientID);
58 }
59 }
60
61 /***
62 * Return the gesture made with the left hand for the wizard specified by the current client Id.
63 */
64 public Gesture getLeftHandGesture() {
65 Gesture result = null;
66
67 WizardDetails wd = (WizardDetails) wizardIDToWizardDetailsMap.get(clientID);
68 if (wd != null) {
69 result = wd.getLeftHandGesture();
70 }
71 else {
72 logger.error("Could not find wizard details for id: " + clientID);
73 }
74
75 return result;
76 }
77
78 /***
79 * Return the gesture made with the right hand for the wizard specified by the current client Id.
80 */
81 public Gesture getRightHandGesture() {
82 Gesture result = null;
83
84 WizardDetails wd = (WizardDetails) wizardIDToWizardDetailsMap.get(clientID);
85 if (wd != null) {
86 result = wd.getRightHandGesture();
87 }
88 else {
89 logger.error("Could not find wizard details for id: " + clientID);
90 }
91
92 return result;
93 }
94
95 public void addWizard(IWizard wizard) {
96 final WizardDetails wd = new WizardDetails(wizard);
97 logger.info("addWizard with id=" + wizard.getId());
98 if (wizard.getId().equals(clientID)) {
99 wd.setClient(true);
100 }
101
102 wizardIDToWizardDetailsMap.put(wizard.getId(), wd);
103 SwingUtilities.invokeLater(new Runnable() {
104 public void run() {
105 add(wd);
106 revalidate();
107 }
108 });
109 }
110
111 public void removeWizard(IWizard wizard) {
112 final WizardDetails wd =
113 (WizardDetails) wizardIDToWizardDetailsMap.get(wizard.getId());
114 if (wd != null) {
115 SwingUtilities.invokeLater(new Runnable() {
116 public void run() {
117 remove(wd);
118 revalidate();
119 }
120 });
121 wizardIDToWizardDetailsMap.remove(wd);
122
123 }
124 else {
125 logger.error("Could not remove Wizard Details for Wizard: " + wizard);
126 }
127 }
128 }