Permalink
Browse files

Add tabs and updates.

  • Loading branch information...
1 parent e8ac7c5 commit 243a7a5400abb5c852a35e9248599d6c663b2323 @nbeloglazov committed May 17, 2012
View
@@ -1,82 +0,0 @@
-drop table Lesson;
-drop table Classroom;
-drop table ClassroomTypeSubject;
-drop table ClassroomType;
-drop table Course;
-drop table Subject;
-drop table Teacher;
-
--- Create for Oracle
-create table Teacher (ID integer not null primary key,
- Name varchar(255) not null,
- Title varchar(255));
-create table Subject (ID integer not null primary key,
- Name varchar(255) not null);
-create table Course (ID integer not null primary key,
- TeacherID integer references Teacher(ID),
- SubjectID integer references Subject(ID),
- HoursPerWeek integer not null check (HoursPerWeek > 0),
- StudentsNumber integer not null check (StudentsNumber > 0));
-create table ClassroomType (ID integer not null primary key,
- Name varchar(255) not null);
-create table ClassroomTypeSubject (ClassroomTypeID integer not null references ClassroomType(ID),
- SubjectID integer not null references Subject(ID),
- constraint pk primary key (ClassroomTypeID, SubjectID));
-create table Classroom (ID integer not null primary key,
- ClassroomTypeID integer references ClassroomType(ID),
- Number integer not null check (Number > 0),
- Building integer not null check (Building > 0),
- Capacity integer not null check (Capacity > 0));
-create table Lesson (ID integer not null primary key,
- CourseID integer references Course(ID),
- ClassroomID integer references Classroom(ID),
- DayOfWeek integer not null check (DayOfWeek >= 1 and DayOfWeek <= 7),
- StartTime integer not null,
- Duration integer not null);
-
-
--- Initialization
-insert into Teacher values (1, 'Ivanov Ivan Ivanovich', 'Prof');
-insert into Teacher values (2, 'Sidorov Sidor Sidorovich', 'Dr');
-insert into Teacher values (3, 'John Black', 'Mr');
-insert into Teacher values (4, 'Smit Brown', 'PhD');
-insert into Teacher values (5, 'White House', 'Hi');
-
-insert into Subject values (1, 'Mathematical analysis');
-insert into Subject values (2, 'Discrete mathematics');
-insert into Subject values (3, 'Philosophy');
-insert into Subject values (4, 'Databases');
-insert into Subject values (5, 'Psychology');
-
-insert into Course values (1, 2, 3, 2, 50);
-insert into Course values (2, 3, 4, 4, 100);
-insert into Course values (3, 4, 5, 8, 150);
-insert into Course values (4, 5, 1, 16, 200);
-insert into Course values (5, 1, 2, 32, 250);
-
-insert into ClassroomType values (1, 'computer');
-insert into ClassroomType values (2, 'Lesson');
-insert into ClassroomType values (3, 'pracktice');
-
-insert into ClassroomTypeSubject values (1, 4);
-insert into ClassroomTypeSubject values (2, 1);
-insert into ClassroomTypeSubject values (2, 2);
-insert into ClassroomTypeSubject values (2, 3);
-insert into ClassroomTypeSubject values (2, 4);
-insert into ClassroomTypeSubject values (2, 5);
-insert into ClassroomTypeSubject values (3, 1);
-insert into ClassroomTypeSubject values (3, 2);
-
-insert into Classroom values (1, 1, 205, 3, 30);
-insert into Classroom values (2, 1, 206, 3, 30);
-insert into Classroom values (3, 2, 207, 4, 250);
-insert into Classroom values (4, 2, 208, 5, 250);
-insert into Classroom values (5, 3, 209, 3, 30);
-insert into Classroom values (6, 3, 210, 2, 30);
-
-insert into Lesson values (1, 1, 3, 1, 780, 120);
-insert into Lesson values (2, 1, 5, 2, 780, 120);
-insert into Lesson values (3, 4, 1, 3, 840, 120);
-insert into Lesson values (4, 4, 4, 4, 780, 120);
-insert into Lesson values (5, 3, 3, 5, 780, 120);
-insert into Lesson values (6, 3, 4, 5, 980, 120);
@@ -21,7 +21,8 @@ public static Connection getConnection() throws SQLException {
public static RowSet getAll(Entity entity) throws SQLException {
- JdbcRowSet rowSet = new JdbcRowSetImpl(getConnection());
+ Connection connection = getConnection();
+ JdbcRowSet rowSet = new JdbcRowSetImpl(connection);
String query = String.format("select %s from %s;",
StringUtils.join(entity.getFields(), ", "),
entity.getName());
@@ -0,0 +1,40 @@
+package by.bsu.fpmi.beloglazov.subd.gui;
+
+import javax.swing.JOptionPane;
+import java.awt.Component;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+public abstract class EntityActionListener implements ActionListener {
+
+ private EntityTableModel entityModel;
+ private Component parent;
+
+ public EntityActionListener(EntityTableModel entityMode, Component parent) {
+ this.entityModel = entityMode;
+ this.parent = parent;
+ }
+
+ public EntityTableModel getEntityModel() {
+ return entityModel;
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ try {
+ actionPerformed();
+ } catch (Exception t) {
+ showError(t);
+ }
+ }
+
+ public abstract void actionPerformed() throws Exception;
+
+ private void showError(Exception e) {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ e.printStackTrace(pw);
+ JOptionPane.showMessageDialog(parent, sw.toString());
+ }
+}
@@ -4,41 +4,40 @@
import by.bsu.fpmi.beloglazov.subd.Entity;
import javax.sql.RowSet;
-import javax.swing.event.TableModelListener;
-import javax.swing.table.TableModel;
+import javax.swing.table.AbstractTableModel;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-public class EntityTableModel implements TableModel {
+public class EntityTableModel extends AbstractTableModel {
private RowSet rowSet;
private Entity entity;
private int rowCount;
private int columnCount;
private List<Class<?>> columnClasses;
- private Map<String, Object> updates;
+ private Map<Integer, Map<Integer, Object>> updates;
public EntityTableModel(Entity entity) throws SQLException, ClassNotFoundException {
this.entity = entity;
this.rowSet = DBUtils.getAll(entity);
- rowCount = 0;
- rowSet.first();
- while (rowSet.next()) {
- rowCount++;
- }
columnCount = entity.getFields().size();
columnClasses = new ArrayList<Class<?>>(columnCount);
for (int i = 0; i < columnCount; i++) {
columnClasses.add(Class.forName(rowSet.getMetaData().getColumnClassName(i + 1)));
}
- updates = new HashMap<String, Object>();
+ recount();
+ updates = new HashMap<Integer, Map<Integer, Object>>();
}
- public void addTableModelListener(TableModelListener l) {
- //To change body of implemented methods use File | Settings | File Templates.
+ private void recount() throws SQLException {
+ rowCount = 1;
+ rowSet.first();
+ while (rowSet.next()) {
+ rowCount++;
+ }
}
public int getRowCount() {
@@ -63,9 +62,8 @@ public boolean isCellEditable(int rowIndex, int columnIndex) {
public Object getValueAt(int rowIndex, int columnIndex) {
try {
- Object result = getUpdate(rowIndex, columnIndex);
- if (result != null) {
- return result;
+ if (updates.containsKey(rowIndex) && updates.get(rowIndex).containsKey(columnIndex)) {
+ return updates.get(rowIndex).get(columnIndex);
}
rowSet.absolute(rowIndex + 1);
return rowSet.getObject(columnIndex + 1);
@@ -75,24 +73,34 @@ public Object getValueAt(int rowIndex, int columnIndex) {
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
- try {
- rowSet.absolute(rowIndex + 1);
- rowSet.updateObject(columnIndex + 1, aValue);
- setUpdate(rowIndex, columnIndex, aValue);
- } catch (SQLException e) {
- throw new RuntimeException(e);
+ if (!updates.containsKey(rowIndex)) {
+ updates.put(rowIndex, new HashMap<Integer, Object>());
}
+ updates.get(rowIndex).put(columnIndex, aValue);
}
- public void removeTableModelListener(TableModelListener l) {
-
+ public void commit() throws SQLException {
+ for (Map.Entry<Integer, Map<Integer, Object>> rowUpdate: updates.entrySet()) {
+ rowSet.absolute(rowUpdate.getKey() + 1);
+ for (Map.Entry<Integer, Object> column : rowUpdate.getValue().entrySet()) {
+ rowSet.updateObject(column.getKey() + 1, column.getValue());
+ }
+ rowSet.updateRow();
+ }
+ refresh();
}
- private void setUpdate(int row, int column, Object value) {
- updates.put(row + ";" + column, value);
+ public void refresh() throws SQLException {
+ updates.clear();
+ rowSet.close();
+ rowSet = DBUtils.getAll(entity);
+ recount();
+ fireTableDataChanged();
}
- private Object getUpdate(int row, int column) {
- return updates.get(row + ";" + column);
+ public void delete(int row) throws SQLException {
+ rowSet.absolute(row);
+ rowSet.deleteRow();
+ refresh();
}
}
@@ -2,19 +2,88 @@
import by.bsu.fpmi.beloglazov.subd.Entity;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
+import javax.swing.table.JTableHeader;
+import java.awt.Component;
+import java.awt.event.ActionListener;
public class SubdFrame extends JFrame {
public SubdFrame() throws Exception {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- addTable();
+ getContentPane().add(createTables());
}
- private void addTable() throws Exception {
- JTable table = new JTable(new EntityTableModel(Entity.TEACHER));
- getContentPane().add(table);
+ private JTabbedPane createTables() throws Exception {
+ JTabbedPane pane = new JTabbedPane();
+ for (Entity entity : Entity.values()) {
+ pane.addTab(entity.getName(), createTab(entity));
+ }
+ return pane;
+ }
+
+ private JPanel createTab(Entity entity) throws Exception {
+ JPanel panel = new JPanel();
+ BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
+ panel.setLayout(boxLayout);
+ EntityTableModel model = new EntityTableModel(entity);
+ JTable table = new JTable(model);
+ panel.add(new JScrollPane(table));
+ panel.add(createButton("Update", new UpdateButtonListener(model, this)));
+ panel.add(createButton("Refresh", new RefreshButtonListener(model, this)));
+ panel.add(createButton("Delete", new DeleteButtonListener(model, table, this)));
+ return panel;
+ }
+
+ private JButton createButton(String title, ActionListener listener) {
+ JButton button = new JButton(title);
+ button.addActionListener(listener);
+ return button;
+ }
+
+ private class UpdateButtonListener extends EntityActionListener {
+
+ private UpdateButtonListener(EntityTableModel entityMode, Component parent) {
+ super(entityMode, parent);
+ }
+
+ public void actionPerformed() throws Exception {
+ getEntityModel().commit();
+ }
+ }
+
+ private class RefreshButtonListener extends EntityActionListener {
+
+ private RefreshButtonListener(EntityTableModel entityMode, Component parent) {
+ super(entityMode, parent);
+ }
+
+ public void actionPerformed() throws Exception {
+ getEntityModel().refresh();
+ }
+ }
+
+ private class DeleteButtonListener extends EntityActionListener {
+
+ private JTable table;
+
+ private DeleteButtonListener(EntityTableModel entityMode, JTable table, Component parent) {
+ super(entityMode, parent);
+ this.table = table;
+ }
+
+ @Override
+ public void actionPerformed() throws Exception {
+ if (table.getSelectedRow() != -1) {
+ getEntityModel().delete(table.getSelectedRow() + 1);
+ }
+ }
}
}

0 comments on commit 243a7a5

Please sign in to comment.