View Javadoc

1   /*
2    *   Magic-Project 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.magicproject.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.magicproject.clickable.targetable.card.MCard;
46  import net.sf.magicproject.clickable.targetable.player.Player;
47  import net.sf.magicproject.mail.MailUtils;
48  import net.sf.magicproject.stack.StackManager;
49  import net.sf.magicproject.token.IdConst;
50  import net.sf.magicproject.token.IdZones;
51  import net.sf.magicproject.tools.Configuration;
52  import net.sf.magicproject.tools.MToolKit;
53  import net.sf.magicproject.ui.UIHelper;
54  import net.sf.magicproject.ui.component.JLink;
55  import net.sf.magicproject.ui.i18n.LanguageManager;
56  import net.sf.magicproject.zone.MZone;
57  import net.sf.magicproject.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=92519&atid=601040",
86  				UIHelper.getIcon("sourceforge_logo.png"));
87  		userPanel.add(sfLabel);
88  		userPanel.setMaximumSize(new Dimension(2000, 22));
89  		gameParamPanel.add(userPanel);
90  
91  		final JPanel summaryPanel = new JPanel();
92  		summaryPanel.setLayout(new BoxLayout(summaryPanel, BoxLayout.X_AXIS));
93  		summaryPanel.add(new JLabel(LanguageManager.getString("wiz_sf.summary")
94  				+ " : "));
95  		subjectTxt = new JTextField();
96  		summaryPanel.add(subjectTxt);
97  		summaryPanel.setMaximumSize(new Dimension(2000, 22));
98  		gameParamPanel.add(summaryPanel);
99  
100 		StringBuilder buffer = new StringBuilder();
101 		messageTxt = new JTextArea(
102 				buffer
103 						.append("Version : ")
104 						.append(IdConst.VERSION)
105 						.append("\nJRE : ")
106 						.append(System.getProperty("java.runtime.version"))
107 						.append("\nJVMm : ")
108 						.append(System.getProperty("java.vm.version"))
109 						.append("\nOS : ")
110 						.append(System.getProperty("os.name"))
111 						.append("\nScreen : ")
112 						.append(Toolkit.getDefaultToolkit().getScreenSize().width)
113 						.append("x")
114 						.append(Toolkit.getDefaultToolkit().getScreenSize().height)
115 						.append(
116 								"\n\nConsole output :\n\t[paste here console output(s)]\n\nContext :\n\t[describe when the error appears]")
117 						.toString());
118 		final JScrollPane scroll = new JScrollPane(
119 				ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
120 				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
121 		scroll.setAutoscrolls(true);
122 		scroll.setViewportView(messageTxt);
123 		gameParamPanel.add(scroll);
124 
125 		includeScreenShot = new JCheckBox(LanguageManager
126 				.getString("wiz_bugreport.includeshot"), true);
127 		JPanel panelTmp = new JPanel();
128 		panelTmp.setLayout(new BoxLayout(panelTmp, BoxLayout.X_AXIS));
129 		panelTmp.add(includeScreenShot);
130 		panelTmp.add(new JLabel(UIHelper.getIcon("snapshot.gif")));
131 		gameParamPanel.add(panelTmp);
132 
133 		includeCardProperties = new JCheckBox(LanguageManager
134 				.getString("wiz_bugreport.includeproperties"));
135 		includeCardProperties.setEnabled(false);
136 		includeCardProperties.setToolTipText(LanguageManager.getString(
137 				"notyetimplemented", "dump card properties"));
138 		panelTmp = new JPanel();
139 		panelTmp.setLayout(new BoxLayout(panelTmp, BoxLayout.X_AXIS));
140 		panelTmp.add(includeCardProperties);
141 		panelTmp.add(new JLabel(UIHelper.getIcon("cardprops.gif")));
142 		gameParamPanel.add(panelTmp);
143 
144 		getRootPane().setDefaultButton(null);
145 		okBtn.setIconTextGap(5);
146 
147 		// create screenshot picture
148 		try {
149 			// create screenshot resized to 800x600
150 			Thread.sleep(1000);
151 			screenShot = (ToolkitImage) new Robot().createScreenCapture(
152 					new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()))
153 					.getScaledInstance(800, 600, Image.SCALE_FAST);
154 		} catch (Exception e) {
155 			e.printStackTrace();
156 			includeScreenShot.setEnabled(false);
157 		}
158 	}
159 
160 	@Override
161 	public void actionPerformed(ActionEvent event) {
162 		final Object source = event.getSource();
163 		if (source == okBtn) {
164 			List<String> fileList = new ArrayList<String>();
165 			try {
166 				Configuration.setProperty("sourceforgeName", userNameTxt.getText());
167 				if (includeScreenShot.isSelected()) {
168 					// create screenshot and save captured image to PNG file
169 					final File tmpPngFile = File.createTempFile("_shot_", ".png",
170 							new File("").getAbsoluteFile());
171 					fileList.add(tmpPngFile.getAbsolutePath());
172 					screenShot.preload(null);
173 					screenShot.check(null);
174 					ImageIO.write(screenShot.getBufferedImage(), "png", tmpPngFile);
175 				}
176 				if (includeCardProperties.isSelected()) {
177 					// Include the properties of ALL cards of ALL zones
178 					final File tmpTxtFile = File.createTempFile("_properties_", ".txt",
179 							new File("").getAbsoluteFile());
180 					fileList.add(tmpTxtFile.getAbsolutePath());
181 					final PrintWriter outStream = new PrintWriter(new FileOutputStream(
182 							tmpTxtFile));
183 					printProperties(outStream, ZoneManager.stack);
184 					printProperties(outStream, ZoneManager.side);
185 					for (int i = StackManager.PLAYERS.length; i-- > 0;) {
186 						final Player player = StackManager.PLAYERS[i];
187 						for (int idZone = IdZones.SIDE; idZone-- > 0;) {
188 							printProperties(outStream, player.zoneManager
189 									.getContainer(idZone));
190 						}
191 					}
192 				}
193 
194 				MailUtils.sendEmail("", "bug_report_form@magic-project.net",
195 						new String[] { "bug_report_jmagicproject@yahoo.fr" }, "Username:"
196 								+ userNameTxt.getText() + "\nSummary:" + subjectTxt.getText()
197 								+ "\n\n" + messageTxt.getText(), null, "[magic-project] "
198 								+ subjectTxt.getText(), fileList, null, "bug_report",
199 						"mx4.mail.yahoo.com");
200 					// "smtp.mail.yahoo.fr");
201 
202 				// Clean temp files
203 				for (int i = fileList.size(); i-- > 0;) {
204 					MToolKit.getFile(fileList.get(i)).delete();
205 				}
206 				dispose();
207 			} catch (Exception e) {
208 				e.printStackTrace();
209 				setVisible(true);
210 			}
211 		} else {
212 			super.actionPerformed(event);
213 			setVisible(false);
214 			dispose();
215 		}
216 	}
217 
218 	/***
219 	 * Print the properties of ALL cards of the specified zone.
220 	 * 
221 	 * @param out
222 	 *          destination output stream.
223 	 * @param zone
224 	 *          the zone to dump
225 	 */
226 	private static void printProperties(PrintWriter out, MZone zone) {
227 		out.println("************* ZONE : " + zone.getZoneName() + "*************");
228 		out.println("Nb = " + zone.getCardCount());
229 		for (int i = zone.getComponentCount(); i-- > 0;) {
230 			Component comp = zone.getComponent(i);
231 			printProperties(out, (MCard) comp);
232 		}
233 	}
234 
235 	/***
236 	 * Print the properties of ALL cards of the specified zone.
237 	 * 
238 	 * @param out
239 	 *          destination output stream.
240 	 * @param card
241 	 *          the card to dump
242 	 */
243 	private static void printProperties(PrintWriter out, MCard card) {
244 		out.println("Card = " + card.getName());
245 		out.print("\tregisters = [");
246 		for (int i = 0; i < card.registers.length; i++) {
247 			out.print("" + card.getValue(i));
248 			if (i != card.registers.length - 1) {
249 				out.print(",");
250 			}
251 		}
252 		out.println("]");
253 		out.print("\tcolor = " + card.getIdColor());
254 		out.print("\tidCard = " + card.getIdCard());
255 	}
256 
257 	/***
258 	 * Textfield containing sourceforge username
259 	 */
260 	private JTextField userNameTxt;
261 
262 	/***
263 	 * Textfield containing message
264 	 */
265 	private JTextArea messageTxt;
266 
267 	/***
268 	 * Textfield containing the summary of this report
269 	 */
270 	private JTextField subjectTxt;
271 
272 	/***
273 	 * Indicates if screenshot will be sent
274 	 */
275 	private JCheckBox includeScreenShot;
276 
277 	/***
278 	 * Indicates if properties of all cards will be sent
279 	 */
280 	private JCheckBox includeCardProperties;
281 
282 	private ToolkitImage screenShot;
283 }