DBConfigGUI.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.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 com.l2jserver.dbinstaller.RunTasks;
  29. import com.l2jserver.dbinstaller.util.mysql.MySqlConnect;
  30. import com.l2jserver.dbinstaller.util.swing.SpringUtilities;
  31. import com.l2jserver.images.ImagesTable;
  32. /**
  33. * @author mrTJO
  34. */
  35. public class DBConfigGUI extends JFrame
  36. {
  37. private static final long serialVersionUID = -8391792251140797076L;
  38. JTextField _dbHost;
  39. JTextField _dbPort;
  40. JTextField _dbUser;
  41. JPasswordField _dbPass;
  42. JTextField _dbDbse;
  43. String _db;
  44. String _dir;
  45. String _cleanUp;
  46. Preferences _prop;
  47. public DBConfigGUI(String db, String dir, String cleanUp)
  48. {
  49. super("L2J Database Installer");
  50. setLayout(new SpringLayout());
  51. setDefaultLookAndFeelDecorated(true);
  52. setIconImage(ImagesTable.getImage("l2j.png").getImage());
  53. _db = db;
  54. _dir = dir;
  55. _cleanUp = cleanUp;
  56. int width = 260;
  57. int height = 220;
  58. Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
  59. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60. setBounds((resolution.width - width) / 2, (resolution.height - height) / 2, width, height);
  61. setResizable(false);
  62. _prop = Preferences.userRoot();
  63. // Host
  64. JLabel labelDbHost = new JLabel("Host: ", JLabel.LEFT);
  65. add(labelDbHost);
  66. _dbHost = new JTextField(15);
  67. _dbHost.setText(_prop.get("dbHost_" + db, "localhost"));
  68. labelDbHost.setLabelFor(_dbHost);
  69. add(_dbHost);
  70. // Port
  71. JLabel labelDbPort = new JLabel("Port: ", JLabel.LEFT);
  72. add(labelDbPort);
  73. _dbPort = new JTextField(15);
  74. _dbPort.setText(_prop.get("dbPort_" + db, "3306"));
  75. labelDbPort.setLabelFor(_dbPort);
  76. add(_dbPort);
  77. // Username
  78. JLabel labelDbUser = new JLabel("Username: ", JLabel.LEFT);
  79. add(labelDbUser);
  80. _dbUser = new JTextField(15);
  81. _dbUser.setText(_prop.get("dbUser_" + db, "root"));
  82. labelDbUser.setLabelFor(_dbUser);
  83. add(_dbUser);
  84. // Password
  85. JLabel labelDbPass = new JLabel("Password: ", JLabel.LEFT);
  86. add(labelDbPass);
  87. _dbPass = new JPasswordField(15);
  88. _dbPass.setText(_prop.get("dbPass_" + db, ""));
  89. labelDbPass.setLabelFor(_dbPass);
  90. add(_dbPass);
  91. // Database
  92. JLabel labelDbDbse = new JLabel("Database: ", JLabel.LEFT);
  93. add(labelDbDbse);
  94. _dbDbse = new JTextField(15);
  95. _dbDbse.setText(_prop.get("dbDbse_" + db, db));
  96. labelDbDbse.setLabelFor(_dbDbse);
  97. add(_dbDbse);
  98. ActionListener cancelListener = new ActionListener()
  99. {
  100. @Override
  101. public void actionPerformed(ActionEvent e)
  102. {
  103. System.exit(0);
  104. }
  105. };
  106. // Cancel
  107. JButton btnCancel = new JButton("Cancel");
  108. btnCancel.addActionListener(cancelListener);
  109. add(btnCancel);
  110. ActionListener connectListener = new ActionListener()
  111. {
  112. @Override
  113. public void actionPerformed(ActionEvent e)
  114. {
  115. MySqlConnect connector = new MySqlConnect(_dbHost.getText(), _dbPort.getText(), _dbUser.getText(), new String(_dbPass.getPassword()), _dbDbse.getText(), false);
  116. if (connector.getConnection() != null)
  117. {
  118. _prop.put("dbHost_" + _db, _dbHost.getText());
  119. _prop.put("dbPort_" + _db, _dbPort.getText());
  120. _prop.put("dbUser_" + _db, _dbUser.getText());
  121. _prop.put("dbDbse_" + _db, _dbDbse.getText());
  122. boolean cleanInstall = false;
  123. DBInstallerGUI dbi = new DBInstallerGUI(connector.getConnection());
  124. setVisible(false);
  125. Object[] options =
  126. {
  127. "Full Install", "Upgrade", "Exit"
  128. };
  129. int n = JOptionPane.showOptionDialog(null, "Select Installation Type", "Installation Type", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
  130. if (n == 2 || n == -1)
  131. System.exit(0);
  132. if (n == 0)
  133. {
  134. int conf = JOptionPane.showConfirmDialog(null, "Do you really want to destroy your db?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
  135. if (conf == 1)
  136. System.exit(0);
  137. cleanInstall = true;
  138. }
  139. dbi.setVisible(true);
  140. RunTasks task = new RunTasks(dbi, _db, _dir, _cleanUp, cleanInstall);
  141. task.setPriority(Thread.MAX_PRIORITY);
  142. task.start();
  143. }
  144. }
  145. };
  146. // Connect
  147. JButton btnConnect = new JButton("Connect");
  148. btnConnect.addActionListener(connectListener);
  149. add(btnConnect);
  150. SpringUtilities.makeCompactGrid(getContentPane(), 6, 2, 5, 5, 5, 5);
  151. setVisible(true);
  152. }
  153. }