Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. //With a few tests, it seems to work
  2. //Event handling may be messed up though
  3. class GrAccentsTextField extends TextField{
  4. private final static HashMap<String, String> accented = new HashMap<String, String>();
  5. private LinkedList<String> queue = new LinkedList<String>();
  6. private boolean prevCharIsAccent = false;
  7.  
  8. static{
  9. accented.put("α", "ά");
  10. accented.put("ε", "έ");
  11. accented.put("η", "ή");
  12. accented.put("ι", "ί");
  13. accented.put("ο", "ό");
  14. accented.put("υ", "ύ");
  15. accented.put("ω", "ώ");
  16. accented.put("Α", "Ά");
  17. accented.put("Ε", "Έ");
  18. accented.put("Η", "Ή");
  19. accented.put("Ι", "Ί");
  20. accented.put("Ο", "Ό");
  21. accented.put("Υ", "Ύ");
  22. accented.put("Ω", "Ώ");
  23. }
  24.  
  25. public GrAccentsTextField(){
  26. this.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
  27. @Override
  28. public void handle(KeyEvent event) {
  29. final String typedChar = event.getText();
  30. if (prevCharIsAccent && accented.containsKey(typedChar)) {
  31. queue.add(typedChar);
  32. } else {
  33. queue.clear();
  34. }
  35.  
  36. prevCharIsAccent = !prevCharIsAccent && event.getCode() == KeyCode.UNDEFINED;
  37. if(prevCharIsAccent){
  38. queue.add(typedChar);
  39. }
  40. }
  41. });
  42.  
  43. this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
  44. @Override
  45. public void handle(KeyEvent event) {
  46. if(!queue.isEmpty() && queue.peek().equals(event.getCharacter())){
  47. final String typedChar = queue.remove();
  48. if(accented.containsKey(typedChar)){
  49. Event.fireEvent(event.getTarget(), new KeyEvent(
  50. event.getSource(),
  51. event.getTarget(),
  52. event.getEventType(),
  53. accented.get(typedChar),
  54. accented.get(typedChar),
  55. event.getCode(),
  56. event.isShiftDown(),
  57. event.isControlDown(),
  58. event.isAltDown(),
  59. event.isMetaDown())
  60. );
  61. }
  62. event.consume();
  63. }
  64. }
  65. });
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement