Search code examples
javaswingdesktop-application

Get the count of code complexity due to type of control structures line by line into a table view


I have developed a desktop application using java swing using the netbeans IDE. The purpose of it is to measure the complexity of a program statement due to the type of control structures. For a program statement with "if", "for", "while" and "catch", a weight of 1, 2, 2, and 1 are assigned respectively. Additionally, a weight of "n" is assigned for a program statement with a "switch" statement with "n" number of cases. All other statements are assigned with a weight of zero.

I have implemented the solution for the above problem already, and I need to demonstrate the complexity "of each program statement" in a tabular format. I have used a jTable for that purpose with the column names "Line no", "Program statement" and "Complexity count".

Though I have taken the total count of complexity, I have no idea about getting the count line by line and display it in the jTable. The complexity count relevant for each program statement should be displayed against each line number and the statement.

Here is my code. https://github.com/nirmaniPathiranage/Complexity-measuring-tool/blob/master/src/spm/FilesDemo.java

I have implemented the function of loading table data from the line 733 onwards. (action performed on jButton11).

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed{}

I expect the output of complexity for each program statement against each line number and program statement in a table view.

I can't seem to figure this out. Any help would be appreciated.


Solution

  • A possible issue i can see with implementing a jTable to show complexities for each line in a program is not knowing the number of lines before hand. But this too can be solved easily with a naive approach of using an ArrayList<> and a TableModel as in below.

    //Names of the columns
    String columns[] = {"Line no","Program statement","Complexity count"};
    
    //Table model, initiate with 0 columns
    DefaultTableModel tableModel = new DefaultTableModel(columns, 0);
    
    JTable table = new JTable(tableModel);
    

    To make things clearer, implementing a POJO class for contents to be showed in a table row would be ideal.

    public class TableRow {
       private int lineNo;
       private String statement;
       private int cmplxCount;
    
       //You may implement the getters and setters as well
    }
    

    While the program iterates through each and every line calculating the complexity, make sure to create a new TableRow object and necessary data and add them to an ArrayList

    ArrayList<TableRow> list = new ArrayList<TableRow>();
    //Create an object each time a line is iterated for complexity calculations.
    TableRow newRow = new TableRow(4,"for-loop",1);
    list.add(newRow);
    

    After the iteration is over, add those data in the ArrayList to an Object array and pass them to the jTable.

    for (int i = 0; i < list.size(); i++){
       int lineNo= list.get(i).getLineNo();
       String statment = list.get(i).getStatement();
       int cmplxCount= list.get(i).getCmplxCount();
    
       Object[] data = {lineNo, statement , cmplxCount};
       tableModel.add(data);
    }