import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.PreparedStatement; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableModel; public class TestSwing04 extends JPanel { private static final long serialVersionUID = 1L; private static final String GET_ALL_RECORDS_BUTTON_TEXT = "全レコード取得"; private static final String ADD_BUTTON_TEXT = "追加"; private static final String MODIFY_BUTTON_TEXT = "変更"; private static final String DELETE_BUTTON_TEXT = "削除"; private static final String SQL_GET_ALL_RECORDS = "select * from favorite order by description asc"; private static final int SQL_GET_ALL_RECORDS_COLUMN_SIZE = 2; private static final String STATUS_ALL_RECORDS_GETTING = "全レコードを取得中です。"; private static final String STATUS_ALL_RECORDS_FAILURE = "全レコードの取得に失敗しました。"; private static final String STATUS_ALL_RECORDS_SUCCESS = "全レコードの取得に成功しました。"; private static final String STATUS_TEXT_IS_NULL = "未設定の項目があります。"; private static final String STATUS_ALREADY_EXISTS = "その概要は既に存在します。"; private static final String STATUS_INSERT_PROCESS = "レコードを追加中です。"; private static final String STATUS_INSERT_FAILURE = "レコードの追加に失敗しました。"; private static final String STATUS_INSERT_SUCCESS = "レコードの追加に成功しました。"; private static final String SQL_INSERT = "insert into favorite values(?, ?)"; private static final String STATUS_DELETE_NOT_FOUND = "削除対象のレコードが存在しません。"; private static final String STATUS_DELETE_PROCESS = "レコードを削除中です。"; private static final String STATUS_DELETE_FAILURE = "レコードの削除に失敗しました。"; private static final String STATUS_DELETE_SUCCESS = "レコードの削除に成功しました。"; private static final String SQL_DELETE = "delete from favorite where description = ? and url = ?"; private static final String STATUS_MODIFY_NOT_FOUND = "更新対象のレコードが存在しません。"; private static final String STATUS_MODIFY_PROCESS = "レコードを更新中です。"; private static final String STATUS_MODIFY_FAILURE = "レコードの更新に失敗しました。"; private static final String STATUS_MODIFY_SUCCESS = "レコードの更新に成功しました。"; private static final String SQL_MODIFY = "update favorite set url = ? where description = ?"; private JButton getAllRecordsButton = null; private JButton addButton = null; private JButton modifyButton = null; private JButton deleteButton = null; private JLabel statusLabel = null; private JPanel panel = null; public TestSwing04() { // create component this.getAllRecordsButton = new JButton(GET_ALL_RECORDS_BUTTON_TEXT); this.getAllRecordsButton.setEnabled(false); this.getAllRecordsButton.addActionListener(new GetAllRecordsActionHandler()); this.addButton = new JButton(ADD_BUTTON_TEXT); this.addButton.setEnabled(false); this.addButton.addActionListener(new AddActionHandler()); this.modifyButton = new JButton(MODIFY_BUTTON_TEXT); this.modifyButton.setEnabled(false); this.modifyButton.addActionListener(new ModifyActionHandler()); this.deleteButton = new JButton(DELETE_BUTTON_TEXT); this.deleteButton.setEnabled(false); this.deleteButton.addActionListener(new DeleteActionHandler()); this.statusLabel = new JLabel(); JPanel controlPanel = new JPanel(); controlPanel.add(this.getAllRecordsButton); controlPanel.add(this.addButton); controlPanel.add(this.modifyButton); controlPanel.add(this.deleteButton); controlPanel.add(this.statusLabel); this.panel = new JPanel(); this.panel.setLayout(new BoxLayout(this.panel, BoxLayout.Y_AXIS)); this.panel.add(controlPanel); setLayout(new BorderLayout()); add(this.panel, BorderLayout.WEST); } private class GetAllRecordsActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { Thread getAllRecordsThread = new GetAllRecordsThread(true); getAllRecordsThread.start(); } } private class GetAllRecordsThread extends Thread { boolean printAllRecordsText = false; public GetAllRecordsThread(boolean printAllRecordsText) { this.printAllRecordsText = printAllRecordsText; } public void run() { getAllRecordsButton.setEnabled(false); TestSwing03 testSwing03 = TestSwingMain.getTestSwing03(); testSwing03.setDescriptionField(null); testSwing03.setUrlField(null); if (this.printAllRecordsText) { statusLabel.setText(STATUS_ALL_RECORDS_GETTING); } getAllRecordsButton.setEnabled(false); SwingUtilities.invokeLater(new Runnable() { public void run() { getAllRecords(printAllRecordsText); } }); } } private void getAllRecords(boolean printAllRecordsText) { try { TestSwingDB testSwingDB = TestSwingMain.getTestSwingDB(); PreparedStatement preparedStatement = testSwingDB.getPreparedStatement(SQL_GET_ALL_RECORDS); Object[][] result = testSwingDB.executeQuery(preparedStatement, SQL_GET_ALL_RECORDS_COLUMN_SIZE); TestSwing02 testSwing02 = TestSwingMain.getTestSwing02(); DefaultTableModel tableModel = testSwing02.getTableModel(); while (tableModel.getRowCount() > 0) { tableModel.removeRow(0); } int recordSize = result.length; for (int i = 0; i < recordSize; i++) { String[] record = new String[SQL_GET_ALL_RECORDS_COLUMN_SIZE]; for (int j = 0; j < SQL_GET_ALL_RECORDS_COLUMN_SIZE; j++) { record[j] = (String)result[i][j]; } tableModel.insertRow(i, record); } if (printAllRecordsText) { this.statusLabel.setText(STATUS_ALL_RECORDS_SUCCESS); } this.getAllRecordsButton.setEnabled(true); TestSwing04 testSwing04 = TestSwingMain.getTestSwing04(); testSwing04.setInsertModifyDeleteButtonEnabled(true, false, false); } catch (TestSwingException testEx) { if (printAllRecordsText) { this.statusLabel.setText(STATUS_ALL_RECORDS_FAILURE); } this.getAllRecordsButton.setEnabled(true); } } private class AddActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { Thread addThread = new AddThread(); addThread.start(); } } private class AddThread extends Thread { public void run() { TestSwing03 testSwing03 = TestSwingMain.getTestSwing03(); String description = testSwing03.getDescriptionField(); String url = testSwing03.getUrlField(); if (description == null || "".equals(description) || url == null || "".equals(url)) { statusLabel.setText(STATUS_TEXT_IS_NULL); return; } TestSwing02 testSwing02 = TestSwingMain.getTestSwing02(); DefaultTableModel tableModel = testSwing02.getTableModel(); int rowSize = tableModel.getRowCount(); for (int i = 0; i < rowSize; i++) { if (tableModel.getValueAt(i, 0).equals(description)) { statusLabel.setText(STATUS_ALREADY_EXISTS); return; } } statusLabel.setText(STATUS_INSERT_PROCESS); addButton.setEnabled(false); SwingUtilities.invokeLater(new Runnable() { public void run() { insert(); } }); } } private void insert() { try { TestSwing03 testSwing03 = TestSwingMain.getTestSwing03(); String description = testSwing03.getDescriptionField(); String url = testSwing03.getUrlField(); TestSwingDB testSwingDB = TestSwingMain.getTestSwingDB(); PreparedStatement preparedStatement = testSwingDB.getPreparedStatement(SQL_INSERT); String[][] params = new String[][]{{"0", description}, {"1", url}}; testSwingDB.setPreparedStatementParameters(preparedStatement, params); testSwingDB.executeUpdate(preparedStatement); testSwingDB.commit(); this.statusLabel.setText(STATUS_INSERT_SUCCESS); Thread getAllRecordsThread = new GetAllRecordsThread(false); getAllRecordsThread.start(); } catch (TestSwingException testEx) { this.statusLabel.setText(STATUS_INSERT_FAILURE); } finally { this.addButton.setEnabled(true); } } private class ModifyActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { Thread modifyThread = new ModifyThread(); modifyThread.start(); } } private class ModifyThread extends Thread { public void run() { TestSwing03 testSwing03 = TestSwingMain.getTestSwing03(); String description = testSwing03.getDescriptionField(); String url = testSwing03.getUrlField(); if (description == null || "".equals(description) || url == null || "".equals(url)) { statusLabel.setText(STATUS_TEXT_IS_NULL); return; } TestSwing02 testSwing02 = TestSwingMain.getTestSwing02(); DefaultTableModel tableModel = testSwing02.getTableModel(); int rowSize = tableModel.getRowCount(); boolean flg = false; for (int i = 0; i < rowSize; i++) { if (tableModel.getValueAt(i, 0).equals(description)) { flg = true; } } if (!flg) { statusLabel.setText(STATUS_MODIFY_NOT_FOUND); return; } statusLabel.setText(STATUS_MODIFY_PROCESS); modifyButton.setEnabled(false); SwingUtilities.invokeLater(new Runnable() { public void run() { modify(); } }); } } private void modify() { try { TestSwing03 testSwing03 = TestSwingMain.getTestSwing03(); String description = testSwing03.getDescriptionField(); String url = testSwing03.getUrlField(); TestSwingDB testSwingDB = TestSwingMain.getTestSwingDB(); PreparedStatement preparedStatement = testSwingDB.getPreparedStatement(SQL_MODIFY); String[][] params = new String[][]{{"0", url}, {"1", description}}; testSwingDB.setPreparedStatementParameters(preparedStatement, params); testSwingDB.executeUpdate(preparedStatement); testSwingDB.commit(); this.statusLabel.setText(STATUS_MODIFY_SUCCESS); Thread getAllRecordsThread = new GetAllRecordsThread(false); getAllRecordsThread.start(); } catch (TestSwingException testEx) { this.statusLabel.setText(STATUS_MODIFY_FAILURE); } finally { this.modifyButton.setEnabled(true); } } private class DeleteActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { Thread deleteThread = new DeleteThread(); deleteThread.start(); } } private class DeleteThread extends Thread { public void run() { TestSwing03 testSwing03 = TestSwingMain.getTestSwing03(); String description = testSwing03.getDescriptionField(); String url = testSwing03.getUrlField(); if (description == null || "".equals(description) || url == null || "".equals(url)) { statusLabel.setText(STATUS_TEXT_IS_NULL); return; } TestSwing02 testSwing02 = TestSwingMain.getTestSwing02(); DefaultTableModel tableModel = testSwing02.getTableModel(); int rowSize = tableModel.getRowCount(); boolean flg = false; for (int i = 0; i < rowSize; i++) { if (tableModel.getValueAt(i, 0).equals(description) && tableModel.getValueAt(i, 1).equals(url)) { flg = true; } } if (!flg) { statusLabel.setText(STATUS_DELETE_NOT_FOUND); return; } statusLabel.setText(STATUS_DELETE_PROCESS); deleteButton.setEnabled(false); SwingUtilities.invokeLater(new Runnable() { public void run() { delete(); } }); } } private void delete() { try { TestSwing03 testSwing03 = TestSwingMain.getTestSwing03(); String description = testSwing03.getDescriptionField(); String url = testSwing03.getUrlField(); TestSwingDB testSwingDB = TestSwingMain.getTestSwingDB(); PreparedStatement preparedStatement = testSwingDB.getPreparedStatement(SQL_DELETE); String[][] params = new String[][]{{"0", description}, {"1", url}}; testSwingDB.setPreparedStatementParameters(preparedStatement, params); testSwingDB.executeUpdate(preparedStatement); testSwingDB.commit(); this.statusLabel.setText(STATUS_DELETE_SUCCESS); Thread getAllRecordsThread = new GetAllRecordsThread(false); getAllRecordsThread.start(); } catch (TestSwingException testEx) { this.statusLabel.setText(STATUS_DELETE_FAILURE); } finally { this.deleteButton.setEnabled(true); } } public void setGetAllRecordsButtonEnabled(boolean flg) { this.getAllRecordsButton.setEnabled(flg); } public void setInsertModifyDeleteButtonEnabled(boolean insert, boolean modify, boolean delete) { this.addButton.setEnabled(insert); this.modifyButton.setEnabled(modify); this.deleteButton.setEnabled(delete); } public void setStatusLabel(String label) { this.statusLabel.setText(label); } }