Revision: 3495
Author: [email protected]
Date: Wed May  5 12:38:23 2010
Log: NEW - bug 2784: Have table properties dialog come before the table is actually created.
http://trillian.sqlpower.ca/bugzilla/show_bug.cgi?id=2784

Refactored some code from TableEditPanel.applyChanges() that generates warning messages based on if the user left the table name and key name blank. This is used in the CreateTableAction class to ensure the fields in the Table Properties dialog is entered correctly before creating a new SQLTable (whereas previously, the table was created first before showing the Table Properties dialog).
http://code.google.com/p/power-architect/source/detail?r=3495

Modified:
 /trunk/src/main/java/ca/sqlpower/architect/swingui/TableEditPanel.java
/trunk/src/main/java/ca/sqlpower/architect/swingui/action/CreateTableAction.java

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/TableEditPanel.java Thu Apr 22 12:03:52 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/TableEditPanel.java Wed May 5 12:38:23 2010
@@ -143,19 +143,9 @@
            if (tp != null) tp.removeSPListener(this);
table.begin(Messages.getString("TableEditPanel.compoundEditName")); //$NON-NLS-1$
         try {
-                   StringBuffer warnings = new StringBuffer();
- //We need to check if the table name and/or primary key name is empty or not - //if they are, we need to warn the user since it will mess up the SQLScripts we create
-            if (logicalName.getText().trim().length() == 0) {
- warnings.append(Messages.getString("TableEditPanel.blankTableNameWarning")); //$NON-NLS-1$
-
-            }
-            if (pkName.isEnabled() &&
-                    pkName.getText().trim().length() == 0) {
- warnings.append(Messages.getString("TableEditPanel.blankPkNameWarning")); //$NON-NLS-1$
-            }
-
-            if (warnings.toString().length() == 0) {
+                   String warnings = generateWarnings();
+
+            if (warnings.length() == 0) {

// important: set the primary key name first, because if the primary // key was called (for example) new_table_pk, and the table was called
@@ -188,7 +178,7 @@
                 }
                 return true;
             } else {
-                JOptionPane.showMessageDialog(panel,warnings.toString());
+                JOptionPane.showMessageDialog(panel,warnings);
//this is done so we can go back to this dialog after the error message
                 return false;
             }
@@ -198,6 +188,25 @@
                        table.commit();
                }
        }
+
+    /**
+ * Returns a String of warning messages if the table name or primary key + * name is empty. An empty String is returned if both names are non-empty.
+     */
+    protected String generateWarnings() {
+        StringBuffer warnings = new StringBuffer();
+ //We need to check if the table name and/or primary key name is empty or not + //if they are, we need to warn the user since it will mess up the SQLScripts we create
+        if (logicalName.getText().trim().length() == 0) {
+ warnings.append(Messages.getString("TableEditPanel.blankTableNameWarning")); //$NON-NLS-1$
+
+        }
+        if (pkName.isEnabled() &&
+                pkName.getText().trim().length() == 0) {
+ warnings.append(Messages.getString("TableEditPanel.blankPkNameWarning")); //$NON-NLS-1$
+        }
+        return warnings.toString();
+    }

        public void discardChanges() {
            SQLPowerUtils.unlistenToHierarchy(session.getRootObject(), this);
@@ -217,7 +226,7 @@
     }

     /**
-     * For testing only
+     * For testing only or when initially creating a table.
      * @param newName new logical name for the table
      */
     public void setNameText(String newName) {
@@ -233,7 +242,7 @@
     }

     /**
-     * For testing only
+     * For testing only or when initially creating a table.
      * @param newPKName new primaryKeyName for the table
      */
     public void setPkNameText(String newPkName) {
@@ -249,7 +258,7 @@
     }

     /**
-     * For testing only
+     * For testing only or when initially creating a table.
      * @param newPhysicalName new physical name for the table
      */
     public void setPhysicalNameText(String newPhysicalName) {
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/action/CreateTableAction.java Thu Apr 22 12:03:52 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/action/CreateTableAction.java Wed May 5 12:38:23 2010
@@ -22,6 +22,7 @@
 import java.awt.event.ActionEvent;
 import java.awt.event.KeyEvent;

+import javax.swing.JOptionPane;
 import javax.swing.KeyStroke;

 import org.apache.log4j.Logger;
@@ -32,7 +33,6 @@
 import ca.sqlpower.architect.swingui.TableEditPanel;
 import ca.sqlpower.architect.swingui.TablePane;
 import ca.sqlpower.architect.swingui.event.SelectionEvent;
-import ca.sqlpower.object.ObjectDependentException;
 import ca.sqlpower.sqlobject.SQLObjectException;
 import ca.sqlpower.sqlobject.SQLTable;
 import ca.sqlpower.swingui.DataEntryPanel;
@@ -43,6 +43,9 @@
  */
 public class CreateTableAction extends AbstractArchitectAction {
private static final Logger logger = Logger.getLogger(CreateTableAction.class);
+
+       // The default name of a table when it is initially created.
+       private static final String NEW_TABLE_NAME = "New_Table";

        private final ArchitectSwingSession session;

@@ -60,7 +63,7 @@
                SQLTable t = null;
                t = new SQLTable();
                t.initFolders(true);
-               t.setName("New_Table"); //$NON-NLS-1$
+               //t.setName("New_Table"); //$NON-NLS-1$

                TablePane tp = new TablePane(t, playpen.getContentPane());
                TablePlacer tablePlacer = new TablePlacer(playpen, tp);
@@ -82,33 +85,44 @@
         }

         @Override
-        public DataEntryPanel place(Point p) throws SQLObjectException {
-            try {
- session.getWorkspace().begin("Creating a SQLTable and TablePane");
-                session.getTargetDatabase().addChild(tp.getModel());
-                playpen.addTablePane(tp, p);
-                session.getWorkspace().commit();
-            } catch (Throwable t) {
- session.getWorkspace().rollback("Error creating table and table pane");
-                throw new RuntimeException(t);
-            }
+ public DataEntryPanel place(final Point p) throws SQLObjectException {
             DataEntryPanel editPanel = null;
-            playpen.selectNone();
-            tp.setSelected(true, SelectionEvent.SINGLE_SELECT);

editPanel = new TableEditPanel(playpen.getSession(), tp.getModel()) {
                 @Override
-                public void discardChanges() {
-                    try {
- playpen.getSession().getTargetDatabase().removeChild(tp.getModel()); - playpen.getTableNames().remove(tp.getModel().getName());
-                    } catch (IllegalArgumentException e) {
-                        throw new RuntimeException(e);
-                    } catch (ObjectDependentException e) {
-                        throw new RuntimeException(e);
+                public boolean applyChanges() {
+                    String warnings = generateWarnings();
+                    if (warnings.length() == 0) {
+                        try {
+ session.getWorkspace().begin("Creating a SQLTable and TablePane");
+                            if (super.applyChanges()) {
+                                tp.setName(table.getName());
+ session.getTargetDatabase().addChild(tp.getModel());
+                                playpen.selectNone();
+                                playpen.addTablePane(tp, p);
+ tp.setSelected(true, SelectionEvent.SINGLE_SELECT);
+                                session.getWorkspace().commit();
+                                return true;
+                            } else {
+ session.getWorkspace().rollback("Error creating table and table pane");
+                                return false;
+                            }
+                        } catch (Throwable t) {
+ session.getWorkspace().rollback("Error creating table and table pane");
+                            throw new RuntimeException(t);
+                        }
+                    } else {
+                        JOptionPane.showMessageDialog(getPanel(),warnings);
+ //this is done so we can go back to this dialog after the error message
+                        return false;
                     }
                 }
             };
+
+            ((TableEditPanel) editPanel).setNameText(NEW_TABLE_NAME);
+ ((TableEditPanel) editPanel).setPhysicalNameText(NEW_TABLE_NAME); + ((TableEditPanel) editPanel).setPkNameText(NEW_TABLE_NAME + "_pk");
+
             return editPanel;
         }
        }

Reply via email to