1. package GUI;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.net.*;
  6. import java.io.*;
  7. import java.util.*;
  8.  
  9. public class GuiMaintain extends javax.swing.JFrame
  10. {
  11.  
  12.     public DefaultListModel ListModel = new DefaultListModel();
  13.     public ArrayList editRecord = new ArrayList();
  14.  
  15.     public GuiMaintain()
  16.     {
  17.         initComponents();
  18.         //maintainLoad();
  19.         jList1.setModel(ListModel);
  20.     }
  21.  
  22.     @SuppressWarnings("unchecked")
  23.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  24.     private void initComponents() {
  25.  
  26.         btnEdit = new javax.swing.JButton();
  27.         jScrollPane1 = new javax.swing.JScrollPane();
  28.         jList1 = new javax.swing.JList();
  29.  
  30.         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  31.         setTitle("Maintain Documents");
  32.  
  33.         btnEdit.setText("Edit");
  34.         btnEdit.addActionListener(new java.awt.event.ActionListener() {
  35.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  36.                 btnEditActionPerformed(evt);
  37.             }
  38.         });
  39.  
  40.         jScrollPane1.setViewportView(jList1);
  41.  
  42.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  43.         getContentPane().setLayout(layout);
  44.         layout.setHorizontalGroup(
  45.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  46.             .addGroup(layout.createSequentialGroup()
  47.                 .addContainerGap(719, Short.MAX_VALUE)
  48.                 .addComponent(btnEdit)
  49.                 .addContainerGap())
  50.             .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
  51.         );
  52.         layout.setVerticalGroup(
  53.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  54.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  55.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
  56.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  57.                 .addComponent(btnEdit)
  58.                 .addGap(6, 6, 6))
  59.         );
  60.  
  61.         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  62.         setBounds((screenSize.width-796)/2, (screenSize.height-493)/2, 796, 493);
  63.     }// </editor-fold>
  64.  
  65. private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
  66.     if (jList1.getSelectedIndex() != -1)
  67.     {
  68.         Functionality.Documents documents = new Functionality.Documents();
  69.         documents.setIndex(jList1.getSelectedIndex());
  70.        
  71.         GuiEdit editForm = new GuiEdit();
  72.         editForm.setVisible(true);
  73.        
  74.         String splittedRecord[] = editRecord.get(documents.getIndex()).toString().split("\\|");
  75.         editForm.txtTitle.setText(splittedRecord[0]);
  76.         editForm.txtSummary.setText(splittedRecord[1]);
  77.         editForm.txtPath.setText(splittedRecord[3]);
  78.         editForm.txtRefNo.setText(splittedRecord[2]);
  79.     }
  80. }                                      
  81.  
  82.     public void maintainLoad()
  83.     {
  84.         try
  85.         {
  86.             Scanner getRecords;
  87.             URL url = getClass().getResource("/Information/records.txt");
  88.             getRecords = new Scanner(new File(url.getPath()));
  89.  
  90.             for (int a = 0; getRecords.hasNextLine(); a++)
  91.             {
  92.                 String line = getRecords.nextLine();
  93.                 String splittedText[] = line.split("\\|");
  94.                 if (splittedText.length == 4)
  95.                 {
  96.                     editRecord.add(a, line);
  97.                     ListModel.add(ListModel.size(), "Title: " + splittedText[0] + " - " + "Summary: " + splittedText[1]
  98.                             + " - " + "Ref no: " + splittedText[2] + " - " + "Path: " + splittedText[3]);
  99.                 }
  100.             }
  101.             getRecords.close();
  102.         } catch (IOException e)
  103.         {
  104.             System.out.println("Error");
  105.         }
  106.     }
  107.  
  108.     public void editArrayUpdate(String title, String summary, String refNo, String path)
  109.     {
  110.         try
  111.         {
  112.             URL url = getClass().getResource("/Information/records.txt");
  113.             Functionality.Documents documents = new Functionality.Documents();
  114.             //re-load empty readUpdate arraylist with records and add the one that needed to be implemented.
  115.             Scanner readUpdate;
  116.             readUpdate = new Scanner(new File(url.getPath()));
  117.             for (int i = 0; readUpdate.hasNextLine(); i++)
  118.             {
  119.                 if(i == documents.getIndex())
  120.                 {
  121.                 editRecord.add(documents.getIndex(), title + "|" + summary + "|" + refNo + "|" + path);
  122.                 }
  123.                 else
  124.                 {
  125.                    editRecord.add(readUpdate.nextLine());
  126.                 }
  127.             }
  128.             readUpdate.close();
  129.  
  130.             //update text file .....
  131.             Formatter writeNew = new Formatter(url.getPath());
  132.             for (int i = 0; i < editRecord.size(); i++)
  133.             {
  134.                 writeNew.format(editRecord.get(i).toString() + "%n");
  135.             }
  136.             writeNew.close();
  137.            
  138.             //update list...
  139.             Scanner Update;
  140.             Update = new Scanner(new File(url.getPath()));
  141.             for (int i = 0; Update.hasNextLine(); i++)
  142.             {
  143.                 String line = Update.nextLine();
  144.                 String splittedText[] = line.split("\\|");
  145.                 ListModel.add(i, "Title: " + splittedText[0] + " - " + "Summary: " +        splittedText[1] + " - " + "Ref no: " + splittedText[2] + " - " + "Path: " + splittedText[3]);
  146.             }
  147.             Update.close();
  148.         } catch (Exception e)
  149.         {
  150.             System.out.println(e.getMessage());
  151.         }
  152.  
  153.  
  154.  
  155.     }
  156.     // Variables declaration - do not modify
  157.     private javax.swing.JButton btnEdit;
  158.     public javax.swing.JList jList1;
  159.     private javax.swing.JScrollPane jScrollPane1;
  160.     // End of variables declaration
  161. }