Revision: 3722
Author: [email protected]
Date: Tue Jul 13 10:39:23 2010
Log: Refactored the findFirstRow and findLastRow methods from CheckConstraintTable into ASUtils so that other classes that handle JTables can use them.
http://code.google.com/p/power-architect/source/detail?r=3722

Modified:
 /trunk/src/main/java/ca/sqlpower/architect/swingui/ASUtils.java
/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

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/ASUtils.java Mon Jul 12 08:21:11 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/ASUtils.java Tue Jul 13 10:39:23 2010
@@ -40,6 +40,7 @@
 import javax.swing.JDialog;
 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
+import javax.swing.table.TableModel;

 import org.apache.log4j.Logger;

@@ -638,4 +639,50 @@

         }
     }
-}
+
+    /**
+     * Finds the first row of a {...@link TableModel} that contains a specific
+ * {...@link String} value in a given column. Note that all leading and trailing
+     * spaces are trimmed when matching.
+     *
+     * @param model
+     *            The {...@link TableModel} to search through
+     * @param col
+     *            The column index to search under.
+     * @param value
+     *            The value of the cell to look for.
+     * @return The index of the matched row or -1 if not found.
+     */
+ public static int findFirstRow(TableModel model, int col, String value) {
+        String trimmedValue = value.trim();
+        for (int i = 0; i < model.getRowCount(); i++) {
+ if (((String) model.getValueAt(i, col)).trim().equals(trimmedValue)) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    /**
+     * Finds the last row of a {...@link TableModel} that contains a specific
+     * {...@link String} value in a given column. Note that all leading and
+     * trailing spaces are trimmed when matching.
+     *
+     * @param model
+     *            The {...@link TableModel} to search through.
+     * @param col
+     *            The column index to search under.
+     * @param value
+     *            The value of the cell to look for.
+     * @return The index of the matched row or -1 if not found.
+     */
+ public static int findLastRow(TableModel model, int col, String value) {
+        String trimmedValue = value.trim();
+        for (int i = model.getRowCount()-1; i >= 0; i--) {
+ if (((String) model.getValueAt(i, 0)).trim().equals(trimmedValue)) {
+                return i;
+            }
+        }
+        return -1;
+    }
+}
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTable.java Thu Jul 8 08:33:18 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTable.java Tue Jul 13 10:39:23 2010
@@ -21,7 +21,6 @@

 import javax.swing.JTable;
 import javax.swing.table.DefaultTableModel;
-import javax.swing.table.TableModel;

 import ca.sqlpower.sqlobject.SQLCheckConstraint;
 import ca.sqlpower.sqlobject.SQLCheckConstraintContainer;
@@ -40,45 +39,5 @@
         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 Thu Jul 8 08:33:18 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/CheckConstraintTableModelListener.java Tue Jul 13 10:39:23 2010
@@ -95,8 +95,8 @@

             String name = (String) model.getValueAt(row, 0);
             String constraint = (String) model.getValueAt(row, 1);
-            firstIndex = CheckConstraintTable.findFirstRow(model, name);
-            lastIndex = CheckConstraintTable.findLastRow(model, name);
+            firstIndex = ASUtils.findFirstRow(model, 0, name);
+            lastIndex = ASUtils.findLastRow(model, 0, name);

             if (name.trim().equals("") || constraint.trim().equals("") ||
                     (firstIndex != -1 && firstIndex != lastIndex)) {
@@ -138,8 +138,8 @@
                 if (newName.trim().equals("")) {
                     model.setValueAt(checkConstraint.getName(), row, 0);
                 } else {
- firstIndex = CheckConstraintTable.findFirstRow(model, newName); - lastIndex = CheckConstraintTable.findLastRow(model, newName);
+                    firstIndex = ASUtils.findFirstRow(model, 0, newName);
+                    lastIndex = ASUtils.findLastRow(model, 0, newName);
                     if (firstIndex == lastIndex) {
                         checkConstraint.setName(newName);
                     } else {
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/action/AddCheckConstraintTableRowAction.java Thu Jul 8 14:18:26 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/action/AddCheckConstraintTableRowAction.java Tue Jul 13 10:39:23 2010
@@ -26,6 +26,7 @@
 import javax.swing.JOptionPane;
 import javax.swing.table.DefaultTableModel;

+import ca.sqlpower.architect.swingui.ASUtils;
 import ca.sqlpower.architect.swingui.CheckConstraintTable;
 import ca.sqlpower.sqlobject.SQLCheckConstraint;

@@ -62,7 +63,7 @@
             return;
         }

-        int index = CheckConstraintTable.findFirstRow(model, name);
+        int index = ASUtils.findFirstRow(model, 0, name);
         if (index != -1) {
             table.setRowSelectionInterval(index, index);
             return;

Reply via email to