From ad19e8d0438bfc8bb91c241bd9ee518b5e6e95be Mon Sep 17 00:00:00 2001 From: Brian Paterni Date: Sat, 3 Dec 2011 14:45:49 -0600 Subject: [PATCH] tie UI Student table to database student table also implement 'master-detail' UI pattern to populate session list with sessions corresponding to the student the user selects from student list... Right now just Repetoire sessions are lists, the other two report types 'should' be trivial to add later. --- gui-playground/src/panther/db/Student.java | 2 +- gui-playground/src/panther/ui/PtMainPanel.java | 32 ++++++++++++++-- .../src/panther/ui/SessionTableModel.java | 43 +++++++++++++++++----- .../src/panther/ui/StudentSelectionListener.java | 39 ++++++++++++++++++++ gui-playground/src/panther/ui/StudentTable.java | 21 ----------- 5 files changed, 102 insertions(+), 35 deletions(-) create mode 100644 gui-playground/src/panther/ui/StudentSelectionListener.java diff --git a/gui-playground/src/panther/db/Student.java b/gui-playground/src/panther/db/Student.java index 835369f..78aaea6 100644 --- a/gui-playground/src/panther/db/Student.java +++ b/gui-playground/src/panther/db/Student.java @@ -33,6 +33,6 @@ public class Student // public String getName() // { return name; } - public int getId() + public int get_id() { return id; } } diff --git a/gui-playground/src/panther/ui/PtMainPanel.java b/gui-playground/src/panther/ui/PtMainPanel.java index 6115160..af6fdca 100644 --- a/gui-playground/src/panther/ui/PtMainPanel.java +++ b/gui-playground/src/panther/ui/PtMainPanel.java @@ -2,20 +2,44 @@ package panther.ui; import java.awt.*; import javax.swing.*; +import javax.swing.event.*; +import javax.swing.table.*; public class PtMainPanel extends JPanel { public PtMainPanel() { - //setLayout(new BorderLayout()); - /* BoxLayout along x-axis may not be what we want here, this is just a * quick and dirty solution to get panels to resize with frame resize * events. */ setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); - add(new StudentPanel()); - add(new SessionPanel()); + JTable tbl_student = new StudentTable(); + JTable tbl_session = new SessionTable(); + + tbl_student.getSelectionModel().addListSelectionListener( + new StudentSelectionListener(tbl_student, tbl_session)); + + + JPanel pnl_student = new JPanel(); + + pnl_student.setLayout(new BorderLayout()); + pnl_student.setBorder(BorderFactory.createTitledBorder( + BorderFactory.createTitledBorder("Student"))); + pnl_student.add(new JScrollPane(tbl_student)); + + JPanel pnl_session = new JPanel(); + + pnl_session.setLayout(new BorderLayout()); + pnl_session.setBorder(BorderFactory.createTitledBorder( + BorderFactory.createTitledBorder("Session"))); + pnl_session.add(new JScrollPane(tbl_session)); + + add(pnl_student); + add(pnl_session); + +// add(new StudentPanel()); +// add(new SessionPanel()); } } diff --git a/gui-playground/src/panther/ui/SessionTableModel.java b/gui-playground/src/panther/ui/SessionTableModel.java index fd4d54e..f068474 100644 --- a/gui-playground/src/panther/ui/SessionTableModel.java +++ b/gui-playground/src/panther/ui/SessionTableModel.java @@ -4,18 +4,26 @@ import java.util.Date; import javax.swing.*; import javax.swing.table.AbstractTableModel; +import panther.db.*; + public class SessionTableModel extends AbstractTableModel { - private final static int N_DATES = 5; - private Date[] dates; + private int n_rows; + + private RepetoireList l_repetoire; +// private final static int N_DATES = 5; +// private Date[] dates; public SessionTableModel() { - dates = new Date[N_DATES]; - for (int i=0; i 0) { + if (l_repetoire != null) { + return l_repetoire.get(row); + } +// return dates[row]; } return null; } + + private void update_n_rows() + { + n_rows = l_repetoire.size(); + } + + public void update_student_sessions(int sid) + { + l_repetoire = MySQLManager.get_student_repetoire(sid); + + update_n_rows(); + + fireTableDataChanged(); + } } diff --git a/gui-playground/src/panther/ui/StudentSelectionListener.java b/gui-playground/src/panther/ui/StudentSelectionListener.java new file mode 100644 index 0000000..c9beed4 --- /dev/null +++ b/gui-playground/src/panther/ui/StudentSelectionListener.java @@ -0,0 +1,39 @@ +package panther.ui; + +import javax.swing.*; +import javax.swing.event.*; +import javax.swing.table.*; + +import panther.db.*; + +public class StudentSelectionListener + implements ListSelectionListener +{ + private JTable master; + private JTable detail; + + public StudentSelectionListener(JTable master, JTable detail) + { + this.master = master; + this.detail = detail; + } + + public void valueChanged(ListSelectionEvent e) + { + if (e.getValueIsAdjusting()) return; + + int m_idx = master.convertRowIndexToModel(master.getSelectedRow()); + + Object student_o = master.getModel().getValueAt(m_idx, 0); + if (student_o instanceof Student) { + Student student = (Student) student_o; + + TableModel tmodel_o = detail.getModel(); + if (tmodel_o instanceof SessionTableModel) { + SessionTableModel tmodel = (SessionTableModel) tmodel_o; + + tmodel.update_student_sessions(student.get_id()); + } + } + } +} diff --git a/gui-playground/src/panther/ui/StudentTable.java b/gui-playground/src/panther/ui/StudentTable.java index ef5e78c..2cf65cc 100644 --- a/gui-playground/src/panther/ui/StudentTable.java +++ b/gui-playground/src/panther/ui/StudentTable.java @@ -8,7 +8,6 @@ import javax.swing.table.*; public class StudentTable extends JTable - implements ListSelectionListener { public StudentTable() { @@ -18,25 +17,5 @@ public class StudentTable /* limit user selection to only one student at a time */ getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION); - //getSelectionModel().addListSelectionListener(this); - } - - public void valueChanged(ListSelectionEvent e) - { - if (e.getValueIsAdjusting()) { - return; - } - - Object src = e.getSource(); - if (src instanceof javax.swing.DefaultListSelectionModel) { - System.out.println(((javax.swing.DefaultListSelectionModel) src). - getAnchorSelectionIndex()); - } - - /* TODO: tell the session table to update its session listings */ - - /* for some reason we need this to make sure the table visibly updates - * its row selection */ - repaint(); } } -- 2.11.4.GIT