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
|
public class PanelAddEspace extends JPanel {
private Espace espace;
private ListGarantModele model;
private JList listGarant;
private JButton btnAddGarant;
private JButton btnDeleteGarant;
/**
* Create the panel.
*
* @throws DocumentException
*/
public PanelAddEspace (Espace espace, final JFrame frame)
throws DocumentException {
this.espace = espace;
/*
* On charge la liste des garants (leurs identifiants) de l'Espace puis, pour chacun
* des éléments de la liste, on cherche le nom du garant dans l'annuaire
* LDAP du site.
* S'il n'est pas trouvé, on ne le met pas
*/
String[] tabNomGar = loadGarantsFromEspace ();
model = new ListGarantModele (tabNomGar);
listGarant = new JList (model);
listGarant.setAlignmentY (0.0f);
listGarant.setAlignmentX (0.0f);
listGarant.setVisibleRowCount (4);
listGarant.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
scrollPane.setViewportView (listGarant);
btnAddGarant = new JButton ("Ajouter");
btnAddGarant.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
DialogSearchEmp dialogGarant = new DialogSearchEmp (frame);
dialogGarant
.setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE);
dialogGarant.setVisible (true);
if (!dialogGarant.hasResult ()) return;
String[] result = dialogGarant.getResult ();
System.out.println ("debut");
/* Cette fonction renvoie vrai si l'élément a été rajouté, et faux
* s'il existe déjà
*/
if (PanelAddEspace.this.espace.addGarant (result[0])) {
System.out.println ("ajout ok");
((ListGarantModele) listGarant.getModel ())
.addElement (result[1]);
}
System.out.println ("fin");
// listGarant.clearSelection ();
repaint ();
revalidate ();
} // actionPerformed
});
GridBagConstraints gbc_btnAddGarant = new GridBagConstraints ();
panelOptionListGar.add (btnAddGarant, gbc_btnAddGarant);
btnDeleteGarant = new JButton ("Supprimer");
GridBagConstraints gbc_btnDeleteGarant = new GridBagConstraints ();
btnDeleteGarant.addActionListener (new ActionListener () {
@Override
public void actionPerformed (ActionEvent arg0) {
int index = listGarant.getSelectedIndex ();
String value = (String) listGarant.getSelectedValue ();
/*
* On récupere le nom et le prénom afin de chercher
* l'identifiant dans l'annuaire LDAP
*/
String[] tabEntree = value.split ("\\s");
String nom = "";
String prenom = "";
for (String string : tabEntree) {
if (string.matches ("[a-z]*"))
prenom += string + " ";
else if (string.matches ("[A-Z]*")) nom += string + " ";
}
prenom = prenom.substring (0, prenom.length () - 1);
nom = nom.substring (0, nom.length () - 1);
/*
* On recupere le premier élément de la liste (forcément un
* employé qui correspond)
*/
String[] garant = null;
DAOLdap dao;
try {
dao = new DAOLdap ();
LDAPConnect ldap = new LDAPConnect (dao.getHost (), dao
.getUser (), dao.getPasswd (), dao.getRoot ());
List <String[]> list = ldap.searchEmp (nom, prenom);
garant = list.get (0);
System.out.println (garant.getClass ());
for (String string : garant) {
System.out.println (string);
}
}
catch (DocumentException e) {
JOptionPane
.showMessageDialog (
frame,
"Erreur, réessayez.\nSi l'erreur persiste, redémarrez le logiciel "
+ "ou vérifiez vos paramètres de configuration.",
"Erreur", JOptionPane.ERROR_MESSAGE);
}
catch (NamingException e) {
JOptionPane
.showMessageDialog (
frame,
"Erreur, réessayez.\nSi l'erreur persiste, redémarrez le logiciel "
+ "ou vérifiez vos paramètres de configuration.",
"Erreur", JOptionPane.ERROR_MESSAGE);
}
PanelAddEspace.this.espace.removeGarant (garant[0]);
// ((ListGarantModele) listGarant.getModel ()).remove (index);
model.remove (index);
// listGarant.setModel (model);
int size = model.getSize ();
System.out.println (size);
if (size == 0) { // Nobody's left, disable firing.
btnDeleteGarant.setEnabled (false);
}
else { // Select an index.
if (index == model.getSize ()) {
// removed item in last position
index--;
}
listGarant.setSelectedIndex (-1);
listGarant.ensureIndexIsVisible (0);
}
repaint ();
revalidate ();
} // actionPerformed ()
});
panelOptionListGar.add (btnDeleteGarant, gbc_btnDeleteGarant);
}
private String[] loadGarantsFromEspace () throws DocumentException {
List <String> listGar = this.espace.getGarants ();
String[] tabNomGar = new String[listGar.size ()];
for (String idGar : listGar) {
DAOLdap dao = new DAOLdap ();
LDAPConnect con = new LDAPConnect (dao.getHost (), dao.getUser (),
dao.getPasswd (), dao.getRoot ());
try {
tabNomGar[listGar.indexOf (idGar)] = (con.exists (idGar) ? con
.getPrenom (idGar).toLowerCase ()
+ " "
+ con.getNom (idGar).toUpperCase () : "");
}
catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
return tabNomGar;
} // loadGarants () |
Partager