com.croer.javaorange.diviner.SimpleOrangeDiviner.java Source code

Java tutorial

Introduction

Here is the source code for com.croer.javaorange.diviner.SimpleOrangeDiviner.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.croer.javaorange.diviner;

import com.croer.javaorange.util.Configuration;
import com.croer.javaorange.util.Util;
import com.croer.javaorange.definition.Mediator;
import com.croer.javaorange.definition.OrangeDiviner;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.apache.commons.configuration.CompositeConfiguration;

/**
 *
 * @author elialva
 */
public class SimpleOrangeDiviner extends javax.swing.JDialog implements OrangeDiviner {

    CompositeConfiguration configuration = Configuration.getCONFIGURATION();
    private JComponent jComponent;

    /**
     * Creates new form SimpleOrangeDiviner2
     */
    public SimpleOrangeDiviner(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public SimpleOrangeDiviner(Mediator mediator) {
        super((Frame) null, true);
        initComponents();
        setUndecorated(true);

        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("ENTER")); //Efectua la seleccin                              
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("control CONTROL")); //Cancela la consulta y oculta el dilogo
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("alt SPACE")); //Reinicia la seleccin de toda la tabla
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction(configuration.getString("Plus"))); //Incrementa en uno la cantidad
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("alt DOWN")); //Decrementa en uno la cantidad
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("UP")); //Recorrido hacia arriba
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("DOWN")); //Recorrido hacia abajo
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("PAGE_UP")); //Retrocede una pgina
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("PAGE_DOWN")); //Avanza una pgina
        Util.activateFunctionKey(SimpleOrangeDiviner.this, new FormAction("F5")); //

        //        getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("pressed UP"), "go");
        //        getRootPane().getActionMap().put("go", new AbstractAction() {
        //
        //            @Override
        //            public void actionPerformed(ActionEvent e) {
        //                System.out.println("Yepa " + System.currentTimeMillis());
        //            }
        //        });
        //        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, InputEvent.CTRL_MASK), "collapse");
        //        getRootPane().getActionMap().put("collapse", new AbstractAction() {
        //
        //            @Override
        //            public void actionPerformed(ActionEvent e) {
        //                SimpleOrangeDiviner.this.setVisible(false);
        //            }
        //        });
        jPanel1.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                System.out.println("Evento: " + evt.getPropertyName() + "-" + evt.getNewValue());
            }
        });
    }

    /**
     * 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();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        simpleOrangeTextPane1 = new com.croer.javaorange.diviner.SimpleOrangeTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(153, 0, 0));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jScrollPane2.setFocusable(false);

        jTable1.setBackground(java.awt.Color.orange);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { { null, null, null },
                { null, null, null }, { null, null, null }, { null, null, null } },
                new String[] { "Title 1", "Title 2", "Title 3" }));
        jTable1.setFocusable(false);
        jScrollPane2.setViewportView(jTable1);

        jPanel2.setBackground(java.awt.Color.orange);
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setFocusable(false);

        jLabel1.setText("jLabel1");
        jLabel1.setFocusable(false);

        jProgressBar1.setBackground(java.awt.Color.orange);
        jProgressBar1.setFocusable(false);

        jButton1.setBackground(java.awt.Color.orange);
        jButton1.setText("F");
        jButton1.setFocusable(false);

        jButton2.setBackground(java.awt.Color.orange);
        jButton2.setText("B");
        jButton2.setFocusable(false);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("01");
        jLabel2.setFocusable(false);
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 154,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116,
                                                Short.MAX_VALUE)
                                        .addComponent(jButton1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton2))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        jPanel2Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 18,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 18,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));

        simpleOrangeTextPane1.setBorder(
                javax.swing.BorderFactory.createTitledBorder(null, "Nomis", javax.swing.border.TitledBorder.CENTER,
                        javax.swing.border.TitledBorder.TOP, null, new java.awt.Color(204, 51, 0)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(simpleOrangeTextPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(simpleOrangeTextPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 161,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

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

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SimpleOrangeDiviner dialog = new SimpleOrangeDiviner(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.pack();
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    public com.croer.javaorange.diviner.SimpleOrangeTextPane simpleOrangeTextPane1;

    // End of variables declaration//GEN-END:variables
    @Override
    public void show(JComponent jComponent) {
        this.jComponent = jComponent;
        System.out.println(configuration.getString(jComponent.getName()));
        Point location = jComponent.getLocationOnScreen();
        Dimension size = jComponent.getSize();
        Point xy = new Point(location.x, location.y + size.height);

        setLocation(xy);
        //        EventQueue.invokeLater(new Runnable() {
        //
        //            public void run() {
        //                jTextField3.requestFocusInWindow();
        //            }
        //        });
        pack();
        setVisible(true);
    }

    @Override
    public void collapse() {
        //Le pide a Controller  cancelar o a JPanel 
        setVisible(false);
    }

    private class FormAction extends AbstractAction {

        FormAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String functionKey = (String) getValue(Action.NAME);
            switch (functionKey) {

            case "control CONTROL":
                System.out.println(functionKey);
                SimpleOrangeDiviner.this.setVisible(false);
                break;
            case "F5":
                simpleOrangeTextPane1.setFilter(Math.random() + "", "^[\\d\\s]*$");
                break;
            default:
                System.out.println(functionKey);
            }
        }
    }
}