Précédent   Forum du club des développeurs et IT Pro > Java > Interfaces Graphiques en Java > SWT/JFace
SWT/JFace Forum d'entraide pour les API SWT/JFace. Avant de poster -> FAQ SWT/JFace
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 22/07/2012, 19h24   #1
pingoui
Membre habitué
 
Avatar de pingoui
 
Inscription : juillet 2004
Messages : 534
Détails du profil
Informations personnelles :
Âge : 32

Informations forums :
Inscription : juillet 2004
Messages : 534
Points : 129
Points : 129
Par défaut Keylistener sur le composite parent

Bonjour,

J'ai un composite parent (Planning) contenant des composites enfants (Cellules du planning).
Afin de faire, fonctionner mon KeyListener, pour sélectionner mes cellules avec le clavier, j'ai ajouter un KeyListener à chaque cellule...Bof...

Code :
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Widget;
 
public class PlanningComposite extends Composite implements MouseListener, FocusListener, KeyListener{
 
    private Color activeSelectionBackground;
    private Color inactiveSelectionBackground;
    private Color activeSelectionForeground;
    private Color inactiveSelectionForeground;
	private static final int NUMBER_DAYS_PER_WEEK = 5;
	private static final int NUMBER_AGENT = 6;
	private static final String[] dayTitles= {"","lundi","mardi","mercredi","jeudi","vendredi"};
 
	private PlanningCell[] pCells;
	private PlanningCell selectedCell;
	private int selectedCellIndex;
 
 
	public PlanningComposite(Composite parent, int style) {
		super(parent, style);
		this.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
 
        activeSelectionBackground = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
        inactiveSelectionBackground = getBackground();
        activeSelectionForeground = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
        inactiveSelectionForeground = getForeground();
 
 
		//Rectangle clientRect = getClientArea();
	    //this.setSize(clientRect.width, clientRect.height);
 
        GridLayout gridLayout = new GridLayout();
        gridLayout.makeColumnsEqualWidth = true;
        gridLayout.numColumns = NUMBER_DAYS_PER_WEEK + 1;
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        gridLayout.horizontalSpacing = 0;
        gridLayout.verticalSpacing = 0;
        setLayout(gridLayout);
 
 
		drawHeader();
		drawCells();
 
        addMouseListener(this);
        addFocusListener(this);
        addKeyListener(this);
 
	}
 
	private void drawHeader() {
		for (int i=0;i < NUMBER_DAYS_PER_WEEK + 1;i++){
            Label label = new Label(this, SWT.CENTER);
            label.setText(dayTitles[i]);
            label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
 
		}
 
	}
 
	private void drawCells() {	
		int nbOfCells = (NUMBER_DAYS_PER_WEEK +1) * NUMBER_AGENT;
		pCells = new PlanningCell[nbOfCells];
		for (int i=0;i < nbOfCells;i++){
            	PlanningCell cell = new PlanningCell(this,SWT.BORDER);
            	cell.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
            	pCells[i] = cell;
            	cell.addMouseListener(this);
            	cell.addKeyListener(this);
        }
	}
 
    private int findCell(Widget cellComposite) {
        for (int i = 0; i < pCells.length; i++) {
            if (pCells[i] == cellComposite) {
            	selectedCellIndex  = i;
                return i;
            }
        }
        return -1;
    }
 
    private void selectCell(int index) {
    	//On sort de la méthode si la cellule n'est pas selectionnable
    	if (index <=0  || index >= (NUMBER_DAYS_PER_WEEK +1) * NUMBER_AGENT 
    			|| index != 0 && ( index % 6 == 0 ) ){
    		return;
    	};
    	//Déselection de la cellule selectionné précédemment 
    	if (selectedCell != null){
        	selectedCell.setBackground(getBackground());
        	selectedCell.setForeground(getBackground());
    	}
    	//Sélection de la cellule
    	selectedCell = pCells[index];
    	selectedCellIndex = index;
    	selectedCell.setBackground(getSelectionBackgroundColor());
    	selectedCell.setForeground(getSelectionForegroundColor());
    }
 
    private Color getSelectionBackgroundColor() {
        return isFocusControl() ? activeSelectionBackground : inactiveSelectionBackground;
    }
 
    private Color getSelectionForegroundColor() {
        return isFocusControl() ? activeSelectionForeground : inactiveSelectionForeground;
    }
 
	@Override
	public boolean isFocusControl() {
        for (Control control = getDisplay().getFocusControl(); control != null; control = control.getParent()) {
            if (control == this) {
                return true;
            }
        }
        return false;
	}
 
	@Override
	public void focusGained(FocusEvent e) {
		selectedCell.setBackground(getSelectionBackgroundColor());
		selectedCell.setForeground(getSelectionForegroundColor());	
	}
 
	@Override
	public void focusLost(FocusEvent e) {
		selectedCell.setBackground(getSelectionBackgroundColor());
		selectedCell.setForeground(getSelectionForegroundColor());
 
	}
 
	@Override
	public void mouseDoubleClick(MouseEvent e) {
 
	}
 
	@Override
	public void mouseDown(MouseEvent e) {
        if (e.button == 1) { // Left click
            setFocus();
            if (e.widget instanceof PlanningCell) {
                int index = findCell(e.widget);
                selectCell(index);
            }
        }
 
	}
 
	@Override
	public void mouseUp(MouseEvent e) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void keyPressed(KeyEvent e) {
		switch (e.keyCode) {
			case SWT.ARROW_LEFT:
				selectCell(selectedCellIndex - 1);
				break;
 
			case SWT.ARROW_RIGHT:
				selectCell(selectedCellIndex + 1);
				break;
 
			case SWT.ARROW_UP:
				selectCell(selectedCellIndex - 6);
				break;
 
			case SWT.ARROW_DOWN:
				selectCell(selectedCellIndex + 6);
				break;
		}
 
	}	
 
	@Override
	public void keyReleased(KeyEvent e) {
	}
 
}
Si je ne fais pas
Code :
1
2
 
cell.addKeyListener(this);
dans drawCells() , le key listener ne fonctionne pas ....Une idée pour mettre mon Listener sur le composite parent (Planning) et non sur tous les enfants (Cellules du planning)?

Cordialement
pingoui est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/07/2012, 11h52   #2
Gueritarish
Modérateur
 
Avatar de Gueritarish
 
Homme Marc
Développeur Java
Inscription : mai 2007
Messages : 1 567
Détails du profil
Informations personnelles :
Nom : Homme Marc
Âge : 28
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations professionnelles :
Activité : Développeur Java
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : mai 2007
Messages : 1 567
Points : 3 426
Points : 3 426
Salut,

A mon sens, le Composite ne recevra pas les évènements du clavier (ils sont interceptés par les Composite fils qui ont le focus). Après, je vois pas où est le problème. Tu peux très bien te faire une classe qui hérite de Composite et ajouter le listener directement dans le constructeur. Il faut juste penser à enlever le listener dans le dispose() et le tour est joué.

Voilà, à+
Gueritarish
__________________
Pas de questions technique par MP, les forums sont là pour ça.

Le 5 et 6 juin à Toulouse, la première EclipseCon France !
Gueritarish est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/07/2012, 12h25   #3
pingoui
Membre habitué
 
Avatar de pingoui
 
Inscription : juillet 2004
Messages : 534
Détails du profil
Informations personnelles :
Âge : 32

Informations forums :
Inscription : juillet 2004
Messages : 534
Points : 129
Points : 129
Bonjour,

je me pose peut être trop de questions mais je me suis dit qu'avoir un listener par cellule de mon planning, cela fait beaucoup !
Si mon planning gère une équipe de 30 personnes, j'ai 150 keyListener.

Cdt,

pingoui
pingoui est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/07/2012, 13h40   #4
Gueritarish
Modérateur
 
Avatar de Gueritarish
 
Homme Marc
Développeur Java
Inscription : mai 2007
Messages : 1 567
Détails du profil
Informations personnelles :
Nom : Homme Marc
Âge : 28
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations professionnelles :
Activité : Développeur Java
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : mai 2007
Messages : 1 567
Points : 3 426
Points : 3 426
Tu peux te servir de la classe EventHandler. Tu as un très bon tutoriel là dessus. Ça va grandement diminué l'impact mémoire de tes listeners.

Voilà, à+
Gueritarish
__________________
Pas de questions technique par MP, les forums sont là pour ça.

Le 5 et 6 juin à Toulouse, la première EclipseCon France !
Gueritarish est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/07/2012, 20h48   #5
pingoui
Membre habitué
 
Avatar de pingoui
 
Inscription : juillet 2004
Messages : 534
Détails du profil
Informations personnelles :
Âge : 32

Informations forums :
Inscription : juillet 2004
Messages : 534
Points : 129
Points : 129
Parfait, c'est exactement ce qu'il me faut

Merci beaucoup
pingoui est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 05h08.


 
 
 
 
Partenaires

Hébergement Web