1
2
3
4
5 package spellcast.ui;
6
7 import java.awt.Color;
8 import java.awt.Dimension;
9 import java.awt.FlowLayout;
10 import java.awt.event.ItemEvent;
11 import java.awt.event.ItemListener;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import javax.swing.BorderFactory;
15 import javax.swing.Box;
16 import javax.swing.BoxLayout;
17 import javax.swing.JComboBox;
18 import javax.swing.JComponent;
19 import javax.swing.JLabel;
20 import javax.swing.JPanel;
21 import org.apache.log4j.Logger;
22
23 import spellcast.questions.Question;
24
25 /***
26 * The Query Display is the place where spellcast asks queries of the user.
27 * Each question has a number of different answers and one must be selected by the user.
28 * When the game is ready it will collect these answers.
29 * The display may also be used to provide informational feedback and in this case there
30 * are no answers to choose.
31 *
32 * @author Barrie Treloar
33 */
34 public class QueryDisplay extends JPanel {
35 private static final Dimension PREFERRED_SIZE = new Dimension(736, 100);
36 private static final int QUESTION_PREFERRED_WIDTH = 430;
37
38 private ArrayList queries = new ArrayList(10);
39 private ArrayList answerComponents = new ArrayList(10);
40
41 private Box verticalBox;
42
43 private static final Logger logger = Logger.getLogger("client.gui");
44
45 public QueryDisplay() {
46 setLayout(new FlowLayout(FlowLayout.LEFT, 4, 0));
47 setBorder(BorderFactory.createLineBorder(Color.black));
48 setPreferredSize(PREFERRED_SIZE);
49 }
50
51 public void init() {
52 removeAll();
53 queries.clear();
54 answerComponents.clear();
55 verticalBox = new Box(BoxLayout.Y_AXIS);
56 add(verticalBox);
57 }
58
59 /***
60 * Enable/Disable the answer components as per the enabled value.
61 */
62 public void setEnabled(boolean enabled) {
63 super.setEnabled(enabled);
64 Iterator answerComponentsIterator = answerComponents.iterator();
65 while (answerComponentsIterator.hasNext()) {
66 JComponent comp = (JComponent) answerComponentsIterator.next();
67 comp.setEnabled(enabled);
68 }
69 }
70
71 /***
72 * Add a query to be displayed to the client.
73 */
74 public void addQuery(final Question query) {
75 Box temp = new Box(BoxLayout.X_AXIS);
76
77 queries.add(query);
78
79 JLabel question = new JLabel(query.getQuestion());
80 question.setPreferredSize(
81 new Dimension(QUESTION_PREFERRED_WIDTH, question.getHeight()));
82 temp.add(question);
83
84 JComboBox answers = new JComboBox(query.getAnswers().toArray());
85 answerComponents.add(answers);
86 answers.setEnabled(isEnabled());
87 answers.addItemListener(new ItemListener() {
88 public void itemStateChanged(ItemEvent e) {
89
90 if (e.getStateChange() == ItemEvent.SELECTED) {
91 query.setAnswer((String) e.getItem());
92 }
93 }
94 });
95 temp.add(answers);
96
97 verticalBox.add(Box.createVerticalStrut(4));
98 verticalBox.add(temp);
99
100 revalidate();
101 }
102
103 /***
104 * Return all the queries that have been added.
105 */
106 public java.util.List getAnswers() {
107 return queries;
108 }
109
110 }