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.ui;
22  
23  import java.awt.event.ActionEvent;
24  import java.awt.event.ActionListener;
25  import java.io.File;
26  import java.io.IOException;
27  
28  import javax.swing.JOptionPane;
29  
30  import net.sf.magicproject.AbstractMainForm;
31  import net.sf.magicproject.Magic;
32  import net.sf.magicproject.token.IdConst;
33  import net.sf.magicproject.tools.Configuration;
34  import net.sf.magicproject.tools.Log;
35  import net.sf.magicproject.tools.MToolKit;
36  import net.sf.magicproject.tools.WebBrowser;
37  import net.sf.magicproject.ui.i18n.LanguageManager;
38  import net.sf.magicproject.xml.XmlConfiguration;
39  
40  import org.apache.commons.io.FileUtils;
41  
42  /***
43   * An ActionListener that listens to the radio buttons menus
44   * 
45   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46   * @since 0.83
47   * @since 0.85 settings are "per TBS"
48   */
49  public class MdbListener implements ActionListener {
50  
51  	/***
52  	 * Create a new instance of MdbListener given an AbstractMainForm instance.
53  	 * 
54  	 * @param abstractMainForm
55  	 */
56  	public MdbListener(AbstractMainForm abstractMainForm) {
57  		this.abstractMainForm = abstractMainForm;
58  	}
59  
60  	/***
61  	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
62  	 */
63  	public void actionPerformed(ActionEvent e) {
64  		if ("menu_options_tbs_more".equals(e.getActionCommand())) {
65  			// goto "more TBS" page
66  			try {
67  				WebBrowser
68  						.launchBrowser("http://sourceforge.net/project/showfiles.php?group_id=92519&package_id=107882");
69  			} catch (Exception e1) {
70  				JOptionPane.showOptionDialog(MagicUIComponents.magicForm,
71  						LanguageManager.getString("error") + " : " + e1.getMessage(),
72  						LanguageManager.getString("web-pb"), JOptionPane.OK_OPTION,
73  						JOptionPane.INFORMATION_MESSAGE, UIHelper
74  								.getIcon("wiz_update_error.gif"), null, null);
75  			}
76  			return;
77  		}
78  		if ("menu_options_tbs_update".equals(e.getActionCommand())) {
79  			// update the current TBS
80  			XmlConfiguration.main(new String[] { "-update", MToolKit.tbsName });
81  			return;
82  		}
83  		if ("menu_options_tbs_rebuild".equals(e.getActionCommand())) {
84  			/*
85  			 * rebuild completely the current TBS
86  			 */
87  			XmlConfiguration.main(new String[] { "-full", MToolKit.tbsName });
88  			return;
89  		}
90  
91  		if (JOptionPane.YES_OPTION == JOptionPane.showOptionDialog(
92  				MagicUIComponents.magicForm, LanguageManager
93  						.getString("warn-disconnect"), LanguageManager
94  						.getString("disconnect"), JOptionPane.YES_NO_OPTION,
95  				JOptionPane.QUESTION_MESSAGE, UIHelper.getIcon("wiz_update.gif"), null,
96  				null)) {
97  
98  			// Save the current settings before changing TBS
99  			Magic.saveSettings();
100 			final File propertyFile = new File(IdConst.FILE_SETTINGS);
101 			try {
102 				FileUtils.copyFile(propertyFile, new File(MToolKit.getTbsFile(
103 						IdConst.FILE_SETTINGS_SAVED, false)));
104 				// Load the one of the new TBS
105 				abstractMainForm.setMdb(e.getActionCommand());
106 				Configuration.loadTemplateFile(IdConst.FILE_SETTINGS_SAVED);
107 				FileUtils.copyFile(new File(MToolKit.getTbsFile(
108 						IdConst.FILE_SETTINGS_SAVED, false)), propertyFile);
109 				Log.info("Successfull TBS swith to " + MToolKit.tbsName);
110 				System.exit(0);
111 			} catch (IOException e1) {
112 				e1.printStackTrace();
113 			}
114 		}
115 	}
116 
117 	/***
118 	 * The AbstractMainForm instance that would receive the "setMdb(String)"
119 	 * message.
120 	 * 
121 	 * @see AbstractMainForm#setMdb(String)
122 	 */
123 	private AbstractMainForm abstractMainForm;
124 }