Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 16.75 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. distributing Java program with Eclipse
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.swing.*;
  9.  
  10. public class PISavior implements MouseListener {
  11.  
  12.     Random random = new Random();
  13.     private static int amount;
  14.     private static int editLine;
  15.  
  16.     private static String locations[];
  17.     private static String usernames[];
  18.     private static String passwords[];
  19.  
  20.     private final char LOWERCASE[] =
  21.     {
  22.         'a', 'b', 'c', 'd', 'e',
  23.         'f', 'g', 'h', 'i', 'j',
  24.         'k', 'l', 'm', 'n', 'o',
  25.         'p', 'q', 'r', 's', 't',
  26.         'u', 'v', 'w', 'x', 'y',
  27.         'z'
  28.     };
  29.     private final char UPPERCASE[] =
  30.     {
  31.         'A', 'B', 'C', 'D', 'E',
  32.         'F', 'G', 'H', 'I', 'J',
  33.         'K', 'L', 'M', 'N', 'O',
  34.         'P', 'Q', 'R', 'S', 'T',
  35.         'U', 'V', 'W', 'X', 'Y',
  36.         'Z'
  37.     };
  38.     private final char NUMBERS[] =
  39.     {
  40.         '0', '1', '2', '3', '4',
  41.         '5', '6', '7', '8', '9'
  42.     };
  43.  
  44.     private static volatile boolean edit = false;
  45.  
  46.     JFrame frame = new JFrame();
  47.     JPanel panel = new JPanel();
  48.  
  49.     JCheckBox jcb_Integers = new JCheckBox("Integers");
  50.     JCheckBox jcb_Capitals = new JCheckBox("Capitals");
  51.  
  52.     JScrollPane jscroll;
  53.  
  54.     JLabel jlb_Location = new JLabel("Location");
  55.     JLabel jlb_Username = new JLabel("Username");
  56.     JLabel jlb_Password = new JLabel("Password");
  57.     JLabel jlb_Amount = new JLabel("Amount:");
  58.     JLabel jlb_Length = new JLabel("Length:");
  59.     JLabel jlb_Locations[];
  60.     JLabel jlb_Usernames[];
  61.     JLabel jlb_Passwords[];
  62.  
  63.     JButton jbt_AddLocation = new JButton("Add Location");
  64.     JButton jbt_Save = new JButton("Save");
  65.     JButton jbt_Add = new JButton("Add");
  66.     JButton jbt_Randomize = new JButton("Randomize");
  67.     JButton jbt_Edit = new JButton("Edit");
  68.  
  69.     JTextField jtf_Locations[];
  70.     JTextField jtf_Usernames[];
  71.     JTextField jtf_Passwords[];
  72.     JTextField jtf_Location = new JTextField();
  73.     JTextField jtf_Username = new JTextField();
  74.     JTextField jtf_Password = new JTextField();
  75.     JTextField jtf_Integers = new JTextField();
  76.     JTextField jtf_Capitals = new JTextField();
  77.     JTextField jtf_Length = new JTextField();
  78.  
  79.     public static void main(String args[]) throws FileNotFoundException{
  80.         new PISavior();
  81.     }
  82.  
  83.     public PISavior() throws FileNotFoundException{
  84.         panel.setLayout(null);
  85.         panel.addMouseListener(this);
  86.         panel.setBounds(0, 0, 400,400);
  87.         load();
  88.         addMainUI();
  89.         //======================================================================
  90.         // ** Main User Interface
  91.         //======================================================================
  92.         panel.add(jlb_Location);
  93.         panel.add(jlb_Username);
  94.         panel.add(jlb_Password);
  95.         jbt_AddLocation.addActionListener(new ActionListener() {
  96.             @Override
  97.             public void actionPerformed(ActionEvent e) {
  98.                 edit = false;
  99.                 removeMainUI();
  100.                 addLocationUI();
  101.                 panel.repaint();
  102.             }
  103.     });
  104.         jbt_Save.addActionListener(new ActionListener() {
  105.             @Override
  106.             public void actionPerformed(ActionEvent e) {
  107.                 try {
  108.                     save();
  109.                 } catch (IOException ex) {
  110.                     Logger.getLogger(PISavior.class.getName()).log(Level.SEVERE, null, ex);
  111.                 }
  112.             }
  113.     });
  114.         //======================================================================
  115.         // ** Add Location User Interface
  116.         //======================================================================
  117.         jtf_Location.setBounds(5, 25, 100, 20);
  118.         jtf_Username.setBounds(5, 65, 100, 20);
  119.         jtf_Password.setBounds(5, 105, 100, 20);
  120.         jlb_Amount.setBounds(210, 10, 100, 20);
  121.         jcb_Integers.setBounds(130, 30, 80, 20);
  122.         jcb_Integers.addActionListener(new ActionListener() {
  123.             @Override
  124.             public void actionPerformed(ActionEvent e) {
  125.                 jtf_Integers.setEnabled(jcb_Integers.isSelected());
  126.             }
  127.     });
  128.         jcb_Capitals.setBounds(130, 60, 80, 20);
  129.         jcb_Capitals.addActionListener(new ActionListener() {
  130.             @Override
  131.             public void actionPerformed(ActionEvent e) {
  132.                 jtf_Capitals.setEnabled(jcb_Capitals.isSelected());
  133.             }
  134.     });
  135.         jtf_Integers.setBounds(210, 30, 50, 20);
  136.         jtf_Integers.setEnabled(false);
  137.         jtf_Integers.setText("0");
  138.         jtf_Capitals.setBounds(210, 60, 50, 20);
  139.         jtf_Capitals.setEnabled(false);
  140.         jtf_Capitals.setText("0");
  141.         jlb_Length.setBounds(135, 90, 80, 20);
  142.         jtf_Length.setBounds(210, 90, 50, 20);
  143.         jtf_Length.setText("0");
  144.         jbt_Randomize.setBounds(140, 120, 100, 20);
  145.         jbt_Randomize.addActionListener(new ActionListener() {
  146.             @Override
  147.             public void actionPerformed(ActionEvent e) {
  148.                 int length = Integer.parseInt(jtf_Length.getText());
  149.                 int integers = Integer.parseInt(jtf_Integers.getText());
  150.                 int capitals = Integer.parseInt(jtf_Capitals.getText());
  151.                 if (length != 0 && length >= (integers + capitals)) {
  152.                     jtf_Password.setText(getRandomPassword(length, jcb_Integers.isSelected() != false ? integers : 0, jcb_Capitals.isSelected() != false ? capitals : 0));
  153.                 }
  154.             }
  155.     });
  156.         jbt_Add.setBounds(20, 135, 60, 15);
  157.         jbt_Add.addActionListener(new ActionListener() {
  158.             @Override
  159.             public void actionPerformed(ActionEvent e) {
  160.                 addInformation(jtf_Location.getText().length() == 0 ? "(Empty)" : jtf_Location.getText(), jtf_Username.getText().length() == 0 ? "(Empty)" : jtf_Username.getText(), jtf_Password.getText().length() == 0 ? "(Empty)" : jtf_Password.getText());
  161.                 jtf_Location.setText("");
  162.                 jtf_Username.setText("");
  163.                 jtf_Password.setText("");
  164.                 removeLocationUI();
  165.                 addMainUI();
  166.                 panel.repaint();
  167.             }
  168.         });
  169.         jbt_Edit.setBounds(20, 135, 60, 15);
  170.         jbt_Edit.addActionListener(new ActionListener() {
  171.             @Override
  172.             public void actionPerformed(ActionEvent e) {
  173.                 int i = editLine;
  174.                 locations[i] = jtf_Location.getText();
  175.                 usernames[i] = jtf_Username.getText();
  176.                 passwords[i] = jtf_Password.getText();
  177.                 jlb_Locations[i].setText(locations[i]);
  178.                 jlb_Usernames[i].setText(usernames[i]);
  179.                 jlb_Passwords[i].setText(passwords[i]);
  180.                 jtf_Location.setText("");
  181.                 jtf_Username.setText("");
  182.                 jtf_Password.setText("");
  183.                 removeLocationUI();
  184.                 addMainUI();
  185.                 panel.repaint();
  186.             }
  187.         });
  188.  
  189.         jscroll = new JScrollPane(panel);
  190.         frame.setTitle("Personal Information Savior");
  191.         frame.setSize(300,240);
  192.         frame.setContentPane(jscroll);
  193.         frame.setLocationRelativeTo(null);
  194.         frame.setVisible(true);
  195.         frame.setResizable(false);
  196.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  197.     }
  198.  
  199.  
  200.     public void removeMainUI() {
  201.         if (amount != 0) {
  202.             for (int i = 0; i < amount; i++) {
  203.                 panel.remove(jlb_Locations[i]);
  204.                 panel.remove(jlb_Usernames[i]);
  205.                 panel.remove(jlb_Passwords[i]);
  206.             }
  207.         }
  208.         panel.remove(jbt_AddLocation);
  209.         panel.remove(jbt_Save);
  210.         panel.repaint();
  211.     }
  212.  
  213.     public void addMainUI() {
  214.         jlb_Location.setBounds(10, 5, 100, 20);
  215.         jlb_Username.setBounds(100, 5, 100, 20);
  216.         jlb_Password.setBounds(200, 5, 100, 20);
  217.         jbt_AddLocation.setBounds(75, (20 * amount) + 25, 110, 15);
  218.         jbt_Save.setBounds(90, (20 * amount) + 45, 80, 15);
  219.         jlb_Location.setText("Location");
  220.         jlb_Username.setText("Username");
  221.         jlb_Password.setText("Password");
  222.         panel.setPreferredSize(new Dimension(200, amount * 30));
  223.         if (amount != 0) {
  224.             for (int i = 0; i < amount; i++) {
  225.                 jlb_Locations[i].setBounds(10, 25 + (i * 20), 100, 20);
  226.                 jlb_Usernames[i].setBounds(100, 25 + (i * 20), 100, 20);
  227.                 jlb_Passwords[i].setBounds(200, 25 + (i * 20), 100, 20);
  228.                 panel.add(jlb_Locations[i]);
  229.                 panel.add(jlb_Usernames[i]);
  230.                 panel.add(jlb_Passwords[i]);
  231.             }
  232.         }
  233.         panel.add(jbt_AddLocation);
  234.         panel.add(jbt_Save);
  235.         panel.repaint();
  236.     }
  237.  
  238.     public void removeLocationUI() {
  239.         panel.remove(jtf_Location);
  240.         panel.remove(jtf_Username);
  241.         panel.remove(jtf_Password);
  242.         panel.remove(jlb_Amount);
  243.         panel.remove(jcb_Integers);
  244.         panel.remove(jcb_Capitals);
  245.         panel.remove(jtf_Integers);
  246.         panel.remove(jtf_Capitals);
  247.         panel.remove(jlb_Length);
  248.         panel.remove(jtf_Length);
  249.         panel.remove(jbt_Randomize);
  250.         panel.remove(jbt_Add);
  251.         panel.remove(jbt_Edit);
  252.  
  253.         panel.repaint();
  254.     }
  255.  
  256.     public void addLocationUI() {
  257.         if (!edit) {
  258.             jlb_Location.setText("New Location:");
  259.             jlb_Location.setBounds(5, 5, 100, 20);
  260.             jlb_Username.setText("New Username:");
  261.             jlb_Username.setBounds(5, 45, 100, 20);
  262.             jlb_Password.setText("New Password:");
  263.             jlb_Password.setBounds(5, 85, 100, 20);
  264.             panel.add(jtf_Location);
  265.             panel.add(jtf_Username);
  266.             panel.add(jtf_Password);
  267.             panel.add(jlb_Amount);
  268.             panel.add(jcb_Integers);
  269.             panel.add(jcb_Capitals);
  270.             panel.add(jtf_Integers);
  271.             panel.add(jtf_Capitals);
  272.             panel.add(jlb_Length);
  273.             panel.add(jtf_Length);
  274.             panel.add(jbt_Randomize);
  275.             panel.add(jbt_Add);
  276.             panel.repaint();
  277.         } else {
  278.             jlb_Location.setText("New Location:");
  279.             jlb_Location.setBounds(5, 5, 100, 20);
  280.             jlb_Username.setText("New Username:");
  281.             jlb_Username.setBounds(5, 45, 100, 20);
  282.             jlb_Password.setText("New Password:");
  283.             jlb_Password.setBounds(5, 85, 100, 20);
  284.             panel.add(jtf_Location);
  285.             panel.add(jtf_Username);
  286.             panel.add(jtf_Password);
  287.             panel.add(jlb_Amount);
  288.             panel.add(jcb_Integers);
  289.             panel.add(jcb_Capitals);
  290.             panel.add(jtf_Integers);
  291.             panel.add(jtf_Capitals);
  292.             panel.add(jlb_Length);
  293.             panel.add(jtf_Length);
  294.             panel.add(jbt_Randomize);
  295.             panel.add(jbt_Edit);
  296.             panel.repaint();
  297.         }
  298.     }
  299.  
  300.     public String getRandomPassword(int length, int integers, int capitals) {
  301.         int lowers = length - (integers + capitals);
  302.         String password = "";
  303.         String scramble[] = new String[length];
  304.         if (capitals != 0) {
  305.             for (int i = 0; i < capitals; i++) {
  306.                 while (true) {
  307.                     int randomScramble = random.nextInt(length);
  308.                     if (scramble[randomScramble] == null || scramble[randomScramble].equals("")) {
  309.                         scramble[randomScramble] = Character.toString(UPPERCASE[random.nextInt(UPPERCASE.length)]);
  310.                         break;
  311.                     }
  312.                 }
  313.             }
  314.         }
  315.         if (integers != 0) {
  316.             for (int i = 0; i < integers; i++) {
  317.                 while (true) {
  318.                     int randomScramble = random.nextInt(length);
  319.                     if (scramble[randomScramble] == null || scramble[randomScramble].equals("")) {
  320.                         scramble[randomScramble] = Character.toString(NUMBERS[random.nextInt(NUMBERS.length)]);
  321.                         break;
  322.                     }
  323.                 }
  324.             }
  325.         }
  326.         for (int i = 0; i < lowers; i++) {
  327.             while (true) {
  328.                 int randomScramble = random.nextInt(length);
  329.                 if (scramble[randomScramble] == null || scramble[randomScramble].equals("")) {
  330.                     scramble[randomScramble] = Character.toString(LOWERCASE[random.nextInt(LOWERCASE.length)]);
  331.                     break;
  332.                 }
  333.             }
  334.         }
  335.         for (int i = 0; i < length; i++) {
  336.             password += scramble[i];
  337.         }
  338.         return password;
  339.     }
  340.  
  341.     public void save() throws IOException {
  342.         File dir = new File("C:/PISavior");
  343.         if (!dir.exists()) {
  344.             dir.mkdir();
  345.         }
  346.         File myfile = new File("C:/PISavior/myfile.txt");
  347.         myfile.delete();
  348.         myfile.createNewFile();
  349.         PrintWriter output = new PrintWriter(myfile);
  350.         output.print(amount + "*");
  351.         for (int i = 0; i < amount; i++) {
  352.             output.print(locations[i] + "*");
  353.             output.print(usernames[i] + "*");
  354.             output.print(passwords[i] + "*");
  355.         }
  356.         output.close();
  357.     }
  358.  
  359.     public void load() throws FileNotFoundException {
  360.         File myfile = new File("C:/PISavior/myfile.txt");
  361.         if (myfile.exists()) {
  362.             Scanner input = new Scanner(myfile);
  363.             while (input.hasNext())
  364.             {
  365.                 input.useDelimiter("[*]");
  366.                 amount = input.nextInt();
  367.  
  368.                 locations = new String[amount];
  369.                 usernames = new String[amount];
  370.                 passwords = new String[amount];
  371.                 jlb_Locations = new JLabel[amount];
  372.                 jlb_Usernames = new JLabel[amount];
  373.                 jlb_Passwords = new JLabel[amount];
  374.  
  375.                 for (int i = 0; i < amount; i++) {
  376.                     locations[i] = input.next();
  377.                     jlb_Locations[i] = new JLabel(locations[i]);
  378.                     usernames[i] = input.next();
  379.                     jlb_Usernames[i] = new JLabel(usernames[i]);
  380.                     passwords[i] = input.next();
  381.                     jlb_Passwords[i] = new JLabel(passwords[i]);
  382.                 }
  383.             }
  384.         }
  385.     }
  386.  
  387.     public void addInformation(String info1, String info2, String info3) {
  388.         if (amount == 0) {
  389.             amount++;
  390.             locations = new String[amount];
  391.             usernames = new String[amount];
  392.             passwords = new String[amount];
  393.             jlb_Locations = new JLabel[amount];
  394.             jlb_Usernames = new JLabel[amount];
  395.             jlb_Passwords = new JLabel[amount];
  396.             locations[0] = info1;
  397.             usernames[0] = info2;
  398.             passwords[0] = info3;
  399.             jlb_Locations[0] = new JLabel(locations[0]);
  400.             jlb_Usernames[0] = new JLabel(usernames[0]);
  401.             jlb_Passwords[0] = new JLabel(passwords[0]);
  402.         } else {
  403.             String temp1[] = new String[amount];
  404.             String temp2[] = new String[amount];
  405.             String temp3[] = new String[amount];
  406.  
  407.             for (int i = 0; i < amount; i++) {
  408.                 temp1[i] = locations[i];
  409.                 temp2[i] = usernames[i];
  410.                 temp3[i] = passwords[i];
  411.             }
  412.  
  413.             amount++;
  414.  
  415.             locations = new String[amount];
  416.             usernames = new String[amount];
  417.             passwords = new String[amount];
  418.  
  419.             jlb_Locations = new JLabel[amount];
  420.             jlb_Usernames = new JLabel[amount];
  421.             jlb_Passwords = new JLabel[amount];
  422.  
  423.             for (int i = 0; i < temp1.length; i++) {
  424.                 locations[i] = temp1[i];
  425.                 usernames[i] = temp2[i];
  426.                 passwords[i] = temp3[i];
  427.  
  428.                 jlb_Locations[i] = new JLabel(temp1[i]);
  429.                 jlb_Usernames[i] = new JLabel(temp2[i]);
  430.                 jlb_Passwords[i] = new JLabel(temp3[i]);
  431.             }
  432.  
  433.             locations[amount-1] = info1;
  434.             usernames[amount-1] = info2;
  435.             passwords[amount-1] = info3;
  436.  
  437.             jlb_Locations[amount-1] = new JLabel(info1);
  438.             jlb_Usernames[amount-1] = new JLabel(info2);
  439.             jlb_Passwords[amount-1] = new JLabel(info3);
  440.         }
  441.     }
  442.  
  443.     @Override
  444.     public void mouseReleased(MouseEvent e) {
  445.         int y = e.getY();
  446.         if (y > 25 && y < (amount * 20) + 25) {
  447.             for (int i = 0; i < amount; i++) {
  448.                 if (y < (i*20) + 45) {
  449.                     edit = true;
  450.                     editLine = i;
  451.                     jtf_Location.setText(locations[i]);
  452.                     jtf_Username.setText(usernames[i]);
  453.                     jtf_Password.setText(passwords[i]);
  454.                     removeMainUI();
  455.                     addLocationUI();
  456.                 }
  457.             }
  458.         }
  459.     }
  460.  
  461.     @Override
  462.     public void mouseClicked(MouseEvent me) {
  463.     }
  464.  
  465.     @Override
  466.     public void mousePressed(MouseEvent me) {
  467.     }
  468.  
  469.     @Override
  470.     public void mouseEntered(MouseEvent me) {
  471.     }
  472.  
  473.     @Override
  474.     public void mouseExited(MouseEvent me) {
  475.     }
  476. }