1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject;
22
23 import java.awt.Color;
24 import java.awt.event.ActionListener;
25 import java.awt.event.MouseEvent;
26 import java.awt.event.MouseListener;
27 import java.awt.event.WindowEvent;
28 import java.awt.event.WindowListener;
29 import java.io.File;
30 import java.io.FileFilter;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33
34 import javax.swing.ButtonGroup;
35 import javax.swing.JFrame;
36 import javax.swing.JMenu;
37 import javax.swing.JMenuItem;
38 import javax.swing.JRadioButtonMenuItem;
39 import javax.swing.JSeparator;
40
41 import net.sf.magicproject.deckbuilder.MdbLoader;
42 import net.sf.magicproject.token.IdConst;
43 import net.sf.magicproject.tools.Log;
44 import net.sf.magicproject.tools.MToolKit;
45 import net.sf.magicproject.ui.MdbListener;
46 import net.sf.magicproject.ui.UIHelper;
47 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
48
49 import org.apache.commons.io.FilenameUtils;
50 import org.apache.commons.io.filefilter.FileFilterUtils;
51
52 /***
53 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
54 * @since 0.83
55 */
56 public abstract class AbstractMainForm extends JFrame implements
57 ActionListener, MouseListener, WindowListener {
58
59 /***
60 * Create a new instance of this class.
61 *
62 * @param title
63 * the form 's title
64 */
65 protected AbstractMainForm(String title) {
66 super(title);
67 setBackground(Color.BLACK);
68 }
69
70 /***
71 * Init the components of this form.
72 */
73 protected void initAbstractMenu() {
74
75 tbsMenu = UIHelper.buildMenu("menu_options_tbs");
76 ButtonGroup group4 = new ButtonGroup();
77 final MdbListener mdbListener = new MdbListener(this);
78 final File[] mdbs = MToolKit.getFile(IdConst.TBS_DIR).listFiles(
79 (FileFilter) FileFilterUtils.suffixFileFilter("xml"));
80 String defaultTbs = MToolKit.tbsName;
81 for (File mdb : mdbs) {
82 String mdbName = FilenameUtils.getBaseName(mdb.getName());
83 JRadioButtonMenuItem itemChck = new JRadioButtonMenuItem();
84 MToolKit.tbsName = mdbName;
85 updateMdbMenu(mdbName, itemChck);
86 itemChck.setActionCommand(mdbName);
87 itemChck.setFont(MToolKit.defaultFont);
88 itemChck.addActionListener(mdbListener);
89 group4.add(itemChck);
90 tbsMenu.add(itemChck);
91 if (mdbName.equals(defaultTbs)) {
92 itemChck.setSelected(true);
93 }
94 }
95 MToolKit.tbsName = defaultTbs;
96 tbsMenu.add(new JSeparator());
97
98
99 tbsMenu.add(UIHelper.buildMenu("menu_options_tbs_more", mdbListener));
100 tbsMenu.add(new JSeparator());
101 final JMenuItem updateMdbMenu = UIHelper.buildMenu(
102 "menu_options_tbs_update", mdbListener);
103 updateMdbMenu.setEnabled(false);
104 tbsMenu.add(updateMdbMenu);
105 tbsMenu.add(UIHelper.buildMenu("menu_options_tbs_rebuild", mdbListener));
106 optionMenu.add(new JSeparator());
107 optionMenu.add(tbsMenu);
108 }
109
110 /***
111 * From the specified mdb file, and the specified radio button menu, we fill
112 * this control with the information read from the MDB file.
113 *
114 * @param mdbName
115 * the the mdb file containing the TBS rules
116 * @param itemChck
117 * the radio button representing this TBS
118 */
119 private void updateMdbMenu(String mdbName, JRadioButtonMenuItem itemChck) {
120 String fullName = null;
121 try {
122 final FileInputStream in = MdbLoader.openMdb(IdConst.TBS_DIR + "/"
123 + mdbName + "/" + mdbName + ".mdb", true);
124 fullName = MToolKit.readString(in);
125 itemChck.setToolTipText("<html><b>" + fullName + "</b> "
126 + MToolKit.readString(in) + "</b> : " + MToolKit.readString(in)
127 + "<br>" + MToolKit.readString(in));
128 MdbLoader.closeMdb();
129 } catch (IOException e) {
130 Log
131 .error("The mdb file associated to the TBS '"
132 + mdbName
133 + "' has not been found.\nYou must rebuild this TBS before playing with it");
134 fullName = "*" + mdbName + " (rebuild it)";
135 } catch (Throwable e) {
136 Log.error("Exception during the TBS initalisation.\n\tInput file = "
137 + IdConst.TBS_DIR + "/" + mdbName + "/" + mdbName + ".mdb", e);
138 fullName = "*" + mdbName + " (rebuild it)";
139 }
140 itemChck.setText(fullName);
141 itemChck.setSelected(mdbName.equalsIgnoreCase(MToolKit.tbsName));
142 }
143
144 /***
145 * Set the current TBS name. Calling this method cause the mana symbols to be
146 * downloaded if it's not yet done.
147 *
148 * @param tbsName
149 * the TBS to define as current.
150 */
151 protected void setToolKitMdb(String tbsName) {
152 if (MToolKit.tbsName == null || !MToolKit.tbsName.equals(tbsName)) {
153 MToolKit.tbsName = tbsName;
154 MToolKit.mdbFile = IdConst.TBS_DIR + "/" + tbsName + "/" + tbsName
155 + ".mdb";
156 LanguageManagerMDB.setMdb(tbsName);
157 MToolKit.translator = null;
158 try {
159 MdbLoader.loadHeader(MToolKit.mdbFile);
160 } catch (IOException e) {
161 System.out.println("Warning : the MDB file '" + MToolKit.mdbFile
162 + "' associated to the TBS '" + tbsName + "' is not built");
163 }
164 }
165 }
166
167 /***
168 * Set the current TBS name. Calling this method cause the mana symbols to be
169 * downloaded if it's not yet done.
170 *
171 * @param tbsName
172 * the TBS to define as current.
173 */
174 public final void setMdb(String tbsName) {
175 setToolKitMdb(tbsName);
176 for (int i = 0; i < tbsMenu.getItemCount(); i++) {
177 if (tbsName.equals(tbsMenu.getItem(i).getActionCommand())) {
178 tbsMenu.getItem(i).setSelected(true);
179 return;
180 }
181 }
182 }
183
184 /***
185 * Comment for <code>optionMenu</code>
186 */
187 protected JMenu optionMenu;
188
189 /***
190 * The TBS menu
191 */
192 private static JMenu tbsMenu;
193
194 public abstract void windowClosing(WindowEvent e);
195
196 public void windowOpened(WindowEvent e) {
197
198 }
199
200 public void windowClosed(WindowEvent e) {
201
202 }
203
204 public void windowIconified(WindowEvent e) {
205
206 }
207
208 public void windowDeiconified(WindowEvent e) {
209
210 }
211
212 public void windowActivated(WindowEvent e) {
213
214 }
215
216 public void windowDeactivated(WindowEvent e) {
217
218 }
219
220 public void mousePressed(MouseEvent e) {
221
222 }
223
224 public void mouseReleased(MouseEvent e) {
225
226 }
227
228 public void mouseEntered(MouseEvent e) {
229
230 }
231
232 public void mouseExited(MouseEvent e) {
233
234 }
235 }