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.BorderLayout;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.Image;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.event.KeyEvent;
29 import java.awt.event.KeyListener;
30
31 import javax.swing.BoxLayout;
32 import javax.swing.JButton;
33 import javax.swing.JDialog;
34 import javax.swing.JPanel;
35 import javax.swing.JSeparator;
36
37 import net.sf.magicproject.action.BackgroundMessaging;
38 import net.sf.magicproject.clickable.ability.Ability;
39 import net.sf.magicproject.event.context.ContextEventListener;
40 import net.sf.magicproject.token.IdConst;
41 import net.sf.magicproject.tools.Picture;
42 import net.sf.magicproject.ui.MagicUIComponents;
43 import net.sf.magicproject.ui.i18n.LanguageManager;
44
45 import org.apache.commons.lang.StringUtils;
46
47 /***
48 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
49 * @since 0.82
50 * @since 0.86 JDialog icon updated
51 * @since 0.86 Auto-mnemonic
52 */
53 public abstract class Wizard extends JDialog implements ActionListener,
54 KeyListener {
55
56 /***
57 * Create a new instance of this class.
58 *
59 * @param context
60 * context of associated ability. This context will be used to
61 * restart this wizard in case of Backgroud button is used.
62 * @param ability
63 * ability to associate to this ability. If this ability has an
64 * assosciated picture, it will be used instead of given picture.
65 * Ability's name is also used to fill the title. This ability will
66 * be used to restart this wizard in case of Background button is
67 * used.
68 * @param action
69 * the action's name and content will be used in the wizard totle and
70 * also message text.
71 * @param title
72 * the title of this wizard.
73 * @param description
74 * the description appendend to the title of this wizard. This
75 * content will be displayed as Html.
76 * @param iconName
77 * the icon's name to display on the top right place.
78 * @param width
79 * the preferred width.
80 * @param height
81 * the preferred height.
82 */
83 public Wizard(ContextEventListener context, Ability ability,
84 BackgroundMessaging action, String title, String description,
85 String iconName, int width, int height) {
86 super(MagicUIComponents.magicForm, StringUtils.capitalize(title), true);
87 getRootPane().setPreferredSize(new Dimension(width, 300));
88 getRootPane().setMinimumSize(new Dimension(width, height));
89 setSize(new Dimension(width, height));
90
91
92 gameParamPanel = new JPanel(null);
93 gameParamPanel.setLayout(new BoxLayout(gameParamPanel, BoxLayout.Y_AXIS));
94 if (ability == null)
95 getContentPane().add(
96 new WizardTitle(new WizardImageIcon((Image) null, iconName),
97 description), BorderLayout.NORTH);
98 else
99 getContentPane().add(
100 new WizardTitle(new WizardImageIcon(ability.getCard(), iconName),
101 description), BorderLayout.NORTH);
102 getContentPane().add(gameParamPanel, BorderLayout.CENTER);
103 getContentPane().add(new JPanel(), BorderLayout.EAST);
104
105
106 final JPanel abstractButtonPanel = new JPanel(new BorderLayout());
107 buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
108
109 if (action != null) {
110 this.context = context;
111 this.action = action;
112 this.ability = ability;
113 backgroundButton = new JButton(LanguageManager
114 .getString("wiz_background"));
115 backgroundButton.setMnemonic(backgroundButton.getText().charAt(0));
116 backgroundButton.setToolTipText(LanguageManager
117 .getString("wiz_background.tooltip"));
118 backgroundButton.addActionListener(this);
119 buttonPanel.add(backgroundButton);
120 }
121 abstractButtonPanel.setBorder(null);
122 abstractButtonPanel.add(new JSeparator(), BorderLayout.NORTH);
123 abstractButtonPanel.add(buttonPanel, BorderLayout.CENTER);
124 abstractButtonPanel.add(wizardInfo, BorderLayout.SOUTH);
125 getContentPane().add(abstractButtonPanel, BorderLayout.SOUTH);
126 setLocationRelativeTo(null);
127 }
128
129 /***
130 * triggers the ok button
131 *
132 * @param event
133 */
134 public void actionPerformed(ActionEvent event) {
135 if (event.getSource() == backgroundButton) {
136
137 optionAnswer = BACKGROUND_OPTION;
138 MagicUIComponents.backgroundBtn.startButton(context, ability, action,
139 this);
140 setVisible(false);
141 }
142 }
143
144 /***
145 * Check the validity of this wizard.
146 *
147 * @return true if this wizard can be validated.
148 */
149 protected abstract boolean checkValidity();
150
151 /***
152 * Add a component to be listened.
153 *
154 * @param component
155 * comopoent to add
156 */
157 protected final void addCheckValidity(Component component) {
158 component.addKeyListener(this);
159 }
160
161 public void keyTyped(KeyEvent e) {
162
163 }
164
165 public void keyPressed(KeyEvent e) {
166
167 }
168
169 public void keyReleased(KeyEvent e) {
170 wizardInfo.noNewMessage = true;
171 if (checkValidity() && wizardInfo.noNewMessage) {
172 wizardInfo.reset();
173 }
174 }
175
176 @Override
177 public void setVisible(boolean visible) {
178 try {
179 if (visible) {
180 MagicUIComponents.magicForm.setIconImage(Picture
181 .loadImage(IdConst.IMAGES_DIR + "mp_wiz.gif"));
182 } else {
183 MagicUIComponents.magicForm.setIconImage(Picture
184 .loadImage(IdConst.IMAGES_DIR + "mp.gif"));
185 }
186 } catch (Exception e) {
187
188 }
189 super.setVisible(visible);
190 }
191
192 /***
193 * @param optionAnswer
194 */
195 protected void validAnswer(int optionAnswer) {
196 Wizard.optionAnswer = optionAnswer;
197 if (isModal()) {
198 setVisible(false);
199 dispose();
200 } else if (action != null) {
201
202 action.replayAction(context, ability, this);
203 }
204 }
205
206 /***
207 * The panel containing the "cancel" button
208 */
209 protected JPanel buttonPanel;
210
211 /***
212 * The panel containing all fields related to the connection.
213 */
214 protected JPanel gameParamPanel;
215
216 /***
217 * Wizard info of this wizard.
218 */
219 protected WizardInfo wizardInfo = new WizardInfo();
220
221 /***
222 * @see javax.swing.JOptionPane#CANCEL_OPTION
223 * @see javax.swing.JOptionPane#OK_OPTION
224 */
225 public static int optionAnswer;
226
227 /***
228 * Optionnal integer answer
229 */
230 public static int indexAnswer;
231
232 /***
233 * The backgroung button code.
234 */
235 public static final int BACKGROUND_OPTION = -2;
236
237 private JButton backgroundButton;
238
239 private ContextEventListener context;
240
241 private Ability ability;
242
243 private BackgroundMessaging action;
244 }