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 nosqltools; import com.mongodb.DBObject; import com.mongodb.util.JSON; import java.awt.Image; import java.io.File; import java.io.IOException; import java.util.Arrays; import javax.imageio.ImageIO; import javax.swing.JOptionPane; import org.json.simple.parser.JSONParser; public class ActionPerformedOnDB extends javax.swing.JFrame { private static String action; private static DBConnection dbcon; JSONUtilities json_util = new JSONUtilities(); /** * Creates new form actionPerformedOnDB * @param value */ public ActionPerformedOnDB(String value, DBConnection db_con) { initComponents(); Image img = null; try { img = ImageIO.read(new File("resources/mongoicon.png")); } catch (IOException e) { } this.setIconImage(img); setLocationRelativeTo(null); action = value; this.dbcon = db_con; actionLabel.setText(action); label1.setText(action); databaseName.setText(dbcon.collection.getName()); this.setTitle(action + " Operation"); if ("Update".equals(action)) { label1.setText("Object in database to update: "); label2.setText("Update:"); } else if ("Find".equals(action)) { label1.setText("Find object in Database:"); label2.setVisible(false); textArea2.setVisible(false); } else { label2.setVisible(false); textArea2.setVisible(false); } } /** * 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() { jPanel1 = new javax.swing.JPanel(); OKBtn = new javax.swing.JButton(); backBtn = new javax.swing.JButton(); cancelBtn = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); textArea1 = new javax.swing.JTextArea(); jScrollPane2 = new javax.swing.JScrollPane(); textArea2 = new javax.swing.JTextArea(); label1 = new javax.swing.JLabel(); label2 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); actionLabel = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); databaseName = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); OKBtn.setText("OK"); OKBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { OKBtnActionPerformed(evt); } }); jPanel1.add(OKBtn, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 300, -1, -1)); backBtn.setText("<< Back"); backBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { backBtnActionPerformed(evt); } }); jPanel1.add(backBtn, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 300, -1, -1)); cancelBtn.setText("Cancel"); cancelBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelBtnActionPerformed(evt); } }); jPanel1.add(cancelBtn, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 300, -1, -1)); textArea1.setColumns(20); textArea1.setRows(5); jScrollPane1.setViewportView(textArea1); jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 50, 500, 110)); textArea2.setColumns(20); textArea2.setRows(5); jScrollPane2.setViewportView(textArea2); jPanel1.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 180, 500, -1)); label1.setText("Insert:"); jPanel1.add(label1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 260, -1)); label2.setText("jLabel6"); jPanel1.add(label2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1)); jLabel1.setText("Action:"); actionLabel.setText("jLabel2"); jLabel4.setText("Collection:"); databaseName.setText("jLabel5"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addGap(168, 168, 168) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(databaseName)) .addGroup(layout.createSequentialGroup().addComponent(jLabel1).addGap(60, 60, 60) .addComponent(actionLabel))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(actionLabel)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(databaseName)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void OKBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKBtnActionPerformed if (action != null) { if (parseText(textArea1.getText())) switch (action) { case "Insert": if (!dbcon.insertInDatabase(textArea1.getText())) dispose(); break; case "Update": parseText(textArea2.getText()); if (!dbcon.updateDatabase(textArea1.getText(), textArea2.getText())) dispose(); break; case "Delete": if (!dbcon.deleteFromDatabase(textArea1.getText())) dispose(); break; case "Find": if (!dbcon.findFromDatabase(textArea1.getText())) dispose(); else textArea1.setText(""); break; default: JOptionPane.showMessageDialog(null, "Invalid action taken on Database!", "Error", JOptionPane.ERROR_MESSAGE); break; } } }//GEN-LAST:event_OKBtnActionPerformed private void backBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBtnActionPerformed dispose(); ActionOnDB actionOnDb = new ActionOnDB(dbcon); actionOnDb.setVisible(true); }//GEN-LAST:event_backBtnActionPerformed private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed dispose(); }//GEN-LAST:event_cancelBtnActionPerformed public boolean parseText(String x) { boolean flag = true; JSONParser parser = new JSONParser(); try { parser.parse(x); } catch (org.json.simple.parser.ParseException pe) { flag = false; if (x.isEmpty()) JOptionPane.showMessageDialog(null, "There are no JSON Objects to " + action + "!", "Error", JOptionPane.ERROR_MESSAGE); else JOptionPane.showMessageDialog(null, "Incorrect JSON format - " + Initializations.ERRORLINE + json_util.getLineNumber(pe.getPosition(), x) + " " + pe + "!", "Error", JOptionPane.ERROR_MESSAGE); } return flag; } /** * @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(ActionPerformedOnDB.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ActionPerformedOnDB.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ActionPerformedOnDB.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ActionPerformedOnDB.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ActionPerformedOnDB(action, dbcon).setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton OKBtn; private javax.swing.JLabel actionLabel; private javax.swing.JButton backBtn; private javax.swing.JButton cancelBtn; private javax.swing.JLabel databaseName; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JLabel label1; private javax.swing.JLabel label2; private javax.swing.JTextArea textArea1; private javax.swing.JTextArea textArea2; // End of variables declaration//GEN-END:variables }