Salut tout le monde!

J'ai implémenté le Key Binding en me basant sur le tuto Sun, mais je l'ai fait à la bourrin on va dire

Ce que j'aimerai c'est avoir votre avis sur la meilleure méthodologie à appliquer pour généraliser au maximum l'utilisation de ce procédé.

Je m'explique...
En fait, j'aimerai pouvoir créer des IHM utilisant le Key Binding, chacune à sa sauce, mais sans avoir à redéfinir tout le mécanisme à chaque fois.
Pour l'instant, je me suis créer ce genre de classe :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 
public abstract class MyDialog extends JDialog {
 
	public MyDialog(Dialog arg0, boolean arg1) throws HeadlessException {
		super(arg0, arg1);
        initKeyManagement();
	}
 
	public MyDialog(Dialog arg0, String arg1, boolean arg2, GraphicsConfiguration arg3) throws HeadlessException {
		super(arg0, arg1, arg2, arg3);
		initKeyManagement();
	}
 
	public MyDialog(Dialog arg0, String arg1, boolean arg2) throws HeadlessException {
		super(arg0, arg1, arg2);
		initKeyManagement();
	}
 
	public MyDialog(Dialog arg0, String arg1) throws HeadlessException {
		super(arg0, arg1);
		initKeyManagement();
	}
 
	public MyDialog(Dialog arg0) throws HeadlessException {
		super(arg0);
		initKeyManagement();
	}
 
	public MyDialog(Frame arg0, boolean arg1) throws HeadlessException {
		super(arg0, arg1);
		initKeyManagement();
	}
 
	public MyDialog(Frame arg0, String arg1, boolean arg2, GraphicsConfiguration arg3) {
		super(arg0, arg1, arg2, arg3);
		initKeyManagement();
	}
 
	public MyDialog(Frame arg0, String arg1, boolean arg2) throws HeadlessException {
		super(arg0, arg1, arg2);
		initKeyManagement();
	}
 
	public MyDialog(Frame arg0, String arg1) throws HeadlessException {
		super(arg0, arg1);
		initKeyManagement();
	}
 
	public MyDialog(Frame arg0) throws HeadlessException {
		super(arg0);
		initKeyManagement();
	}
 
	public MyDialog(){
		super();
		initKeyManagement();
	}
 
 
 
 
	protected void initKeyManagement() {
		Object escapeCommand = new Object();
		// Object enterCommand = new Object();
		KeyStroke ksEscape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
		// KeyStroke ksEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
		InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		InputMap im2 = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		InputMap im3 = getRootPane().getInputMap(JComponent.WHEN_FOCUSED);
		ActionMap am = getRootPane().getActionMap();
 
		im.put(ksEscape, escapeCommand);
		im2.put(ksEscape, escapeCommand);
		im3.put(ksEscape, escapeCommand);
		// im.put(ksEnter, enterCommand);
 
		if (am == null)
			am = new ActionMap();
 
		am.put(escapeCommand, new MyKeyAction(ksEscape.getKeyCode()));
		// am.put(enterCommand, new MyKeyAction(ksEnter.getKeyCode()));
	}
 
	protected abstract void escapeActionPerformed();
 
 
 
 
 
	class MyKeyAction extends AbstractAction {
		protected int keyCode;
 
 
		public MyKeyAction(int keyCode) {
			this.keyCode = keyCode;
		}
 
		public void actionPerformed(ActionEvent arg0) {
			switch (keyCode) {
			case KeyEvent.VK_ESCAPE :
//				bCancel.doClick();
				escapeActionPerformed();
				break;
 
//			case KeyEvent.VK_ENTER :
//				enterActionPerformed();
//				break;
 
			default :
				break;
			}
		}
	}
}
Ensuite, chacune de mes JDialog étend cette classe et redéfinit la méthode abstraite
Code : Sélectionner tout - Visualiser dans une fenêtre à part
escapeActionPerformed()
de manière à réaliser le traitement adéquat (ça ferme la JDialog, mais il y a parfois d'autres traitements à réaliser).

Le "problème", c'est que certaines IHM doivent répondre à d'autres touches (par exemple la touche "Entrée"), et certaines IHM peuvent avoir des comportements différents sur la pression de ces touches suivant le composant ayant le focus (par exemple, sur une JDialog contenant un champs texte, la pression de la touche "Entrée" doit soit valider la saisie du champs texte si ce dernier a le focus, soit valider toute la JDialog [clic sur bouton OK] si c'est un autre composant qui a le focus).

Ce que j'aimerai c'est trouver un mécanisme assez général me permettant de gérer tout ceci, sans tout redéfinir à chaque fois.
Par exemple, j'ai l'impression que la classe MyKeyAction que j'ai créée n'est peut-être pas la meilleure solution et qu'il vaudrait mieux créer une nouvelle AbstractAction à chque fois. Mais j'aimerai votre avis.