Java tutorial
package com.floreantpos.ui.dialog; import com.floreantpos.config.AppConfig; import com.floreantpos.swing.MessageDialog; import org.apache.commons.codec.binary.Base64; import org.hyperic.sigar.NetInterfaceConfig; import org.hyperic.sigar.Sigar; import java.awt.event.ActionEvent; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * * @author Leonar Tambunan */ public class LicenseDialog extends javax.swing.JFrame { /** * Creates new form LicenseDialog */ public LicenseDialog() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { this.setTitle("License Key POS"); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); idTF = new javax.swing.JTextField(); keyTF = new javax.swing.JTextField(); saveBtn = new javax.swing.JButton(); cancelBtn = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); //if (StringUtils.isEmpty(AppConfig.getMachineId())) { try { String[] nets = new Sigar().getNetInterfaceList(); NetInterfaceConfig c = new Sigar().getNetInterfaceConfig(nets[0]); Base64 base64 = new Base64(true); String machineId = base64.encodeAsString(c.getHwaddr().getBytes()).trim().toUpperCase(); idTF.setText(machineId); } catch (Exception e) { e.printStackTrace(); } // } // else { // idTF.setText(AppConfig.getMachineId()); // } jLabel1.setText("ID Mesin"); jLabel2.setText("License Key"); saveBtn.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N saveBtn.setText("S a v e"); saveBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveBtnActionPerformed(evt); } }); cancelBtn.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N cancelBtn.setText("C a n c e l"); cancelBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelBtnActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2)) .addGap(31, 31, 31) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(idTF).addComponent(keyTF))) .addGroup(layout.createSequentialGroup().addGap(90, 90, 90) .addComponent(saveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(32, 32, 32).addComponent(cancelBtn).addGap(0, 49, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(61, 61, 61) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(idTF, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(32, 32, 32) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(keyTF, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(saveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(45, 45, 45))); pack(); }// </editor-fold> private void cancelBtnActionPerformed(ActionEvent evt) { this.setVisible(false); System.exit(0); } private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) { try { AppConfig.setMachineId(idTF.getText()); MessageDigest md = MessageDigest.getInstance("SHA"); md.update(idTF.getText().getBytes("UTF-8")); String key = new String(md.digest(), "UTF-8"); Base64 base64 = new Base64(true); key = base64.encodeAsString(key.getBytes("UTF-8")).trim().toUpperCase(); key = key.replaceAll("_", "").replaceAll("-", ""); if (key.equalsIgnoreCase(keyTF.getText())) { AppConfig.setLicenceKey(key); System.exit(0); } else { MessageDialog.showError("License Key tidak valid"); } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * @param args the command line arguments */ /* public static void main(String args[]) { */ /* Set the Nimbus look and feel *//* //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> *//* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html *//* try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(LicenseDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(LicenseDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(LicenseDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(LicenseDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> *//* Create and display the form *//* java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new LicenseDialog().setVisible(true); } }); }*/ // Variables declaration - do not modify private javax.swing.JButton cancelBtn; private javax.swing.JTextField idTF; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField keyTF; private javax.swing.JButton saveBtn; // End of variables declaration }