com.oz.digital.sign.window.MainView.java Source code

Java tutorial

Introduction

Here is the source code for com.oz.digital.sign.window.MainView.java

Source

/*
 * 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 com.oz.digital.sign.window;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 
 * @author <a href="mailto:sock.sqt@gmail.com">Samuel Quintana</a>
 * @since Jun 3, 2014, 11:58:45 PM
 */
public class MainView extends javax.swing.JFrame {

    /** Permite escribir en el archivo de log. */
    private static final Logger LOG = Logger.getLogger(MainView.class.getName());

    /**
     * Creates new form MainView
     */
    public MainView() {
        this.initComponents();
    }

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

        txtfEntry = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAreaEntryHistory = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menItemExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        menItemAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtfEntry.setToolTipText("Introduce cualquier cadena");

        txtAreaEntryHistory.setColumns(20);
        txtAreaEntryHistory.setRows(5);
        jScrollPane1.setViewportView(txtAreaEntryHistory);

        jMenu1.setText("Archivo");

        menItemExit.setText("Salir");
        menItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menItemExitActionPerformed(evt);
            }
        });
        jMenu1.add(menItemExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ayuda");

        menItemAbout.setText("Acerca de...");
        menItemAbout.setToolTipText("");
        menItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menItemAboutActionPerformed(evt);
            }
        });
        jMenu2.add(menItemAbout);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addContainerGap()
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        376, Short.MAX_VALUE)
                                                .addComponent(txtfEntry))
                                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(txtfEntry, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                        .addContainerGap()));

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

    private void menItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menItemExitActionPerformed
        System.exit(NumberUtils.INTEGER_ZERO);
    }//GEN-LAST:event_menItemExitActionPerformed

    private void menItemAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menItemAboutActionPerformed
        AboutView aboutView = new AboutView(this, rootPaneCheckingEnabled);
        aboutView.setTitle("Acerca de...");
        aboutView.setVisible(true);
    }//GEN-LAST:event_menItemAboutActionPerformed

    /**
     * @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 ("GTK+".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | javax.swing.UnsupportedLookAndFeelException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
        //</editor-fold>

        MainView mainView = new MainView();
        mainView.setVisible(true);

        while (true) {
            final String entry = mainView.getTxtfEntry().getText();

            if (StringUtils.isNotBlank(entry)) {
                LOG.log(Level.INFO, "== Agregando la entrada : {0}", entry);

                String entryFormatted = String.format("%s -> %s\n",
                        DateFormatUtils.format(new Date(), "dd/MM/yyyy hh:mm:ss"), entry);
                mainView.getTxtAreaEntryHistory().append(entryFormatted);
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                LOG.log(Level.SEVERE, ex.getMessage());
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem menItemAbout;
    private javax.swing.JMenuItem menItemExit;
    private javax.swing.JTextArea txtAreaEntryHistory;
    private javax.swing.JTextField txtfEntry;
    // End of variables declaration//GEN-END:variables

    public javax.swing.JTextArea getTxtAreaEntryHistory() {
        return txtAreaEntryHistory;
    }

    public void setTxtAreaEntryHistory(javax.swing.JTextArea txtAreaEntryHistory) {
        this.txtAreaEntryHistory = txtAreaEntryHistory;
    }

    public javax.swing.JTextField getTxtfEntry() {
        return txtfEntry;
    }

    public void setTxtfEntry(javax.swing.JTextField txtfEntry) {
        this.txtfEntry = txtfEntry;
    }

}