View Javadoc

1   /*
2    *   Firemox is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.firemox.ui.wizard;
20  
21  import java.awt.Component;
22  import java.awt.Dimension;
23  import java.awt.Image;
24  import java.awt.Rectangle;
25  import java.awt.Robot;
26  import java.awt.Toolkit;
27  import java.awt.event.ActionEvent;
28  import java.io.File;
29  import java.io.FileOutputStream;
30  import java.io.PrintWriter;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import javax.imageio.ImageIO;
35  import javax.swing.BoxLayout;
36  import javax.swing.JCheckBox;
37  import javax.swing.JLabel;
38  import javax.swing.JPanel;
39  import javax.swing.JScrollPane;
40  import javax.swing.JTextArea;
41  import javax.swing.JTextField;
42  import javax.swing.ScrollPaneConstants;
43  import javax.swing.WindowConstants;
44  
45  import net.sf.firemox.clickable.target.card.MCard;
46  import net.sf.firemox.clickable.target.player.Player;
47  import net.sf.firemox.mail.MailUtils;
48  import net.sf.firemox.stack.StackManager;
49  import net.sf.firemox.token.IdConst;
50  import net.sf.firemox.token.IdZones;
51  import net.sf.firemox.tools.Configuration;
52  import net.sf.firemox.tools.MToolKit;
53  import net.sf.firemox.ui.UIHelper;
54  import net.sf.firemox.ui.component.JLink;
55  import net.sf.firemox.ui.i18n.LanguageManager;
56  import net.sf.firemox.zone.MZone;
57  import net.sf.firemox.zone.ZoneManager;
58  import sun.awt.image.ToolkitImage;
59  
60  /***
61   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
62   * @since 0.82
63   */
64  public class Bug extends YesNo {
65  
66  	/***
67  	 * Creates a new instance of Bug <br>
68  	 */
69  	public Bug() {
70  		super(LanguageManager.getString("wiz_bugreport.title"), LanguageManager
71  				.getString("wiz_bugreport.description"), "wiz_bugreport.gif",
72  				LanguageManager.getString("send"), LanguageManager.getString("cancel"),
73  				600, 400);
74  		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
75  		gameParamPanel.setLayout(new BoxLayout(gameParamPanel, BoxLayout.Y_AXIS));
76  
77  		final JPanel userPanel = new JPanel();
78  		userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.X_AXIS));
79  		userPanel.add(new JLabel(LanguageManager
80  				.getString("wiz_sf.sourceforgename")
81  				+ " : "));
82  		userNameTxt = new JTextField(Configuration.getString("sourceforgeName", ""));
83  		userPanel.add(userNameTxt);
84  		JLink sfLabel = new JLink(
85  				"http://sourceforge.net/tracker/?func=add&group_id="
86  						+ IdConst.PROJECT_ID + "&atid=601040", UIHelper
87  						.getIcon("sourceforge_logo.png"));
88  		userPanel.add(sfLabel);
89  		userPanel.setMaximumSize(new Dimension(2000, 22));
90  		gameParamPanel.add(userPanel);
91  
92  		final JPanel summaryPanel = new JPanel();
93  		summaryPanel.setLayout(new BoxLayout(summaryPanel, BoxLayout.X_AXIS));
94  		summaryPanel.add(new JLabel(LanguageManager.getString("wiz_sf.summary")
95  				+ " : "));
96  		subjectTxt = new JTextField();
97  		summaryPanel.add(subjectTxt);
98  		summaryPanel.setMaximumSize(new Dimension(2000, 22));
99  		gameParamPanel.add(summaryPanel);
100 
101 		StringBuilder buffer = new StringBuilder();
102 		messageTxt = new JTextArea(
103 				buffer
104 						.append("Version : ")
105 						.append(IdConst.VERSION)
106 						.append("\nJRE : ")
107 						.append(System.getProperty("java.runtime.version"))
108 						.append("\nJVMm : ")
109 						.append(System.getProperty("java.vm.version"))
110 						.append("\nOS : ")
111 						.append(System.getProperty("os.name"))
112 						.append("\nScreen : ")
113 						.append(Toolkit.getDefaultToolkit().getScreenSize().width)
114 						.append("x")
115 						.append(Toolkit.getDefaultToolkit().getScreenSize().height)
116 						.append(
117 								"\n\nConsole output :\n\t[paste here console output(s)]\n\nContext :\n\t[describe when the error appears]")
118 						.toString());
119 		final JScrollPane scroll = new JScrollPane(
120 				ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
121 				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
122 		scroll.setAutoscrolls(true);
123 		scroll.setViewportView(messageTxt);
124 		gameParamPanel.add(scroll);
125 
126 		includeScreenShot = new JCheckBox(LanguageManager
127 				.getString("wiz_bugreport.includeshot"), true);
128 		JPanel panelTmp = new JPanel();
129 		panelTmp.setLayout(new BoxLayout(panelTmp, BoxLayout.X_AXIS));
130 		panelTmp.add(includeScreenShot);
131 		panelTmp.add(new JLabel(UIHelper.getIcon("snapshot.gif")));
132 		gameParamPanel.add(panelTmp);
133 
134 		includeCardProperties = new JCheckBox(LanguageManager
135 				.getString("wiz_bugreport.includeproperties"));
136 		includeCardProperties.setEnabled(false);
137 		includeCardProperties.setToolTipText(LanguageManager.getString(
138 				"notyetimplemented", "dump card properties"));
139 		panelTmp = new JPanel();
140 		panelTmp.setLayout(new BoxLayout(panelTmp, BoxLayout.X_AXIS));
141 		panelTmp.add(includeCardProperties);
142 		panelTmp.add(new JLabel(UIHelper.getIcon("cardprops.gif")));
143 		gameParamPanel.add(panelTmp);
144 
145 		getRootPane().setDefaultButton(null);
146 		okBtn.setIconTextGap(5);
147 
148 		// create screenshot picture
149 		try {
150 			// create screenshot resized to 800x600
151 			Thread.sleep(1000);
152 			screenShot = (ToolkitImage) new Robot().createScreenCapture(
153 					new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()))
154 					.getScaledInstance(800, 600, Image.SCALE_FAST);
155 		} catch (Exception e) {
156 			e.printStackTrace();
157 			includeScreenShot.setEnabled(false);
158 		}
159 	}
160 
161 	@Override
162 	public void actionPerformed(ActionEvent event) {
163 		final Object source = event.getSource();
164 		if (source == okBtn) {
165 			List<String> fileList = new ArrayList<String>();
166 			try {
167 				Configuration.setProperty("sourceforgeName", userNameTxt.getText());
168 				if (includeScreenShot.isSelected()) {
169 					// create screenshot and save captured image to PNG file
170 					final File tmpPngFile = File.createTempFile("_shot_", ".png",
171 							new File("").getAbsoluteFile());
172 					fileList.add(tmpPngFile.getAbsolutePath());
173 					screenShot.preload(null);
174 					screenShot.check(null);
175 					ImageIO.write(screenShot.getBufferedImage(), "png", tmpPngFile);
176 				}
177 				if (includeCardProperties.isSelected()) {
178 					// Include the properties of ALL cards of ALL zones
179 					final File tmpTxtFile = File.createTempFile("_properties_", ".txt",
180 							new File(MToolKit.getRootDir()));
181 					fileList.add(tmpTxtFile.getAbsolutePath());
182 					final PrintWriter outStream = new PrintWriter(new FileOutputStream(
183 							tmpTxtFile));
184 					printProperties(outStream, ZoneManager.stack);
185 					printProperties(outStream, ZoneManager.side);
186 					for (int i = StackManager.PLAYERS.length; i-- > 0;) {
187 						final Player player = StackManager.PLAYERS[i];
188 						for (int idZone = IdZones.SIDE; idZone-- > 0;) {
189 							printProperties(outStream, player.zoneManager
190 									.getContainer(idZone));
191 						}
192 					}
193 				}
194 
195 				MailUtils.sendEmail("", "bug_report_form@" + IdConst.PROJECT_NAME
196 						+ ".net", new String[] { "bug_report_jfiremox@yahoo.fr" },
197 						"Username:" + userNameTxt.getText() + "\nSummary:"
198 								+ subjectTxt.getText() + "\n\n" + messageTxt.getText(), "["
199 								+ IdConst.PROJECT_NAME + "] " + subjectTxt.getText(), fileList,
200 						null, "bug_report", "mx4.mail.yahoo.com");
201 				// "smtp.mail.yahoo.fr");
202 
203 				// Clean temp files
204 				for (int i = fileList.size(); i-- > 0;) {
205 					MToolKit.getFile(fileList.get(i)).delete();
206 				}
207 				dispose();
208 			} catch (Exception e) {
209 				e.printStackTrace();
210 				setVisible(true);
211 			}
212 		} else {
213 			super.actionPerformed(event);
214 			setVisible(false);
215 			dispose();
216 		}
217 	}
218 
219 	/***
220 	 * Print the properties of ALL cards of the specified zone.
221 	 * 
222 	 * @param out
223 	 *          destination output stream.
224 	 * @param zone
225 	 *          the zone to dump
226 	 */
227 	private static void printProperties(PrintWriter out, MZone zone) {
228 		out.println("************* ZONE : " + zone.getZoneName() + "*************");
229 		out.println("Nb = " + zone.getCardCount());
230 		for (int i = zone.getComponentCount(); i-- > 0;) {
231 			Component comp = zone.getComponent(i);
232 			printProperties(out, (MCard) comp);
233 		}
234 	}
235 
236 	/***
237 	 * Print the properties of ALL cards of the specified zone.
238 	 * 
239 	 * @param out
240 	 *          destination output stream.
241 	 * @param card
242 	 *          the card to dump
243 	 */
244 	private static void printProperties(PrintWriter out, MCard card) {
245 		out.println("Card = " + card.getName());
246 		out.print("\tregisters = [");
247 		for (int i = 0; i < card.registers.length; i++) {
248 			out.print("" + card.getValue(i));
249 			if (i != card.registers.length - 1) {
250 				out.print(",");
251 			}
252 		}
253 		out.println("]");
254 		out.print("\tcolor = " + card.getIdColor());
255 		out.print("\tidCard = " + card.getIdCard());
256 	}
257 
258 	/***
259 	 * Textfield containing sourceforge username
260 	 */
261 	private JTextField userNameTxt;
262 
263 	/***
264 	 * Textfield containing message
265 	 */
266 	private JTextArea messageTxt;
267 
268 	/***
269 	 * Textfield containing the summary of this report
270 	 */
271 	private JTextField subjectTxt;
272 
273 	/***
274 	 * Indicates if screenshot will be sent
275 	 */
276 	private JCheckBox includeScreenShot;
277 
278 	/***
279 	 * Indicates if properties of all cards will be sent
280 	 */
281 	private JCheckBox includeCardProperties;
282 
283 	private ToolkitImage screenShot;
284 }