/*
 * JFrame.java
 *
 * Created on 17 Μάρτιος 2003, 4:08 μμ
 */

/**
 *
 * @author  paredros
 */

import javax.swing.border.Border;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
import java.io.File;

//==================================GK=======================================        
import java.rmi.*;
import java.rmi.server.*;
//==================================GK=======================================        


public class Client extends javax.swing.JFrame {
    
    /** Creates new form JFrame */
    public Client() {
        initComponents();
        this.setSize(400,400);
    }
    
    public Client(String temp) {
        initComponents();
        this.setSize(400,400);
        message = temp;
    }
 
               private int compare(String tr,String te) {
           int i=0,n1,n2,j;

                 if(tr==null || te==null) return 0;
                 n1=tr.length();
                 n2=te.length();
                 if(n1!=n2) return 0;
                 for(j=0;j<n1;j++)
                         if(tr.charAt(j)!=te.charAt(j)) break;
                 if(j==n1) return 1;
                 return 0;
           }

    /** 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.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        ButtonVote = new javax.swing.JButton();
        CandidatesList = new javax.swing.JComboBox();
        ButtonListCandidates = new javax.swing.JButton();
        ButtonResults = new javax.swing.JButton();
        ResultsList = new javax.swing.JComboBox();

        setTitle("RMI");
        setBackground(new java.awt.Color(204, 204, 204));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jTextField1.setText("enter name");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jPanel1.add(jTextField1);

        ButtonVote.setText("Vote");
        ButtonVote.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonVoteActionPerformed(evt);
            }
        });

        jPanel1.add(ButtonVote);

        CandidatesList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CandidatesListActionPerformed(evt);
            }
        });

        jPanel1.add(CandidatesList);

        ButtonListCandidates.setText("List Candidates");
        ButtonListCandidates.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonListCandidatesActionPerformed(evt);
            }
        });

        jPanel1.add(ButtonListCandidates);

        ButtonResults.setText("Results for");
        ButtonResults.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonResultsActionPerformed(evt);
            }
        });

        jPanel1.add(ButtonResults);

        jPanel1.add(ResultsList);

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

        pack();
    }//GEN-END:initComponents

    private void CandidatesListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CandidatesListActionPerformed
        // Add your handling code here:
    }//GEN-LAST:event_CandidatesListActionPerformed

    private void ButtonResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonResultsActionPerformed
        // Add your handling code here:
        System.setSecurityManager(new RMISecurityManager());
        try {
       
        String url = "//" + host + "/~stefanos/rmi/simple";
  //         String url="";

        // lookup  server
        System.out.println("Trying to lookup remote object:");
        echo = (simple) java.rmi.Naming.lookup(url);
        
        message_results = echo.results((String) ResultsList.getSelectedItem());
        
               } catch (Exception e) {
                   System.out.println("Client: an exception occurred: " +
                                      e.getMessage());
                   e.printStackTrace();
               }

        
        
/*        try {
        message_results = echo.results((String) ResultsList.getSelectedItem()); 
        } catch (Exception e) {
        System.out.println("Client: an exception occurred: " +
                   e.getMessage());
                   e.printStackTrace();
               }*/
 
        
        JOptionPane.showMessageDialog(this, message_results);
        
    }//GEN-LAST:event_ButtonResultsActionPerformed

    private void ButtonListCandidatesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonListCandidatesActionPerformed
       //CandidatesList.insertItemAt("Georgia",0);
       //CandidatesList.insertItemAt("Stefanos Petsios",1);
        String list;
        

        System.setSecurityManager(new RMISecurityManager());
        try {
       
        String url = "//" + "/~stefanos/rmi/simple";
  //         String url="";

        // lookup  server
        System.out.println("Trying to lookup remote object:");
        echo = (simple) java.rmi.Naming.lookup(url);
        
        message = echo.list_candidates();
        
               } catch (Exception e) {
                   System.out.println("Client: an exception occurred: " +
                                      e.getMessage());
                   e.printStackTrace();
               }
        
        
        
        
/*        try {
        message = echo.list_candidates(); 
        } catch (Exception e) {
        System.out.println("Client: an exception occurred: " +
                   e.getMessage());
                   e.printStackTrace();
               }*/

        
        list=message;
        
        if(CandidatesList.getItemCount()!=0)
            CandidatesList.removeAllItems();
        updateCandidatesList(list);
       CandidatesList.setSelectedIndex(0);
       updateResultsList(list);
       ResultsList.setSelectedIndex(0);
       ButtonListCandidates.setEnabled(false);
        //System.out.println(message);
// Add your handling code here:
    }//GEN-LAST:event_ButtonListCandidatesActionPerformed

    public void updateCandidatesList(String lista){
        String tmp;
        char c;
        int num=0,count=0;
        
        tmp="";
        num=lista.length();
        for(int i=0; i<num; i++) {
            tmp=tmp+lista.charAt(i);
            c=lista.charAt(i);
            if(c=='\n') {
                System.out.println(tmp);
                CandidatesList.insertItemAt(tmp,count);
                count++;
                tmp="";
            }
        }
        //CandidatesList.insertItemAt("Georgia",0);
        //CandidatesList.insertItemAt("Stefanos Petsios",1);

    }
    
    public void updateResultsList(String lista){
        String tmp;
        char c;
        int num=0,count=1;
        
        ResultsList.insertItemAt("all", 0);
        tmp="";
        num=lista.length();
        for(int i=0; i<num; i++) {
            tmp=tmp+lista.charAt(i);
            c=lista.charAt(i);
            if(c=='\n') {
                System.out.println(tmp);
                ResultsList.insertItemAt(tmp,count);
                count++;
                tmp="";
            }
        }
        ResultsList.insertItemAt("Leyka", count);
        ResultsList.insertItemAt("Akyra", count+1);
        
        
    }
    private void ButtonVoteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonVoteActionPerformed
        String msg;
        msg = jTextField1.getText();
        System.out.println(msg);
        System.setSecurityManager(new RMISecurityManager());
        try {
       
        String url = "//" + host + "/~stefanos/rmi/simple";
  //         String url="";

        // lookup  server
        System.out.println("Trying to lookup remote object:");
        echo = (simple) java.rmi.Naming.lookup(url);
        
        message = echo.vote(msg, (String) CandidatesList.getSelectedItem());
        
               } catch (Exception e) {
                   System.out.println("Client: an exception occurred: " +
                                      e.getMessage());
                   e.printStackTrace();
               }

        
/*        try {
        message_vote = echo.vote(msg, (String) CandidatesList.getSelectedItem()); 
        } catch (Exception e) {
        System.out.println("Client: an exception occurred: " +
                   e.getMessage());
                   e.printStackTrace();
               }*/
        
        
        if(compare(message_vote,"Sorry but you can vote only once.")==1) {
            System.out.println("only one vote per person");
            JOptionPane.showMessageDialog(this, "Only one vote per person!");
        }
        else if(compare(message_vote,"Sorry you don't have the right to vote")==1) {
            System.out.println("no right to vote man");
            JOptionPane.showMessageDialog(this, "No right to vote!");
        }
        else if(compare(message_vote,"OK")==1) {
            System.out.println("you have voted");
            JOptionPane.showMessageDialog(this, "Your vote has been recorded");
            
        }
 //       frame2 = new JFrame("egine allagh");
   //     frame2.show();
        
    }//GEN-LAST:event_ButtonVoteActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // Add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
//        String message = "\n";
//        message = message + "Candidates List" + "\n" + "---------------" + "\n";
 //       message="";
   //     message = message + "stefanos petsios" + "   " + "pasok" + "\n";
     //   message = message + "georgia" + "   " + "kke" + "\n";
       // message_vote="Sorry you don't have the right to vote";
     //   message_results="Georgia    KKE   4    5%\n";
      //  message_results=message_results+"Stefanos Petsios   PASOK   76   95%\n"; 
//        CandidatesList.addItem("Georgia");
//        CandidatesList.addItem("Stefanos Petsios");
      //  updateCandidatesList();

//==================================GK=======================================        
        // Create and install the security manager
        /*System.setSecurityManager(new RMISecurityManager());
        try {
       
        String url = "//" + args[0] + "/~stefanos/rmi/simple";
  //         String url="";

        // lookup  server
        System.out.println("Trying to lookup remote object:");
        echo = (simple) java.rmi.Naming.lookup(url);*/
        
//==================================GK=======================================        
        argv=args;
        host=args[0];
        new Client().show();
//==================================GK=======================================        
               /*} catch (Exception e) {
                   System.out.println("Client: an exception occurred: " +
                                      e.getMessage());
                   e.printStackTrace();
               }*/
               // new Client().show();

//==================================GK=======================================                
}
    
    public static String host;
    public static String argv[];
    public static simple echo;
    public static String message;
    public static String message_vote;
    public static String message_results;
    public Client frame2;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox CandidatesList;
    private javax.swing.JButton ButtonVote;
    private javax.swing.JComboBox ResultsList;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton ButtonResults;
    private javax.swing.JButton ButtonListCandidates;
    // End of variables declaration//GEN-END:variables
    
}
