DBConfigGUI.java 5.4 KB

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