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.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.awt.Color; import java.awt.Font; import java.awt.Image; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.List; import javax.swing.tree.TreePath; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import org.fife.ui.rtextarea.*; import org.fife.ui.rsyntaxtextarea.*; import org.json.simple.parser.JSONParser; public class MainForm extends javax.swing.JFrame { private final JSONUtilities json_util = new JSONUtilities(); private final Utilities util = new Utilities(); StringBuilder sb = new StringBuilder(); private File file = null; RSyntaxTextArea textArea; RSyntaxTextArea textArea1Comp; RSyntaxTextArea textArea2Comp; JSONParser parser = new JSONParser(); ObjectMapper mapper = new ObjectMapper(); DBConnection dbcon = new DBConnection(); public static JProgressBar progBar; TreePath tp; int indexOfCurrentCollection; final JFileChooser fc = new JFileChooser(); String[] ext_array = new String[] { Initializations.TXT, Initializations.JSON }; String ext = util.formatExtentsions(ext_array); FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files (" + ext + ")", ext_array); /** * Creates new form MainForm and sets the necessary properties for the main * form */ public MainForm() { Image img = null; try { img = ImageIO.read(new File("resources/mongoicon.png")); } catch (IOException e) { } this.setIconImage(img); initComponents(); setExtendedState(JFrame.MAXIMIZED_BOTH); fc.setFileFilter(filter); textArea = new RSyntaxTextArea(20, 60); textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON); textArea.setCodeFoldingEnabled(true); textArea.setAntiAliasingEnabled(true); Table_JSON.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseClicked(java.awt.event.MouseEvent evt) { int row = Table_JSON.rowAtPoint(evt.getPoint()); int col = Table_JSON.columnAtPoint(evt.getPoint()); JOptionPane.showMessageDialog(null, "Value in the cell clicked :" + " " + Table_JSON.getValueAt(row, col).toString()); if (json_util.isArray(Table_JSON.getValueAt(row, col).toString())) { if (JOptionPane.showConfirmDialog(null, "View the array as a table:", "VIEW", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.YES_OPTION) { Panel_Views.setEnabled(false); Panel_Connections.setEnabled(false); jMenuBar1.setEnabled(false); if (row >= 0 && col >= 0) { TableForm tableForm = new TableForm(Table_JSON.getValueAt(row, col).toString()); } } } } }); RTextScrollPane sp = new RTextScrollPane(textArea); sp.setFoldIndicatorEnabled(true); Panel_Text.add(sp); Save_File.setEnabled(false); Panel_Text.setVisible(false); Panel_Table.setVisible(false); Panel_Hierarchical.setVisible(false); Panel_Compare.setVisible(false); Panel_Compare_Upper.setVisible(false); Panel_Connect.setVisible(false); util.changeTextAreaTheme(textArea); } /** * 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() { jSplitPane1 = new javax.swing.JSplitPane(); Panel_Connections = new javax.swing.JPanel(); jScrollPane2 = new javax.swing.JScrollPane(); jTree1 = new javax.swing.JTree(); Panel_Views = new javax.swing.JPanel(); Panel_Text = new javax.swing.JPanel(); Panel_Hierarchical = new javax.swing.JPanel(); jScrollPane4 = new javax.swing.JScrollPane(); jTreeHierarchicalJson = new javax.swing.JTree(); Panel_Table = new javax.swing.JPanel(); jScrollPane3 = new javax.swing.JScrollPane(); Table_JSON = new javax.swing.JTable(); Panel_Compare = new javax.swing.JPanel(); ComparePane = new javax.swing.JSplitPane(); jScrollPane6 = new javax.swing.JScrollPane(); Compare_ResultText = new javax.swing.JTextArea(); Panel_Compare_Upper = new javax.swing.JPanel(); left_label = new javax.swing.JLabel(); left_obj_to_array = new javax.swing.JCheckBox(); Compare_Button = new javax.swing.JButton(); right_obj_to_array = new javax.swing.JCheckBox(); right_label = new javax.swing.JLabel(); Panel_Connect = new javax.swing.JPanel(); Text_MessageBar = new javax.swing.JTextField(); jMenuBar1 = new javax.swing.JMenuBar(); Menu_File = new javax.swing.JMenu(); Import_JSON = new javax.swing.JMenuItem(); Save_File = new javax.swing.JMenuItem(); Save_Mongo = new javax.swing.JMenuItem(); connect_DB = new javax.swing.JMenuItem(); file_close = new javax.swing.JMenuItem(); Menu_Views = new javax.swing.JMenu(); View_Text = new javax.swing.JMenuItem(); View_Hierarchical = new javax.swing.JMenuItem(); View_Table = new javax.swing.JMenuItem(); Menu_Operations = new javax.swing.JMenu(); Op_Compare = new javax.swing.JMenuItem(); Op_Validate = new javax.swing.JMenuItem(); Op_Refresh = new javax.swing.JMenuItem(); Op_DBActions = new javax.swing.JMenuItem(); Menu_Collections = new javax.swing.JMenu(); Import_File = new javax.swing.JMenuItem(); Export_File = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Mongo View"); jSplitPane1.setDividerLocation(200); jSplitPane1.setPreferredSize(new java.awt.Dimension(568, 453)); jScrollPane2.setViewportView(jTree1); jTree1.setModel(null); javax.swing.GroupLayout Panel_ConnectionsLayout = new javax.swing.GroupLayout(Panel_Connections); Panel_Connections.setLayout(Panel_ConnectionsLayout); Panel_ConnectionsLayout.setHorizontalGroup( Panel_ConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)); Panel_ConnectionsLayout.setVerticalGroup(Panel_ConnectionsLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane2)); jSplitPane1.setLeftComponent(Panel_Connections); Panel_Text.setLayout(new java.awt.BorderLayout()); Panel_Hierarchical.setLayout(new java.awt.BorderLayout()); jScrollPane4.setViewportView(jTreeHierarchicalJson); Panel_Hierarchical.add(jScrollPane4, java.awt.BorderLayout.CENTER); Panel_Table.setLayout(new java.awt.BorderLayout()); jScrollPane3.setSize(Panel_Table.getSize()); Table_JSON .setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane3.setViewportView(Table_JSON); Table_JSON.setSize(Panel_Table.getSize()); Panel_Table.add(jScrollPane3, java.awt.BorderLayout.CENTER); Panel_Compare.setPreferredSize(new java.awt.Dimension(1203, 511)); Panel_Compare.setLayout(new java.awt.BorderLayout(2, 2)); ComparePane.setResizeWeight(0.5); ComparePane.setToolTipText(""); Panel_Compare.add(ComparePane, java.awt.BorderLayout.CENTER); Compare_ResultText.setColumns(20); Compare_ResultText.setRows(5); Compare_ResultText.setDisabledTextColor(new java.awt.Color(0, 51, 0)); Compare_ResultText.setEnabled(false); jScrollPane6.setViewportView(Compare_ResultText); Panel_Compare.add(jScrollPane6, java.awt.BorderLayout.PAGE_END); Panel_Compare_Upper.setPreferredSize(new java.awt.Dimension(75, 23)); left_label.setText("Document 1"); Panel_Compare_Upper.add(left_label); Font font = new Font("Courier", Font.BOLD, 12); left_label.setFont(font); left_obj_to_array.setText("Convert Document 1 to Array"); left_obj_to_array.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { left_obj_to_arrayActionPerformed(evt); } }); Panel_Compare_Upper.add(left_obj_to_array); Compare_Button.setText("Compare"); Compare_Button.setPreferredSize(new java.awt.Dimension(100, 20)); Compare_Button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Compare_ButtonActionPerformed(evt); } }); Panel_Compare_Upper.add(Compare_Button); right_obj_to_array.setText("Convert Document 2 to Array"); right_obj_to_array.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { right_obj_to_arrayActionPerformed(evt); } }); Panel_Compare_Upper.add(right_obj_to_array); right_label.setText("Document 2"); Panel_Compare_Upper.add(right_label); right_label.setFont(font); Panel_Compare.add(Panel_Compare_Upper, java.awt.BorderLayout.NORTH); Panel_Connect.setLayout(new java.awt.BorderLayout()); javax.swing.GroupLayout Panel_ViewsLayout = new javax.swing.GroupLayout(Panel_Views); Panel_Views.setLayout(Panel_ViewsLayout); Panel_ViewsLayout.setHorizontalGroup(Panel_ViewsLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Panel_Text, javax.swing.GroupLayout.DEFAULT_SIZE, 1294, Short.MAX_VALUE) .addGroup(Panel_ViewsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Panel_Hierarchical, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(Panel_ViewsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Panel_Table, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(Panel_ViewsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(Panel_ViewsLayout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(Panel_Compare, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(0, 0, Short.MAX_VALUE))) .addGroup(Panel_ViewsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(Panel_ViewsLayout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(Panel_Connect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)))); Panel_ViewsLayout.setVerticalGroup(Panel_ViewsLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Panel_Text, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE) .addGroup(Panel_ViewsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Panel_Hierarchical, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(Panel_ViewsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Panel_Table, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(Panel_ViewsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(Panel_ViewsLayout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(Panel_Compare, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(0, 0, Short.MAX_VALUE))) .addGroup(Panel_ViewsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(Panel_ViewsLayout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(Panel_Connect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)))); Panel_Hierarchical.setSize(Panel_Views.getSize()); Panel_Table.setSize(Panel_Views.getSize()); jSplitPane1.setRightComponent(Panel_Views); Text_MessageBar.setEditable(false); Text_MessageBar.setForeground(new java.awt.Color(255, 0, 0)); Text_MessageBar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Text_MessageBarActionPerformed(evt); } }); Menu_File.setText("File"); Menu_File.setName(""); // NOI18N Import_JSON.setText("Open JSON file"); Import_JSON.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Import_JSONActionPerformed(evt); } }); Menu_File.add(Import_JSON); Save_File.setText("Save File"); Save_File.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Save_FileActionPerformed(evt); } }); Menu_File.add(Save_File); Save_Mongo.setText("Save to MongoDB"); Save_Mongo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Save_MongoActionPerformed(evt); } }); Menu_File.add(Save_Mongo); connect_DB.setText("Connect"); connect_DB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { connect_DBActionPerformed(evt); } }); Menu_File.add(connect_DB); file_close.setText("Close"); file_close.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { file_closeActionPerformed(evt); } }); Menu_File.add(file_close); jMenuBar1.add(Menu_File); Menu_Views.setText("Views"); View_Text.setText("Text"); View_Text.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { View_TextActionPerformed(evt); } }); Menu_Views.add(View_Text); View_Hierarchical.setText("Hierarchical"); View_Hierarchical.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { View_HierarchicalActionPerformed(evt); } }); Menu_Views.add(View_Hierarchical); View_Table.setText("Table"); View_Table.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { View_TableActionPerformed(evt); } }); Menu_Views.add(View_Table); jMenuBar1.add(Menu_Views); Menu_Operations.setText("Operations"); Op_Compare.setText("Compare"); Op_Compare.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Op_CompareActionPerformed(evt); } }); Menu_Operations.add(Op_Compare); Op_Validate.setText("Validate"); Op_Validate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Op_ValidateActionPerformed(evt); } }); Menu_Operations.add(Op_Validate); Op_Refresh.setText("Refresh"); Op_Refresh.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Op_RefreshActionPerformed(evt); } }); Menu_Operations.add(Op_Refresh); Op_DBActions.setText("Database Actions"); Op_DBActions.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Op_DBActionsActionPerformed(evt); } }); Menu_Operations.add(Op_DBActions); jMenuBar1.add(Menu_Operations); Menu_Collections.setText("Collections"); Import_File.setText("Import File"); Import_File.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Import_FileActionPerformed(evt); } }); Menu_Collections.add(Import_File); Export_File.setText("Export File"); Export_File.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Export_FileActionPerformed(evt); } }); Menu_Collections.add(Export_File); jMenuBar1.add(Menu_Collections); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(Text_MessageBar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1500, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Text_MessageBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); pack(); }// </editor-fold>//GEN-END:initComponents /** * This method is an event listener which sets the UI necessary when the * View Text option is done * @param evt */ private void View_TextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_View_TextActionPerformed Panel_Text.setVisible(true); Text_MessageBar.setText(Initializations.INITSTRING); Panel_Hierarchical.setVisible(false); Panel_Table.setVisible(false); Panel_Compare.setVisible(false); Panel_Compare_Upper.setVisible(false); Panel_Connect.setVisible(false); }//GEN-LAST:event_View_TextActionPerformed /** * The View_HierarchicalActionPerformed method is an event handler which * sets the UI for the Hierarchical if the Text View is not empty * @param evt */ private void View_HierarchicalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_View_HierarchicalActionPerformed if (!textArea.getText().isEmpty()) { Panel_Text.setVisible(false); Panel_Hierarchical.setVisible(true); Panel_Table.setVisible(false); Panel_Compare.setVisible(false); Panel_Compare_Upper.setVisible(false); Panel_Connect.setVisible(false); jTreeHierarchicalJson.setVisible(true); jTreeHierarchicalJson.setModel(null); //parse the text within the text view and set model if JSON object is correct if (!textArea.getText().trim().equals("") && json_util.isDataParsed(textArea.getText())) { jTreeHierarchicalJson.setModel(json_util.makeJtreeModel("Collection")); } else //parsing error { Text_MessageBar.setText(Initializations.JSONINCORRECTFORMAT); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.JSONINCORRECTFORMAT, "Error", JOptionPane.ERROR_MESSAGE); } } else //case when no text is found { Text_MessageBar.setText(Initializations.NOTEXT); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.NOTEXT, "Error", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_View_HierarchicalActionPerformed /** * The View_TableActionPerformed is an event handler which sets the Table View * if text is found within the Text View and if correct JSON objects are found * @param evt */ private void View_TableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_View_TableActionPerformed if (!textArea.getText().isEmpty()) { Panel_Text.setVisible(false); Panel_Hierarchical.setVisible(false); Panel_Compare.setVisible(false); Panel_Compare_Upper.setVisible(false); Panel_Connect.setVisible(false); //parsing if (json_util.isDataParsed(textArea.getText())) { Panel_Table.setVisible(true); String[] json_field_names = json_util.getFields(); String[][] json_row_data = json_util.getRows(json_field_names); DefaultTableModel model = (DefaultTableModel) Table_JSON.getModel(); Table_JSON.setModel(new DefaultTableModel(json_row_data, json_field_names)); } else //case for incorrect JSON format { Text_MessageBar.setText(Initializations.JSONINCORRECTFORMAT); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.JSONINCORRECTFORMAT, "Error", JOptionPane.ERROR_MESSAGE); } } else //case for no JSON objects found { Text_MessageBar.setText(Initializations.NOTEXT); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.NOTEXT, "Error", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_View_TableActionPerformed /** * This method is an event handler which is used to import a JSON or text file * chosen by the user and determines its correctness * @param evt */ private void Import_JSONActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Import_JSONActionPerformed file = null; sb.setLength(0); int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { Save_File.setEnabled(true); file = fc.getSelectedFile(); sb.append(util.readFromFile(file)); Panel_Text.setVisible(true); Panel_Hierarchical.setVisible(false); Panel_Table.setVisible(false); Panel_Compare.setVisible(false); Panel_Compare_Upper.setVisible(false); Panel_Connect.setVisible(false); textArea.setText(Initializations.INITSTRING); textArea.setText(sb.toString()); //validation validateDataPanel_text(sb); } }//GEN-LAST:event_Import_JSONActionPerformed /** * This event handler is used to save the JSON objects to the file chosen * @param evt */ private void Save_FileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Save_FileActionPerformed try { file = null; sb.setLength(0); //choosable file extensions FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files (" + ext + ")", ext_array); fc.setFileFilter(filter); //save is OK int returnVal = fc.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { Save_File.setEnabled(true); file = fc.getSelectedFile(); parser.parse(textArea.getText()); //parse data if (json_util.isDataParsed(textArea.getText())) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(fc.getSelectedFile())); writer.write(textArea.getText()); writer.close(); Text_MessageBar.setText(Initializations.JSONSAVESUCCESS); Text_MessageBar.setForeground(Color.GREEN); } catch (FileNotFoundException ex) { Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex); } } else //case for saving error { Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText(Initializations.VALIDATIONERROR); JOptionPane.showMessageDialog(null, Initializations.JSONSAVEERROR + Initializations.VALIDATIONERROR, "Error", JOptionPane.ERROR_MESSAGE); } } } catch (org.json.simple.parser.ParseException pe) //case for incorrect JSON object { Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText(Initializations.JSONSAVEERROR + Initializations.ERRORLINE + json_util.getLineNumber(pe.getPosition(), textArea.getText()) + " - " + pe); } }//GEN-LAST:event_Save_FileActionPerformed private void Text_MessageBarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Text_MessageBarActionPerformed // TODO add your handling code here: }//GEN-LAST:event_Text_MessageBarActionPerformed /** * This event handler is used to load the Compare operation and prepare for * the necessary UI * @param evt */ private void Op_CompareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Op_CompareActionPerformed Text_MessageBar.setText(Initializations.COMPARELOADED); Text_MessageBar.setForeground(Color.GREEN); //create a rsyntaxTextArea textArea1Comp = new RSyntaxTextArea(); textArea1Comp.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON); textArea1Comp.setCodeFoldingEnabled(true); textArea1Comp.setAntiAliasingEnabled(true); RTextScrollPane spComp1 = new RTextScrollPane(textArea1Comp); spComp1.setFoldIndicatorEnabled(true); util.changeTextAreaTheme(textArea1Comp); textArea2Comp = new RSyntaxTextArea(); textArea2Comp.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON); textArea2Comp.setCodeFoldingEnabled(true); textArea2Comp.setAntiAliasingEnabled(true); RTextScrollPane spComp2 = new RTextScrollPane(textArea2Comp); spComp2.setFoldIndicatorEnabled(true); util.changeTextAreaTheme(textArea2Comp); ComparePane.add(spComp1, ComparePane.LEFT, 60); ComparePane.add(spComp2, ComparePane.RIGHT, 60); Panel_Text.setVisible(false); Panel_Hierarchical.setVisible(false); Panel_Table.setVisible(false); Panel_Compare.setVisible(true); Panel_Compare_Upper.setVisible(true); Panel_Connect.setVisible(false); Compare_ResultText.setEditable(false); }//GEN-LAST:event_Op_CompareActionPerformed /** * This event handler will do comparison between the two JSON objects or JSON * Arrays inserted into the text. * @param evt */ private void Compare_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Compare_ButtonActionPerformed //check if empty if (!(textArea1Comp.getText().isEmpty() || textArea2Comp.getText().isEmpty())) { Compare_ResultText.setText(""); //validate both text areas if (json_util.isDataParsed(textArea1Comp.getText()) && json_util.isDataParsed(textArea2Comp.getText())) { Text_MessageBar.setForeground(Color.GREEN); Text_MessageBar.setText(Initializations.JSONFILESUCCESS + Initializations.COMPARESTART); //call compare_result method found in JSONUtilitites try { //Json Node created from document 1 JsonNode jNode1 = mapper.readTree(textArea1Comp.getText()); textArea1Comp.setText(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jNode1)); //JsonNode created for document 2 JsonNode jNode2 = mapper.readTree(textArea2Comp.getText()); textArea2Comp.setText(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jNode2)); //compares the two Json Nodes and outputs the results JsonNode jNodeCompRes = json_util.compareResult(jNode1, jNode2); if (jNodeCompRes == null) { Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText(Initializations.COMPAREFAIL); Compare_ResultText.setText(Initializations.COMPOBJARRERR); } else { Text_MessageBar.setForeground(Color.GREEN); //Compare_ResultText.setText(jNodeCompRes.toString()); Compare_ResultText.setText(Initializations.SUMMARY + json_util.printDiff(jNodeCompRes)); Text_MessageBar.setText(Initializations.COMPARESUCCESS); } } catch (IOException e) { e.printStackTrace(); } // may need to cater for json processing exception } else { Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(this, Initializations.JSONINCORRECTFORMAT, Initializations.VALIDATIONERROR, JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.COMPAREFAIL); } } else { Compare_ResultText.setText(Initializations.COMPAREEMPTY); Text_MessageBar.setText(Initializations.COMPAREEMPTY); Text_MessageBar.setForeground(Color.ORANGE); } }//GEN-LAST:event_Compare_ButtonActionPerformed private void left_obj_to_arrayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_left_obj_to_arrayActionPerformed if (left_obj_to_array.isSelected() && (json_util.isDataParsed(textArea1Comp.getText()))) { try { if (textArea1Comp.getText().isEmpty()) { JOptionPane.showMessageDialog(this, Initializations.CONVERRDOC1, Initializations.CONVERR, JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.DOC1EMPTY); Text_MessageBar.setForeground(Color.ORANGE); left_obj_to_array.setSelected(false); } else { JsonNode jNode1 = mapper.readTree(textArea1Comp.getText()); if (jNode1.isObject()) { jNode1 = json_util.convertJArr(jNode1); textArea1Comp.setText(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jNode1)); } else { Text_MessageBar.setForeground(Color.ORANGE); Text_MessageBar.setText(Initializations.OBJCONVERTED); } } } catch (IOException e) { e.printStackTrace(); } } }//GEN-LAST:event_left_obj_to_arrayActionPerformed //method is used to validate data in Panel_text when a file is imported from file or when connected to the database to view collection data private void validateDataPanel_text(StringBuilder sb) { if (json_util.isValid(sb.toString())) { // json_util.isDataParsed(textArea.getText()); if (json_util.isDataParsed(sb.toString())) { Text_MessageBar.setText(Initializations.JSONFILESUCCESS); Text_MessageBar.setForeground(Color.GREEN); } else { Text_MessageBar.setText(Initializations.JSONINCORRECTFORMAT); Text_MessageBar.setForeground(Color.ORANGE); JOptionPane.showMessageDialog(null, Initializations.JSONINCORRECTFORMAT, "Error", JOptionPane.ERROR_MESSAGE); } } else { sb.setLength(0); JOptionPane.showMessageDialog(this, Initializations.JSONINCORRECTFORMAT, Initializations.VALIDATIONERROR, JOptionPane.ERROR_MESSAGE); try { Object obj = parser.parse(sb.toString()); } catch (org.json.simple.parser.ParseException pe) { Text_MessageBar.setText(Initializations.ERRORLINE + json_util.getLineNumber(pe.getPosition(), textArea.getText()) + " - " + pe); Text_MessageBar.setForeground(Color.ORANGE); } } } private void right_obj_to_arrayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_right_obj_to_arrayActionPerformed if (right_obj_to_array.isSelected() && (json_util.isDataParsed(textArea1Comp.getText()))) { try { if (textArea2Comp.getText().isEmpty()) { JOptionPane.showMessageDialog(this, Initializations.CONVERRDOC2, Initializations.CONVERR, JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText("Document 2 is empty!"); Text_MessageBar.setForeground(Color.RED); right_obj_to_array.setSelected(false); } else { JsonNode jNode2 = mapper.readTree(textArea2Comp.getText()); if (jNode2.isObject()) { jNode2 = json_util.convertJArr(jNode2); textArea2Comp.setText(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jNode2)); } else { Text_MessageBar.setText("Object is already converted to array!"); Text_MessageBar.setForeground(Color.ORANGE); } } } catch (IOException e) { e.printStackTrace(); } } }//GEN-LAST:event_right_obj_to_arrayActionPerformed private void connect_DBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connect_DBActionPerformed Panel_Text.setVisible(false); Panel_Hierarchical.setVisible(false); Panel_Table.setVisible(false); Panel_Compare.setVisible(false); Panel_Compare_Upper.setVisible(false); Panel_Connect.setVisible(true); Text_MessageBar.setText(Initializations.WAITINGFORCONNECTION); Text_MessageBar.setForeground(Color.ORANGE); if (dbcon.isConnectionSuccess()) { if (dbcon.checkDatabaseConnection()) { int result = JOptionPane.showConfirmDialog(null, "A connection to MongoDB" + "already exists. Are you sure you want to disconnect and open " + "a different connection?", "Confirm", JOptionPane.YES_NO_OPTION); if (result == JOptionPane.YES_OPTION) { dbcon.closeConnection(); connect(); } } else { Text_MessageBar.setText(Initializations.MONGOSERVERERROR); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.MONGOSERVERERROR, "Error", JOptionPane.ERROR_MESSAGE); } } else { connect(); } }//GEN-LAST:event_connect_DBActionPerformed private void Import_FileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Import_FileActionPerformed Text_MessageBar.setText(Initializations.WAITINGFORCONNECTION); Text_MessageBar.setForeground(Color.ORANGE); if (dbcon.isConnectionSuccess()) { if (dbcon.checkDatabaseConnection()) { List<String> connectionStrings = dbcon.getAllCollectionsLessSystem(); connectionStrings.add(0, "none"); ImportFileDialog dlg_import = new ImportFileDialog(null, connectionStrings); dlg_import.setVisible(true); if (dlg_import.isToImport()) { String collectionToImport = dlg_import.collectionToImport(); String typeToImport = dlg_import.typeToImport(); String locToImport = dlg_import.locToImport(); StringBuilder sb = new StringBuilder(); if (locToImport != null && !locToImport.isEmpty()) { //Converting JSON to CSV and export it to file is done in the dbcon.export method //Exporting JSON on the other hand, is done here if ("CSV".equals(typeToImport)) { try { if ("none".equals(collectionToImport)) { textArea.setText(""); Panel_Text.setVisible(true); JsonNode jNode = mapper.readTree( dbcon.import_CSV(collectionToImport, typeToImport, locToImport)); textArea.append( mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jNode)); Text_MessageBar.setForeground(Color.GREEN); Text_MessageBar.setText("Import to " + typeToImport + " has been successful."); } else if (connectionStrings.contains(collectionToImport)) { sb = dbcon.getCollectionData(collectionToImport); if (sb != null) { Panel_Text.setVisible(true); JsonNode jNode1; try { jNode1 = mapper.readTree(sb.toString()); textArea.setText(mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(jNode1)); JsonNode jNode = mapper.readTree(dbcon.import_CSV(collectionToImport, typeToImport, locToImport)); textArea.append(mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(jNode)); } catch (IOException ex) { Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex); } Text_MessageBar.setText(Initializations.INITSTRING); // textArea.setText(sb.toString()); validateDataPanel_text(sb); } else { Panel_Text.setVisible(false); Text_MessageBar.setText(Initializations.SYSTEMCOLL); Text_MessageBar.setForeground(Color.RED); } } } catch (IOException ex) { Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex); } } else { Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText("Import to " + typeToImport + " has been unsuccessful."); } } else { JOptionPane.showMessageDialog(this, Initializations.FILENOTFOUND, Initializations.IMPORTERROR, JOptionPane.ERROR_MESSAGE); Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText("Import to " + typeToImport + " has been unsuccessful."); } } else { Text_MessageBar.setForeground(Color.ORANGE); Text_MessageBar.setText("Export action has been aborted"); } } else { JOptionPane.showMessageDialog(null, Initializations.MONGOSERVERERROR, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.MONGOSERVERERROR); Text_MessageBar.setForeground(Color.RED); } } else { // JOptionPane.showMessageDialog(this, Initializations.FILENOTFOUND , Initializations.IMPORTERROR , JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, Initializations.NODBCONNECTION, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.NODBCONNECTION); Text_MessageBar.setForeground(Color.RED); } }//GEN-LAST:event_Import_FileActionPerformed private void Save_MongoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Save_MongoActionPerformed // Text_MessageBar.setText(Initializations.WAITINGFORCONNECTION); // Text_MessageBar.setForeground(Color.ORANGE); if (dbcon.isConnectionSuccess()) { if (dbcon.checkDatabaseConnection()) { if (indexOfCurrentCollection != 0) { try { file = null; sb.setLength(0); if (dbcon.isConnectionSuccess() && textArea.getText() != null) { Save_Mongo.setEnabled(true); parser.parse(textArea.getText()); String json = textArea.getText(); if (dbcon.saveColl(json)) { Text_MessageBar.setText(Initializations.JSONSAVESUCCESS); Text_MessageBar.setForeground(Color.GREEN); } else { Text_MessageBar.setText(Initializations.VALIDATIONERROR); Text_MessageBar.setForeground(Color.RED); } } } catch (org.json.simple.parser.ParseException pe) { Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText(Initializations.ERRORLINE + json_util.getLineNumber(pe.getPosition(), textArea.getText()) + " - " + pe); } } else { Text_MessageBar.setText(Initializations.DBACTIONNOCOLLECTION); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.DBACTIONNOCOLLECTION, "Error", JOptionPane.ERROR_MESSAGE); } } else { JOptionPane.showMessageDialog(null, Initializations.MONGOSERVERERROR, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.MONGOSERVERERROR); Text_MessageBar.setForeground(Color.RED); } } else { JOptionPane.showMessageDialog(null, Initializations.NODBCONNECTION, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.NODBCONNECTION); Text_MessageBar.setForeground(Color.RED); } }//GEN-LAST:event_Save_MongoActionPerformed private void Export_FileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Export_FileActionPerformed Text_MessageBar.setText(Initializations.WAITINGFORCONNECTION); Text_MessageBar.setForeground(Color.ORANGE); if (dbcon.isConnectionSuccess()) { if (dbcon.checkDatabaseConnection()) { List<String> connectionStrings = dbcon.getAllCollectionsLessSystem(); ExportFileDialog dlg_export = new ExportFileDialog(null, connectionStrings); dlg_export.setVisible(true); if (dlg_export.isToExport()) { String collectionToExport = dlg_export.collectionToExport(); String typeToExport = dlg_export.typeToExport(); String locToExport = dlg_export.locToExport(); if (locToExport != null && !locToExport.trim().equals("")) { //Converting JSON to CSV and export it to file is done in the dbcon.export method //Exporting JSON on the other hand, is done here if (typeToExport.equals("CSV") || typeToExport.equals("TSV")) { if (dbcon.export(collectionToExport, typeToExport, locToExport).equals("true")) { Text_MessageBar.setForeground(Color.GREEN); Text_MessageBar.setText("Export to " + typeToExport + " has been successful."); } else { Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText("Export to " + typeToExport + " has been unsuccessful."); } } else { String dataToExport = dbcon.export(collectionToExport, typeToExport, locToExport); if (util.writeToFile(locToExport, dataToExport)) { Text_MessageBar.setForeground(Color.GREEN); Text_MessageBar.setText("Export to " + typeToExport + " has been successful."); } else { Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText("Export to " + typeToExport + " has been unsuccessful."); } } } else { JOptionPane.showMessageDialog(this, Initializations.FILENOTFOUND, Initializations.EXPORTERROR, JOptionPane.ERROR_MESSAGE); Text_MessageBar.setForeground(Color.RED); Text_MessageBar.setText("Export to " + typeToExport + " has been unsuccessful."); } } else { Text_MessageBar.setForeground(Color.ORANGE); Text_MessageBar.setText("Export action has been aborted"); } } else { JOptionPane.showMessageDialog(null, Initializations.MONGOSERVERERROR, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.MONGOSERVERERROR); Text_MessageBar.setForeground(Color.RED); } } else { JOptionPane.showMessageDialog(null, Initializations.NODBCONNECTION, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.NODBCONNECTION); Text_MessageBar.setForeground(Color.RED); } }//GEN-LAST:event_Export_FileActionPerformed private void Op_RefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Op_RefreshActionPerformed Text_MessageBar.setText(Initializations.WAITINGFORCONNECTION); Text_MessageBar.setForeground(Color.ORANGE); if (dbcon.isConnectionSuccess()) { if (dbcon.checkDatabaseConnection()) { if (indexOfCurrentCollection != 0) { String loc = tp.getPathComponent(indexOfCurrentCollection).toString(); if (dbcon.checkSystemColl(loc)) { String new_data = dbcon.getCollectionData(loc).toString(); JsonNode jNode1; try { jNode1 = mapper.readTree(new_data); textArea.setText(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jNode1)); } catch (IOException ex) { Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex); } } } else { Text_MessageBar.setText(Initializations.DBACTIONNOCOLLECTION); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.DBACTIONNOCOLLECTION, "Error", JOptionPane.ERROR_MESSAGE); } } else { JOptionPane.showMessageDialog(null, Initializations.MONGOSERVERERROR, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.MONGOSERVERERROR); Text_MessageBar.setForeground(Color.RED); } } else { Text_MessageBar.setText(Initializations.SYSTEMCOLLNOREFRESH); Text_MessageBar.setForeground(Color.RED); } }//GEN-LAST:event_Op_RefreshActionPerformed private void file_closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_file_closeActionPerformed if (JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) dispose(); }//GEN-LAST:event_file_closeActionPerformed private void Op_DBActionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Op_DBActionsActionPerformed Text_MessageBar.setText(Initializations.WAITINGFORCONNECTION); Text_MessageBar.setForeground(Color.ORANGE); if (dbcon.isConnectionSuccess()) { if (dbcon.checkDatabaseConnection()) { if (indexOfCurrentCollection != 0) { ActionOnDB dbAction = new ActionOnDB(dbcon); Text_MessageBar.setText(Initializations.DBACTIONSSUCCESS); Text_MessageBar.setForeground(Color.GREEN); dbAction.setVisible(true); } else { Text_MessageBar.setText(Initializations.DBACTIONNOCOLLECTION); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.DBACTIONNOCOLLECTION, "Error", JOptionPane.ERROR_MESSAGE); } } else { JOptionPane.showMessageDialog(null, Initializations.MONGOSERVERERROR, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.MONGOSERVERERROR); Text_MessageBar.setForeground(Color.RED); } } else { JOptionPane.showMessageDialog(null, Initializations.NODBCONNECTION, "Error", JOptionPane.ERROR_MESSAGE); Text_MessageBar.setText(Initializations.NODBCONNECTION); Text_MessageBar.setForeground(Color.RED); } }//GEN-LAST:event_Op_DBActionsActionPerformed private void Op_ValidateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Op_ValidateActionPerformed JSONParser parser = new JSONParser(); String text = null; String object = ""; try { if (!Panel_Text.isVisible() && !Panel_Compare.isVisible()) { Text_MessageBar.setText(Initializations.VALIDATEEMPTY); Text_MessageBar.setForeground(Color.ORANGE); } else if (Panel_Text.isVisible()) { text = textArea.getText(); if (textArea.getText().isEmpty()) { Text_MessageBar.setText(Initializations.VALIDATEEMPTY); Text_MessageBar.setForeground(Color.ORANGE); } else parser.parse(textArea.getText()); } else if (Panel_Compare.isVisible()) { if (textArea1Comp.getText().isEmpty() && textArea1Comp.getText().isEmpty()) { Text_MessageBar.setText(Initializations.VALIDATEEMPTY); Text_MessageBar.setForeground(Color.ORANGE); } else { if (textArea1Comp.getText().isEmpty()) { Text_MessageBar .setText(Initializations.VALIDATEEMPTY + Initializations.VALIDATIONTEXTAREA1); Text_MessageBar.setForeground(Color.ORANGE); } else { text = textArea1Comp.getText(); object = "" + Initializations.VALIDATIONTEXTAREA1; text = textArea1Comp.getText(); parser.parse(textArea1Comp.getText()); } if (textArea2Comp.getText().isEmpty()) { Text_MessageBar .setText(Initializations.VALIDATEEMPTY + Initializations.VALIDATIONTEXTAREA2); Text_MessageBar.setForeground(Color.ORANGE); } else { text = textArea2Comp.getText(); object = "" + Initializations.VALIDATIONTEXTAREA2; text = textArea2Comp.getText(); parser.parse(textArea2Comp.getText()); } } } } catch (org.json.simple.parser.ParseException pe) { Text_MessageBar.setText(Initializations.VALIDATIONERROR + object + " - " + Initializations.ERRORLINE + json_util.getLineNumber(pe.getPosition(), text) + " - " + pe); Text_MessageBar.setForeground(Color.RED); } if (json_util.isDataParsed(text)) { Text_MessageBar.setText(Initializations.VALIDATIONSUCCESS); Text_MessageBar.setForeground(Color.GREEN); JOptionPane.showMessageDialog(null, Initializations.VALIDATIONSUCCESS, "Success", JOptionPane.INFORMATION_MESSAGE); } else { Text_MessageBar.setText(Initializations.VALIDATIONERROR); Text_MessageBar.setForeground(Color.RED); JOptionPane.showMessageDialog(null, Initializations.VALIDATIONERROR, "Error", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_Op_ValidateActionPerformed public void connect() { String user; String pass; String dbname; String serveradd; int port; LoginDialog dlg_login = new LoginDialog(null); dlg_login.setVisible(true); //If user chose login and not cancel option on dialog box if (dlg_login.isToLogin()) { Text_MessageBar.setText(Initializations.DBATTEMPTING); Text_MessageBar.setForeground(Color.ORANGE); user = dlg_login.getUsername(); pass = dlg_login.getPassword(); dbname = dlg_login.getDatabase(); serveradd = dlg_login.getServerAddr(); port = dlg_login.getPort(); if (dbcon.connect(user, pass, dbname, serveradd, port)) { DefaultTreeModel defTableMod = dbcon.buildDBTree(); if (defTableMod != null && dbcon.isConnectionSuccess()) { jTree1.setModel(defTableMod); Text_MessageBar.setText(Initializations.DBCONNSUCCESS); Text_MessageBar.setForeground(Color.GREEN); Menu_Collections.setEnabled(true); //load the data of collection in panel_text on double click jTree1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent me) { if (me.getButton() == MouseEvent.BUTTON1) { if (me.getClickCount() == 2) { //get the path of the mouse click ex:[localhost,test,testData] Op_Refresh.setEnabled(true); tp = jTree1.getPathForLocation(me.getX(), me.getY()); if (tp != null) { List<String> coll_db = dbcon.getAllCollections(); int cnt = tp.getPathCount(); for (int i = 0; i < cnt; i++) { //if one of the collection matches the coll that was clicked by the user load data if (coll_db.contains(tp.getPathComponent(i).toString())) { indexOfCurrentCollection = i; sb = dbcon.getCollectionData(tp.getPathComponent(i).toString()); if (sb != null) { Panel_Text.setVisible(true); JsonNode jNode1; try { jNode1 = mapper.readTree(sb.toString()); textArea.setText(mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(jNode1)); } catch (IOException ex) { Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex); } Text_MessageBar.setText(Initializations.INITSTRING); // textArea.setText(sb.toString()); validateDataPanel_text(sb); /* if (json_util.isValid(sb.toString())) { json_util.isDataParsed(textArea.getText()); Text_MessageBar.setText(Initializations.JSONFILESUCCESS); } else { sb.setLength(0); //JOptionPane.showMessageDialog(this, Initializations.JSONINCORRECTFORMAT , Initializations.VALIDATIONERROR , JOptionPane.ERROR_MESSAGE); try { Object obj = parser.parse(sb.toString()); } catch(org.json.simple.parser.ParseException pe) { Text_MessageBar.setText(Initializations.ERRORLINE + json_util.getLineNumber(pe.getPosition(), textArea.getText()) + " - " + pe); } } */ } else { Panel_Text.setVisible(false); Text_MessageBar.setText(Initializations.SYSTEMCOLL); Text_MessageBar.setForeground(Color.RED); } } } } } } } }); } else { jTree1.setModel(null); Text_MessageBar.setText(Initializations.DBCONNFAIL); Text_MessageBar.setForeground(Color.RED); } } else { jTree1.setModel(null); Text_MessageBar.setText(Initializations.DBCONNFAIL); Text_MessageBar.setForeground(Color.RED); } } else { jTree1.setModel(null); Text_MessageBar.setText(Initializations.DBCONNFAIL); Text_MessageBar.setForeground(Color.RED); } } public void setImageIcon() { ImageIcon leafIcon = createImageIcon("/resources/json_node.png"); if (leafIcon != null) { DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); renderer.setLeafIcon(leafIcon); jTreeHierarchicalJson.setCellRenderer(renderer); } } private ImageIcon createImageIcon(String path) { java.net.URL imgUrl = getClass().getResource(path); if (imgUrl != null) { return new ImageIcon(imgUrl); } else { Text_MessageBar.setText(Initializations.LEAFICON); return null; } } /** * @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(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainForm.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 MainForm().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JSplitPane ComparePane; private javax.swing.JButton Compare_Button; private javax.swing.JTextArea Compare_ResultText; private javax.swing.JMenuItem Export_File; private javax.swing.JMenuItem Import_File; private javax.swing.JMenuItem Import_JSON; private javax.swing.JMenu Menu_Collections; private javax.swing.JMenu Menu_File; private javax.swing.JMenu Menu_Operations; private javax.swing.JMenu Menu_Views; private javax.swing.JMenuItem Op_Compare; private javax.swing.JMenuItem Op_DBActions; private javax.swing.JMenuItem Op_Refresh; private javax.swing.JMenuItem Op_Validate; private javax.swing.JPanel Panel_Compare; private javax.swing.JPanel Panel_Compare_Upper; private javax.swing.JPanel Panel_Connect; private javax.swing.JPanel Panel_Connections; private javax.swing.JPanel Panel_Hierarchical; private javax.swing.JPanel Panel_Table; private javax.swing.JPanel Panel_Text; private javax.swing.JPanel Panel_Views; private javax.swing.JMenuItem Save_File; private javax.swing.JMenuItem Save_Mongo; private javax.swing.JTable Table_JSON; private javax.swing.JTextField Text_MessageBar; private javax.swing.JMenuItem View_Hierarchical; private javax.swing.JMenuItem View_Table; private javax.swing.JMenuItem View_Text; private javax.swing.JMenuItem connect_DB; private javax.swing.JMenuItem file_close; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane4; private javax.swing.JScrollPane jScrollPane6; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JTree jTree1; public javax.swing.JTree jTreeHierarchicalJson; private javax.swing.JLabel left_label; private javax.swing.JCheckBox left_obj_to_array; private javax.swing.JLabel right_label; private javax.swing.JCheckBox right_obj_to_array; // End of variables declaration//GEN-END:variables }