Package UI

Source Code of UI.Frame_Search

package UI;

import Common.Account;
import Common.Keep_It_Safe;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/*
* NewJFrame.java
*
* Created on February 15, 2008, 12:48 AM
*/


/**
*
* @author  psychok7
*/
public class Frame_Search extends javax.swing.JFrame {
    private String type;
    private Frame_Main UI_main;
    private DefaultMutableTreeNode treeRoot = new javax.swing.tree.DefaultMutableTreeNode(this.type);
    private DefaultMutableTreeNode treeNode1=null;
    private DefaultMutableTreeNode treeNode2=null;

    /** Creates new form NewJFrame */
    public Frame_Search(String type) {
        this.type=type;
        initComponents();
        this.setResizable(false);
        createNodes();
       
    }
    public Frame_Search(String type,Frame_Main UI_main) {
        this.type=type;
        this.UI_main=UI_main;
        initComponents();
        this.setResizable(false);
        createNodes();

    }
   
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        Back_btn1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        clear_btn = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        exit_btn2 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        about_btn = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Search", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Purisa", 0, 13))); // NOI18N

        Back_btn1.setFont(new java.awt.Font("Purisa", 0, 13));
        Back_btn1.setText("Back"); // NOI18N
        Back_btn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Back_btn1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Purisa", 0, 13));
        jLabel3.setText("Name"); // NOI18N

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                SearchKeyPressed(evt);
            }
        });

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTree1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        clear_btn.setFont(new java.awt.Font("Purisa", 0, 13));
        clear_btn.setText("Clear");
        clear_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clear_btnActionPerformed(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()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel3)
                        .addGap(29, 29, 29)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 484, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(237, 237, 237)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Back_btn1)
                            .addComponent(jLabel2))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(233, Short.MAX_VALUE)
                .addComponent(clear_btn)
                .addGap(228, 228, 228))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(clear_btn)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(Back_btn1))
                .addContainerGap())
        );

        jMenu1.setText("Ficheiro"); // NOI18N
        jMenu1.setFont(new java.awt.Font("Purisa", 0, 13));

        exit_btn2.setFont(new java.awt.Font("Purisa", 0, 13));
        exit_btn2.setText("Sair"); // NOI18N
        exit_btn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit_btn2ActionPerformed(evt);
            }
        });
        jMenu1.add(exit_btn2);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Ajuda"); // NOI18N
        jMenu3.setFont(new java.awt.Font("Purisa", 0, 13));

        about_btn.setFont(new java.awt.Font("Purisa", 0, 13));
        about_btn.setText("Sobre Fusion Management"); // NOI18N
        about_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                about_btnActionPerformed(evt);
            }
        });
        jMenu3.add(about_btn);

        jMenuBar1.add(jMenu3);

        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.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        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
 


    public String getjTextField1(){
        return jTextField1.getText();
    }


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

    private void about_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_about_btnActionPerformed
        // TODO add your handling code here:
        new Frame_About().setVisible(true);
    }//GEN-LAST:event_about_btnActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed

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

   
    private void SearchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SearchKeyPressed
        // TODO add your handling code here:
    this.treeRoot=new javax.swing.tree.DefaultMutableTreeNode(this.type);
    this.treeNode1=null;
    this.treeNode2=null;

        if(getjTextField1().equalsIgnoreCase("")){
            createNodes();
        }

        else{
            filteredNodes(getjTextField1());
            this.treeRoot.add(this.treeNode1);
            jTree1.setModel(new javax.swing.tree.DefaultTreeModel(this.treeRoot));
            jScrollPane1.setViewportView(jTree1);
        }
    }//GEN-LAST:event_SearchKeyPressed

    private void clear_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clear_btnActionPerformed
        // TODO add your handling code here:
        this.treeRoot=new javax.swing.tree.DefaultMutableTreeNode(this.type);
        this.treeNode1=null;
        this.treeNode2=null;
        jTextField1.setText("");
        if(getjTextField1().equalsIgnoreCase("")){
            createNodes();
        }
       

    }//GEN-LAST:event_clear_btnActionPerformed

    private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
        // TODO add your handling code here:
                boolean eventInTree = false;

        if(this.type.equalsIgnoreCase("Show Accounts")){


            if(evt.getClickCount() == 2) {
                try {
                    eventInTree = true;
                }
                catch (Exception e) {
                    System.out.println("Exception occured:OutSide Tree Region");
                    eventInTree = false;
                }
                if (eventInTree){
                    int selRow = jTree1.getRowForLocation(evt.getX(), evt.getY());

                     if(selRow != -1){
                         TreePath path = jTree1.getPathForRow(selRow);
                         TreeNode node = (TreeNode) path.getLastPathComponent();
                         node.toString(); //id da pessoa
                         Account aux;
                         aux=Check_Exists(node.toString());
                         if(aux!=null){
                            new Frame_Login(1,null,aux).setVisible(true);
                            this.dispose();
//                            jTextField1.setText("");
//                            SearchKeyPressed(null);
                         }

                         else{
                             JOptionPane.showMessageDialog(null, "Error choosing accounts");
                         }

                     }

                }

            }

        }
       else if(this.type.equalsIgnoreCase("Remove Account")){


            if(evt.getClickCount() == 2) {
                try {
                    eventInTree = true;
                }
                catch (Exception e) {
                    System.out.println("Exception occured:OutSide Tree Region");
                    eventInTree = false;
                }
                if (eventInTree){
                    int selRow = jTree1.getRowForLocation(evt.getX(), evt.getY());

                     if(selRow != -1){
                         TreePath path = jTree1.getPathForRow(selRow);
                         TreeNode node = (TreeNode) path.getLastPathComponent();
                         node.toString(); //id da pessoa
                         Account aux;
                         aux=Check_Exists(node.toString());
                         int aux2=JOptionPane.showConfirmDialog(null,"Are you sure you want to remove this account?");
                         if(aux!=null){
                             if(aux2==0){       //yes
                                this.setVisible(false);
                                new Frame_Login(2,this.UI_main,this,aux).setVisible(true);
                                jTextField1.setText("");
                                SearchKeyPressed(null);
                             }
                         }

                         else{
                             JOptionPane.showMessageDialog(null, "Error removing account");
                         }

                     }

                }

            }

        }

    }//GEN-LAST:event_jTree1MouseClicked


    public void createNodes() {
        this.treeRoot=new javax.swing.tree.DefaultMutableTreeNode(this.type);
        this.treeNode1=null;
        this.treeNode2=null;
        this.treeNode1 = new javax.swing.tree.DefaultMutableTreeNode(this.type);

        for (int i=0;i<Keep_It_Safe.accounts.size();i++){
               this.treeNode2 = new javax.swing.tree.DefaultMutableTreeNode(Keep_It_Safe.accounts.get(i).get_name());
               treeNode1.add(treeNode2);
        }
        this.treeRoot.add(this.treeNode1);
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(this.treeRoot));
        jScrollPane1.setViewportView(jTree1);

    }

     private Account Check_Exists(String node) {
         for (int i=0;i<Keep_It_Safe.accounts.size();i++){
             if(Keep_It_Safe.accounts.get(i).get_name().equalsIgnoreCase(node)){
                 return Keep_It_Safe.accounts.get(i);
             }
         }
         return null;

     }

    public void filteredNodes (String search_text){
        this.treeNode1 = new javax.swing.tree.DefaultMutableTreeNode(this.type);

        for (int i=0;i<Keep_It_Safe.accounts.size();i++){
            if(Keep_It_Safe.accounts.get(i).get_name().toLowerCase().contains(search_text.toLowerCase()) ){        //pesquisar em realtime
                this.treeNode2 = new javax.swing.tree.DefaultMutableTreeNode(Keep_It_Safe.accounts.get(i).get_name());
                treeNode1.add(treeNode2);
            }

        }


    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new  Frame_Search("client").setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Back_btn1;
    private javax.swing.JMenuItem about_btn;
    private javax.swing.JButton clear_btn;
    private javax.swing.JMenuItem exit_btn2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables

}
TOP

Related Classes of UI.Frame_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.