Lauda

Untitled

Dec 15th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. // ***
  2. // InputHandler
  3. // ***
  4.     public class InputHandler {
  5.     private int MAX_INPUT_LEN = 30;
  6.     private int MIN_INPUT_LEN = 3; // Koristi se za Oznaku drzave i tako tih slicnih stvari...
  7.     private int MAX_LOGIN_ATEMPTS = 5;
  8.    
  9.     public char[] invalidChars = new char[] {'(', ')', '{', '}', '[', ']', '!', '?', '@', '/', '*', '.', ';', '-', '+', ',', '|', '^', '&', '%', '$', '_'};
  10.    
  11.     private boolean ok = true; // Kontrola unosa
  12.  
  13.  
  14.     public InputHandler() {
  15.        
  16.     }
  17.  
  18.     public void setOk(boolean ok) {
  19.         this.ok = ok;
  20.         System.out.println(this.ok + " u setteru InputHandler-a");
  21.     }
  22.    
  23.     public boolean isOk() {
  24.         return ok;
  25.     }
  26.  
  27.  
  28. // ***
  29. // MyKeyListener
  30. // ***
  31. import gui.handlers.InputHandler;
  32.  
  33. public class MyKeyListener implements KeyListener {
  34. // U ovoj klasi se setuje prom. ok na false iz InputHandler klase
  35. // i koristi se kod JTextField-a u klasi PanelDetailDrzave da provjeri unos
  36.     @Override
  37.     public void keyTyped(KeyEvent e) {
  38.         // TODO Auto-generated method stub
  39.         if (e.getSource() instanceof JTextField) {
  40.             JTextField txtf = (JTextField)e.getSource();
  41.             char c = e.getKeyChar();
  42.             InputHandler ih = new InputHandler();
  43.             boolean ok = true;
  44.             for (int i = 0; i < ih.invalidChars.length; i++) {
  45.                  if (c == ih.invalidChars[i]) {
  46.                      System.out.println("DETEKTOVAN NEDOZVOLJEN ZNAK!");
  47.                      txtf.setBackground(Color.RED);
  48.                      ih.setOk(false); // OVDE SE SETUJE NA FALSE IZ INPUTHANDLER-a
  49.                      System.out.println(ih.isOk() + " u MyKeyListeneru");
  50.                  }
  51.                 // else
  52.                     // txtf.setBackground(Color.WHITE);
  53.             }
  54.         }
  55.     }
  56. // ...
  57.  
  58.  
  59. // ***
  60. // PanelDetailDrzave
  61. // ***
  62.  
  63. import gui.actions.MyKeyListener;
  64. import gui.handlers.InputHandler;
  65. public class PanelDetailDrzave extends GPanelDetail {
  66.  
  67.     private JTextField txtNazivDrzave;
  68.     InputHandler ih = new InputHandler();
  69.  
  70.  
  71. public PanelDetailDrzave(StanjeDijaloga mode) {
  72. // ...
  73.     KeyListener kl = new MyKeyListener();
  74.     txtNazivDrzave.addKeyListener(kl);
  75.  
  76. // ...
  77.     @Override
  78.     public boolean proveriIspravnostPodataka() {
  79.         System.out.println(ih.isOk() + " u proveriIspravnostPodataka()");
  80.         if (!ih.isOk()) {
  81.             ok = false;
  82.             txtNazivDrzave.setBackground(Color.RED);
  83.             System.out.println("jebe mater");
  84.         }
  85. // ...
  86.  
  87. /* IZLAZ KONZOLE:
  88. DETEKTOVAN NEDOZVOLJEN ZNAK!
  89. false u setteru InputHandler-a
  90. false u MyKeyListeneru
  91. true u proveriIspravnostPodataka() -- OVDE JE GRESKA!
  92.  
  93. */
Advertisement
Add Comment
Please, Sign In to add comment