Bonjour,
Voila mon problème est le suivant, à chaque fois que je fais un clique droit et que je clique dans le menu contextuel qui s'affiche l'action que je veux faire il m'effectue l'action plusieurs fois alors que je voudrais qu'il le fasse qu'une seule fois
Voici le code:
Merci d'avance
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; public class ArbreCanaux extends JPanel implements ActionListener { /** * */ private static final long serialVersionUID = 1L; /** * Instance de la classe Fenêtre afin de pouvoir appeler ses méthodes */ private Fenetre fenetre; /** * Arbre représentant la liste des canaux disponible dans le serveur */ private JTree arbre; /** * Racine de l'arbre des canaux */ private DefaultMutableTreeNode racine; /** * */ private DefaultTreeModel model = new DefaultTreeModel(this.racine); /** * Menu contextuel du panneau */ private JPopupMenu menu; /** * Attribut du menu permettant de rejoindre canal */ private JMenuItem rejoindre = new JMenuItem("Rejoindre le canal"); //$NON-NLS-1$ /** * Attribut du menu permettant d'ajouter un nouveau canal */ private JMenuItem ajouter = new JMenuItem("Ajouter un canal"); //$NON-NLS-1$ /** * Attribut du menu permettant de changer le sujet d'un canal */ private JMenuItem renommer = new JMenuItem("Changer le sujet"); //$NON-NLS-1$ /** * Attribut du menu permettant de supprimer canal */ private JMenuItem supprimer = new JMenuItem("Supprimer le canal"); //$NON-NLS-1$ private int verif; /** * Constructeur paramétré * @param fenetre1 : un pointeur vers la classe Fenêtre */ ArbreCanaux (Fenetre fenetre1) { this.fenetre = fenetre1; this.setBackground(Color.WHITE); } /* Getteurs ------------------------------------------------------------ */ /** * Obtenir le menu contextuel * @return le menu contextuel */ public JPopupMenu getMenu() { return this.menu; } /** * Obtenir l'arbre des canaux * @return l'arbre des canaux */ public JTree getArbre() { return this.arbre; } /* Setteurs ------------------------------------------------------------ */ /** * Définir la liste des canaux présents sur le serveur * @param nom_canaux : liste des canaux présents sur le serveur */ public void setArbre (List<List<Object>> nom_canaux) { if ( this.getArbre() != null ) { this.remove(this.getArbre()); } this.racine = new DefaultMutableTreeNode("Canaux"); //$NON-NLS-1$ for (List<Object> e : nom_canaux ) { String valeur = (String) e.get(0); if (valeur != null) { DefaultMutableTreeNode rep = new DefaultMutableTreeNode(valeur); this.racine.add(rep); } } this.arbre = new JTree(this.racine); this.model = new DefaultTreeModel(this.racine); this.arbre.setModel(this.model); this.arbre.setEditable(false); this.add(this.getArbre()); this.updateUI(); this.menu = new JPopupMenu(); this.getArbre().addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent event) { if ((event.getButton() == MouseEvent.BUTTON3) && (ArbreCanaux.this.getArbre().getLastSelectedPathComponent() != null)) { if (ArbreCanaux.this.getFenetre ().getClient().getDroits() == Droit.ADMINISTRATEUR) { if (!ArbreCanaux.this.getArbre().getSelectionPath ().getLastPathComponent ().toString ().equals ("Canaux")) { //$NON-NLS-1$ ArbreCanaux.this.getRenommer ().addActionListener(ArbreCanaux.this); ArbreCanaux.this.getSupprimer ().addActionListener(ArbreCanaux.this); ArbreCanaux.this.getRejoindre ().addActionListener(ArbreCanaux.this); ArbreCanaux.this.getMenu ().add(ArbreCanaux.this.getRejoindre ()); ArbreCanaux.this.getMenu ().add(ArbreCanaux.this.getRenommer ()); ArbreCanaux.this.getMenu ().add(ArbreCanaux.this.getSupprimer ()); } else { ArbreCanaux.this.getAjouter ().addActionListener(ArbreCanaux.this); ArbreCanaux.this.getMenu ().add(ArbreCanaux.this.getAjouter ()); } } else { if (!ArbreCanaux.this.getArbre().getSelectionPath ().getPathComponent (0).toString ().equals ("Canaux")) { //$NON-NLS-1$ ArbreCanaux.this.getRejoindre ().addActionListener(ArbreCanaux.this); ArbreCanaux.this.getMenu ().add(ArbreCanaux.this.getRejoindre ()); } } ArbreCanaux.this.getMenu().show(event.getComponent(), event.getX(), event.getY()); } } }); } /** * * @return Renvoi la fenêtre actuel. */ public Fenetre getFenetre () { return this.fenetre; } /** * * @return Renvoi l'item ajouter. */ public JMenuItem getAjouter () { return this.ajouter; } /** * * @return Renvoi l'item rejoindre. */ public JMenuItem getRejoindre () { return this.rejoindre; } /** * * @return Renvoi l'item renommer. */ public JMenuItem getRenommer () { return this.renommer; } /** * * @return Renvoi l'item supprimer. */ public JMenuItem getSupprimer () { return this.supprimer; } /* Methodes publiques ================================================ */ /** * Supprime un canal dans l'arbre * @param nom_canal : nom du canal a supprimer */ public void supprimerNoeud (String nom_canal) { for (int i = 0 ; i < this.racine.getChildCount() ; i++ ) { if ( this.racine.getChildAt(i).toString().equals(nom_canal) ) { this.model.removeNodeFromParent((DefaultMutableTreeNode) this.racine.getChildAt(i)); break; } } } /** * Ajouter un canal dans l'arbre * @param nom_canal : nom du canal a ajouter */ public void ajouterNoeud (String nom_canal) { DefaultMutableTreeNode canalSelectionne = this.racine; DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(nom_canal); this.model.insertNodeInto (newNode,canalSelectionne, canalSelectionne.getChildCount()); TreeNode[] nodes = this.model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); this.arbre.scrollPathToVisible(path); } /** * Écouteur du menu contextuel de la liste des canaux */ @Override public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); if ( "Ajouter un canal".equals(ac)) { //$NON-NLS-1$ this.fenetre.setPopup(TypePopup.NOUVEAU_CANAL); ArrayList<String> res = this.fenetre.getPopup().afficher(); if ( res.size() == 2) { this.fenetre.getClient().creerCanal(res.get(0),res.get(1)); this.fenetre.getPopup ().setVisible (false); } } if ( this.arbre.getLastSelectedPathComponent().toString() != this.racine.toString() ) { if ( "Rejoindre le canal".equals(ac)) { //$NON-NLS-1$ this.fenetre.getClient().rejoindreCanal(this.arbre.getLastSelectedPathComponent().toString()); } else if ( "Supprimer le canal".equals(ac)) { //$NON-NLS-1$ this.fenetre.getClient().supprimerCanal(this.arbre.getLastSelectedPathComponent().toString()); this.verif++; } else if ( "Changer le sujet".equals(ac)) { //$NON-NLS-1$ String nom = ""; //$NON-NLS-1$ while ( "".equals(nom)) { //$NON-NLS-1$ nom = JOptionPane.showInputDialog(null, "Nouveau sujet", "Changer le sujet", JOptionPane.QUESTION_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ if ( !"".equals(nom) ) { //$NON-NLS-1$ this.fenetre.getClient().changerSujetCanal(this.arbre.getLastSelectedPathComponent().toString(),nom); break; } } } } } }![]()
Partager