di.uniba.it.tee2.gui.TEEgui.java Source code

Java tutorial

Introduction

Here is the source code for di.uniba.it.tee2.gui.TEEgui.java

Source

/**
 * Copyright (c) 2014, the TEE2 AUTHORS.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the University of Bari nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007
 *
 */
package di.uniba.it.tee2.gui;

import di.uniba.it.tee2.extraction.TemporalExtractor;
import di.uniba.it.tee2.search.SearchResult;
import di.uniba.it.tee2.search.TemporalEventSearch;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 *
 * @author pierpaolo
 */
public class TEEgui extends javax.swing.JFrame {

    private TemporalExtractor te;

    public static TemporalEventSearch search;

    private int ntop = 25;

    private static String language;

    private static String maindir;

    private boolean nlsearch = false;

    /**
     * Creates new form TEEgui
     *
     * @throws java.io.IOException
     */
    public TEEgui() throws IOException {
        initComponents();
        te = new TemporalExtractor(language);
        te.init();
        search = new TemporalEventSearch(maindir, te);
        search.init();
    }

    /**
     * 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() {

        searchTB = new javax.swing.JToolBar();
        searchTF = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        tempTF = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        searchB = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultP = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TEE2 - GUI 0.20");

        searchTB.setRollover(true);

        searchTF.setColumns(20);
        searchTB.add(searchTF);
        searchTB.add(jSeparator1);

        tempTF.setColumns(10);
        searchTB.add(tempTF);
        searchTB.add(jSeparator2);

        searchB.setText("Search");
        searchB.setFocusable(false);
        searchB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        searchB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        searchB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBActionPerformed(evt);
            }
        });
        searchTB.add(searchB);

        jCheckBox1.setText("NL");
        jCheckBox1.setFocusable(false);
        jCheckBox1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jCheckBox1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });
        searchTB.add(jCheckBox1);

        getContentPane().add(searchTB, java.awt.BorderLayout.NORTH);

        jScrollPane1.setMinimumSize(new java.awt.Dimension(480, 320));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(640, 480));

        resultP.setLayout(new javax.swing.BoxLayout(resultP, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPane1.setViewportView(resultP);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

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

    private void searchBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBActionPerformed
        try {
            List<SearchResult> list = null;
            if (nlsearch) {
                list = search.naturalSearch(searchTF.getText(), tempTF.getText(), ntop);
            } else {
                list = search.search(searchTF.getText(), tempTF.getText(), ntop);
            }
            resultP.removeAll();
            Logger.getLogger(TEEgui.class.getName()).log(Level.INFO, "Number of results: " + list.size());
            for (SearchResult sr : list) {
                SearchResultPanel srp = new SearchResultPanel(sr);
                resultP.add(srp);
            }
            jScrollPane1.validate();
        } catch (Exception ex) {
            Logger.getLogger(TEEgui.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_searchBActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        nlsearch = jCheckBox1.isSelected();
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        try {
            CommandLine cmd = cmdParser.parse(options, args);
            if (cmd.hasOption("l") && cmd.hasOption("d")) {
                language = cmd.getOptionValue("l");
                maindir = cmd.getOptionValue("d");
                /* 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(TEEgui.class.getName()).log(java.util.logging.Level.SEVERE,
                            null, ex);
                } catch (InstantiationException ex) {
                    java.util.logging.Logger.getLogger(TEEgui.class.getName()).log(java.util.logging.Level.SEVERE,
                            null, ex);
                } catch (IllegalAccessException ex) {
                    java.util.logging.Logger.getLogger(TEEgui.class.getName()).log(java.util.logging.Level.SEVERE,
                            null, ex);
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                    java.util.logging.Logger.getLogger(TEEgui.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() {
                        try {
                            new TEEgui().setVisible(true);
                        } catch (IOException ex) {
                            Logger.getLogger(TEEgui.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });
            } else {
                HelpFormatter helpFormatter = new HelpFormatter();
                helpFormatter.printHelp("Run GUI", options, true);
            }
        } catch (ParseException ex) {
            Logger.getLogger(TEEgui.class.getName()).log(Level.SEVERE, null, ex);
        }
        //</editor-fold>
    }

    static final Options options;

    static CommandLineParser cmdParser = new BasicParser();

    static {
        options = new Options();
        options.addOption("l", true, "language (italian, english)").addOption("d", true, "the index directory");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JPanel resultP;
    private javax.swing.JButton searchB;
    private javax.swing.JToolBar searchTB;
    private javax.swing.JTextField searchTF;
    private javax.swing.JTextField tempTF;
    // End of variables declaration//GEN-END:variables
}