Revision: 3342
Author: silva.josemanuel1
Date: Fri Feb 26 12:26:46 2010
Log: Implemented the ability to open an old revision.
http://code.google.com/p/power-architect/source/detail?r=3342

Modified:
 /trunk/src/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java
/trunk/src/ca/sqlpower/architect/swingui/enterprise/CompareRevisionsPanel.java
 /trunk/src/ca/sqlpower/architect/swingui/enterprise/RevisionListPanel.java

=======================================
--- /trunk/src/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java Thu Feb 25 15:27:51 2010 +++ /trunk/src/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java Fri Feb 26 12:26:46 2010
@@ -48,6 +48,7 @@
 import ca.sqlpower.architect.ArchitectSessionContext;
 import ca.sqlpower.architect.ArchitectSessionImpl;
 import ca.sqlpower.architect.ddl.DDLGenerator;
+import ca.sqlpower.architect.swingui.ArchitectSwingSession;
 import ca.sqlpower.architect.swingui.ArchitectSwingSessionContext;
 import ca.sqlpower.dao.HttpMessageSender;
 import ca.sqlpower.dao.SPPersistenceException;
@@ -374,7 +375,9 @@

     }

- public static ProjectLocation createNewServerSession(SPServerInfo serviceInfo, String name) throws URISyntaxException, ClientProtocolException, IOException, JSONException { + public static ProjectLocation createNewServerSession(SPServerInfo serviceInfo, String name) + throws URISyntaxException, ClientProtocolException, IOException, JSONException {
+
        HttpClient httpClient = createHttpClient(serviceInfo);
        try {
HttpUriRequest request = new HttpGet(getServerURI(serviceInfo, "/jcr/projects/new", "name=" + name));
@@ -424,6 +427,47 @@
         }

        }
+
+ public void persistRevisionFromServer(int revisionNo, SPJSONMessageDecoder targetDecoder) + throws IOException, URISyntaxException, SPPersistenceException, IllegalArgumentException { + ArchitectClientSideSession.persistRevisionFromServer(projectLocation, revisionNo, targetDecoder);
+       }
+
+       /**
+ * Requests the server for persist calls from version 0 to the given revision
+        * of the given project, and persists them to the given decoder.
+        *
+        * @param projectLocation
+ * @param revisionNo Must be greater than zero, and no greater than the current revision number
+        * @param decoder
+        * @throws IOException
+        * @throws URISyntaxException
+        * @throws SPPersistenceException
+ * @throws IllegalArgumentException Thrown if the server rejects the given revisionNo
+        */
+ public static void persistRevisionFromServer(ProjectLocation projectLocation,
+               int revisionNo, SPJSONMessageDecoder decoder)
+ throws IOException, URISyntaxException, SPPersistenceException, IllegalArgumentException {
+
+           SPServerInfo serviceInfo = projectLocation.getServiceInfo();
+           HttpClient httpClient = createHttpClient(serviceInfo);
+
+        try {
+ JSONMessage response = executeServerRequest(httpClient, serviceInfo, + "/project/" + projectLocation.getUUID() + "/" + revisionNo,
+                    new JSONResponseHandler());
+
+            if (response.isSuccessful()) {
+                decoder.decode(response.getBody());
+            } else {
+ throw new IllegalArgumentException("The server rejected the revision number " + + "(it must be greater than 0, and no greater than the current revision number)");
+            }
+
+        } finally {
+            httpClient.getConnectionManager().shutdown();
+        }
+       }

        public int getLocalRevisionNo() {
            return currentRevision;
@@ -936,5 +980,10 @@
                 throw new RuntimeException(ex);
             }
            }
-       }
-}
+       }
+
+ public void createRevisionSession(int revisionNo, ArchitectSwingSession swingSession) {
+        // TODO Auto-generated method stub
+
+    }
+}
=======================================
--- /trunk/src/ca/sqlpower/architect/swingui/enterprise/CompareRevisionsPanel.java Thu Feb 25 09:33:27 2010 +++ /trunk/src/ca/sqlpower/architect/swingui/enterprise/CompareRevisionsPanel.java Fri Feb 26 12:26:46 2010
@@ -37,7 +37,11 @@
 import javax.swing.event.ChangeListener;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
+import javax.swing.text.BadLocationException;
 import javax.swing.text.DefaultStyledDocument;
+import javax.swing.text.SimpleAttributeSet;
+
+import org.apache.log4j.Logger;

 import ca.sqlpower.architect.enterprise.ArchitectClientSideSession;
 import ca.sqlpower.architect.swingui.CompareDMFormatter;
@@ -54,6 +58,8 @@
  */
 public class CompareRevisionsPanel {

+ private static final Logger logger = Logger.getLogger(CompareRevisionsPanel.class);
+
     private final ArchitectClientSideSession session;

     private final RevisionsTable revisionsTableLeft;
@@ -66,7 +72,7 @@
     private final long toRevision;

     private final Action refreshAction = new AbstractAction("Refresh...") {
-        public void actionPerformed(ActionEvent e) {
+        public void actionPerformed(ActionEvent e) {
revisionsTableLeft.refreshRevisionsList(fromRevision, toRevision); revisionsTableRight.refreshRevisionsList(fromRevision, toRevision);
             refreshPanel();
@@ -75,29 +81,20 @@

     private final Action compareAction = new AbstractAction("Compare...") {
         public void actionPerformed(ActionEvent e) {
- int oldRevisionNo = revisionsTableLeft.getSelectedRevisionNumber(); - int newRevisionNo = revisionsTableRight.getSelectedRevisionNumber();
-
-            DefaultStyledDocument resultDoc = new DefaultStyledDocument();
+            this.setEnabled(false);
+            DefaultStyledDocument d = new DefaultStyledDocument();
             try {
-                this.setEnabled(false);
-                if (oldRevisionNo >= 0 && newRevisionNo >= 0) {
- List<DiffChunk<DiffInfo>> diff = session.getComparisonDiffChunks(oldRevisionNo, newRevisionNo);
-                    if (diff.size() == 0) {
- resultDoc.insertString(0, "Revisions are identical", null);
-                    } else {
- resultDoc = CompareDMFormatter.generateEnglishDescription(CompareDMFormatter.DIFF_STYLES, diff);
-                    }
-                } else {
- JOptionPane.showMessageDialog(panel, "A revision must be selected from each table.");
-                }
-            } catch (Throwable t) {
-                throw new RuntimeException("Error making comparison", t);
-            } finally {
-                refreshPanel();
-            }
-
-            comparePane.setStyledDocument(resultDoc);
+                d.insertString(0, "...", new SimpleAttributeSet());
+            } catch (BadLocationException ex) {
+                // Impossible
+                logger.error(ex);
+            }
+            comparePane.setStyledDocument(d);
+            new Thread(new Runnable() {
+                public void run() {
+                    doCompare();
+                }
+            }).start();
         }
     };

@@ -179,5 +176,29 @@
     public JPanel getPanel() {
         return panel;
     }
-
-}
+
+    private void doCompare() {
+        int oldRevisionNo = revisionsTableLeft.getSelectedRevisionNumber();
+ int newRevisionNo = revisionsTableRight.getSelectedRevisionNumber();
+
+        DefaultStyledDocument resultDoc = new DefaultStyledDocument();
+        try {
+            if (oldRevisionNo >= 0 && newRevisionNo >= 0) {
+ List<DiffChunk<DiffInfo>> diff = session.getComparisonDiffChunks(oldRevisionNo, newRevisionNo);
+                if (diff.size() == 0) {
+ resultDoc.insertString(0, "Revisions are identical", null);
+                } else {
+ resultDoc = CompareDMFormatter.generateEnglishDescription(CompareDMFormatter.DIFF_STYLES, diff);
+                }
+            } else {
+ JOptionPane.showMessageDialog(panel, "A revision must be selected from each table.");
+            }
+        } catch (Throwable t) {
+            throw new RuntimeException("Error making comparison", t);
+        } finally {
+            refreshPanel();
+        }
+        comparePane.setStyledDocument(resultDoc);
+    }
+}
+
=======================================
--- /trunk/src/ca/sqlpower/architect/swingui/enterprise/RevisionListPanel.java Thu Feb 25 15:27:51 2010 +++ /trunk/src/ca/sqlpower/architect/swingui/enterprise/RevisionListPanel.java Fri Feb 26 12:26:46 2010
@@ -22,6 +22,8 @@
 import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.event.ActionEvent;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.text.NumberFormat;
@@ -35,15 +37,18 @@
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
-import javax.swing.event.ChangeEvent;
-import javax.swing.event.ChangeListener;
+import javax.swing.SwingUtilities;
 import javax.swing.text.NumberFormatter;

 import org.apache.log4j.Logger;

 import ca.sqlpower.architect.enterprise.ArchitectClientSideSession;
+import ca.sqlpower.architect.enterprise.ArchitectSessionPersister;
+import ca.sqlpower.architect.enterprise.ProjectLocation;
 import ca.sqlpower.architect.swingui.ArchitectFrame;
 import ca.sqlpower.architect.swingui.ArchitectSwingSession;
+import ca.sqlpower.dao.json.SPJSONMessageDecoder;
+import ca.sqlpower.dao.session.SessionPersisterSuperConverter;
 import ca.sqlpower.swingui.SPSUtils;

 import com.jgoodies.forms.builder.DefaultFormBuilder;
@@ -54,31 +59,40 @@

     private class JLongField {

-        private JFormattedTextField field;
-
-        public JLongField() {
+        private JFormattedTextField field;
+        private long value;
+
+        public JLongField(long value) {
NumberFormatter f = new NumberFormatter(NumberFormat.getInstance());
             f.setValueClass(Long.class);
-            f.setMinimum(new Long(0));
-            field = new JFormattedTextField(f);
-        }
-
-        public JLongField(long value) {
-            this();
+            f.setMinimum(new Long(1));
+            field = new JFormattedTextField(f);
             setValue(value);
         }

         public void setValue(long value) {
+            this.value = value;
             field.setValue(new Long(value));
         }

         public long getValue() {
-            return ((Long) field.getValue()).longValue();
+            return getValue(true);
         }

+        public long getValue(boolean update) {
+            if (update) {
+                this.value = ((Long) field.getValue()).longValue();
+            }
+            return this.value;
+        }
+
         public JFormattedTextField getField() {
             return field;
         }
+
+        public boolean update() {
+            return (this.value != this.getValue(true));
+        }

     }

@@ -96,78 +110,112 @@
     private JLongField toVersion;
     private int currentVersion;

+    private final JCheckBox autoRefreshBox;
     private final Runnable autoRefresh;

     private final Action refreshAction = new AbstractAction("Refresh...") {
         public void actionPerformed(ActionEvent e) {
-
-            String message = "ok";
-
-            // revertAction updates the currentVersion
+
+ boolean filterChange = fromVersion.update() || toVersion.update();
+
             int difference = session.getLocalRevisionNo() - currentVersion;
-            if (e.getSource() != revertAction) {
-                if (difference > 0) {
-                    currentVersion += difference;
- // If the end of the filter range was the previously most recent revision, - // update the filter range to the new current revision number. - if (toVersion.getValue() == currentVersion - difference) { - fromVersion.setValue(fromVersion.getValue() + difference);
-                        toVersion.setValue(currentVersion);
-                    }
-                } else if (e.getSource() == autoRefresh) {
-                    return;
-                }
-            }
-
-            if (!e.getActionCommand().equals("ignoreWarnings")) {
-                if (toVersion.getValue() > currentVersion) {
- message = "Revisions up to " + toVersion.getValue() + " cannot be shown " + - "because the current revision is only " + currentVersion;
-                } else if (fromVersion.getValue() > toVersion.getValue()) {
- message = "Cannot show revisions from a higher version to a lower version";
-                }
-            }
-
-            if (message.equals("ok")) {
+            if (difference > 0) currentVersion += difference;
+
+            String message = null;
+            if (toVersion.getValue() > currentVersion) {
+ message = "Revisions up to " + toVersion.getValue() + " cannot be shown " +
+                "because the current revision is only " + currentVersion;
+                toVersion.setValue(currentVersion);
+            } else if (fromVersion.getValue() > toVersion.getValue()) {
+ message = "Cannot show revisions from a higher version to a lower version";
+                long to = toVersion.getValue() - 100;
+                if (to <= 0) to = 1;
+                fromVersion.setValue(to);
+            }
+            if (message != null) {
+                JOptionPane.showMessageDialog(dialogOwner, message);
+            }
+
+            if (difference > 0) {
+ // If the end of the filter range was the previously most recent revision, + // update the filter range to the new current revision number.
+                if (toVersion.getValue() == currentVersion - difference) {
+ fromVersion.setValue(fromVersion.getValue() + difference);
+                    toVersion.setValue(currentVersion);
+                }
+            }
+            if (difference > 0 || filterChange) {
revisionsTable.refreshRevisionsList(fromVersion.getValue(), toVersion.getValue());
                 refreshPanel();
-            } else {
-                JOptionPane.showMessageDialog(dialogOwner, message);
             }
         }
     };

     private final Action revertAction = new AbstractAction("Revert...") {
         public void actionPerformed(ActionEvent e) {
-            int revisionNo = revisionsTable.getSelectedRevisionNumber();
+ final int revisionNo = revisionsTable.getSelectedRevisionNumber();
+            if (revisionNo >= currentVersion) {
+ JOptionPane.showMessageDialog(dialogOwner, "Cannot revert to the current version number");
+                return;
+            }
             int response = JOptionPane.showConfirmDialog(dialogOwner,
"Are you sure you would like to revert to version " + revisionNo,
                     "Revert...", JOptionPane.OK_CANCEL_OPTION);
-            if (response == JOptionPane.OK_OPTION) {
-                try {
- int currentVersion = session.revertServerWorkspace(revisionNo);
-                    if (currentVersion == -1) {
- JOptionPane.showMessageDialog(dialogOwner, "The server did not revert" + - " because the target and current revisions are identical");
-                    } else {
- RevisionListPanel.this.currentVersion = currentVersion; - long range = toVersion.getValue() - fromVersion.getValue();
-                        fromVersion.setValue(currentVersion - range);
-                        toVersion.setValue(currentVersion);
- refreshAction.actionPerformed(new ActionEvent(this, 0, "ignoreWarnings"));
-                        int last = revisionsTable.getRowCount() - 1;
- revisionsTable.getSelectionModel().setSelectionInterval(last, last);
-                    }
-                } catch (Throwable t) {
- throw new RuntimeException("Error requesting server revert", t);
-                }
+            if (response == JOptionPane.OK_OPTION) {
+                revertAction.setEnabled(false);
+                new Thread(new Runnable() {
+                    public void run() {
+                        try {
+ int currentVersion = session.revertServerWorkspace(revisionNo);
+                            if (currentVersion == -1) {
+ JOptionPane.showMessageDialog(dialogOwner, "The server did not revert" + + " because the target and current revisions are identical");
+                            } else {
+ RevisionListPanel.this.currentVersion = currentVersion; + long range = toVersion.getValue() - fromVersion.getValue(); + fromVersion.setValue(currentVersion - range);
+                                toVersion.setValue(currentVersion);
+ revisionsTable.refreshRevisionsList(fromVersion.getValue(), toVersion.getValue()); + int last = revisionsTable.getRowCount() - 1; + revisionsTable.getSelectionModel().setSelectionInterval(last, last);
+                            }
+                        } catch (Throwable t) {
+ throw new RuntimeException("Error requesting server revert", t);
+                        } finally {
+                            refreshPanel();
+                        }
+                    }
+                }).start();
             }
         }
     };

     private final Action openAction = new AbstractAction("Open...") {
         public void actionPerformed(ActionEvent e) {
- JOptionPane.showMessageDialog(dialogOwner, "This feature is not yet supported. Try again soon!");
+            int revisionNo = revisionsTable.getSelectedRevisionNumber();
+            ProjectLocation location = session.getProjectLocation();
+            ArchitectSwingSession revisionSession = null;
+            try {
+ revisionSession = swingSession.getContext().createSession(false); + revisionSession.setName(location.getName() + " - Revision " + revisionNo);
+
+ ArchitectSessionPersister sessionPersister = new ArchitectSessionPersister( + "inbound-" + location.getUUID(), revisionSession.getWorkspace(),
+                        new SessionPersisterSuperConverter(
+ revisionSession.getDataSources(), revisionSession.getWorkspace()));
+
+                sessionPersister.setSession(revisionSession);
+
+ SPJSONMessageDecoder decoder = new SPJSONMessageDecoder(sessionPersister);
+
+                session.persistRevisionFromServer(revisionNo, decoder);
+                revisionSession.initGUI(swingSession);
+            } catch (Exception ex) {
+                if (revisionSession != null) {
+                    revisionSession.close();
+                }
+                throw new RuntimeException(ex);
+            }
         }
     };

@@ -205,7 +253,7 @@

         int currentRevision = session.getLocalRevisionNo();
         long from = currentRevision - 100;
-        if (from < 0) from = 0;
+        if (from <= 0) from = 1;
         fromVersion = new JLongField(from);
         toVersion = new JLongField(currentRevision);

@@ -229,26 +277,32 @@
         filterBuilder.append(new JLabel("Display revisions "));
         filterBuilder.append(textFieldBuilder.getPanel());

- final JCheckBox autoRefreshBox = new JCheckBox("Auto-refresh", false);
+        autoRefreshBox = new JCheckBox("Auto-refresh", false);
         autoRefresh = new Runnable() {
             public void run() {
                 while (autoRefreshBox.isSelected()) {
- refreshAction.actionPerformed(new ActionEvent(this, 0, "ignoreWarnings"));
+                    SwingUtilities.invokeLater(new Runnable() {
+                        public void run() {
+                            refreshAction.actionPerformed(null);
+                        }
+                    });
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException ex) {
-                        break;
+                        autoRefreshBox.setSelected(false);
                     }
                 }
             }
-        };
-        autoRefreshBox.addChangeListener(new ChangeListener() {
-            public void stateChanged(ChangeEvent e) {
-                new Thread(autoRefresh).start();
-            }
-        });
+        };
+        autoRefreshBox.addItemListener(new ItemListener() {
+            public void itemStateChanged(ItemEvent e) {
+                if (autoRefreshBox.isSelected()) {
+                    new Thread(autoRefresh).start();
+                }
+            }
+        });
         autoRefreshBox.setSelected(true);
-
+
DefaultFormBuilder buttonBarBuilder = new DefaultFormBuilder(new FormLayout("pref"));
         buttonBarBuilder.append(filterBuilder.getPanel());
         buttonBarBuilder.append(new JLabel("\n"));

Reply via email to