Revision: 3983
Author: [email protected]
Date: Tue Nov  9 14:03:08 2010
Log: Fixed an oddity in CompareDM: the cancel button now works intuitively and cancels the operation instead of closing the window and letting the operation continue.
http://code.google.com/p/power-architect/source/detail?r=3983

Modified:
 /trunk/src/main/java/ca/sqlpower/architect/swingui/CompareDMDialog.java
 /trunk/src/main/java/ca/sqlpower/architect/swingui/CompareDMPanel.java

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/CompareDMDialog.java Thu Jan 29 12:02:55 2009 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/CompareDMDialog.java Tue Nov 9 14:03:08 2010
@@ -20,7 +20,11 @@
 package ca.sqlpower.architect.swingui;

 import java.awt.BorderLayout;
-
+import java.awt.event.ActionEvent;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
+
+import javax.swing.AbstractAction;
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
 import javax.swing.JDialog;
@@ -29,9 +33,9 @@
 import ca.sqlpower.sqlobject.SQLCatalog;
 import ca.sqlpower.sqlobject.SQLDatabase;
 import ca.sqlpower.sqlobject.SQLSchema;
-import ca.sqlpower.swingui.CommonCloseAction;
 import ca.sqlpower.swingui.JDefaultButton;
 import ca.sqlpower.swingui.SPSUtils;
+import ca.sqlpower.swingui.SPSwingWorker;


 /**
@@ -44,14 +48,68 @@

     private CompareDMPanel compareDMPanel;

-    public CompareDMDialog(ArchitectSwingSession session) {
-
-
+    /**
+ * Cancels a compare operation if it's running, and closes the window otherwise.
+     */
+    class CancelCompareAction extends AbstractAction {
+        final JDialog dialog;
+        CancelCompareAction(JDialog d) {
+            super("Cancel");
+            dialog = d;
+        }
+
+        @Override
+        public void actionPerformed(ActionEvent e) {
+ SPSwingWorker spsw = ((CompareDMPanel.StartCompareAction)compareDMPanel
+                    .getStartCompareAction())
+                    .getCompareWorker();
+            if (spsw == null || spsw.isCancelled() || spsw.isFinished()) {
+                dialog.setVisible(false);
+                dialog.dispose();
+            } else {
+                spsw.setCancelled(true);
+            }
+        }
+    }
+
+    public CompareDMDialog(ArchitectSwingSession session) {
         // This can not easily be replaced with ArchitectPanelBuilder
         // because the current CompareDMPanel is not an ArchitectPanel
         // (and has no intention of becoming one, without some work).

super(session.getArchitectFrame(),Messages.getString("CompareDMDialog.compareDmDialogTitle")); //$NON-NLS-1$
+
+        this.addWindowListener(new WindowListener() {
+            @Override
+            public void windowOpened(WindowEvent e) { }
+
+            @Override
+            public void windowIconified(WindowEvent e) { }
+
+            @Override
+            public void windowDeiconified(WindowEvent e) { }
+
+            @Override
+            public void windowDeactivated(WindowEvent e) { }
+
+            @Override
+            public void windowClosing(WindowEvent e) {
+ SPSwingWorker spsw = ((CompareDMPanel.StartCompareAction)compareDMPanel
+                        .getStartCompareAction())
+                        .getCompareWorker();
+                if (spsw != null) {
+                    spsw.setCancelled(true);
+                }
+            }
+
+            @Override
+            public void windowClosed(WindowEvent e) {
+                windowClosing(e);
+            }
+
+            @Override
+            public void windowActivated(WindowEvent e) { }
+        });
         JPanel cp = new JPanel(new BorderLayout(12,12));
         cp.setBorder(BorderFactory.createEmptyBorder(12,12,12,12));

@@ -68,7 +126,9 @@
JDefaultButton okButton = new JDefaultButton(compareDMPanel.getStartCompareAction());
         buttonPanel.add(okButton);

-        JButton cancelButton = new JButton(new CommonCloseAction(this));
+
+
+        JButton cancelButton = new JButton(new CancelCompareAction(this));
         buttonPanel.add(cancelButton);

         bottomPanel.add(buttonPanel,BorderLayout.EAST);
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/CompareDMPanel.java Thu Nov 4 08:06:24 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/CompareDMPanel.java Tue Nov 9 14:03:08 2010
@@ -35,6 +35,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.concurrent.CancellationException;

 import javax.swing.AbstractAction;
 import javax.swing.AbstractButton;
@@ -1233,7 +1234,13 @@

                private Collection<SQLTable> targetTables;

-               public StartCompareAction() {
+        private SPSwingWorker compareWorker;
+
+               public SPSwingWorker getCompareWorker() {
+            return compareWorker;
+        }
+
+        public StartCompareAction() {
super(Messages.getString("CompareDMPanel.startCompareActionName")); //$NON-NLS-1$
                }

@@ -1293,56 +1300,81 @@
                     reenableGUIComponents();
                        }

-                       SPSwingWorker compareWorker = new 
SPSwingWorker(session) {
+                       compareWorker = new SPSwingWorker(session) {

                                private List<DiffChunk<SQLObject>> diff;
                                private List<DiffChunk<SQLObject>> diff1;

                                private String message;
+
+                               /**
+ * Checks if this engine has been cancelled by another thread. If so,
+                            * throws a CancellationException.
+                            *
+                            * @throws CancellationException if this engine has 
been cancelled
+                            */
+                           protected void checkCancelled() {
+                               if (isCancelled()) {
+                                   throw new CancellationException("User-requested 
abort");
+                               }
+                           }

                                public void doStuff() throws SQLObjectException 
{
-                       if (source.physicalRadio.isSelected()) {
+                       try {
+                           if (source.physicalRadio.isSelected()) {
                            message = "Refreshing older database";
                            logger.debug(message);
                            source.getDatabase().refresh();
+                           checkCancelled();
                        }
                        if (target.physicalRadio.isSelected()) {
                            message = "Refreshing newer database";
                         logger.debug(message);
                            target.getDatabase().refresh();
+                           checkCancelled();
                        }
setJobSize(sourceComp.getJobSize() + targetComp.getJobSize());
                        logger.debug("Generating TableDiffs for source");
                        diff = sourceComp.generateTableDiffs(session);
+                       checkCancelled();
                        logger.debug("Generating TableDiffs for target");
                        diff1 = targetComp.generateTableDiffs(session);
+                       checkCancelled();
                                        message = "Finished";
                                        logger.debug("Finished Compare");
+                       } catch (CancellationException e) {
+                           setFinished(true);
+                       }
                                }

                                public void cleanup() {
-                                   reenableGUIComponents();
-                    if (getDoStuffException() != null) {
-                        Throwable exc = getDoStuffException();
- logger.error("Error in doStuff()", exc); //$NON-NLS-1$
-                        ASUtils.showExceptionDialog(session,
- Messages.getString("CompareDMPanel.databaseComparisonFailed"), exc); //$NON-NLS-1$
-                        return;
-                    }
-                                       logger.debug("cleanup starts"); 
//$NON-NLS-1$
- CompareDMFormatter dmFormat = new CompareDMFormatter(session, parentDialog, session.getCompareDMSettings()); - switch (session.getCompareDMSettings().getOutputFormat()) {
-                    case SQL:
-                    case LIQUIBASE:
- dmFormat.formatForSQLOutput(diff, diff1, left, right);
-                        break;
-                    case ENGLISH:
- dmFormat.formatForEnglishOutput(diff, diff1, left, right);
-                        break;
-                    default:
- throw new IllegalStateException("Don't know what type of output to make");
-                    }
-                    logger.debug("cleanup finished"); //$NON-NLS-1$
+                                   try {
+                        reenableGUIComponents();
+                        if (getDoStuffException() != null) {
+                            Throwable exc = getDoStuffException();
+ logger.error("Error in doStuff()", exc); //$NON-NLS-1$
+                            ASUtils.showExceptionDialog(session,
+ Messages.getString("CompareDMPanel.databaseComparisonFailed"), exc); //$NON-NLS-1$
+                            return;
+                        }
+                        logger.debug("cleanup starts"); //$NON-NLS-1$
+ CompareDMFormatter dmFormat = new CompareDMFormatter(session, parentDialog, session.getCompareDMSettings());
+                        checkCancelled();
+ switch (session.getCompareDMSettings().getOutputFormat()) {
+                        case SQL:
+                        case LIQUIBASE:
+ dmFormat.formatForSQLOutput(diff, diff1, left, right);
+                            break;
+                        case ENGLISH:
+ dmFormat.formatForEnglishOutput(diff, diff1, left, right);
+                            break;
+                        default:
+ throw new IllegalStateException("Don't know what type of output to make");
+                        }
+                        logger.debug("cleanup finished"); //$NON-NLS-1$
+                    } catch (CancellationException e) {
+                        setFinished(true);
+                    }
                                }

                                @Override

Reply via email to