Package ui

Source Code of ui.Search

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ui;

import beans.Author;
import beans.Reservation;
import design_patterns.decorator.SearchAll;
import design_patterns.decorator.SearchBookDecorator;
import design_patterns.decorator.SearchVideoDecorator;
import design_patterns.strategy.Product;
import javax.swing.tree.DefaultMutableTreeNode;

/**
*
* @author root
*/
public class Search extends javax.swing.JFrame {

    private DefaultMutableTreeNode top,products_node,videos_node,books_node,authors_node,reservations_node;
    private Login window_login;
    /**
     * Creates new form Search
     */
    public Search() {
        initComponents();
        createNodes();
    }
    public Search(Login window_login) {
        initComponents();
        this.window_login = window_login;
        createNodes();
    }
   
    private void createNodes(){
        this.jTree1.removeAll();
        this.top=new DefaultMutableTreeNode("Search");
        products_node = new DefaultMutableTreeNode("Products");
        videos_node = new DefaultMutableTreeNode("Videos");
        books_node = new DefaultMutableTreeNode("Books");
        authors_node = new DefaultMutableTreeNode("Authors");
        reservations_node = new DefaultMutableTreeNode("Reservations");
         
        //call decorator pattern
        SearchAll aux = new SearchAll();
        products_node = aux.search(this.window_login.getProducts());
       
        videos_node = new SearchVideoDecorator(aux).search(this.window_login.getVideos());
       
        books_node = new SearchBookDecorator(aux).search(this.window_login.getBooks());

        //***************************************************************************************
        for (int i = 0; i < this.window_login.getAuthors().size();i++){
            Author temp = (Author)this.window_login.getAuthors().get(i);
            if (temp!=null){
                authors_node.add(new javax.swing.tree.DefaultMutableTreeNode(temp.getName()));
            }
        }
        for (int i = 0; i< this.window_login.getReservations().size();i++){
            Reservation temp = (Reservation)this.window_login.getReservations().get(i);
            if (temp!=null){
                if (temp.getProduct()!=null){
                    reservations_node.add(new javax.swing.tree.DefaultMutableTreeNode(temp.getProduct().getName()));
                }
            }
        }
        //***************************************************************************************

        this.top.add(products_node);
        this.top.add(videos_node);
        this.top.add(books_node);
        this.top.add(authors_node);
        this.top.add(reservations_node);
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(this.top));
        jScrollPane1.setViewportView(jTree1);
    }

    /**
     * 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();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jScrollPane1.setViewportView(jTree1);

        jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldKeyPressed(evt);
            }
        });

        jButton1.setText("Close");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        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(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                    .addComponent(jTextField))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(150, 150, 150)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        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(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void filteredNodes (String search_text){      
            products_node = null;
            authors_node = null;
            reservations_node = null;


            for (int i = 0; i < this.window_login.getProducts().size();i++){
                Product temp = (Product)this.window_login.getProducts().get(i);
                if(temp!=null){
                    if (this.jTextField.getText().toLowerCase().contains(temp.getName().toLowerCase())){
                        products_node = new DefaultMutableTreeNode("Products");
                        products_node.add(new javax.swing.tree.DefaultMutableTreeNode(temp.getName()));
                        break;
                    }
                }
            }

            //***************************************************************************************
            for (int i = 0; i < this.window_login.getAuthors().size();i++){
                Author temp = (Author)this.window_login.getAuthors().get(i);
                if (temp!=null){
                    if (this.jTextField.getText().toLowerCase().contains(temp.getName().toLowerCase())){
                        authors_node = new DefaultMutableTreeNode("Authors");
                        authors_node.add(new javax.swing.tree.DefaultMutableTreeNode(temp.getName()));
                        break;
                    }
                }
            }
            for (int i = 0; i< this.window_login.getReservations().size();i++){
                Reservation temp = (Reservation)this.window_login.getReservations().get(i);
                if (temp!=null){
                    if (temp.getProduct()!=null){
                        if (this.jTextField.getText().toLowerCase().contains(temp.getProduct().getName().toLowerCase())){
                            reservations_node = new DefaultMutableTreeNode("Reservations");
                            reservations_node.add(new javax.swing.tree.DefaultMutableTreeNode(temp.getProduct().getName()));
                            break;
                        }
                    }
                }
            }
            //***************************************************************************************
            if (products_node!=null){
                this.top.add(products_node);
            }
            if (authors_node!=null){
                this.top.add(authors_node);
            }
            if (reservations_node!=null){
                this.top.add(reservations_node);
            }                          
    }
    private void jTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldKeyPressed
        // TODO add your handling code here:
        this.top=new DefaultMutableTreeNode("Search");
       
        if(this.jTextField.getText().equalsIgnoreCase("")){
            createNodes();
        }
        else{
            filteredNodes(this.jTextField.getText());
            jTree1.setModel(new javax.swing.tree.DefaultTreeModel(this.top));
            jScrollPane1.setViewportView(jTree1);
        }
    }//GEN-LAST:event_jTextFieldKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @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(Search.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Search.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Search.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Search.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() {
                new Search().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of ui.Search

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.