Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package view; import controller.MobilePartnerController; import db_ddl.CreateDB; import db_ddl.ReadQuery; import db_pojo.MessageDefine; import db_pojo.SavedContacts; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.net.URISyntaxException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.persistence.EntityManagerFactory; import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.RowFilter; import javax.swing.table.TableColumn; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; import jpa_controllers.InboundMessageJpaController; import jpa_controllers.MessageDefineJpaController; import jpa_controllers.SavedContactsJpaController; import jpa_controllers.exceptions.NonexistentEntityException; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; /** * * @author Shanaka */ public class MobilePartnerView extends javax.swing.JFrame { /** * Creates new form MobilePartnerView */ private final MobilePartnerController controller; private transient TableRowSorter<? extends TableModel> sorterMessages; private transient TableRowSorter<? extends TableModel> sorterContacts; private transient TableRowSorter<? extends TableModel> sorterPredefines; private static final int LR_PAGE_SIZE = 5; private final MessagesTableModel M_model; private final ContactsTableModel C_model; private final PredefinesTableModel P_model; private final CustomComboModel Combo_model; private final CreateDB dbCreate = CreateDB.getInstance(); final String dir = System.getProperty("user.dir"); public MobilePartnerView() throws URISyntaxException, IOException, SQLException { //If mobile Partner DB not exist then create DB if (!dbCreate.DBExist()) { File file = new File(CreateDB.class.getResource("mobile_partner_ddl.sql").toURI()); ArrayList<String> createQueries = ReadQuery.createQueries(file.getCanonicalPath()); for (String query : createQueries) { try (Connection conn = dbCreate.getConnection(); PreparedStatement ps = conn.prepareStatement(query)) { ps.execute(); ps.close(); } } } controller = MobilePartnerController.getInstance(); M_model = MessagesTableModel.getInstance(); C_model = ContactsTableModel.getInstance(); P_model = PredefinesTableModel.getInstance(); Combo_model = CustomComboModel.getInstance(); initComponents(); checkDBConfigure(); messageTable.removeColumn(messageTable.getColumnModel().getColumn(0)); contactsTable.removeColumn(contactsTable.getColumnModel().getColumn(0)); predefinesTable.removeColumn(predefinesTable.getColumnModel().getColumn(0)); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { int result = JOptionPane.showConfirmDialog(MobilePartnerView.this, "Are you sure you want to exit the application?", "Exit Application", JOptionPane.YES_NO_OPTION); if (result == JOptionPane.YES_OPTION) { MobilePartnerView.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } }); } /** * 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">//GEN-BEGIN:initComponents private void initComponents() { jTabbedPane1 = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); startConnectionBtn = new javax.swing.JButton(); connectionErrorLabel = new javax.swing.JLabel(); jPanel2 = new javax.swing.JPanel(); box_messages = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); messageTable = new javax.swing.JTable(); saveAsContact = new javax.swing.JButton(); deleteMessage = new javax.swing.JButton(); tableFilterBox = new javax.swing.JComboBox<>(); jLabel1 = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); jScrollPane2 = new javax.swing.JScrollPane(); contactsTable = new javax.swing.JTable(); box_contacts = new javax.swing.JPanel(); deleteContact = new javax.swing.JButton(); jPanel4 = new javax.swing.JPanel(); jTabbedPane2 = new javax.swing.JTabbedPane(); jPanel5 = new javax.swing.JPanel(); jScrollPane3 = new javax.swing.JScrollPane(); predefinesTable = new javax.swing.JTable(); newPredefines = new javax.swing.JButton(); deletePredefines = new javax.swing.JButton(); jPanel6 = new javax.swing.JPanel(); jdbcUrl = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); dbUser = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); dbDriver = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); dbPass = new javax.swing.JPasswordField(); saveXml = new javax.swing.JButton(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); dbMessage = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setTitle("Mobile Partner"); setResizable(false); startConnectionBtn.setText("Start Connection"); startConnectionBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { startConnectionBtnActionPerformed(evt); } }); connectionErrorLabel.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N connectionErrorLabel.setForeground(new java.awt.Color(255, 51, 51)); connectionErrorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(299, 299, 299).addComponent( startConnectionBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(116, 116, 116).addComponent( connectionErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 536, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(147, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(65, 65, 65) .addComponent(connectionErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(76, 76, 76).addComponent(startConnectionBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(227, Short.MAX_VALUE))); jTabbedPane1.addTab("CONNECTION", jPanel1); box_messages.setLayout(new javax.swing.BoxLayout(box_messages, javax.swing.BoxLayout.LINE_AXIS)); sorterMessages = new TableRowSorter<>(M_model); messageTable.setModel(M_model); messageTable.setRowSorter(sorterMessages); messageTable.setRowHeight(40); messageTable.setDefaultRenderer(Object.class, new CustomRenderer()); Enumeration<TableColumn> columnsMessages = messageTable.getColumnModel().getColumns(); while (columnsMessages.hasMoreElements()) { TableColumn nextElement = columnsMessages.nextElement(); nextElement.setHeaderRenderer(new ColumnHeaderRenderer()); } jScrollPane1.setViewportView(messageTable); initLinkBoxMessages(7, 1); saveAsContact.setText("Save to Contacts"); saveAsContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveAsContactActionPerformed(evt); } }); deleteMessage.setText("Delete Message"); deleteMessage.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deleteMessageActionPerformed(evt); } }); tableFilterBox.setModel(Combo_model); tableFilterBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tableFilterBoxActionPerformed(evt); } }); jLabel1.setText("Reload Table With :"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(29, 29, 29).addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel1).addGap(18, 18, 18) .addComponent(tableFilterBox, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup( jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(box_messages, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 611, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(saveAsContact, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(deleteMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)))) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(23, 23, 23) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tableFilterBox, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE) .addComponent(jLabel1)) .addGap(18, 18, 18) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(saveAsContact, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(deleteMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(box_messages, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); jTabbedPane1.addTab("MESSAGES", jPanel2); sorterContacts = new TableRowSorter<>(C_model); contactsTable.setModel(C_model); contactsTable.setRowSorter(sorterContacts); contactsTable.setRowHeight(40); contactsTable.setDefaultRenderer(Object.class, new CustomRenderer()); Enumeration<TableColumn> columnsContacts = contactsTable.getColumnModel().getColumns(); while (columnsContacts.hasMoreElements()) { TableColumn nextElement = columnsContacts.nextElement(); nextElement.setHeaderRenderer(new ColumnHeaderRenderer()); } jScrollPane2.setViewportView(contactsTable); initLinkBoxContacts(7, 1); box_contacts.setLayout(new javax.swing.BoxLayout(box_contacts, javax.swing.BoxLayout.LINE_AXIS)); deleteContact.setText("Delete Contact"); deleteContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deleteContactActionPerformed(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(29, 29, 29) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(box_contacts, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 608, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(deleteContact, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE) .addGap(13, 13, 13))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(65, 65, 65) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(deleteContact, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(box_contacts, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(16, Short.MAX_VALUE))); jTabbedPane1.addTab("CONTACTS", jPanel3); sorterPredefines = new TableRowSorter<>(P_model); predefinesTable.setModel(P_model); predefinesTable.setRowSorter(sorterPredefines); predefinesTable.setRowHeight(40); predefinesTable.setDefaultRenderer(Object.class, new CustomRendererPredefines()); Enumeration<TableColumn> columnsPredefines = predefinesTable.getColumnModel().getColumns(); while (columnsPredefines.hasMoreElements()) { TableColumn nextElement = columnsPredefines.nextElement(); nextElement.setHeaderRenderer(new ColumnHeaderRenderer()); } jScrollPane3.setViewportView(predefinesTable); newPredefines.setText("New"); newPredefines.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { newPredefinesActionPerformed(evt); } }); deletePredefines.setText("Delete"); deletePredefines.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deletePredefinesActionPerformed(evt); } }); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 608, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(newPredefines, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(deletePredefines, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel5Layout.createSequentialGroup() .addComponent(newPredefines, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(deletePredefines, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(22, Short.MAX_VALUE))); jTabbedPane2.addTab("Pre-define Messages", jPanel5); jLabel2.setText("JDBC Url :"); jLabel3.setText("DB User :"); dbDriver.setText("com.mysql.jdbc.Driver"); jLabel4.setText("DB Driver :"); jLabel5.setText("DB Password :"); saveXml.setText("Save"); saveXml.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveXmlActionPerformed(evt); } }); jLabel6.setText("Ex : jdbc:mysql://<host>:<port>/<db_name>"); jLabel7.setText("Ex : com.mysql.jdbc.Driver"); dbMessage.setForeground(new java.awt.Color(204, 0, 0)); javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6); jPanel6.setLayout(jPanel6Layout); jPanel6Layout.setHorizontalGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addGap(121, 121, 121).addGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel6Layout.createSequentialGroup() .addGroup( jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)) .addGap(41, 41, 41) .addGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(dbDriver, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE) .addComponent(dbPass).addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addComponent(saveXml, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel6Layout.createSequentialGroup().addComponent(jLabel3).addGap(41, 41, 41) .addComponent(dbUser, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(dbMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel6Layout.createSequentialGroup().addComponent(jLabel2) .addGap(41, 41, 41) .addGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE) .addComponent(jdbcUrl))))) .addContainerGap(119, Short.MAX_VALUE))); jPanel6Layout.setVerticalGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addGap(20, 20, 20) .addComponent(dbMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jdbcUrl, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel6).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(dbUser, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addGap(28, 28, 28) .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(dbDriver, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel7) .addGap(11, 11, 11) .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(dbPass, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(36, 36, 36).addComponent(saveXml, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(30, 30, 30))); jTabbedPane2.addTab("DB Settings", jPanel6); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout .setHorizontalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addGap(21, 21, 21) .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 752, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(26, Short.MAX_VALUE))); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout .createSequentialGroup().addGap(30, 30, 30).addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 403, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(33, Short.MAX_VALUE))); jTabbedPane1.addTab("SETTINGS", jPanel4); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane1)); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane1)); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void startConnectionBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startConnectionBtnActionPerformed if (controller.IsServiceStarted()) { controller.endConnection(); startConnectionBtn.setText("Start Connection"); } else { boolean connectionStarted = controller.beginConnection(); if (connectionStarted) { connectionErrorLabel.setText(""); startConnectionBtn.setText("End Connection"); } else { connectionErrorLabel.setText("Something wrong with your connection. Please check the usb modem."); } } }//GEN-LAST:event_startConnectionBtnActionPerformed private void saveAsContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsContactActionPerformed if (messageTable.getSelectedRows().length > 0) { int tableSelectedRow = messageTable.getSelectedRow(); String contactNum = M_model.getValueAt(tableSelectedRow, 2).toString(); saveContactsPanel savPanel = new saveContactsPanel(); savPanel.contNumber.setText(contactNum); if (!contactNum.matches("[0-9]+")) { JOptionPane.showMessageDialog(this, "Contact Already Saved !", "Message", JOptionPane.INFORMATION_MESSAGE); return; } int confirm = JOptionPane.showConfirmDialog(this, savPanel, "Save As Contact", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (confirm == 0) { SavedContacts savedContacts = new SavedContacts(); savedContacts.setContactName(savPanel.contName.getText()); savedContacts.setNumber(contactNum); savedContacts.setDescription(savPanel.contDesc.getText()); savedContacts.setDateAdded(new Date()); savedContacts.setDateUpdated(new Date()); try { saveToContacts(savedContacts); } catch (Exception ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } JOptionPane.showMessageDialog(this, "Contact Saved !", "Success", JOptionPane.INFORMATION_MESSAGE); M_model.addAllData(); messageTable.setModel(M_model); M_model.fireTableDataChanged(); //refresh contact table too C_model.addAllData(); contactsTable.setModel(C_model); C_model.fireTableDataChanged(); } } }//GEN-LAST:event_saveAsContactActionPerformed private void deleteMessageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteMessageActionPerformed if (messageTable.getSelectedRows().length > 0) { int tableSelectedRow = messageTable.getSelectedRow(); String messageId = M_model.getValueAt(tableSelectedRow, 0).toString(); int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this message ?", "Warning !", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (confirm == 0) { try { EntityManagerFactory emf = controller.getEntityManagerFactory(); if (emf == null) { dbMessage.setText("Your database is not configured yet. Please provide db information."); return; } InboundMessageJpaController inboundController = new InboundMessageJpaController(emf); inboundController.destroy(Integer.parseInt(messageId)); emf.close(); JOptionPane.showMessageDialog(this, "Message deleted Successfully !", "Success", JOptionPane.INFORMATION_MESSAGE); M_model.addAllData(); messageTable.setModel(M_model); M_model.fireTableDataChanged(); } catch (NonexistentEntityException ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } } } }//GEN-LAST:event_deleteMessageActionPerformed private void deleteContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteContactActionPerformed if (contactsTable.getSelectedRows().length > 0) { int tableSelectedRow = contactsTable.getSelectedRow(); String contactId = C_model.getValueAt(tableSelectedRow, 0).toString(); int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this contact ?", "Warning !", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (confirm == 0) { try { EntityManagerFactory emf = controller.getEntityManagerFactory(); SavedContactsJpaController savedMsgController = new SavedContactsJpaController(emf); savedMsgController.destroy(Integer.parseInt(contactId)); emf.close(); JOptionPane.showMessageDialog(this, "Contact deleted Successfully !", "Success", JOptionPane.INFORMATION_MESSAGE); C_model.addAllData(); contactsTable.setModel(C_model); C_model.fireTableDataChanged(); /// refresh message table too M_model.addAllData(); messageTable.setModel(M_model); M_model.fireTableDataChanged(); } catch (NonexistentEntityException ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } } } }//GEN-LAST:event_deleteContactActionPerformed private void newPredefinesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPredefinesActionPerformed savePredefinesPanel panelPre = new savePredefinesPanel(); int confirm = JOptionPane.showConfirmDialog(this, panelPre, "Save Pre-defines !", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (confirm == 0) { String messageType = savePredefinesPanel.messageType.getText(); int rgb = savePredefinesPanel.colorChooser.getSelectionModel().getSelectedColor().getRGB(); try { EntityManagerFactory emf = controller.getEntityManagerFactory(); MessageDefineJpaController defineController = new MessageDefineJpaController(emf); MessageDefine define = new MessageDefine(); define.setDateAdded(new Date()); define.setTypeName(messageType); define.setDescription(String.valueOf(rgb)); defineController.create(define); JOptionPane.showMessageDialog(this, "Saved Successfully !", "Success", JOptionPane.INFORMATION_MESSAGE); P_model.addAllData(); predefinesTable.setModel(P_model); P_model.fireTableDataChanged(); //refresh combobox model Combo_model.addAllData(); Combo_model.fireContentsChanged(this, 0, 0); tableFilterBox.setModel(Combo_model); tableFilterBox.showPopup(); /// refresh message table too M_model.addAllData(); messageTable.setModel(M_model); M_model.fireTableDataChanged(); } catch (Exception ex) { // Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_newPredefinesActionPerformed private void deletePredefinesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletePredefinesActionPerformed if (predefinesTable.getSelectedRows().length > 0) { int tableSelectedRow = predefinesTable.getSelectedRow(); String defineId = P_model.getValueAt(tableSelectedRow, 0).toString(); int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete ?", "Warning !", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (confirm == 0) { try { EntityManagerFactory emf = controller.getEntityManagerFactory(); if (emf == null) { dbMessage.setText("Your database is not configured yet. Please provide db information."); return; } MessageDefineJpaController defineMsgController = new MessageDefineJpaController(emf); defineMsgController.destroy(Integer.parseInt(defineId)); emf.close(); JOptionPane.showMessageDialog(this, "Deleted Successfully !", "Success", JOptionPane.INFORMATION_MESSAGE); P_model.addAllData(); predefinesTable.setModel(P_model); P_model.fireTableDataChanged(); //refresh combobox model Combo_model.addAllData(); Combo_model.fireContentsChanged(this, 0, 0); tableFilterBox.setModel(Combo_model); tableFilterBox.showPopup(); /// refresh message table too M_model.addAllData(); messageTable.setModel(M_model); M_model.fireTableDataChanged(); } catch (NonexistentEntityException ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { // Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } } } }//GEN-LAST:event_deletePredefinesActionPerformed private void saveXmlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveXmlActionPerformed try { boolean isConfigured = createXML(jdbcUrl.getText(), dbUser.getText(), dbDriver.getText(), dbPass.getText()); if (isConfigured) { JOptionPane.showMessageDialog(this, "Configured Successfully !", "Success", JOptionPane.INFORMATION_MESSAGE); } } catch (Exception ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_saveXmlActionPerformed private void tableFilterBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tableFilterBoxActionPerformed M_model.filterData(tableFilterBox.getSelectedItem().toString()); messageTable.setModel(M_model); M_model.fireTableDataChanged(); }//GEN-LAST:event_tableFilterBoxActionPerformed /** * @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(MobilePartnerView.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MobilePartnerView.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MobilePartnerView.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MobilePartnerView.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() { try { new MobilePartnerView().setVisible(true); } catch (URISyntaxException ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel box_contacts; private javax.swing.JPanel box_messages; private javax.swing.JLabel connectionErrorLabel; private javax.swing.JTable contactsTable; private javax.swing.JTextField dbDriver; private javax.swing.JLabel dbMessage; private javax.swing.JPasswordField dbPass; private javax.swing.JTextField dbUser; private javax.swing.JButton deleteContact; private javax.swing.JButton deleteMessage; private javax.swing.JButton deletePredefines; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTabbedPane jTabbedPane2; private javax.swing.JTextField jdbcUrl; public static javax.swing.JTable messageTable; private javax.swing.JButton newPredefines; private javax.swing.JTable predefinesTable; private javax.swing.JButton saveAsContact; private javax.swing.JButton saveXml; private javax.swing.JButton startConnectionBtn; private javax.swing.JComboBox<String> tableFilterBox; // End of variables declaration//GEN-END:variables private void initLinkBoxMessages(final int itemsPerPage, final int currentPageIndex) { //assert currentPageIndex > 0; sorterMessages.setRowFilter(new RowFilter<TableModel, Integer>() { @Override public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) { int ti = currentPageIndex - 1; int ei = entry.getIdentifier(); return ti * itemsPerPage <= ei && ei < ti * itemsPerPage + itemsPerPage; } }); int startPageIndex = currentPageIndex - LR_PAGE_SIZE; if (startPageIndex <= 0) { startPageIndex = 1; } int rowCount = M_model.getRowCount(); int v = rowCount % itemsPerPage == 0 ? 0 : 1; int maxPageIndex = rowCount / itemsPerPage + v; int endPageIndex = currentPageIndex + LR_PAGE_SIZE - 1; if (endPageIndex > maxPageIndex) { endPageIndex = maxPageIndex; } box_messages.removeAll(); if (startPageIndex >= endPageIndex) { //if I only have one page, Y don't want to see pagination buttons return; } ButtonGroup bg = new ButtonGroup(); JButton f = makePrevNextButtonMessages(itemsPerPage, 1, "<<", currentPageIndex > 1); box_messages.add(f); bg.add(f); JButton p = makePrevNextButtonMessages(itemsPerPage, currentPageIndex - 1, "<", currentPageIndex > 1); box_messages.add(p); bg.add(p); box_messages.add(Box.createHorizontalGlue()); for (int i = startPageIndex; i <= endPageIndex; i++) { JButton c = makeButtonMessages(itemsPerPage, currentPageIndex, i); box_messages.add(c); bg.add(c); } box_messages.add(Box.createHorizontalGlue()); JButton n = makePrevNextButtonMessages(itemsPerPage, currentPageIndex + 1, ">", currentPageIndex < maxPageIndex); box_messages.add(n); bg.add(n); JButton l = makePrevNextButtonMessages(itemsPerPage, maxPageIndex, ">>", currentPageIndex < maxPageIndex); box_messages.add(l); bg.add(l); box_messages.revalidate(); box_messages.repaint(); } private void initLinkBoxContacts(final int itemsPerPage, final int currentPageIndex) { //assert currentPageIndex > 0; sorterContacts.setRowFilter(new RowFilter<TableModel, Integer>() { @Override public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) { int ti = currentPageIndex - 1; int ei = entry.getIdentifier(); return ti * itemsPerPage <= ei && ei < ti * itemsPerPage + itemsPerPage; } }); int startPageIndex = currentPageIndex - LR_PAGE_SIZE; if (startPageIndex <= 0) { startPageIndex = 1; } int rowCount = C_model.getRowCount(); int v = rowCount % itemsPerPage == 0 ? 0 : 1; int maxPageIndex = rowCount / itemsPerPage + v; int endPageIndex = currentPageIndex + LR_PAGE_SIZE - 1; if (endPageIndex > maxPageIndex) { endPageIndex = maxPageIndex; } box_contacts.removeAll(); if (startPageIndex >= endPageIndex) { //if I only have one page, Y don't want to see pagination buttons return; } ButtonGroup bg = new ButtonGroup(); JButton f = makePrevNextButtonContacts(itemsPerPage, 1, "<<", currentPageIndex > 1); box_contacts.add(f); bg.add(f); JButton p = makePrevNextButtonContacts(itemsPerPage, currentPageIndex - 1, "<", currentPageIndex > 1); box_contacts.add(p); bg.add(p); box_contacts.add(Box.createHorizontalGlue()); for (int i = startPageIndex; i <= endPageIndex; i++) { JButton c = makeButtonContacts(itemsPerPage, currentPageIndex, i); box_contacts.add(c); bg.add(c); } box_contacts.add(Box.createHorizontalGlue()); JButton n = makePrevNextButtonContacts(itemsPerPage, currentPageIndex + 1, ">", currentPageIndex < maxPageIndex); box_contacts.add(n); bg.add(n); JButton l = makePrevNextButtonContacts(itemsPerPage, maxPageIndex, ">>", currentPageIndex < maxPageIndex); box_contacts.add(l); bg.add(l); box_contacts.revalidate(); box_contacts.repaint(); } private JButton makePrevNextButtonMessages(final int itemsPerPage, final int target, String title, boolean flag) { JButton button = new JButton(title); button.setForeground(Color.BLUE); //button.setUI(LINKVIEW_BUTTON_UI); button.setEnabled(flag); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { initLinkBoxMessages(itemsPerPage, target); } }); return button; } private JButton makePrevNextButtonContacts(final int itemsPerPage, final int target, String title, boolean flag) { JButton button = new JButton(title); button.setForeground(Color.BLUE); //button.setUI(LINKVIEW_BUTTON_UI); button.setEnabled(flag); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { initLinkBoxContacts(itemsPerPage, target); } }); return button; } private JButton makeButtonMessages(final int itemsPerPage, int current, final int target) { JButton button = new JButton(String.valueOf(target)) { @Override protected void fireStateChanged() { ButtonModel model = getModel(); if (model.isEnabled()) { if (model.isPressed() && model.isArmed()) { setForeground(Color.GREEN); } else if (model.isSelected()) { setForeground(Color.RED); //} else if (isRolloverEnabled() && M_model.isRollover()) { // setForeground(Color.BLUE); } } else { setForeground(Color.GRAY); } super.fireStateChanged(); } }; button.setForeground(Color.BLUE); // button.setUI(LINKVIEW_BUTTON_UI); if (target == current) { button.setSelected(true); } button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { initLinkBoxMessages(itemsPerPage, target); } }); return button; } private JButton makeButtonContacts(final int itemsPerPage, int current, final int target) { JButton button = new JButton(String.valueOf(target)) { @Override protected void fireStateChanged() { ButtonModel model = getModel(); if (model.isEnabled()) { if (model.isPressed() && model.isArmed()) { setForeground(Color.GREEN); } else if (model.isSelected()) { setForeground(Color.RED); //} else if (isRolloverEnabled() && M_model.isRollover()) { // setForeground(Color.BLUE); } } else { setForeground(Color.GRAY); } super.fireStateChanged(); } }; button.setForeground(Color.BLUE); // button.setUI(LINKVIEW_BUTTON_UI); if (target == current) { button.setSelected(true); } button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { initLinkBoxContacts(itemsPerPage, target); } }); return button; } private boolean saveToContacts(SavedContacts savedContacts) throws Exception { EntityManagerFactory emf = controller.getEntityManagerFactory(); SavedContactsJpaController svdCntctController = new SavedContactsJpaController(emf); try { svdCntctController.create(savedContacts); emf.close(); return true; } catch (Exception e) { return false; } } public Map<String, Object> getConfiguredOverrides() throws Exception { Map<String, Object> configOverrides = new HashMap<>(); SAXBuilder builder = new SAXBuilder(); File xmlFile = new File(dir + "\\dbconf.xml"); Document document = (Document) builder.build(xmlFile); Element rootNode = document.getRootElement(); List<Element> list = rootNode.getChildren(); for (Element element : list) { configOverrides.put(element.getAttributeValue("name"), element.getText()); } return configOverrides; } public boolean createXML(String url, String user, String driver, String password) throws Exception { Element root = new Element("dbconf"); Document doc = new Document(); Element child1 = new Element("url"); // child1.addContent("jdbc:mysql://localhost:3306/mobile_partner"); child1.addContent(url); child1.setAttribute("name", "javax.persistence.jdbc.url"); Element child2 = new Element("user"); // child2.addContent("root"); child2.addContent(user); child2.setAttribute("name", "javax.persistence.jdbc.user"); Element child3 = new Element("driver"); //child3.addContent("com.mysql.jdbc.Driver"); child3.addContent(driver); child3.setAttribute("name", "javax.persistence.jdbc.driver"); Element child4 = new Element("password"); // child4.addContent("nbuser"); child4.addContent(password); child4.setAttribute("name", "javax.persistence.jdbc.password"); root.addContent(child1); root.addContent(child2); root.addContent(child3); root.addContent(child4); doc.setRootElement(root); XMLOutputter outter = new XMLOutputter(); outter.setFormat(Format.getPrettyFormat()); outter.output(doc, new FileWriter(new File(dir + "\\dbconf.xml"))); File xmlFile = new File(dir + "\\dbconf.xml"); if (xmlFile.exists()) { return true; } else { return false; } } private void checkDBConfigure() { File xmlFile = new File(dir + "\\dbconf.xml"); if (!xmlFile.exists()) { dbMessage.setText("Your database is not configured yet. Please provide db information."); } else { try { Map<String, Object> configuredOverrides = getConfiguredOverrides(); for (Map.Entry<String, Object> entry : configuredOverrides.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); switch (key) { case "javax.persistence.jdbc.url": jdbcUrl.setText(value.toString()); break; case "javax.persistence.jdbc.user": dbUser.setText(value.toString()); break; case "javax.persistence.jdbc.driver": dbDriver.setText(value.toString()); break; case "javax.persistence.jdbc.password": dbPass.setText(value.toString()); break; } } } catch (Exception ex) { Logger.getLogger(MobilePartnerView.class.getName()).log(Level.SEVERE, null, ex); } } } }