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
|
//les imports
public class IHMListe extends JPanel implements ActionListener, ItemListener {
private JPanel cmd = new JPanel();
private JLabel afficheur = new JLabel();
private JTextField saisie = new JTextField();
private JPanel panelBoutons = new JPanel();
private JButton boutonRechercher = new JButton("rechercher");
private JButton boutonRetirer = new JButton("retirer");
private CheckboxGroup mode = new CheckboxGroup();
private Checkbox ordreCroissant = new Checkbox("croissant", mode, false);
private Checkbox ordreDecroissant = new Checkbox("décroissant", mode, false);
private JButton boutonOccurrences = new JButton("occurrence");
private JButton boutonAnnuler = new JButton("annuler");
private TextArea texte = new TextArea();
private List<String> liste;
private Map<String, Integer> occurrences;
public IHMListe(List<String> liste, Map<String, Integer> occurrences) {
this.liste = liste;
this.occurrences = occurrences;
cmd.setLayout(new GridLayout(3, 1));
cmd.add(afficheur);
cmd.add(saisie);
panelBoutons.setLayout(new FlowLayout(FlowLayout.LEFT));
panelBoutons.add(boutonRechercher);
panelBoutons.add(boutonRetirer);
panelBoutons.add(new JLabel("tri du texte :"));
panelBoutons.add(ordreCroissant);
panelBoutons.add(ordreDecroissant);
panelBoutons.add(boutonOccurrences);
panelBoutons.add(boutonAnnuler);
cmd.add(panelBoutons);
afficheur.setText(liste.getClass().getName() + " et "
+ occurrences.getClass().getName());
texte.setText(liste.toString());
setLayout(new BorderLayout());
add(cmd, "North");
add(texte, "Center");
boutonRechercher.addActionListener(this);
boutonRetirer.addActionListener(this);
boutonOccurrences.addActionListener(this);
boutonAnnuler.addActionListener(this);
ordreCroissant.addItemListener(this);
ordreDecroissant.addItemListener(this);
}
public void actionPerformed(ActionEvent ae) {
try {
boolean res = false;
if (ae.getSource() == boutonRechercher || ae.getSource() == saisie) {
res = liste.contains(saisie.getText());
Integer occur = occurrences.get(saisie.getText());
afficheur.setText("résultat de la recherche de : "
+ saisie.getText() + " --> " + res);
} else if (ae.getSource() == boutonRetirer) {
res = retirerDeLaListeTousLesElementsCommencantPar(saisie
.getText());
afficheur
.setText("résultat du retrait de tous les éléments commençant par --> "
+ saisie.getText() + " : " + res);
} else if (ae.getSource() == boutonOccurrences) {
Integer occur = occurrences.get(saisie.getText());
if (occur != null)
afficheur.setText(" --> " + occur + " occurrence(s)");
else
afficheur.setText(" --> ??? ");
}
texte.setText(liste.toString());
} catch (Exception e) {
afficheur.setText(e.toString());
}
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == ordreCroissant)
Collections.sort(liste);
else if (ie.getSource() == ordreDecroissant) {
Stack<String> stack = new Stack<String>();
Collections.sort(liste);
for (Iterator<String> it = liste.iterator() ; it.hasNext();) {
stack.push(it.next()) ;
}
liste.clear(); //vider liste
while (!stack.isEmpty()) {
liste.add(stack.pop());
}
}
texte.setText(liste.toString());
}
private boolean retirerDeLaListeTousLesElementsCommencantPar(String prefixe) {
boolean resultat = false;
for (Iterator <String> it = liste.iterator() ; it.hasNext();) {
String s = it.next();
if (s.startsWith(prefixe)) {
it.remove();
occurrences.put(prefixe, 0);
resultat = true;
}
}
return resultat;
}
} |
Partager