ExpansionFrame.java Source code

Java tutorial

Introduction

Here is the source code for ExpansionFrame.java

Source

import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.IOException;
import static java.lang.System.exit;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;
/*
 * 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.
 */

/**
 *
 * @author Makli
 */
public final class ExpansionFrame extends javax.swing.JFrame {

    /**
     * Creates new form ExpansionFrame
     * @param DocIDModel
     * @param SummaryModel
     */

    DefaultListModel<String> SelectedDocIDListModel = new DefaultListModel<>();
    DefaultListModel<String> SummaryListModel = new DefaultListModel<>();
    DefaultListModel<String> TermListModel = new DefaultListModel<>();
    Directory myIndex;
    PorterStemAnalyzer myAnalyzer;

    public ExpansionFrame(DefaultListModel<String> DocIDModel, DefaultListModel<String> SummaryModel,
            DefaultListModel<String> TermModel) throws IOException, ParseException {
        initComponents();
        AddBtn.setEnabled(false);
        RemoveBtn.setEnabled(false);
        SetIcon();
        ResultsList.setModel(DocIDModel);
        TermsList.setModel(TermModel);

        for (int i = 0; i < SummaryModel.size(); i++) {
            SummaryListModel.addElement(SummaryModel.elementAt(i));
        }

        // Get directory in order to get term frequencies
        myAnalyzer = new PorterStemAnalyzer();
        GUIFrame createIndex = new GUIFrame();
        myIndex = createIndex.InitializeIndex(myIndex, myAnalyzer);
    }

    private void SetIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("IRicon.png")));
    }

    /**
     * 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();
        ResultScrollPane = new javax.swing.JScrollPane();
        ResultsList = new javax.swing.JList();
        ChoiceScrollPane = new javax.swing.JScrollPane();
        ChoicesList = new javax.swing.JList();
        AddBtn = new javax.swing.JButton();
        RemoveBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        TermScrollPane = new javax.swing.JScrollPane();
        TermsList = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        ExpansionBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Query Expansion");
        setAlwaysOnTop(true);
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));

        ResultsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ResultsListMouseClicked(evt);
            }
        });
        ResultScrollPane.setViewportView(ResultsList);

        ChoicesList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ChoicesListMouseClicked(evt);
            }
        });
        ChoiceScrollPane.setViewportView(ChoicesList);

        AddBtn.setText("Add");
        AddBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddBtnActionPerformed(evt);
            }
        });

        RemoveBtn.setText("Remove");
        RemoveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveBtnActionPerformed(evt);
            }
        });

        jLabel1.setText("Results");

        jLabel2.setText("Choices");

        TermScrollPane.setViewportView(TermsList);

        jLabel3.setText("Terms");

        ExpansionBtn.setText("Expand");
        ExpansionBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExpansionBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ResultScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 177,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(
                                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(RemoveBtn, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(
                                                AddBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ChoiceScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 177,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(TermScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 177,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap())
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(231, 231, 231)
                                        .addComponent(ExpansionBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 108,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(234, 234, 234))))
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(70, 70, 70)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(220, 220, 220)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 54,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3).addGap(82, 82, 82)));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel1Layout
                                        .createSequentialGroup()
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(AddBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 64,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(RemoveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 64,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(133, 133, 133))
                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(50, 50, 50)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel1).addComponent(jLabel2)))
                                .addGroup(
                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                        jPanel1Layout.createSequentialGroup().addContainerGap()
                                                .addComponent(jLabel3)))
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(TermScrollPane).addComponent(ChoiceScrollPane)
                                                        .addComponent(ResultScrollPane))
                                                .addGap(60, 60, 60))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout
                                                .createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        129, Short.MAX_VALUE)
                                                .addComponent(ExpansionBtn, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(186, 186, 186)))));

        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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AddBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddBtnActionPerformed
        String selectedID = ResultsList.getSelectedValue().toString();
        boolean IDexists = false;

        for (int i = 0; i < SelectedDocIDListModel.size(); i++) {
            if (selectedID.equals(SelectedDocIDListModel.elementAt(i))) {
                IDexists = true;
                break;
            }
        }

        if (!IDexists) {
            SelectedDocIDListModel.addElement(selectedID);
            ChoicesList.setModel(SelectedDocIDListModel);
        }

        RemoveBtn.setEnabled(false);
    }//GEN-LAST:event_AddBtnActionPerformed

    private void RemoveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveBtnActionPerformed
        SelectedDocIDListModel.removeElementAt(ChoicesList.getSelectedIndex());
        ChoicesList.setModel(SelectedDocIDListModel);

        RemoveBtn.setEnabled(false);
    }//GEN-LAST:event_RemoveBtnActionPerformed

    private void ExpansionBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExpansionBtnActionPerformed
        String vocabulary = "";
        DefaultListModel<String> TermsListModel = new DefaultListModel<>();

        for (int docIndex = 0; docIndex < ChoicesList.getModel().getSize(); docIndex++) {
            int currentIndex = 0;
            boolean exists = false;

            for (int resultDocIndex = 0; resultDocIndex < ResultsList.getModel().getSize(); resultDocIndex++) {
                if (ResultsList.getModel().getElementAt(resultDocIndex)
                        .equals(ChoicesList.getModel().getElementAt(docIndex))) {
                    currentIndex = resultDocIndex;
                    exists = true;
                    break;
                }
            }

            if (exists) {
                Set<Term> results;

                IndexReader indoReader;
                try {
                    indoReader = DirectoryReader.open(myIndex);

                    QueryParser qp = new QueryParser(Version.LUCENE_30, "summary", myAnalyzer);
                    Query query;

                    query = qp.parse(SummaryListModel.elementAt(currentIndex));

                    query.rewrite(indoReader);
                    results = new HashSet<>();
                    query.extractTerms(results);

                    Iterator<Term> iterator = results.iterator();
                    while (iterator.hasNext()) {
                        String term = iterator.next().toString();
                        vocabulary += term.substring(8) + ";";
                    }

                    vocabulary += "!";

                } catch (IOException | ParseException ex) {
                    Logger.getLogger(ExpansionFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                System.out.println("Error!");
            }
        }

        String term = "";

        for (int i = 0; i < vocabulary.length(); i++) {
            if (vocabulary.charAt(i) != ';' && vocabulary.charAt(i) != '!') {
                term += vocabulary.charAt(i);
            } else {
                if (term.length() != 0) {
                    boolean exists = false;
                    for (int k = 0; k < TermsListModel.size(); k++) {
                        if (term.equals(TermsListModel.elementAt(k))) {
                            exists = true;
                            break;
                        }
                    }

                    if (!exists) {
                        TermsListModel.addElement(term);
                        term = "";
                    }
                }
            }
        }

        term = "";

        int[][] termsFreqArray = new int[TermsListModel.size()][ChoicesList.getModel().getSize()];

        for (int i = 0; i < TermsListModel.size(); i++) {
            for (int j = 0; j < ChoicesList.getModel().getSize(); j++) {
                termsFreqArray[i][j] = 0;
            }
        }

        int docIndex = 0;
        for (int i = 0; i < vocabulary.length(); i++) {
            if (vocabulary.charAt(i) != ';' && vocabulary.charAt(i) != '!') {
                term += vocabulary.charAt(i);
            } else {
                if (vocabulary.charAt(i) == '!') {
                    docIndex++;
                }

                if (term.length() != 0) {
                    int index = 0;
                    for (int k = 0; k < TermsListModel.size(); k++) {
                        if (term.equals(TermsListModel.elementAt(k))) {
                            index = k;
                            break;
                        }
                    }

                    termsFreqArray[index][docIndex]++;

                    term = "";
                }
            }
        }

        /*
        for (int i = 0; i < TermsListModel.size(); i++) {
        System.out.print(TermsListModel.elementAt(i) + " : ");
        for (int j = 0; j < ChoicesList.getModel().getSize(); j++) {
            System.out.print(termsFreqArray[i][j] + ";");
        }
        System.out.println();
        }
        */
    }//GEN-LAST:event_ExpansionBtnActionPerformed

    private void ResultsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ResultsListMouseClicked
        if (ResultsList.getModel().getSize() > 0) {
            AddBtn.setEnabled(true);
        }
    }//GEN-LAST:event_ResultsListMouseClicked

    private void ChoicesListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ChoicesListMouseClicked
        if (ChoicesList.getModel().getSize() > 0) {
            RemoveBtn.setEnabled(true);
        }
    }//GEN-LAST:event_ChoicesListMouseClicked

    /**
     * @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(ExpansionFrame.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ExpansionFrame.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ExpansionFrame.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ExpansionFrame.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        final DefaultListModel<String> DocIDModel = null;
        final DefaultListModel<String> SummaryModel = null;
        final DefaultListModel<String> TermModel = null;

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new ExpansionFrame(DocIDModel, SummaryModel, TermModel).setVisible(true);
                } catch (IOException | ParseException ex) {
                    Logger.getLogger(ExpansionFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddBtn;
    private javax.swing.JScrollPane ChoiceScrollPane;
    private javax.swing.JList ChoicesList;
    private javax.swing.JButton ExpansionBtn;
    private javax.swing.JButton RemoveBtn;
    private javax.swing.JScrollPane ResultScrollPane;
    private javax.swing.JList ResultsList;
    private javax.swing.JScrollPane TermScrollPane;
    private javax.swing.JList TermsList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}