IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

SWT/JFace Java Discussion :

Keylistener sur le composite parent


Sujet :

SWT/JFace Java

  1. #1
    Membre habitué Avatar de pingoui
    Homme Profil pro
    Activité professionnelle sans liens avec le developpement
    Inscrit en
    Juillet 2004
    Messages
    582
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Activité professionnelle sans liens avec le developpement
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2004
    Messages : 582
    Points : 185
    Points
    185
    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 : 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
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  2. #2
    Membre expert
    Avatar de Gueritarish
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2007
    Messages
    1 800
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    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 800
    Points : 3 919
    Points
    3 919
    Par défaut
    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.

  3. #3
    Membre habitué Avatar de pingoui
    Homme Profil pro
    Activité professionnelle sans liens avec le developpement
    Inscrit en
    Juillet 2004
    Messages
    582
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Activité professionnelle sans liens avec le developpement
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2004
    Messages : 582
    Points : 185
    Points
    185
    Par défaut
    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

  4. #4
    Membre expert
    Avatar de Gueritarish
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2007
    Messages
    1 800
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    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 800
    Points : 3 919
    Points
    3 919
    Par défaut
    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.

  5. #5
    Membre habitué Avatar de pingoui
    Homme Profil pro
    Activité professionnelle sans liens avec le developpement
    Inscrit en
    Juillet 2004
    Messages
    582
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Activité professionnelle sans liens avec le developpement
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2004
    Messages : 582
    Points : 185
    Points
    185
    Par défaut
    Parfait, c'est exactement ce qu'il me faut

    Merci beaucoup

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. debutant: un keyListener sur un objet non graphique
    Par ataya dans le forum API standards et tierces
    Réponses: 1
    Dernier message: 18/05/2006, 18h45
  2. KeyListener sur JDialog
    Par seiryujay dans le forum Agents de placement/Fenêtres
    Réponses: 17
    Dernier message: 18/04/2006, 18h40
  3. JLabel et keylistener sur un JPanel
    Par Azanael dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 19/06/2005, 21h41
  4. Probleme heritage : References sur la table parente
    Par Belgar dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 03/05/2005, 22h11
  5. Composant TImage sur fenêtre MDI Parent
    Par charles.apollon dans le forum C++Builder
    Réponses: 6
    Dernier message: 10/02/2005, 15h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo