1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui.wizard;
20
21 import java.awt.Dimension;
22 import java.awt.event.ActionEvent;
23 import java.util.List;
24
25 import javax.swing.DefaultListModel;
26 import javax.swing.JList;
27 import javax.swing.JOptionPane;
28 import javax.swing.JScrollPane;
29 import javax.swing.ListSelectionModel;
30 import javax.swing.WindowConstants;
31 import javax.swing.event.ListSelectionEvent;
32 import javax.swing.event.ListSelectionListener;
33
34 import net.sf.magicproject.action.BackgroundMessaging;
35 import net.sf.magicproject.clickable.ability.Ability;
36 import net.sf.magicproject.event.context.ContextEventListener;
37 import net.sf.magicproject.ui.i18n.LanguageManager;
38
39 /***
40 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41 * @since 0.85
42 */
43 public class Choice extends YesNo implements ListSelectionListener {
44
45 /***
46 * Creates a new instance of Choice <br>
47 *
48 * @param context
49 * context of associated ability. This context will be used to
50 * restart this wizard in case of Backgroud button is used.
51 * @param ability
52 * ability to associate to this ability. If this ability has an
53 * assosciated picture, it will be used instead of given picture.
54 * Ability's name is also used to fill the title. This ability will
55 * be used to restart this wizard in case of Background button is
56 * used.
57 * @param action
58 * the action's name and content will be used in the wizard totle and
59 * also message text.
60 * @param allowCancel
61 * Is the cancel button is allowed.
62 * @param actions
63 * set of avalaible choices?
64 */
65 public Choice(ContextEventListener context, Ability ability,
66 BackgroundMessaging action, boolean allowCancel, List<String> actions) {
67 super(context, ability, action, LanguageManager
68 .getString("wiz_choice.title"), LanguageManager
69 .getString("wiz_choice.description")
70 + ability.getAbilityTitle(), "wiz_choice.gif", LanguageManager
71 .getString("ok"), LanguageManager.getString("cancel"), 500, 300);
72 this.actions = actions;
73 int count = actions.size();
74 for (int i = actions.size(); i-- > 0;) {
75 if (actions.get(i) == null) {
76 count--;
77 }
78 }
79 if (count == 0) {
80
81 indexAnswer = 0;
82 optionAnswer = JOptionPane.NO_OPTION;
83 return;
84 }
85
86
87
88
89
90
91
92 if (allowCancel) {
93
94 setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
95 } else {
96
97 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
98 this.cancelBtn.setEnabled(false);
99 this.okBtn.setEnabled(false);
100 }
101 final DefaultListModel model = new DefaultListModel();
102 actionList = new JList(model);
103 actionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
104 actionList.setLayoutOrientation(JList.VERTICAL);
105 actionList.addListSelectionListener(this);
106 for (int i = 0; i < actions.size(); i++) {
107 if (actions.get(i) != null) {
108 model.addElement("<html>" + actions.get(i));
109 }
110 }
111 final JScrollPane listScrollerLeft = new JScrollPane(actionList);
112 listScrollerLeft.setPreferredSize(new Dimension(100, 280));
113 gameParamPanel.add(listScrollerLeft);
114
115
116
117
118 }
119
120 @Override
121 public void setVisible(boolean visible) {
122 if (visible && actionList == null) {
123 return;
124 }
125 super.setVisible(visible);
126 }
127
128 public void valueChanged(ListSelectionEvent e) {
129 if (!e.getValueIsAdjusting()) {
130 if (actionList.getSelectedIndex() == -1) {
131
132 okBtn.setEnabled(false);
133 } else {
134
135 okBtn.setEnabled(true);
136 }
137 }
138 }
139
140 @Override
141 public void actionPerformed(ActionEvent event) {
142 if (event.getSource() == cancelBtn) {
143
144 indexAnswer = 0;
145 } else if (event.getSource() == okBtn) {
146 if (actionList.getSelectedValue() == null) {
147
148 return;
149 }
150
151 indexAnswer = -1;
152 for (int i = actions.size(); i-- > 0;) {
153 if (actions.get(i).equals(
154 actionList.getSelectedValue().toString().substring(
155 "<html>".length()))) {
156 indexAnswer = i;
157 break;
158 }
159 }
160 if (indexAnswer == -1) {
161 throw new IllegalStateException(
162 "Unable to find selected index in choice");
163 }
164
165 }
166 super.actionPerformed(event);
167 }
168
169 /***
170 * The list of available actions
171 */
172 private JList actionList;
173
174 /***
175 * The actions list displayed in List GUI component. This array may contain
176 * <code>null</code> objects. These ones are ignored.
177 */
178 private final List<String> actions;
179 }