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
| public class CodePostalDialog extends javax.swing.JDialog {
private JLabel jLabel1;
private JTextField txtFiltre;
private JButton btCancel;
private JXTable tCodePostaux;
private JScrollPane jscp;
private JButton btOK;
private static List<CodePostal> selectedCP;
private Session currentSession;
public CodePostalDialog(JFrame frame, boolean modal) {
super(frame, modal);
currentSession = HibernateUtil.getSessionFactory().openSession();
initGUI();
}
private void initGUI() {
try {
//START >> this
FormLayout thisLayout = new FormLayout(
"5dlu, 27dlu, 30dlu, 336dlu, 50dlu, 5dlu, 53dlu",
"5dlu, 13dlu, 5dlu, 11dlu, 280dlu, 5dlu, max(p;5dlu)");
getContentPane().setLayout(thisLayout);
this.setPreferredSize(new java.awt.Dimension(780, 540));
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent evt) {
currentSession.close();
}
});
setSize(getPreferredSize());
//START >> jLabel1
jLabel1 = new JLabel();
getContentPane().add(jLabel1, new CellConstraints("2, 2, 1, 1, default, default"));
jLabel1.setText("Filtre :");
//END << jLabel1
//START >> txtFiltre
txtFiltre = new JTextField();
getContentPane().add(txtFiltre, new CellConstraints("3, 2, 5, 1, default, default"));
//END << txtFiltre
//START >> btCancel
btCancel = new JButton();
getContentPane().add(btCancel, new CellConstraints("7, 7, 1, 1, default, default"));
btCancel.setText("Annuler");
btCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedCP = null;
dispose();
}
});
//END << btCancel
//START >> jscp
jscp = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(jscp, new CellConstraints("2, 4, 6, 2, fill, fill"));
jscp.setAutoscrolls(true);
//START >> tCodePostaux
tCodePostaux = new JXTable();
tCodePostaux.getSelectionMapper().setEnabled(false);
jscp.setViewportView(tCodePostaux);
jscp.setAutoscrolls(true);
//Récupère la liste des code postaux non attribués
List<CodePostal> lst = getData();
//Créé une EventList à partir de la List
EventList<CodePostal> lstCP = GlazedLists.eventList(lst);
//Mise en place du filtrage sur le code postal
MatcherEditor<CodePostal> m = new TextComponentMatcherEditor<CodePostal>(txtFiltre, new CodePostalFilterator());
((TextComponentMatcherEditor<CodePostal>) m).setMode(TextMatcherEditor.STARTS_WITH);
FilterList<CodePostal> fl = new FilterList<CodePostal>(lstCP, m);
//Correspondance entre le nom des colonnes et les propriétés de la classe CodePostal
String[] propertyNames = {"code", "villes"};
String[] columnLabels = {"Code postal", "Villes"};
TableFormat<CodePostal> tf = GlazedLists.tableFormat(CodePostal.class, propertyNames, columnLabels);
//Modèle de la JXTable
EventTableModel<CodePostal> etm = new EventTableModel<CodePostal>(fl, tf);
tCodePostaux.setModel(etm);
final EventSelectionModel<CodePostal> es = new EventSelectionModel<CodePostal>(fl);
es.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
System.out.println(es.getSelected());
}
});
es.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
tCodePostaux.setSelectionModel(es);
//END << tCodePostaux
//START >> btOK
btOK = new JButton();
btOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedCP = es.getSelected();
dispose();
}
});
getContentPane().add(btOK, new CellConstraints("5, 7, 1, 1, fill, default"));
btOK.setText("OK");
//END << btOK
//END << jscp
//END << this
pack();
getRootPane().setDefaultButton(btOK);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this.getParent(), "Impossible de récupérer les données. Veuillez vérifier la connection avec le serveur", "Erreur de connection", JOptionPane.ERROR_MESSAGE);
}
}
private List<CodePostal> getData() throws Exception{
currentSession.beginTransaction();
List<CodePostal> data = currentSession.createCriteria(CodePostal.class).add(Restrictions.sqlRestriction("id_secteur is null")).addOrder(Order.asc("code")).list();
currentSession.getTransaction().commit();
return data;
}
public static List<CodePostal> getCP(JFrame parent) {
selectedCP = null;
CodePostalDialog me = new CodePostalDialog(parent, true);
me.setLocationRelativeTo(null);
me.setVisible(true);
return selectedCP;
}
} |
Partager