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
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Stack;
public class IHMListe2 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("decroissant", 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;
private Stack<List<String>> backup = new Stack<List<String>>();
public IHMListe2(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("resultat de la recherche de : " + saisie.getText() + " --> "+ res);
}
else if (ae.getSource() == boutonRetirer) {
res = retirerDeLaListeTousLesElementsCommencantPar(saisie.getText());
afficheur.setText("resultat du retrait de tous les elements commencant 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(" --> ??? ");
}
else if (ae.getSource() == boutonAnnuler) {
if ( !backup.empty() ) liste = backup.pop();
}
texte.setText(liste.toString());
} catch(Exception e){
afficheur.setText( e.toString());
}
}
public void itemStateChanged(ItemEvent ie){
saveListe(liste);
if (ie.getSource() == ordreCroissant)
Collections.sort(liste);
else if (ie.getSource() == ordreDecroissant)
Collections.sort(liste,
new Comparator<String>() {
public int compare(String s1, String s2) { return -s1.compareTo(s2); }
}
);
texte.setText(liste.toString());
}
private boolean retirerDeLaListeTousLesElementsCommencantPar(String prefixe){
boolean resultat = false;
int i=0;
int taille = liste.size();
String s;
String p = prefixe;
saveListe(liste);
if ( Pattern.matches("[ * ? ( ) [ ] { } ^ $ | + ].*",prefixe) ) p = "\\"+prefixe;
while ( !resultat ){
if ( Pattern.matches(p+".*", (s=liste.get(i))) ) {
for(int j=0; j<occurrences.get(s); j++)
liste.remove(s);
occurrences.put(s,0);
taille = liste.size();
} else i++;
if ( i >= taille ) resultat = true;
}
return resultat;
}
private void saveListe(List<String> l) {
backup.push( l.subList(0,l.size()) );
}
} |
Partager