View Javadoc

1   /*
2    * Created on 8 avr. 2005
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  		// "TBS" menu
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  		// "More TBS" menu item
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 		// nothing to do
198 	}
199 
200 	public void windowClosed(WindowEvent e) {
201 		// nothing to do
202 	}
203 
204 	public void windowIconified(WindowEvent e) {
205 		// nothing to do
206 	}
207 
208 	public void windowDeiconified(WindowEvent e) {
209 		// nothing to do
210 	}
211 
212 	public void windowActivated(WindowEvent e) {
213 		// nothing to do
214 	}
215 
216 	public void windowDeactivated(WindowEvent e) {
217 		// nothing to do
218 	}
219 
220 	public void mousePressed(MouseEvent e) {
221 		// nothing to do
222 	}
223 
224 	public void mouseReleased(MouseEvent e) {
225 		// nothing to do
226 	}
227 
228 	public void mouseEntered(MouseEvent e) {
229 		// nothing to do
230 	}
231 
232 	public void mouseExited(MouseEvent e) {
233 		// nothing to do
234 	}
235 }