Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FeedbackDialog.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.ServerManager;
  26. import com.dmdirc.ui.core.util.Info;
  27. import com.dmdirc.addons.ui_swing.SwingController;
  28. import com.dmdirc.addons.ui_swing.UIUtilities;
  29. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  30. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  31. import com.dmdirc.config.IdentityManager;
  32. import com.dmdirc.util.Downloader;
  33. import java.awt.Insets;
  34. import java.awt.Window;
  35. import java.awt.event.ActionEvent;
  36. import java.awt.event.ActionListener;
  37. import java.io.IOException;
  38. import java.net.MalformedURLException;
  39. import java.util.HashMap;
  40. import java.util.List;
  41. import java.util.Map;
  42. import javax.swing.BorderFactory;
  43. import javax.swing.JButton;
  44. import javax.swing.JCheckBox;
  45. import javax.swing.JLabel;
  46. import javax.swing.JScrollPane;
  47. import javax.swing.JTextArea;
  48. import javax.swing.JTextField;
  49. import javax.swing.event.DocumentEvent;
  50. import javax.swing.event.DocumentListener;
  51. import net.miginfocom.swing.MigLayout;
  52. /** Feedback form. */
  53. public class FeedbackDialog extends StandardDialog implements ActionListener,
  54. DocumentListener {
  55. /**
  56. * A version number for this class. It should be changed whenever the class
  57. * structure is changed (or anything else that would prevent serialized
  58. * objects being unserialized with the new class).
  59. */
  60. private static final long serialVersionUID = 1;
  61. /** A previously created instance of FeedbackDialog. */
  62. private static volatile FeedbackDialog me;
  63. /** Information label. */
  64. private TextLabel info;
  65. /** Name field. */
  66. private JTextField name;
  67. /** Email field. */
  68. private JTextField email;
  69. /** Feedback area. */
  70. private JTextArea feedback;
  71. /** Server info checkbox. */
  72. private JCheckBox serverCheckbox;
  73. /** DMDirc info checkbox. */
  74. private JCheckBox DMDircCheckbox;
  75. /** Sent. */
  76. private boolean sentReport = false;
  77. /**
  78. * Instantiates the feedback dialog.
  79. *
  80. * @param parentWindow Parent window
  81. */
  82. private FeedbackDialog(final Window parentWindow) {
  83. super(parentWindow, ModalityType.MODELESS);
  84. initComponents();
  85. layoutComponents();
  86. addListeners();
  87. setTitle("DMDirc: Feedback");
  88. setResizable(false);
  89. }
  90. /**
  91. * Creates the new feedback dialog if one doesn't exist, and displays it.
  92. *
  93. * @param parentWindow Parent window
  94. */
  95. public static void showFeedbackDialog(final Window parentWindow) {
  96. me = getFeedbackDialog(parentWindow);
  97. me.display();
  98. me.requestFocusInWindow();
  99. }
  100. /**
  101. * Returns the current instance of the FeedbackDialog.
  102. *
  103. * @param parentWindow Parent window
  104. *
  105. * @return The current FeedbackDialog instance
  106. */
  107. public static FeedbackDialog getFeedbackDialog(final Window parentWindow) {
  108. synchronized (FeedbackDialog.class) {
  109. if (me == null) {
  110. me = new FeedbackDialog(parentWindow);
  111. me.serverCheckbox.setEnabled(ServerManager.getServerManager().
  112. numServers() > 0);
  113. } else if (!me.isVisible()) {
  114. me = new FeedbackDialog(parentWindow);
  115. me.serverCheckbox.setEnabled(ServerManager.getServerManager().
  116. numServers() > 0);
  117. }
  118. }
  119. return me;
  120. }
  121. /** Initialises the components. */
  122. private void initComponents() {
  123. orderButtons(new JButton(), new JButton());
  124. getOkButton().setText("Send");
  125. getOkButton().setActionCommand("Send");
  126. getOkButton().setEnabled(false);
  127. getCancelButton().setActionCommand("Close");
  128. info = new TextLabel("Thank you for using DMDirc. If you have any " +
  129. "feedback about the client, such as bug reports or feature " +
  130. "requests, please send it to us using the form below. " +
  131. "The name and e-mail address fields are optional if you " +
  132. "don't want us to contact you about your feedback.\n\n" +
  133. "Please note that this is for feedback such as bug reports " +
  134. "and suggestions, not for technical support. For " +
  135. "technical support, please join #DMDirc using the button " +
  136. "in the help menu.");
  137. name = new JTextField();
  138. email = new JTextField();
  139. feedback = new JTextArea();
  140. serverCheckbox =
  141. new JCheckBox("Include information about connected servers.");
  142. DMDircCheckbox = new JCheckBox("Include information about DMDirc.");
  143. UIUtilities.addUndoManager(name);
  144. UIUtilities.addUndoManager(email);
  145. UIUtilities.addUndoManager(feedback);
  146. }
  147. /** Lays out the components. */
  148. private void layoutComponents() {
  149. serverCheckbox.setMargin(new Insets(0, 0, 0, 0));
  150. serverCheckbox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  151. DMDircCheckbox.setMargin(new Insets(0, 0, 0, 0));
  152. DMDircCheckbox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  153. setLayout(new MigLayout("fill, wmin 600, wmax 600, hmin 400, hmax 400"));
  154. add(info, "span, growx, wrap, gapbottom unrel");
  155. add(new JLabel("Name: "), "aligny top, shrink");
  156. add(name, "growx, pushx, wrap");
  157. add(new JLabel("Email: "), "aligny top, shrink");
  158. add(email, "growx, pushx, wrap");
  159. add(new JLabel("Feedback: "), "aligny top, shrink");
  160. add(new JScrollPane(feedback), "grow, push, wrap");
  161. add(serverCheckbox, "skip 1, growx, wrap");
  162. add(DMDircCheckbox, "skip 1, growx, wrap");
  163. add(getCancelButton(), "skip, split 2, right, sg button");
  164. add(getOkButton(), "right, sg button");
  165. }
  166. /**
  167. * Lays out the components.
  168. *
  169. * @param error Did the submission error?
  170. */
  171. protected void layoutComponents2(final StringBuilder error) {
  172. getContentPane().setVisible(false);
  173. getContentPane().removeAll();
  174. getOkButton().setEnabled(true);
  175. getOkButton().setText("Close");
  176. getOkButton().setActionCommand("Close");
  177. setLayout(new MigLayout("fill, wmin 600, wmax 600, hmin 400, hmax 400"));
  178. info.setText(error.toString());
  179. add(info, "span 3, grow, push, wrap");
  180. add(getOkButton(), "skip, right, tag ok, sg button");
  181. getContentPane().setVisible(true);
  182. }
  183. /** Adds listeners to the components. */
  184. private void addListeners() {
  185. getOkButton().addActionListener(this);
  186. getCancelButton().addActionListener(this);
  187. feedback.getDocument().addDocumentListener(this);
  188. }
  189. /** Checks and sends the feedback. */
  190. private void send() {
  191. sentReport = true;
  192. getOkButton().setEnabled(false);
  193. getCancelButton().setEnabled(false);
  194. final StringBuilder serverInfo = new StringBuilder();
  195. final StringBuilder dmdircInfo = new StringBuilder();
  196. if (serverCheckbox.isSelected()) {
  197. for (Server server : ServerManager.getServerManager().getServers()) {
  198. serverInfo.append("Server name: ").append(server.getName()).
  199. append("\n");
  200. serverInfo.append("Actual name: ").
  201. append(server.getParser().getServerName()).append("\n");
  202. serverInfo.append("Network: ").append(server.getNetwork()).
  203. append("\n");
  204. serverInfo.append("IRCd: ").append(server.getParser().getServerSoftware()).
  205. append(" - ");
  206. serverInfo.append(server.getParser().getServerSoftwareType()).append("\n");
  207. serverInfo.append("Modes: ").
  208. append(server.getParser().getBooleanChannelModes()).
  209. append(" ");
  210. serverInfo.append(server.getParser().getListChannelModes()).append(" ");
  211. serverInfo.append(server.getParser().getParameterChannelModes()).
  212. append(" ");
  213. serverInfo.append(server.getParser().getDoubleParameterChannelModes());
  214. }
  215. }
  216. if (DMDircCheckbox.isSelected()) {
  217. dmdircInfo.append("DMDirc version: " + Info.getDMDircVersion()).
  218. append("\n");
  219. dmdircInfo.append("Profile directory: " + Main.getConfigDir()).
  220. append("\n");
  221. dmdircInfo.append("Java version: " + Info.getJavaVersion()).append("\n");
  222. dmdircInfo.append("OS Version: " + Info.getOSVersion()).append("\n");
  223. dmdircInfo.append("Look & Feel: " + SwingController.getLookAndFeel());
  224. }
  225. new SendWorker(me, name.getText().trim(),
  226. email.getText().trim(), feedback.getText().trim(), serverInfo.
  227. toString().trim(), dmdircInfo.toString().trim()).execute();
  228. }
  229. /** Validates the input. */
  230. private void validateInput() {
  231. if (feedback.getDocument().getLength() > 0) {
  232. getOkButton().setEnabled(true);
  233. } else {
  234. getOkButton().setEnabled(false);
  235. }
  236. }
  237. /**
  238. * {@inheritDoc}
  239. *
  240. * @param e action event
  241. */
  242. @Override
  243. public void actionPerformed(final ActionEvent e) {
  244. if (e.getActionCommand().equals("Send")) {
  245. if (!sentReport) {
  246. send();
  247. }
  248. } else if (e.getActionCommand().equals("Close")) {
  249. dispose();
  250. }
  251. }
  252. /** {@inheritDoc} */
  253. @Override
  254. public void insertUpdate(DocumentEvent e) {
  255. validateInput();
  256. }
  257. /** {@inheritDoc} */
  258. @Override
  259. public void removeUpdate(DocumentEvent e) {
  260. validateInput();
  261. }
  262. /** {@inheritDoc} */
  263. @Override
  264. public void changedUpdate(DocumentEvent e) {
  265. //Ignore
  266. }
  267. /** {@inheritDoc} */
  268. @Override
  269. public void dispose() {
  270. if (me == null) {
  271. return;
  272. }
  273. synchronized (me) {
  274. super.dispose();
  275. me = null;
  276. }
  277. }
  278. }
  279. /**
  280. * Sends feedback worker thread.
  281. */
  282. class SendWorker extends LoggingSwingWorker {
  283. /** Parent feedback dialog. */
  284. private FeedbackDialog dialog;
  285. /** Name. */
  286. private String name;
  287. /** Email. */
  288. private String email;
  289. /** Feedback. */
  290. private String feedback;
  291. /** Server name. */
  292. private String serverInfo;
  293. /** DMDirc Info. */
  294. private String dmdircInfo;
  295. /** Error/Success message. */
  296. private StringBuilder error;
  297. /**
  298. * Creates a new send worker to send feedback.
  299. *
  300. * @param dialog Parent feedback dialog
  301. * @param name Name
  302. * @param email Email
  303. * @param feedback Feedback
  304. */
  305. public SendWorker(FeedbackDialog dialog, String name, String email,
  306. String feedback) {
  307. this(dialog, name, email, feedback, "", "");
  308. }
  309. /**
  310. * Creates a new send worker to send feedback.
  311. *
  312. * @param dialog Parent feedback dialog
  313. * @param name Name
  314. * @param email Email
  315. * @param feedback Feedback
  316. * @param serverInfo serverInfo
  317. * @param dmdircInfo DMDirc info
  318. */
  319. public SendWorker(final FeedbackDialog dialog, final String name,
  320. final String email, final String feedback,
  321. final String serverInfo, final String dmdircInfo) {
  322. this.dialog = dialog;
  323. this.name = name;
  324. this.email = email;
  325. this.feedback = feedback;
  326. this.serverInfo = serverInfo;
  327. this.dmdircInfo = dmdircInfo;
  328. error = new StringBuilder();
  329. }
  330. /**
  331. * {@inheritDoc}
  332. *
  333. * @throws java.lang.Exception If unable to return a result
  334. */
  335. @Override
  336. protected Object doInBackground() throws Exception {
  337. final Map<String, String> postData =
  338. new HashMap<String, String>();
  339. if (!name.isEmpty()) {
  340. postData.put("name", name);
  341. }
  342. if (!email.isEmpty()) {
  343. postData.put("email", email);
  344. }
  345. if (!feedback.isEmpty()) {
  346. postData.put("feedback", feedback);
  347. }
  348. postData.put("version", IdentityManager.getGlobalConfig().getOption("version", "version"));
  349. if (!serverInfo.isEmpty()) {
  350. postData.put("serverInfo", serverInfo);
  351. }
  352. if (!dmdircInfo.isEmpty()) {
  353. postData.put("dmdircInfo", dmdircInfo);
  354. }
  355. try {
  356. final List<String> response =
  357. Downloader.getPage("http://www.dmdirc.com/feedback.php",
  358. postData);
  359. if (response.size() >= 1) {
  360. for (String responseLine : response) {
  361. error.append(responseLine).append("\n");
  362. }
  363. } else {
  364. error.append("Failure: Unknown response from the server.");
  365. }
  366. } catch (MalformedURLException ex) {
  367. error.append("Malformed feedback URL.");
  368. } catch (IOException ex) {
  369. error.append("Failure: " + ex.getMessage());
  370. }
  371. return error;
  372. }
  373. /** {@inheritDoc} */
  374. @Override
  375. protected void done() {
  376. super.done();
  377. dialog.layoutComponents2(error);
  378. }
  379. }