Revision: 3301
Author: silva.josemanuel1
Date: Wed Feb 17 12:26:45 2010
Log: Added ability/UI to compare revisions on the server. Currently only compares select object types, and does not show the actual changes. This will be implemented later.
http://code.google.com/p/power-architect/source/detail?r=3301

Added:
/trunk/src/ca/sqlpower/architect/swingui/enterprise/CompareRevisionsPanel.java
 /trunk/src/ca/sqlpower/architect/swingui/enterprise/RevisionsTable.java
Modified:
 /trunk/src/ca/sqlpower/architect/swingui/CompareDMFormatter.java
 /trunk/src/ca/sqlpower/architect/swingui/enterprise/RevisionListPanel.java

=======================================
--- /dev/null
+++ /trunk/src/ca/sqlpower/architect/swingui/enterprise/CompareRevisionsPanel.java Wed Feb 17 12:26:45 2010
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2010, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * 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.
+ *
+ * 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.enterprise;
+
+import java.awt.Dimension;
+import java.awt.Insets;
+import java.awt.event.ActionEvent;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.util.List;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JButton;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextPane;
+import javax.swing.text.DefaultStyledDocument;
+
+import ca.sqlpower.architect.enterprise.ArchitectClientSideSession;
+import ca.sqlpower.architect.swingui.CompareDMFormatter;
+import ca.sqlpower.diff.DiffChunk;
+import ca.sqlpower.sqlobject.SQLObject;
+
+import com.jgoodies.forms.builder.DefaultFormBuilder;
+import com.jgoodies.forms.layout.CellConstraints;
+import com.jgoodies.forms.layout.FormLayout;
+
+/**
+ * This class sets up a panel with two (identical) lists of revisions on the server,
+ * and displays the differences between two selected revisions below them.
+ */
+public class CompareRevisionsPanel {
+
+    private final ArchitectClientSideSession session;
+
+    private final RevisionsTable revisionsTableLeft;
+    private final RevisionsTable revisionsTableRight;
+    private final JTextPane comparePane;
+
+    private final JPanel panel;
+
+    private final Action refreshAction = new AbstractAction("Refresh...") {
+        public void actionPerformed(ActionEvent e) {
+            revisionsTableLeft.refreshRevisionsList();
+            revisionsTableRight.refreshRevisionsList();
+            refreshPanel();
+        }
+    };
+
+    private final Action compareAction = new AbstractAction("Compare...") {
+        public void actionPerformed(ActionEvent e) {
+ int oldRevisionNo = revisionsTableLeft.getSelectedRevisionNumber(); + int newRevisionNo = revisionsTableRight.getSelectedRevisionNumber();
+
+            DefaultStyledDocument resultDoc = new DefaultStyledDocument();
+            try {
+                if (oldRevisionNo >= 0 && newRevisionNo >= 0) {
+ List<DiffChunk<SQLObject>> diff = session.getComparisonDiffChunks(oldRevisionNo, newRevisionNo);
+                    if (diff.size() == 0) {
+ resultDoc.insertString(0, "Revisions are identical", null);
+                    } else {
+ resultDoc = CompareDMFormatter.generateEnglishDescription(CompareDMFormatter.DIFF_STYLES, true, diff);
+                    }
+                } else {
+ JOptionPane.showMessageDialog(panel, "A revision must be selected from each table.");
+                }
+            } catch (Throwable t) {
+                throw new RuntimeException("Error making comparison", t);
+            }
+
+            comparePane.setStyledDocument(resultDoc);
+        }
+    };
+
+ public CompareRevisionsPanel(ArchitectClientSideSession session, Action closeAction) {
+
+        this.session = session;
+
+        revisionsTableLeft = new RevisionsTable(this.session);
+        revisionsTableRight = new RevisionsTable(this.session);
+
+        MouseListener tableListener = new MouseAdapter() {
+            @Override
+            public void mouseClicked(MouseEvent e) {
+                refreshPanel();
+            }
+        };
+
+        revisionsTableLeft.addMouseListener(tableListener);
+        revisionsTableRight.addMouseListener(tableListener);
+
+ JScrollPane revisionsPaneLeft = new JScrollPane(revisionsTableLeft); + JScrollPane revisionsPaneRight = new JScrollPane(revisionsTableRight);
+
+ revisionsPaneLeft.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); + revisionsPaneRight.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); + revisionsPaneLeft.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + revisionsPaneRight.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+
+        comparePane = new JTextPane();
+        comparePane.setEditable(false);
+        comparePane.setMargin(new Insets(6, 10, 4, 6));
+        JScrollPane sp = new JScrollPane(comparePane);
+        sp.setPreferredSize(revisionsPaneLeft.getPreferredSize());
+
+        CellConstraints cc = new CellConstraints();
+
+        DefaultFormBuilder revisionListsBuilder = new DefaultFormBuilder(
+ new FormLayout("default:grow, 5dlu, default:grow", "pref, 2dlu, default:grow")); + revisionListsBuilder.add(new JLabel("From revision..."), cc.xy(1, 1)); + revisionListsBuilder.add(new JLabel("To revision..."), cc.xy(3, 1));
+        revisionListsBuilder.add(revisionsPaneLeft, cc.xy(1, 3));
+        revisionListsBuilder.add(revisionsPaneRight, cc.xy(3, 3));
+
+ DefaultFormBuilder buttonBarBuilder = new DefaultFormBuilder(new FormLayout("pref"));
+        buttonBarBuilder.append(new JButton(refreshAction));
+        buttonBarBuilder.append(new JButton(compareAction));
+        buttonBarBuilder.append(new JButton(closeAction));
+
+        DefaultFormBuilder bottomBuilder = new DefaultFormBuilder(
+ new FormLayout("default:grow, right:default", "default:grow"));
+        bottomBuilder.add(sp, cc.xy(1, 1));
+        bottomBuilder.add(buttonBarBuilder.getPanel(), cc.xy(2, 1));
+
+        DefaultFormBuilder builder = new DefaultFormBuilder(
+ new FormLayout("default:grow", "default:grow, 5dlu, default:grow"));
+        builder.add(revisionListsBuilder.getPanel(), cc.xy(1, 1));
+        builder.add(bottomBuilder.getPanel(), cc.xy(1, 3));
+
+        builder.setDefaultDialogBorder();
+        panel = builder.getPanel();
+        panel.setPreferredSize(new Dimension(900, 500));
+        refreshPanel();
+
+    }
+
+    private void refreshPanel() {
+        compareAction.setEnabled(
+                revisionsTableLeft.getSelectedRow() != -1 &&
+                revisionsTableRight.getSelectedRow() != -1);
+    }
+
+    public JPanel getPanel() {
+        return panel;
+    }
+
+}
=======================================
--- /dev/null
+++ /trunk/src/ca/sqlpower/architect/swingui/enterprise/RevisionsTable.java Wed Feb 17 12:26:45 2010
@@ -0,0 +1,218 @@
+/*
+ * Copyright (c) 2010, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * 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.
+ *
+ * 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.enterprise;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Insets;
+import java.awt.event.ActionEvent;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JDialog;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.JTextArea;
+import javax.swing.ListSelectionModel;
+import javax.swing.SwingUtilities;
+import javax.swing.table.DefaultTableCellRenderer;
+import javax.swing.table.DefaultTableModel;
+
+import ca.sqlpower.architect.enterprise.ArchitectClientSideSession;
+import ca.sqlpower.enterprise.TransactionInformation;
+import ca.sqlpower.swingui.SPSUtils;
+
+/**
+ * This class is used to display revisions on the server, obtained through a client session, in a table.
+ */
+public class RevisionsTable extends JTable {
+
+ private static final String[] HEADERS = {"Version", "Time Created", "Author", "Description"};
+
+    private ArchitectClientSideSession session;
+
+ private List<TransactionInformation> transactions = new ArrayList<TransactionInformation>();
+
+    /**
+     * Displays information about a revision when it is double clicked
+     */
+    private final Action infoAction = new AbstractAction("Information") {
+        public void actionPerformed(ActionEvent e) {
+            int revisionNo = e.getID();
+
+ final JDialog d = SPSUtils.makeOwnedDialog(RevisionsTable.this.getParent(), "Revision " + revisionNo);
+            d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+            d.setContentPane(new RevisionInformationPanel(revisionNo));
+
+            SPSUtils.makeJDialogCancellable(d, null);
+            d.setPreferredSize(new Dimension(600, 300));
+            d.pack();
+            d.setLocationRelativeTo(RevisionsTable.this.getParent());
+            d.setVisible(true);
+        }
+    };
+
+    /**
+ * A simple component that displays the information of a revision in a scrolling text area.
+     */
+    private class RevisionInformationPanel extends JPanel {
+
+        public RevisionInformationPanel(int revisionNo) {
+
+            super(new BorderLayout());
+ TransactionInformation info = RevisionsTable.this.getRevisionInformation(revisionNo);
+            JTextArea infoArea = new JTextArea();
+            infoArea.setMargin(new Insets(6, 10, 4, 6));
+            infoArea.setEditable(false);
+            infoArea.append("Revision " + revisionNo);
+            infoArea.append("\n\nAuthor: " + info.getVersionAuthor());
+ infoArea.append("\nTime created: " + info.getTimeCreated().toString());
+            String description = "";
+            for (String line : info.getVersionDescription().split(", ")) {
+                description += "\n" + line;
+            }
+            infoArea.append("\n\nDescription:" + description);
+            final JScrollPane sp = new JScrollPane(infoArea);
+            this.add(sp);
+
+            // Reset scroll bars
+            SwingUtilities.invokeLater(new Runnable() {
+                public void run() {
+ sp.getHorizontalScrollBar().setValue(sp.getHorizontalScrollBar().getMinimum()); + sp.getVerticalScrollBar().setValue(sp.getVerticalScrollBar().getMinimum());
+                }
+            });
+
+        }
+
+    }
+
+    /**
+     * Creates a RevisionsTable with a custom mouse listener, or none.
+     * @param session
+     * @param listener
+     */
+    public RevisionsTable(ArchitectClientSideSession session) {
+        super();
+
+        this.session = session;
+
+        this.setColumnSelectionAllowed(false);
+        this.setShowVerticalLines(false);
+        this.setShowHorizontalLines(false);
+        this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
+
+        refreshRevisionsList();
+
+        ListSelectionModel selectionModel = this.getSelectionModel();
+ selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+
+        this.addMouseListener(new MouseAdapter() {
+            public void mouseClicked(MouseEvent e) {
+ if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e) && getSelectedRow() != -1) {
+                    int revisionNo = getSelectedRevisionNumber();
+ infoAction.actionPerformed(new ActionEvent(this, revisionNo, null));
+                }
+            }
+        });
+    }
+
+    /**
+ * Gets the revision list from the server and puts it into a table model.
+     */
+    public void refreshRevisionsList() {
+
+        String[][] data = {{""}};
+        int selected = this.getSelectedRow();
+
+ // Determine the length of each of the headers by using the DefaultTableCellRenderer's FontMetrics
+        Font f = new DefaultTableCellRenderer().getFont();
+        FontMetrics fm = new DefaultTableCellRenderer().getFontMetrics(f);
+        int[] maxWidth = new int[HEADERS.length];
+        for (int i = 0; i < HEADERS.length; i++) {
+            maxWidth[i] = fm.stringWidth(HEADERS[i]);
+        }
+
+        try {
+            transactions = session.getTransactionList();
+
+            data = new String[transactions.size()][HEADERS.length];
+
+            for (int i = 0; i < transactions.size(); i++) {
+                TransactionInformation transaction = transactions.get(i);
+ data[i][0] = String.valueOf(transaction.getVersionNumber());
+                data[i][1] = transaction.getTimeCreated().toString();
+                data[i][2] = transaction.getVersionAuthor();
+                data[i][3] = transaction.getVersionDescription();
+
+ // Determine the length of each column by determining the longest piece of data within each.
+                for (int j = 0; j < HEADERS.length; j++) {
+                    int cellWidth = fm.stringWidth(data[i][j]);
+                    if (cellWidth > maxWidth[j]) {
+                        maxWidth[j] = cellWidth;
+                    }
+                }
+            }
+
+        } catch (Throwable e) {
+ throw new RuntimeException("Error getting revision list from server: " + e);
+        } finally {
+            this.setModel(new DefaultTableModel(data, HEADERS) {
+                public boolean isCellEditable(int x, int y) {
+                    return false;
+                }
+            });
+
+            for (int i = 0; i < HEADERS.length; i++) {
+ this.getColumnModel().getColumn(i).setPreferredWidth(maxWidth[i]);
+            }
+
+            if (selected < data.length && selected != -1) {
+                this.setRowSelectionInterval(selected, selected);
+            }
+        }
+
+
+    }
+
+    public TransactionInformation getRevisionInformation(int revisionNo) {
+
+        return transactions.get(revisionNo);
+
+    }
+
+    public int getSelectedRevisionNumber() {
+        int headersColumn = 0;
+        for (int i = 0; i < HEADERS.length; i++) {
+            if (HEADERS[i].contains("Version")) {
+                headersColumn = i;
+            }
+        }
+ return Integer.parseInt((String) this.getModel().getValueAt(this.getSelectedRow(), headersColumn));
+    }
+
+}
=======================================
--- /trunk/src/ca/sqlpower/architect/swingui/CompareDMFormatter.java Fri Feb 12 15:21:05 2010 +++ /trunk/src/ca/sqlpower/architect/swingui/CompareDMFormatter.java Wed Feb 17 12:26:45 2010
@@ -63,6 +63,33 @@
* The dialog that owns any additional dialogs popped up by this formatter.
      */
     private final Dialog dialogOwner;
+
+    /**
+ * A hash map of styles which dictate the color of different kinds of changes/differences.
+     */
+ public final static Map<DiffType, AttributeSet> DIFF_STYLES = new HashMap<DiffType, AttributeSet>();
+    static {
+        SimpleAttributeSet att = new SimpleAttributeSet();
+        StyleConstants.setForeground(att, Color.red);
+        DIFF_STYLES.put(DiffType.LEFTONLY, att);
+
+        att = new SimpleAttributeSet();
+        StyleConstants.setForeground(att, Color.green.darker().darker());
+        DIFF_STYLES.put(DiffType.RIGHTONLY, att);
+
+        att = new SimpleAttributeSet();
+        StyleConstants.setForeground(att, Color.black);
+        DIFF_STYLES.put(DiffType.SAME, att);
+
+        att = new SimpleAttributeSet();
+        StyleConstants.setForeground(att, Color.orange);
+        DIFF_STYLES.put(DiffType.MODIFIED, att);
+
+        att = new SimpleAttributeSet();
+        StyleConstants.setForeground(att, Color.blue);
+        DIFF_STYLES.put(DiffType.KEY_CHANGED, att);
+        DIFF_STYLES.put(DiffType.DROP_KEY, att);
+   }

public CompareDMFormatter(ArchitectSwingSession session, Dialog dialogOwner, CompareDMSettings compDMSet) {
         super();
@@ -87,30 +114,7 @@
gen.setTargetCatalog(cat == null ? null : cat.getPhysicalName()); gen.setTargetSchema(sch == null ? null : sch.getPhysicalName());
             }
-
- final Map<DiffType, AttributeSet> styles = new HashMap<DiffType, AttributeSet>();
-            {
-                SimpleAttributeSet att = new SimpleAttributeSet();
-                StyleConstants.setForeground(att, Color.red);
-                styles.put(DiffType.LEFTONLY, att);
-
-                att = new SimpleAttributeSet();
- StyleConstants.setForeground(att, Color.green.darker().darker());
-                styles.put(DiffType.RIGHTONLY, att);
-
-                att = new SimpleAttributeSet();
-                StyleConstants.setForeground(att, Color.black);
-                styles.put(DiffType.SAME, att);
-
-                att = new SimpleAttributeSet();
-                StyleConstants.setForeground(att, Color.orange);
-                styles.put(DiffType.MODIFIED, att);
-
-                att = new SimpleAttributeSet();
-                StyleConstants.setForeground(att, Color.blue);
-                styles.put(DiffType.KEY_CHANGED, att);
-                styles.put(DiffType.DROP_KEY, att);
-            }
+
if (dmSetting.getOutputFormat().equals(CompareDMSettings.OutputFormat.SQL)) {

List<DiffChunk<SQLObject>> addRelationships = new ArrayList<DiffChunk<SQLObject>>();
@@ -128,13 +132,13 @@
                         nonRelationship.add(d);
                     }
                 }
-                sqlScriptGenerator(styles, dropRelationships, gen);
-                sqlScriptGenerator(styles, nonRelationship, gen);
-                sqlScriptGenerator(styles, addRelationships, gen);
+                sqlScriptGenerator(DIFF_STYLES, dropRelationships, gen);
+                sqlScriptGenerator(DIFF_STYLES, nonRelationship, gen);
+                sqlScriptGenerator(DIFF_STYLES, addRelationships, gen);

} else if (dmSetting.getOutputFormat().equals(CompareDMSettings.OutputFormat.ENGLISH)) {
-                generateEnglishDescription(styles, diff, sourceDoc);
-                generateEnglishDescription(styles, diff1, targetDoc);
+ sourceDoc = generateEnglishDescription(DIFF_STYLES, dmSetting.getSuppressSimilarities(), diff); + targetDoc = generateEnglishDescription(DIFF_STYLES, dmSetting.getSuppressSimilarities(), diff1);
             } else {
                 throw new IllegalStateException(
                 "Don't know what type of output to make");
@@ -254,23 +258,24 @@

     /**
* This method generates english descriptions by taking in the diff list - * and putting the appropiate statements in the document. It will iterate + * and putting the appropiate statements in the returned document. It will iterate
      * through the diff list and identify which type of DiffChunk it is and
-     * what kind of SQLType it is to produce the proper english description
-     * output
+ * what kind of SQLType it is to produce the proper english description output
      * @throws BadLocationException
      * @throws SQLObjectException
      */
-    private void generateEnglishDescription(
-            Map<DiffType, AttributeSet> styles,
- List<DiffChunk<SQLObject>> diff, DefaultStyledDocument sourceDoc)
+    public static DefaultStyledDocument generateEnglishDescription(
+ Map<DiffType, AttributeSet> styles, boolean suppressSimilarities,
+            List<DiffChunk<SQLObject>> diff)
             throws BadLocationException, SQLObjectException {
+
+        DefaultStyledDocument resultDoc = new DefaultStyledDocument();

         String currentTableName = "";

         for (DiffChunk<SQLObject> chunk : diff) {
             SQLObject o = chunk.getData();
- if (dmSetting.getSuppressSimilarities() && chunk.getType().equals(DiffType.SAME)) { + if (suppressSimilarities && chunk.getType().equals(DiffType.SAME)) {
                 if (o instanceof SQLTable) {
                     currentTableName = o.getName();
                 }
@@ -287,34 +292,34 @@
             StyleConstants.setBold(boldAttributes, true);

             if (o == null) {
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
                         "ERROR: null object in diff list\n",
                         attributes);
             } else if (o instanceof SQLTable) {
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
                         "Table ",
                         attributes);
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
(o.getPhysicalName() != null || o.getPhysicalName().trim().equals("") ? o.getName() : o.getPhysicalName())+ " ",
                         boldAttributes);
             } else if (o instanceof SQLColumn) {
- if (dmSetting.getSuppressSimilarities() && !currentTableName.equals("")) {
+                if (suppressSimilarities && !currentTableName.equals("")) {
                     attributes = styles.get(DiffType.SAME);
                     boldAttributes = new SimpleAttributeSet(attributes);
                     StyleConstants.setBold(boldAttributes, true);
-                    sourceDoc.insertString(
-                            sourceDoc.getLength(),
+                    resultDoc.insertString(
+                            resultDoc.getLength(),
                             "Table ",
                             attributes);
-                    sourceDoc.insertString(
-                            sourceDoc.getLength(),
+                    resultDoc.insertString(
+                            resultDoc.getLength(),
                             currentTableName,
                             boldAttributes);
-                    sourceDoc.insertString(
-                            sourceDoc.getLength(),
+                    resultDoc.insertString(
+                            resultDoc.getLength(),
                             " needs no changes\n",
                             attributes);
                     currentTableName = "";
@@ -322,30 +327,30 @@
                 attributes = styles.get(chunk.getType());
                 boldAttributes = new SimpleAttributeSet(attributes);
                 StyleConstants.setBold(boldAttributes, true);
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
                         "\tColumn ",
                         attributes);
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
                         o.getName() + " ",
                         boldAttributes);
             } else if (o instanceof SQLRelationship) {
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
                         "Foreign Key ",
                         attributes);
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
                         o.getName() + " ",
                         boldAttributes);
             } else {
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
                         "Unknown object type ",
                         attributes);
-                sourceDoc.insertString(
-                        sourceDoc.getLength(),
+                resultDoc.insertString(
+                        resultDoc.getLength(),
                         o.getClass().getName() + " ",
                         boldAttributes);
             }
@@ -383,11 +388,12 @@
                 break;
             }

-            sourceDoc.insertString(
-                    sourceDoc.getLength(),
+            resultDoc.insertString(
+                    resultDoc.getLength(),
                     diffTypeEnglish + "\n",
                     attributes);
         }
+        return resultDoc;
     }

 //  Generates the proper title text for compareDMFrame or SQLScriptDialog
=======================================
--- /trunk/src/ca/sqlpower/architect/swingui/enterprise/RevisionListPanel.java Thu Feb 11 14:26:15 2010 +++ /trunk/src/ca/sqlpower/architect/swingui/enterprise/RevisionListPanel.java Wed Feb 17 12:26:45 2010
@@ -24,64 +24,52 @@
 import java.awt.event.ActionEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
-import java.util.List;

 import javax.swing.AbstractAction;
 import javax.swing.Action;
 import javax.swing.JButton;
+import javax.swing.JDialog;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
-import javax.swing.JTable;
-import javax.swing.ListSelectionModel;
-import javax.swing.SwingUtilities;
-import javax.swing.table.DefaultTableModel;

 import ca.sqlpower.architect.enterprise.ArchitectClientSideSession;
 import ca.sqlpower.architect.swingui.ArchitectFrame;
 import ca.sqlpower.architect.swingui.ArchitectSwingSession;
-import ca.sqlpower.enterprise.TransactionInformation;
-import ca.sqlpower.util.UserPrompter;
-import ca.sqlpower.util.UserPrompter.UserPromptOptions;
-import ca.sqlpower.util.UserPrompter.UserPromptResponse;
-import ca.sqlpower.util.UserPrompterFactory.UserPromptType;
+import ca.sqlpower.swingui.SPSUtils;

 import com.jgoodies.forms.builder.DefaultFormBuilder;
 import com.jgoodies.forms.layout.CellConstraints;
 import com.jgoodies.forms.layout.FormLayout;

 public class RevisionListPanel {
-
- private static final String[] headers = {"Version", "Time Created", "Author", "Description"};

     private final Component dialogOwner;
     private final ArchitectClientSideSession session;
     private final ArchitectSwingSession swingSession;
-    private final UserPrompter revertPrompt;
-    private final Action closeAction;
-
-    private List<TransactionInformation> transactions;
-
-    private final JTable revisionsTable;
+
+    private final RevisionsTable revisionsTable;
+
     private final JPanel panel;

     private final Action refreshAction = new AbstractAction("Refresh...") {
-        public void actionPerformed(ActionEvent e) {
-            refreshRevisionsList();
+        public void actionPerformed(ActionEvent e) {
+            revisionsTable.refreshRevisionsList();
+            refreshPanel();
         }
     };

     private final Action revertAction = new AbstractAction("Revert...") {
         public void actionPerformed(ActionEvent e) {
- int revisionNo = Integer.parseInt((String) revisionsTable.getValueAt(revisionsTable.getSelectedRow(), 0));
+            int revisionNo = revisionsTable.getSelectedRevisionNumber();
             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 {
                     session.revertServerWorkspace(revisionNo);
-                    refreshRevisionsList();
+                    revisionsTable.refreshRevisionsList();
                 } catch (Throwable t) {
throw new RuntimeException("Error requesting server revert", t);
                 }
@@ -97,92 +85,72 @@

     private final Action compareAction = new AbstractAction("Compare...") {
         public void actionPerformed(ActionEvent e) {
- JOptionPane.showMessageDialog(dialogOwner, "This feature is not yet supported. Try again soon!");
-        }
-    };
-
-    private void refreshRevisionsList() {
-
-        try {
-            transactions = session.getTransactionList();
-        } catch (Throwable e) {
- throw new RuntimeException("Error getting revision list from server: " + e);
-        }
-
-        String[][] data = new String[transactions.size()][4];
-
-        for (int i = 0; i < transactions.size(); i++) {
-            TransactionInformation transaction = transactions.get(i);
-            data[i][0] = String.valueOf(transaction.getVersionNumber());
-            data[i][1] = transaction.getTimeCreated().toString();
-            data[i][2] = transaction.getVersionAuthor();
-            data[i][3] = transaction.getVersionDescription();
-        }
-
-        revisionsTable.setModel(new DefaultTableModel(data, headers) {
-            public boolean isCellEditable(int x, int y) {
-                return false;
-            }
-        });
-
-    }
+ final JDialog d = SPSUtils.makeOwnedDialog(swingSession.getArchitectFrame(), "Compare Revisions");
+            Action closeAction = new AbstractAction("Close") {
+                public void actionPerformed(ActionEvent e) {
+                    d.dispose();
+                }
+            };
+
+ CompareRevisionsPanel p = new CompareRevisionsPanel(session, closeAction);
+            d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+            d.setContentPane(p.getPanel());
+
+            SPSUtils.makeJDialogCancellable(d, null);
+            d.pack();
+            d.setLocationRelativeTo(RevisionListPanel.this.getPanel());
+            d.setVisible(true);
+        }
+    };

public RevisionListPanel(ArchitectSwingSession swingSession, ArchitectFrame architectFrame, Action closeAction) {

         this.dialogOwner = architectFrame;
         this.swingSession = swingSession;
         this.session = swingSession.getEnterpriseSession();
-        this.closeAction = closeAction;

         DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout(
-                "pref:grow, 5dlu, pref:grow, 5dlu, pref",
-                "pref, pref, pref"));
-
-        revisionsTable = new JTable();
-        revisionsTable.setColumnSelectionAllowed(false);
-        revisionsTable.setShowVerticalLines(false);
-        revisionsTable.setShowHorizontalLines(false);
-
-        refreshRevisionsList();
-
- ListSelectionModel selectionModel = revisionsTable.getSelectionModel(); - selectionModel.setSelectionInterval(transactions.size(), transactions.size()); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
-
+                "pref:grow, 5dlu, pref",
+                "pref, 2dlu, default:grow"));
+
+        revisionsTable = new RevisionsTable(this.session);
         revisionsTable.addMouseListener(new MouseAdapter() {
             @Override
             public void mouseClicked(MouseEvent e) {
- if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
-                    revertAction.actionPerformed(null);
-                }
+                refreshPanel();
             }
         });
-
-        JScrollPane revisionsPane = new JScrollPane(revisionsTable);
+
+        JScrollPane revisionsPane = new JScrollPane(revisionsTable);

         CellConstraints cc = new CellConstraints();
-        builder.add(new JLabel("Revisions:"), cc.xyw(3, 1, 2));
-        builder.nextLine();
-        builder.add(revisionsPane, cc.xywh(3, 2, 1, 2));
-
- revertPrompt = swingSession.createUserPrompter("Are you sure you would like the server to revert to version {0}?",
-                UserPromptType.BOOLEAN,
-                UserPromptOptions.OK_CANCEL,
-                UserPromptResponse.OK,
-                UserPromptResponse.OK,
-                "OK", "Cancel");
+        builder.add(new JLabel("Revisions:"), cc.xy(1, 1));
+        builder.add(revisionsPane, cc.xy(1, 3));

DefaultFormBuilder buttonBarBuilder = new DefaultFormBuilder(new FormLayout("pref"));
         buttonBarBuilder.append(new JButton(refreshAction));
         buttonBarBuilder.append(new JButton(revertAction));
         buttonBarBuilder.append(new JButton(openAction));
         buttonBarBuilder.append(new JButton(compareAction));
-        buttonBarBuilder.append(new JButton(closeAction));
-        builder.add(buttonBarBuilder.getPanel(), cc.xy(5, 2));
+        buttonBarBuilder.append(new JButton(closeAction));
+        builder.add(buttonBarBuilder.getPanel(), cc.xy(3, 3));
         builder.setDefaultDialogBorder();
-        panel = builder.getPanel();
+
+        panel = builder.getPanel();
         panel.setPreferredSize(new Dimension(700, 250));

+        refreshPanel();
+
+    }
+
+    private void refreshPanel() {
+        if (revisionsTable.getSelectedRow() == -1) {
+            openAction.setEnabled(false);
+            revertAction.setEnabled(false);
+        } else {
+            openAction.setEnabled(true);
+            revertAction.setEnabled(true);
+        }
     }

     public JPanel getPanel() {

Reply via email to