Revision: 3690
Author: [email protected]
Date: Thu Jul 8 08:33:18 2010
Log: Updated the CheckConstraintTableModelListener to not check for
duplicate check constraint conditions. We only care about restricting check
constraint names. Also, added tests for the CheckConstraintTable and the
listener.
http://code.google.com/p/power-architect/source/detail?r=3690
Added:
/trunk/regress/ca/sqlpower/architect/swingui/TestCheckConstraintTable.java
Modified:
/trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTable.java
/trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTableModelListener.java
/trunk/src/main/java/ca/sqlpower/architect/swingui/action/AddCheckConstraintTableRowAction.java
=======================================
--- /dev/null
+++
/trunk/regress/ca/sqlpower/architect/swingui/TestCheckConstraintTable.java
Thu Jul 8 08:33:18 2010
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2010, SQL Power Group Inc.
+ *
+ * This file is part of SQL Power Architect.
+ *
+ * SQL Power Architect is free software; you can redistribute it and/or
modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * SQL Power Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package ca.sqlpower.architect.swingui;
+
+import javax.swing.table.DefaultTableModel;
+import javax.swing.table.TableModel;
+
+import junit.framework.TestCase;
+import ca.sqlpower.sqlobject.SQLCheckConstraint;
+import ca.sqlpower.sqlobject.SQLCheckConstraintContainer;
+import ca.sqlpower.testutil.GenericNewValueMaker;
+import ca.sqlpower.testutil.NewValueMaker;
+import ca.sqlpower.testutil.SPObjectRoot;
+
+public class TestCheckConstraintTable extends TestCase {
+
+ private SPObjectRoot root;
+
+ private NewValueMaker newValueMaker;
+
+ private SQLCheckConstraintContainer container;
+
+ private CheckConstraintTable table;
+
+ private CheckConstraintTableModelListener listener;
+
+ @Override
+ protected void setUp() throws Exception {
+ root = new SPObjectRoot();
+ newValueMaker = new GenericNewValueMaker(root);
+ container = (SQLCheckConstraintContainer)
newValueMaker.makeNewValue(SQLCheckConstraintContainer.class,
null, "SQLCheckConstraintContainer for TestCheckConstraintTable");
+ table = new CheckConstraintTable();
+ listener = new CheckConstraintTableModelListener(container);
+ table.getModel().addTableModelListener(listener);
+ }
+
+ public void testTableUsesCorrectTableModel() throws Exception {
+ TableModel model = table.getModel();
+ assertTrue(model instanceof DefaultTableModel);
+ assertEquals(2, table.getColumnCount());
+ assertEquals(0, ((DefaultTableModel) model).getRowCount());
+ }
+
+ public void testAddRowCausesAddCheckConstraint() throws Exception {
+ int constraintCount = container.getCheckConstraints().size();
+ assertEquals(0, constraintCount);
+
+ DefaultTableModel model = (DefaultTableModel) table.getModel();
+ String name = (String) newValueMaker.makeNewValue(String.class,
null, "SQLCheckConstraint name");
+ String constraint = (String)
newValueMaker.makeNewValue(String.class, null, "SQLCheckConstraint
constraint condition");
+ String[] row = {name, constraint};
+ model.addRow(row);
+
+ assertEquals((String) model.getValueAt(0, 0),
container.getCheckConstraints().get(0).getName());
+ assertEquals((String) model.getValueAt(0, 1),
container.getCheckConstraints().get(0).getConstraint());
+
+ assertEquals(constraintCount + 1,
container.getCheckConstraints().size());
+ assertEquals(name,
container.getCheckConstraints().get(0).getName());
+ assertEquals(constraint,
container.getCheckConstraints().get(0).getConstraint());
+ }
+
+ public void testRemoveRowCausesRemoveCheckConstraint() throws
Exception {
+ testAddRowCausesAddCheckConstraint();
+ int constraintCount = container.getCheckConstraints().size();
+ DefaultTableModel model = (DefaultTableModel) table.getModel();
+ model.removeRow(0);
+ assertEquals(0, model.getRowCount());
+ assertEquals(constraintCount - 1,
container.getCheckConstraints().size());
+ }
+
+ public void testUpdateRowCausesUpdateCheckConstraint() throws
Exception {
+ testAddRowCausesAddCheckConstraint();
+ int constraintCount = container.getCheckConstraints().size();
+ DefaultTableModel model = (DefaultTableModel) table.getModel();
+
+ String oldName = (String) model.getValueAt(0, 0);
+ String newName = (String) newValueMaker.makeNewValue(String.class,
oldName, "SQLCheckConstraint new name");
+ model.setValueAt(newName, 0, 0);
+ assertEquals(constraintCount,
container.getCheckConstraints().size());
+ assertEquals(newName,
container.getCheckConstraints().get(0).getName());
+
+ String oldConstraint = (String) model.getValueAt(0, 0);
+ String newConstraint = (String)
newValueMaker.makeNewValue(String.class, oldConstraint, "SQLCheckConstraint
new constraint");
+ model.setValueAt(newConstraint, 0, 1);
+ assertEquals(constraintCount,
container.getCheckConstraints().size());
+ assertEquals(newConstraint,
container.getCheckConstraints().get(0).getConstraint());
+ }
+
+ public void testAddOrUpdateRowWithBlankNameOrConstraintHasNoEffect()
throws Exception {
+ DefaultTableModel model = (DefaultTableModel) table.getModel();
+ String name = (String) newValueMaker.makeNewValue(String.class,
null, "SQLCheckConstraint name");
+ String constraint = (String)
newValueMaker.makeNewValue(String.class, null, "SQLCheckConstraint
constraint condition");
+
+ assertEquals(0, model.getRowCount());
+ assertTrue(container.getCheckConstraints().isEmpty());
+ String[] blankNameRow = {" ", constraint};
+ model.addRow(blankNameRow);
+ assertEquals(0, model.getRowCount());
+ assertTrue(container.getCheckConstraints().isEmpty());
+
+ assertEquals(0, model.getRowCount());
+ assertTrue(container.getCheckConstraints().isEmpty());
+ String[] blankConstraintRow = {name, " "};
+ model.addRow(blankConstraintRow);
+ assertEquals(0, model.getRowCount());
+ assertTrue(container.getCheckConstraints().isEmpty());
+
+ assertEquals(0, model.getRowCount());
+ assertTrue(container.getCheckConstraints().isEmpty());
+ String[] blankNameAndConstraintRow = {" ", " "};
+ model.addRow(blankNameAndConstraintRow);
+ assertEquals(0, model.getRowCount());
+ assertTrue(container.getCheckConstraints().isEmpty());
+
+ testAddRowCausesAddCheckConstraint();
+ model.setValueAt(" ", 0, 0);
+ assertEquals(1, model.getRowCount());
+ assertEquals(1, container.getCheckConstraints().size());
+ model.setValueAt(" ", 0, 1);
+ assertEquals(1, model.getRowCount());
+ assertEquals(1, container.getCheckConstraints().size());
+ }
+
+ public void testAddRowWithDuplicateNameHasNoEffect() throws Exception {
+ testAddRowCausesAddCheckConstraint();
+
+ DefaultTableModel model = (DefaultTableModel) table.getModel();
+ SQLCheckConstraint checkConstraint =
container.getCheckConstraints().get(0);
+ String oldName = checkConstraint.getName();
+ String oldConstraint = checkConstraint.getConstraint();
+
+ String[] row = {oldName, "new " + oldConstraint};
+ model.addRow(row);
+ assertEquals(1, model.getRowCount());
+ assertEquals(1, container.getCheckConstraints().size());
+ assertEquals(oldName,
container.getCheckConstraints().get(0).getName());
+ assertEquals(oldConstraint,
container.getCheckConstraints().get(0).getConstraint());
+ }
+
+}
=======================================
---
/trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTable.java
Wed Jul 7 11:16:36 2010
+++
/trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTable.java
Thu Jul 8 08:33:18 2010
@@ -21,6 +21,7 @@
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
+import javax.swing.table.TableModel;
import ca.sqlpower.sqlobject.SQLCheckConstraint;
import ca.sqlpower.sqlobject.SQLCheckConstraintContainer;
@@ -33,10 +34,51 @@
public CheckConstraintTable() {
super(new DefaultTableModel());
+ setCellSelectionEnabled(true);
getTableHeader().setReorderingAllowed(false);
DefaultTableModel tableModel = (DefaultTableModel) getModel();
tableModel.addColumn("Name");
tableModel.addColumn("Check Constraint");
}
+
+ /**
+ * Finds the first row of a {...@link TableModel} that contains a specific
+ * {...@link SQLCheckConstraint} name.
+ *
+ * @param model
+ * The {...@link TableModel} to search through.
+ * @param name
+ * The name of the {...@link SQLCheckConstraint}.
+ * @return The index of the matched row or -1 if not found.
+ */
+ public static int findFirstRow(TableModel model, String name) {
+ String trimmedName = name.trim();
+ for (int i = 0; i < model.getRowCount(); i++) {
+ if (((String) model.getValueAt(i,
0)).trim().equals(trimmedName)) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Finds the last row of a {...@link TableModel} that contains a specific
+ * {...@link SQLCheckConstraint} name.
+ *
+ * @param model
+ * The {...@link TableModel} to search through.
+ * @param name
+ * The name of the {...@link SQLCheckConstraint}.
+ * @return The index of the matched row or -1 if not found.
+ */
+ public static int findLastRow(TableModel model, String name) {
+ String trimmedName = name.trim();
+ for (int i = model.getRowCount()-1; i >= 0; i--) {
+ if (((String) model.getValueAt(i,
0)).trim().equals(trimmedName)) {
+ return i;
+ }
+ }
+ return -1;
+ }
}
=======================================
---
/trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTableModelListener.java
Wed Jul 7 15:10:34 2010
+++
/trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTableModelListener.java
Thu Jul 8 08:33:18 2010
@@ -57,6 +57,12 @@
*/
private final String platform;
+ /**
+ * A boolean value to determine if a table row removal event was caused
+ * after an insert with a blank name or constraint condition.
+ */
+ private boolean removalAfterInsert = false;
+
/**
* Creates a new {...@link CheckConstraintTableModelListener} that does
not
* specify a platform.
@@ -80,70 +86,72 @@
public void tableChanged(TableModelEvent e) {
DefaultTableModel model = (DefaultTableModel) e.getSource();
int row = e.getFirstRow();
- SQLCheckConstraint constraint;
+ SQLCheckConstraint checkConstraint;
+ int firstIndex;
+ int lastIndex;
switch (e.getType()) {
case TableModelEvent.INSERT:
- constraint = new SQLCheckConstraint(
- (String) model.getValueAt(row, 0),
- (String) model.getValueAt(row, 1));
- if (udt == null) {
- container.addCheckConstraint(constraint, row);
+
+ String name = (String) model.getValueAt(row, 0);
+ String constraint = (String) model.getValueAt(row, 1);
+ firstIndex = CheckConstraintTable.findFirstRow(model, name);
+ lastIndex = CheckConstraintTable.findLastRow(model, name);
+
+ if (name.trim().equals("") || constraint.trim().equals("") ||
+ (firstIndex != -1 && firstIndex != lastIndex)) {
+ removalAfterInsert = true;
+ model.removeRow(row);
+ removalAfterInsert = false;
} else {
- udt.addCheckConstraint(platform, constraint, row);
+ checkConstraint = new SQLCheckConstraint(name, constraint);
+ if (udt == null) {
+ container.addCheckConstraint(checkConstraint, row);
+ } else {
+ udt.addCheckConstraint(platform, checkConstraint, row);
+ }
}
break;
case TableModelEvent.DELETE:
- if (udt == null) {
- constraint = container.getCheckConstraints().get(row);
- container.removeCheckConstraint(constraint);
- } else {
- constraint = udt.getCheckConstraints(platform).get(row);
- udt.removeCheckConstraint(platform, constraint);
+ if (!removalAfterInsert) {
+ if (udt == null) {
+ checkConstraint =
container.getCheckConstraints().get(row);
+ container.removeCheckConstraint(checkConstraint);
+ } else {
+ checkConstraint =
udt.getCheckConstraints(platform).get(row);
+ udt.removeCheckConstraint(platform, checkConstraint);
+ }
}
break;
case TableModelEvent.UPDATE:
final int column = e.getColumn();
final String newName = (String) model.getValueAt(row, 0);
final String newConstraint = (String) model.getValueAt(row, 1);
- final int rowCount = model.getRowCount();
if (udt == null) {
- constraint = container.getCheckConstraints().get(row);
+ checkConstraint = container.getCheckConstraints().get(row);
} else {
- constraint = udt.getCheckConstraints(platform).get(row);
+ checkConstraint =
udt.getCheckConstraints(platform).get(row);
}
if (column != 1) {
- if (newName.equals("")) {
- model.setValueAt(constraint.getName(), row, 0);
+ if (newName.trim().equals("")) {
+ model.setValueAt(checkConstraint.getName(), row, 0);
} else {
- int i;
- for (i = 0; i < rowCount; i++) {
- if (i != row && model.getValueAt(i,
0).equals(newName)) {
- model.setValueAt(constraint.getName(), row, 0);
- break;
- }
- }
- if (i == rowCount) {
- constraint.setName((String) newName);
+ firstIndex = CheckConstraintTable.findFirstRow(model,
newName);
+ lastIndex = CheckConstraintTable.findLastRow(model,
newName);
+ if (firstIndex == lastIndex) {
+ checkConstraint.setName(newName);
+ } else {
+ model.setValueAt(checkConstraint.getName(), row,
0);
}
}
}
if (column != 0) {
- if (newConstraint.equals("")) {
- model.setValueAt(constraint.getConstraint(), row, 1);
+ if (newConstraint.trim().equals("")) {
+ model.setValueAt(checkConstraint.getConstraint(), row,
1);
} else {
- int i;
- for (i = 0; i < rowCount; i++) {
- if (i != row && model.getValueAt(i,
1).equals(newConstraint)) {
- model.setValueAt(constraint.getConstraint(),
row, 1);
- break;
- }
- }
- if (i == rowCount) {
- constraint.setConstraint((String) newConstraint);
- }
+ checkConstraint.setConstraint(newConstraint);
}
}
=======================================
---
/trunk/src/main/java/ca/sqlpower/architect/swingui/action/AddCheckConstraintTableRowAction.java
Wed Jul 7 11:16:36 2010
+++
/trunk/src/main/java/ca/sqlpower/architect/swingui/action/AddCheckConstraintTableRowAction.java
Thu Jul 8 08:33:18 2010
@@ -59,33 +59,25 @@
(DefaultTableModel) table.getModel();
String name = JOptionPane.showInputDialog("Define a new check
constraint name.");
- int index;
- for (index = 0; index < model.getRowCount(); index++) {
- if (model.getValueAt(index, 0).equals(name)) {
- table.setRowSelectionInterval(index, index);
- break;
- }
+ if (name == null || name.trim().equals("")) {
+ return;
}
- if (index == model.getRowCount() && name != null
&& !name.equals("")) {
- String constraint = JOptionPane.showInputDialog("Define the
check constraint condition.");
- for (index = 0; index < model.getRowCount(); index++) {
- if (model.getValueAt(index, 1).equals(constraint)) {
- table.setRowSelectionInterval(index, index);
- break;
- }
- }
-
- if (constraint != null && !constraint.equals("")) {
- if (index == model.getRowCount()) {
- String[] row = {name, constraint};
- model.addRow(row);
- }
-
- table.setRowSelectionInterval(index, index);
-
- }
- }
+ int index = CheckConstraintTable.findFirstRow(model, name);
+ if (index != -1) {
+ table.setRowSelectionInterval(index, index);
+ return;
+ }
+
+ String constraint = JOptionPane.showInputDialog("Define the check
constraint condition.");
+ if (constraint == null || constraint.trim().equals("")) {
+ return;
+ }
+
+ String[] row = {name, constraint};
+ model.addRow(row);
+ table.setRowSelectionInterval(model.getRowCount() - 1,
model.getRowCount() - 1);
+
}
}