DBConfigGUI.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.tools.dbinstaller.gui;
  16. import java.awt.Dimension;
  17. import java.awt.Toolkit;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.util.prefs.Preferences;
  21. import javax.swing.JButton;
  22. import javax.swing.JFrame;
  23. import javax.swing.JLabel;
  24. import javax.swing.JOptionPane;
  25. import javax.swing.JPasswordField;
  26. import javax.swing.JTextField;
  27. import javax.swing.SpringLayout;
  28. import javax.swing.SwingConstants;
  29. import com.l2jserver.tools.dbinstaller.RunTasks;
  30. import com.l2jserver.tools.dbinstaller.util.mysql.MySqlConnect;
  31. import com.l2jserver.tools.dbinstaller.util.swing.SpringUtilities;
  32. import com.l2jserver.tools.images.ImagesTable;
  33. /**
  34. * @author mrTJO
  35. */
  36. public class DBConfigGUI extends JFrame
  37. {
  38. private static final long serialVersionUID = -8391792251140797076L;
  39. JTextField _dbHost;
  40. JTextField _dbPort;
  41. JTextField _dbUser;
  42. JPasswordField _dbPass;
  43. JTextField _dbDbse;
  44. String _db;
  45. String _dir;
  46. String _cleanUp;
  47. Preferences _prop;
  48. public DBConfigGUI(String db, String dir, String cleanUp)
  49. {
  50. super("L2J Database Installer");
  51. setLayout(new SpringLayout());
  52. setDefaultLookAndFeelDecorated(true);
  53. setIconImage(ImagesTable.getImage("l2j.png").getImage());
  54. _db = db;
  55. _dir = dir;
  56. _cleanUp = cleanUp;
  57. int width = 260;
  58. int height = 220;
  59. Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
  60. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  61. setBounds((resolution.width - width) / 2, (resolution.height - height) / 2, width, height);
  62. setResizable(false);
  63. _prop = Preferences.userRoot();
  64. // Host
  65. JLabel labelDbHost = new JLabel("Host: ", SwingConstants.LEFT);
  66. add(labelDbHost);
  67. _dbHost = new JTextField(15);
  68. _dbHost.setText(_prop.get("dbHost_" + db, "localhost"));
  69. labelDbHost.setLabelFor(_dbHost);
  70. add(_dbHost);
  71. // Port
  72. JLabel labelDbPort = new JLabel("Port: ", SwingConstants.LEFT);
  73. add(labelDbPort);
  74. _dbPort = new JTextField(15);
  75. _dbPort.setText(_prop.get("dbPort_" + db, "3306"));
  76. labelDbPort.setLabelFor(_dbPort);
  77. add(_dbPort);
  78. // Username
  79. JLabel labelDbUser = new JLabel("Username: ", SwingConstants.LEFT);
  80. add(labelDbUser);
  81. _dbUser = new JTextField(15);
  82. _dbUser.setText(_prop.get("dbUser_" + db, "root"));
  83. labelDbUser.setLabelFor(_dbUser);
  84. add(_dbUser);
  85. // Password
  86. JLabel labelDbPass = new JLabel("Password: ", SwingConstants.LEFT);
  87. add(labelDbPass);
  88. _dbPass = new JPasswordField(15);
  89. _dbPass.setText(_prop.get("dbPass_" + db, ""));
  90. labelDbPass.setLabelFor(_dbPass);
  91. add(_dbPass);
  92. // Database
  93. JLabel labelDbDbse = new JLabel("Database: ", SwingConstants.LEFT);
  94. add(labelDbDbse);
  95. _dbDbse = new JTextField(15);
  96. _dbDbse.setText(_prop.get("dbDbse_" + db, db));
  97. labelDbDbse.setLabelFor(_dbDbse);
  98. add(_dbDbse);
  99. ActionListener cancelListener = new ActionListener()
  100. {
  101. @Override
  102. public void actionPerformed(ActionEvent e)
  103. {
  104. System.exit(0);
  105. }
  106. };
  107. // Cancel
  108. JButton btnCancel = new JButton("Cancel");
  109. btnCancel.addActionListener(cancelListener);
  110. add(btnCancel);
  111. ActionListener connectListener = new ActionListener()
  112. {
  113. @Override
  114. public void actionPerformed(ActionEvent e)
  115. {
  116. MySqlConnect connector = new MySqlConnect(_dbHost.getText(), _dbPort.getText(), _dbUser.getText(), new String(_dbPass.getPassword()), _dbDbse.getText(), false);
  117. if (connector.getConnection() != null)
  118. {
  119. _prop.put("dbHost_" + _db, _dbHost.getText());
  120. _prop.put("dbPort_" + _db, _dbPort.getText());
  121. _prop.put("dbUser_" + _db, _dbUser.getText());
  122. _prop.put("dbDbse_" + _db, _dbDbse.getText());
  123. boolean cleanInstall = false;
  124. DBInstallerGUI dbi = new DBInstallerGUI(connector.getConnection());
  125. setVisible(false);
  126. Object[] options =
  127. {
  128. "Full Install", "Upgrade", "Exit"
  129. };
  130. int n = JOptionPane.showOptionDialog(null, "Select Installation Type", "Installation Type", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
  131. if (n == 2 || n == -1)
  132. System.exit(0);
  133. if (n == 0)
  134. {
  135. int conf = JOptionPane.showConfirmDialog(null, "Do you really want to destroy your db?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
  136. if (conf == 1)
  137. System.exit(0);
  138. cleanInstall = true;
  139. }
  140. dbi.setVisible(true);
  141. RunTasks task = new RunTasks(dbi, _db, _dir, _cleanUp, cleanInstall);
  142. task.setPriority(Thread.MAX_PRIORITY);
  143. task.start();
  144. }
  145. }
  146. };
  147. // Connect
  148. JButton btnConnect = new JButton("Connect");
  149. btnConnect.addActionListener(connectListener);
  150. add(btnConnect);
  151. SpringUtilities.makeCompactGrid(getContentPane(), 6, 2, 5, 5, 5, 5);
  152. setVisible(true);
  153. }
  154. }