RegisterDialog.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.gsregistering;
  16. import java.awt.GridBagConstraints;
  17. import java.awt.GridBagLayout;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.Map;
  23. import java.util.ResourceBundle;
  24. import javax.swing.JButton;
  25. import javax.swing.JComboBox;
  26. import javax.swing.JDialog;
  27. import javax.swing.JFileChooser;
  28. import javax.swing.JLabel;
  29. import javax.swing.JTextPane;
  30. import javax.swing.filechooser.FileFilter;
  31. import com.l2jserver.loginserver.GameServerTable;
  32. /**
  33. *
  34. * @author KenM
  35. */
  36. @SuppressWarnings("serial")
  37. public class RegisterDialog extends JDialog implements ActionListener
  38. {
  39. private final ResourceBundle _bundle;
  40. private final JComboBox<ComboServer> _combo;
  41. private final GUserInterface _owner;
  42. public RegisterDialog(final GUserInterface owner)
  43. {
  44. super(owner.getFrame(), true);
  45. _owner = owner;
  46. this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  47. _bundle = owner.getBundle();
  48. this.setTitle(_bundle.getString("registerGS"));
  49. this.setResizable(false);
  50. this.setLayout(new GridBagLayout());
  51. GridBagConstraints cons = new GridBagConstraints();
  52. cons.weightx = 0.5;
  53. cons.weighty = 0.5;
  54. cons.gridx = 0;
  55. cons.gridy = 0;
  56. cons.fill = GridBagConstraints.BOTH;
  57. final JLabel label = new JLabel(_bundle.getString("serverName"));
  58. this.add(label, cons);
  59. _combo = new JComboBox<ComboServer>();
  60. _combo.setEditable(false);
  61. for (Map.Entry<Integer, String> entry : GameServerTable.getInstance().getServerNames().entrySet())
  62. {
  63. if (!GameServerTable.getInstance().hasRegisteredGameServerOnId(entry.getKey()))
  64. {
  65. _combo.addItem(new ComboServer(entry.getKey(), entry.getValue()));
  66. }
  67. }
  68. cons.gridx = 1;
  69. cons.gridy = 0;
  70. this.add(_combo, cons);
  71. cons.gridx = 0;
  72. cons.gridy = 1;
  73. cons.gridwidth = 2;
  74. JTextPane textPane = new JTextPane();
  75. textPane.setText(_bundle.getString("saveHexId"));
  76. textPane.setEditable(false);
  77. textPane.setBackground(label.getBackground());
  78. this.add(textPane, cons);
  79. cons.gridwidth = 1;
  80. JButton btnSave = new JButton(_bundle.getString("save"));
  81. btnSave.setActionCommand("save");
  82. btnSave.addActionListener(this);
  83. cons.gridx = 0;
  84. cons.gridy = 2;
  85. this.add(btnSave, cons);
  86. JButton btnCancel = new JButton(_bundle.getString("cancel"));
  87. btnCancel.setActionCommand("cancel");
  88. btnCancel.addActionListener(this);
  89. cons.gridx = 1;
  90. cons.gridy = 2;
  91. this.add(btnCancel, cons);
  92. final double leftSize = Math.max(label.getPreferredSize().getWidth(), btnSave.getPreferredSize().getWidth());
  93. final double rightSize = Math.max(_combo.getPreferredSize().getWidth(), btnCancel.getPreferredSize().getWidth());
  94. final double height = _combo.getPreferredSize().getHeight()+ 4 * textPane.getPreferredSize().getHeight()+btnSave.getPreferredSize().getHeight();
  95. this.setSize((int) (leftSize + rightSize + 30), (int) (height + 20));
  96. this.setLocationRelativeTo(owner.getFrame());
  97. }
  98. class ComboServer
  99. {
  100. private final int _id;
  101. private final String _name;
  102. public ComboServer(int id, String name)
  103. {
  104. _id = id;
  105. _name = name;
  106. }
  107. /**
  108. * @return Returns the id.
  109. */
  110. public int getId()
  111. {
  112. return _id;
  113. }
  114. /**
  115. * @return Returns the name.
  116. */
  117. public String getName()
  118. {
  119. return _name;
  120. }
  121. @Override
  122. public String toString()
  123. {
  124. return this.getName();
  125. }
  126. }
  127. /**
  128. * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  129. */
  130. @Override
  131. public void actionPerformed(ActionEvent e)
  132. {
  133. String cmd = e.getActionCommand();
  134. if (cmd.equals("save"))
  135. {
  136. ComboServer server = (ComboServer) _combo.getSelectedItem();
  137. int gsId = server.getId();
  138. JFileChooser fc = new JFileChooser();
  139. //fc.setS
  140. fc.setDialogTitle(_bundle.getString("hexidDest"));
  141. fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  142. fc.setFileFilter
  143. (
  144. new FileFilter()
  145. {
  146. @Override
  147. public boolean accept(File f)
  148. {
  149. return f.isDirectory();
  150. }
  151. @Override
  152. public String getDescription()
  153. {
  154. return null;
  155. }
  156. }
  157. );
  158. fc.showOpenDialog(this);
  159. try
  160. {
  161. GUserInterface.registerGameServer(gsId, fc.getSelectedFile().getAbsolutePath());
  162. _owner.refreshAsync();
  163. this.setVisible(false);
  164. }
  165. catch (IOException e1)
  166. {
  167. _owner.showError(_bundle.getString("ioErrorRegister"), e1);
  168. }
  169. }
  170. else if (cmd.equals("cancel"))
  171. {
  172. this.setVisible(false);
  173. }
  174. }
  175. }