/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Copyright (C) 2011-2012 Marchand Eric <ricoh51@free.fr>
This file is part of Freegressi.
Freegressi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Freegressi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Freegressi. If not, see <http://www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package freegressi.utils;
import freegressi.main.UndoRedo;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
/**
* Classe qui intercepte tous les caractères tapés au clavier et qui :
* * transforme les caractères latins en caractères grecs si F3 a été tapé avant
* * Appelle la classe statique UndoRedo si on tape Ctrl Z ou Ctrl Y
*
* @author marchand
*/
public class KeyBoard {
private static boolean greek = false;
private static KeyBoard clavier = null;
private static boolean undoredo = true;
public static KeyBoard getInstance() {
if (clavier == null) {
clavier = new KeyBoard();
}
return clavier;
}
/**
* Active ou desactive le Ctrl Z et le Ctrl Y, pour permettre les undo
* redo dans le JDialogEditColumns
* @param undoredo true pour activer, false pour desactiver
*/
public static void setUndoRedo(boolean undoredo){
KeyBoard.undoredo = undoredo;
}
private KeyBoard() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_RELEASED) {
if (e.getKeyCode() == KeyEvent.VK_F3) {
greek = true;
}
else if (undoredo && e.getKeyCode() == KeyEvent.VK_Z) {
int modifiersEx = e.getModifiersEx();
if (KeyEvent.getModifiersExText(modifiersEx).equals("Ctrl")) {
UndoRedo.getInstance().undo();
}
} else if (undoredo && e.getKeyCode() == KeyEvent.VK_Y) {
int modifiersEx = e.getModifiersEx();
if (KeyEvent.getModifiersExText(modifiersEx).equals("Ctrl")) {
UndoRedo.getInstance().redo();
}
}
} else if (greek && e.getID() == KeyEvent.KEY_TYPED) {
e.setKeyChar(latinToGreek(e.getKeyChar()));
}
//return true: supprime l'event de la queue de message
return false;
}
});
}
public static char latinToGreek(char c) {
greek = false;
switch (c) {
case 'a':
return '\u03B1'; // alpha
//case 'a' : return '\u237A'; // alpha plus beau mais ce n'est pas une lettre...
case 'A':
return '\u0391';
case 'b':
return '\u03B2'; // bêta
case 'B':
return '\u0392';
case 'c':
return '\u03C7'; // chi ??
case 'C':
return '\u03A7';
case 'd':
return '\u03B4'; // delta
case 'D':
return '\u0394';
case 'e':
return '\u03B5'; // epsilon
case 'E':
return '\u0395';
case 'f':
return '\u03c6'; // phi
case 'F':
return '\u03A6';
case 'g':
return '\u03B3'; // gamma
case 'G':
return '\u0393';
case 'h':
return '\u03B7'; // êta
case 'H':
return '\u0397';
case 'i':
return '\u03B9'; // ??
case 'I':
return '\u0399';
case 'j':
return '\u03C2'; // ??
case 'J':
return c;
case 'k':
return '\u03BA'; // kappa
case 'K':
return c;
case 'l':
return '\u03BB'; // lambda
case 'L':
return '\u039B';
case 'm':
return '\u03BC'; // mu
case 'M':
return '\u039C';
case 'n':
return '\u03BD'; // nu
case 'N':
return '\u039D';
case 'o':
return '\u03BF'; // ???
case 'O':
return '\u039F';
case 'p':
return '\u03C0'; // pi
case 'P':
return '\u03A0';
case 'q':
return '\u03B8'; // thêta
case 'Q':
return '\u0398';
case 'r':
return '\u03C1'; // rhô
case 'R':
return '\u03A1';
case 's':
return '\u03C3'; // sigma
case 'S':
return '\u03A3';
case 't':
return '\u03C4'; // tau
case 'T':
return '\u03A4';
case 'u':
return '\u03C5'; // nu rond
case 'U':
return '\u03A5';
case 'v':
return '\u03D6'; // oméga barre
case 'V':
return c;
case 'w':
return '\u03C9'; // oméga
case 'W':
return '\u03A9';
case 'x':
return '\u03BE'; // xi
case 'X':
return '\u039E';
case 'y':
return '\u03C8'; // psi
case 'Y':
return '\u03A8';
case 'z':
return '\u03B6'; // zêta
case 'Z':
return '\u0396';
}
return c;
}
}