DBConfigGUI.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2004-2018 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.tools.dbinstaller.frontend.swing;
  20. import java.awt.Dimension;
  21. import java.awt.Toolkit;
  22. import java.awt.event.ActionListener;
  23. import java.sql.Connection;
  24. import java.text.MessageFormat;
  25. import java.util.prefs.Preferences;
  26. import javax.swing.JButton;
  27. import javax.swing.JLabel;
  28. import javax.swing.JOptionPane;
  29. import javax.swing.JPasswordField;
  30. import javax.swing.JTextField;
  31. import javax.swing.SpringLayout;
  32. import javax.swing.SwingConstants;
  33. import javax.swing.WindowConstants;
  34. import com.l2jserver.tools.dbinstaller.RunTasks;
  35. import com.l2jserver.tools.images.ImagesTable;
  36. import com.l2jserver.tools.util.SQLUtil;
  37. import com.l2jserver.tools.util.SwingUtil;
  38. import com.l2jserver.tools.util.swing.SpringUtilities;
  39. /**
  40. * @author mrTJO, HorridoJoho
  41. */
  42. public class DBConfigGUI extends AbstractGUI
  43. {
  44. private static final long serialVersionUID = -8391792251140797076L;
  45. JTextField _dbHost;
  46. JTextField _dbPort;
  47. JTextField _dbUser;
  48. JPasswordField _dbPass;
  49. JTextField _dbDbse;
  50. String _db;
  51. String _dir;
  52. String _cleanUp;
  53. Preferences _prop;
  54. public DBConfigGUI(String db, String dir, String cleanUp)
  55. {
  56. super("L2J Database Installer");
  57. setLayout(new SpringLayout());
  58. setDefaultLookAndFeelDecorated(true);
  59. setIconImage(ImagesTable.getImage("l2j.png").getImage());
  60. _db = db;
  61. _dir = dir;
  62. _cleanUp = cleanUp;
  63. int width = 320;
  64. int height = 240;
  65. Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
  66. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  67. setBounds((resolution.width - width) / 2, (resolution.height - height) / 2, width, height);
  68. setResizable(false);
  69. _prop = Preferences.userRoot();
  70. // Host
  71. JLabel labelDbHost = new JLabel("Host: ", SwingConstants.LEFT);
  72. add(labelDbHost);
  73. _dbHost = new JTextField(15);
  74. _dbHost.setText(_prop.get("dbHost_" + db, "localhost"));
  75. labelDbHost.setLabelFor(_dbHost);
  76. add(_dbHost);
  77. // Port
  78. JLabel labelDbPort = new JLabel("Port: ", SwingConstants.LEFT);
  79. add(labelDbPort);
  80. _dbPort = new JTextField(15);
  81. _dbPort.setText(_prop.get("dbPort_" + db, "3306"));
  82. labelDbPort.setLabelFor(_dbPort);
  83. add(_dbPort);
  84. // Username
  85. JLabel labelDbUser = new JLabel("Username: ", SwingConstants.LEFT);
  86. add(labelDbUser);
  87. _dbUser = new JTextField(15);
  88. _dbUser.setText(_prop.get("dbUser_" + db, "root"));
  89. labelDbUser.setLabelFor(_dbUser);
  90. add(_dbUser);
  91. // Password
  92. JLabel labelDbPass = new JLabel("Password: ", SwingConstants.LEFT);
  93. add(labelDbPass);
  94. _dbPass = new JPasswordField(15);
  95. _dbPass.setText(_prop.get("dbPass_" + db, ""));
  96. labelDbPass.setLabelFor(_dbPass);
  97. add(_dbPass);
  98. // Database
  99. JLabel labelDbDbse = new JLabel("Database: ", SwingConstants.LEFT);
  100. add(labelDbDbse);
  101. _dbDbse = new JTextField(15);
  102. _dbDbse.setText(_prop.get("dbDbse_" + db, db));
  103. labelDbDbse.setLabelFor(_dbDbse);
  104. add(_dbDbse);
  105. // Quit
  106. JButton btnCancel = new JButton("Quit");
  107. btnCancel.addActionListener(e -> close());
  108. add(btnCancel);
  109. ActionListener connectListener = e ->
  110. {
  111. Connection con = null;
  112. try
  113. {
  114. final JOptionPane optionPane = new JOptionPane(MessageFormat.format("Connecting to SQL server at {0}:{1} with user {2}...", _dbHost.getText(), _dbPort.getText(), _dbUser.getText()), JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[] {}, null);
  115. con = SwingUtil.runBackgroundTaskWithDialog(this, "Database Installer", optionPane, () -> SQLUtil.connect(_dbHost.getText(), _dbPort.getText(), _dbUser.getText(), new String(_dbPass.getPassword()), _dbDbse.getText()));
  116. }
  117. catch (Throwable t)
  118. {
  119. reportError(true, t, "Failed to establish connection with user {0} to SQL server at {1}:{2}!", _dbUser.getText(), _dbHost.getText(), _dbPort.getText());
  120. return;
  121. }
  122. try
  123. {
  124. final Connection con2 = con;
  125. final JOptionPane optionPane = new JOptionPane(MessageFormat.format("Ensuring that database {0} exists and is in use...", _dbDbse.getText()), JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[] {}, null);
  126. SwingUtil.runBackgroundTaskWithDialog(this, "Database Installer", optionPane, () ->
  127. {
  128. SQLUtil.ensureDatabaseUsage(con2, _dbDbse.getText());
  129. return null;
  130. });
  131. }
  132. catch (Throwable t)
  133. {
  134. SQLUtil.close(con);
  135. reportError(true, t, MessageFormat.format("Failed to ensure that the database {0} exists and is in use!", _dbDbse.getText()));
  136. return;
  137. }
  138. _prop.put("dbHost_" + _db, _dbHost.getText());
  139. _prop.put("dbPort_" + _db, _dbPort.getText());
  140. _prop.put("dbUser_" + _db, _dbUser.getText());
  141. _prop.put("dbDbse_" + _db, _dbDbse.getText());
  142. try
  143. {
  144. boolean cleanInstall = false;
  145. Object[] options =
  146. {
  147. "Full Install",
  148. "Upgrade",
  149. "Abbort"
  150. };
  151. int n = JOptionPane.showOptionDialog(null, "Select the installation type for your database:", "Database Installer", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
  152. if ((n == 2) || (n == -1))
  153. {
  154. SQLUtil.close(con);
  155. return;
  156. }
  157. if (n == 0)
  158. {
  159. if (!requestUserConfirm("A clean install will delete your current database! Do you want to continue?"))
  160. {
  161. SQLUtil.close(con);
  162. return;
  163. }
  164. cleanInstall = true;
  165. }
  166. DBInstallerGUI dbi = new DBInstallerGUI();
  167. RunTasks task = new RunTasks(dbi, con, _db, _dir, _cleanUp, cleanInstall);
  168. task.setPriority(Thread.MAX_PRIORITY);
  169. task.start();
  170. }
  171. catch (Throwable t)
  172. {
  173. SQLUtil.close(con);
  174. reportError(true, t, "Failed to launch database installation!");
  175. }
  176. };
  177. // Connect
  178. JButton btnConnect = new JButton("Connect");
  179. btnConnect.addActionListener(connectListener);
  180. add(btnConnect);
  181. SpringUtilities.makeCompactGrid(getContentPane(), 6, 2, 5, 5, 5, 5);
  182. setVisible(true);
  183. }
  184. }