|
| 1 | +/* |
| 2 | + * This file is part of StatusMCServerTool. |
| 3 | + * |
| 4 | + * StatusMCServerTool is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * StatusMCServerTool is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with StatusMCServerTool. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package com.devparada.frame; |
| 18 | + |
| 19 | +import javax.swing.JOptionPane; |
| 20 | + |
| 21 | +/** |
| 22 | + * |
| 23 | + * @author devparada |
| 24 | + */ |
| 25 | +public class AddServerDialog extends javax.swing.JDialog { |
| 26 | + |
| 27 | + private final InitFrame jFrame; |
| 28 | + |
| 29 | + /** |
| 30 | + * Saves the host and ip of dialog |
| 31 | + */ |
| 32 | + private String HostIpDialog; |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates new form AddServerDialog |
| 36 | + * |
| 37 | + * @param parent |
| 38 | + * @param modal |
| 39 | + */ |
| 40 | + public AddServerDialog(java.awt.Frame parent, boolean modal) { |
| 41 | + super(parent, modal); |
| 42 | + initComponents(); |
| 43 | + jFrame = (InitFrame) parent; |
| 44 | + } |
| 45 | + |
| 46 | + public String getHostIpDialog() { |
| 47 | + return HostIpDialog; |
| 48 | + } |
| 49 | + |
| 50 | + public void setHostIpDialog(String HostIpDialog) { |
| 51 | + this.HostIpDialog = HostIpDialog; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * This method is called from within the constructor to initialize the form. |
| 56 | + * WARNING: Do NOT modify this code. The content of this method is always |
| 57 | + * regenerated by the Form Editor. |
| 58 | + */ |
| 59 | + @SuppressWarnings("unchecked") |
| 60 | + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
| 61 | + private void initComponents() { |
| 62 | + |
| 63 | + jLblAdd = new javax.swing.JTextField(); |
| 64 | + jTxtAdd = new javax.swing.JTextField(); |
| 65 | + jBtnAdd = new javax.swing.JButton(); |
| 66 | + |
| 67 | + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); |
| 68 | + setTitle("Add Server"); |
| 69 | + setLocation(new java.awt.Point(0, 0)); |
| 70 | + setResizable(false); |
| 71 | + |
| 72 | + jLblAdd.setEditable(false); |
| 73 | + jLblAdd.setBackground(new java.awt.Color(214, 217, 223)); |
| 74 | + jLblAdd.setForeground(new java.awt.Color(0, 0, 0)); |
| 75 | + jLblAdd.setHorizontalAlignment(javax.swing.JTextField.CENTER); |
| 76 | + jLblAdd.setText("Intraduce the server IP (with port if different)"); |
| 77 | + jLblAdd.setBorder(null); |
| 78 | + |
| 79 | + jTxtAdd.setHorizontalAlignment(javax.swing.JTextField.CENTER); |
| 80 | + jTxtAdd.setText("myserverminecraft.ddns.net:24321"); |
| 81 | + |
| 82 | + jBtnAdd.setBackground(new java.awt.Color(0, 204, 51)); |
| 83 | + jBtnAdd.setText("Add server"); |
| 84 | + jBtnAdd.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); |
| 85 | + jBtnAdd.addActionListener(new java.awt.event.ActionListener() { |
| 86 | + public void actionPerformed(java.awt.event.ActionEvent evt) { |
| 87 | + jBtnAddActionPerformed(evt); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); |
| 92 | + getContentPane().setLayout(layout); |
| 93 | + layout.setHorizontalGroup( |
| 94 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 95 | + .addGroup(layout.createSequentialGroup() |
| 96 | + .addContainerGap(54, Short.MAX_VALUE) |
| 97 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 98 | + .addComponent(jTxtAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE) |
| 99 | + .addComponent(jLblAdd)) |
| 100 | + .addContainerGap(54, Short.MAX_VALUE)) |
| 101 | + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
| 102 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
| 103 | + .addComponent(jBtnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE) |
| 104 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
| 105 | + ); |
| 106 | + layout.setVerticalGroup( |
| 107 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 108 | + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
| 109 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
| 110 | + .addComponent(jLblAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE) |
| 111 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
| 112 | + .addComponent(jTxtAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) |
| 113 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE) |
| 114 | + .addComponent(jBtnAdd) |
| 115 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
| 116 | + ); |
| 117 | + |
| 118 | + pack(); |
| 119 | + }// </editor-fold>//GEN-END:initComponents |
| 120 | + |
| 121 | + private void jBtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAddActionPerformed |
| 122 | + this.setHostIpDialog(jTxtAdd.getText()); |
| 123 | + |
| 124 | + if (checkAddDialog()) { |
| 125 | + String[] ipServerArray = getHostIpDialog().split(":"); |
| 126 | + int port = 25565; |
| 127 | + |
| 128 | + String serverIp = getHostIpDialog(); |
| 129 | + jTxtAdd.setText(""); |
| 130 | + |
| 131 | + if (ipServerArray.length == 2) { |
| 132 | + port = Integer.parseInt(ipServerArray[1]); |
| 133 | + } else { |
| 134 | + serverIp = getHostIpDialog() + ":" + port; |
| 135 | + } |
| 136 | + |
| 137 | + this.setVisible(false); |
| 138 | + jFrame.database.addRow(serverIp); |
| 139 | + jFrame.addPanel(ipServerArray[0], port); |
| 140 | + } else { |
| 141 | + this.setVisible(true); |
| 142 | + } |
| 143 | + }//GEN-LAST:event_jBtnAddActionPerformed |
| 144 | + |
| 145 | + /** |
| 146 | + * Check the JTextField the dialog of dialog add server of the database |
| 147 | + * |
| 148 | + * @return Return true if checks are correct |
| 149 | + */ |
| 150 | + private boolean checkAddDialog() { |
| 151 | + String JTxtText = getHostIpDialog(); |
| 152 | + |
| 153 | + if (JTxtText.length() != 0) { |
| 154 | + String[] ipServerArray = JTxtText.split(":"); |
| 155 | + |
| 156 | + // \\d+ -> only numbers |
| 157 | + if (ipServerArray.length == 2 && !ipServerArray[1].matches("\\d+")) { |
| 158 | + JOptionPane.showMessageDialog(this, "Port not is number", "Error Add Server", JOptionPane.WARNING_MESSAGE); |
| 159 | + } else if (ipServerArray.length == 2 && ipServerArray[1].matches("\\d+")) { |
| 160 | + return true; |
| 161 | + } else { |
| 162 | + return true; |
| 163 | + } |
| 164 | + } else { |
| 165 | + JOptionPane.showMessageDialog(this, "IP Server not blank", "Error Add Server", JOptionPane.WARNING_MESSAGE); |
| 166 | + } |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @param args the command line arguments |
| 172 | + */ |
| 173 | + public static void main(String args[]) { |
| 174 | + /* Set the Nimbus look and feel */ |
| 175 | + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> |
| 176 | + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. |
| 177 | + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html |
| 178 | + */ |
| 179 | + try { |
| 180 | + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { |
| 181 | + if ("Nimbus".equals(info.getName())) { |
| 182 | + javax.swing.UIManager.setLookAndFeel(info.getClassName()); |
| 183 | + break; |
| 184 | + } |
| 185 | + } |
| 186 | + } catch (ClassNotFoundException ex) { |
| 187 | + java.util.logging.Logger.getLogger(AddServerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 188 | + } catch (InstantiationException ex) { |
| 189 | + java.util.logging.Logger.getLogger(AddServerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 190 | + } catch (IllegalAccessException ex) { |
| 191 | + java.util.logging.Logger.getLogger(AddServerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 192 | + } catch (javax.swing.UnsupportedLookAndFeelException ex) { |
| 193 | + java.util.logging.Logger.getLogger(AddServerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 194 | + } |
| 195 | + //</editor-fold> |
| 196 | + |
| 197 | + /* Create and display the dialog */ |
| 198 | + java.awt.EventQueue.invokeLater(new Runnable() { |
| 199 | + @Override |
| 200 | + public void run() { |
| 201 | + AddServerDialog dialog = new AddServerDialog(new javax.swing.JFrame(), true); |
| 202 | + dialog.addWindowListener(new java.awt.event.WindowAdapter() { |
| 203 | + @Override |
| 204 | + public void windowClosing(java.awt.event.WindowEvent e) { |
| 205 | + System.exit(0); |
| 206 | + } |
| 207 | + }); |
| 208 | + dialog.setVisible(true); |
| 209 | + } |
| 210 | + }); |
| 211 | + } |
| 212 | + |
| 213 | + // Variables declaration - do not modify//GEN-BEGIN:variables |
| 214 | + private javax.swing.JButton jBtnAdd; |
| 215 | + private javax.swing.JTextField jLblAdd; |
| 216 | + private javax.swing.JTextField jTxtAdd; |
| 217 | + // End of variables declaration//GEN-END:variables |
| 218 | +} |
0 commit comments