/* * Compare_Buffer_Backups.bsh - a BeanShell macro script for the * jEdit text editor - Allows user to easily compare backup version * of the current buffer, using the JDiff plugin. * Copyright (C) 2001,2002 Ollie Rutherfurd, oliver@rutherfurd.net * Portions Copyright (C) 2001 by John Gellene * * This program 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 2 * of the License, or any later version. * * This program 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 the jEdit program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * BASE VERSION: * $Id: Compare_Buffer_Backups.bsh 28 2003-03-25 22:40:28Z oliver $ * * MODIFICATIONS: * Anthony Keeley - keeleyt83@yahoo.com - 2009.07.24 * Updated to use JDiff 3.0.0. * Added 'Compare and Close' button. * Removed compatability code for JDiff <= 1.2.2 * * NOTES: * Fold expanding code doesn't work! * */ import javax.swing.border.EmptyBorder; /* * returns a vector containing paths to all the backups for the buffer * (as well as the buffer itself). */ getBackups(_buffer) { directory = jEdit.getProperty("backup.directory"); prefix = jEdit.getProperty("backup.prefix"); suffix = jEdit.getProperty("backup.suffix"); bufferName = _buffer.getName(); bufferPath = _buffer.getPath(); bufferDir = _buffer.getVFS().getParentOfPath(_buffer.getPath()); // backups in a single backup directory if(directory != null && !directory.equals("")) { // QUESTION: better way to deal with backup paths on windows? if(bufferPath.charAt(1) == ':' && bufferPath.charAt(2) == '\\') { directory = MiscUtilities.constructPath(directory, bufferDir.substring(0,1) + bufferDir.substring(2)); } else // not windows { directory = MiscUtilities.constructPath(directory, bufferDir.substring(1)); } } else // backups not in a single directory, but local to the buffer file { directory = bufferDir; } Vector backups = new Vector(); backups.add(bufferPath); // look for a single backup (in buffer dir) f = new File(MiscUtilities.constructPath( bufferDir, prefix + bufferName + suffix)); if(f.exists()) backups.addElement(f.getPath()); // look for a single backup (in backups dir) // if not the same directory as the buffer's dir if(directory.equals(bufferDir) == false) { f = new File(MiscUtilities.constructPath( directory, prefix + bufferName + suffix)); if(f.exists()) backups.addElement(f.getPath()); } // get numbered backups in buffer directory for(int i = 1; ; i++) { f = new File(bufferDir, prefix + bufferName + suffix + i + suffix); if(f.exists() == false) break; backups.addElement(f.getPath()); } // if 'backups directory' not the buffer dir, look there too if(directory.equals(bufferDir) == false) { // get numbered backups in backups directory for(int i = 1; ; i++) { f = new File(MiscUtilities.constructPath( directory, prefix + bufferName + suffix + i + suffix)); if(f.exists() == false) break; backups.addElement(f.getPath()); } } return backups; } /* * Copied and modified from 'Compare_Buffer_to_Disk.bsh', by John Gellene */ openDiffFiles(v, fileOne, fileTwo, _mode) { if(jdiff.DualDiffManager.isEnabledFor(v)) jdiff.DualDiffManager.toggleFor(v); while(v.getEditPanes().length > 1) v.unsplit(); v.splitVertically(); jdiff.DualDiffManager.toggleFor(v); /* * We defer the opening of the files to be displayed to give * the JDiff plugin a chance to set up the side-by-side * edit panes. */ run() { bufferOne = jEdit.openFile(v, fileOne); bufferTwo = jEdit.openFile(v, fileTwo); /* * Here we wait for the new buffer (the file on disk) * to load. */ VFSManager.waitForRequests(); editPanes = v.getEditPanes(); /* * If you want your in-memory buffer on the left * side, reverse oldbuf and new buf. */ editPanes[0].setBuffer(bufferOne); editPanes[1].setBuffer(bufferTwo); // want to expand all folds (but this seems to // need to be done differently for jEdit 3.x and 4.0) if(jEdit.getBuild().compareTo("04.00.00.00") > 0) { editPanes[0].getTextArea().getFoldVisibilityManager().expandAllFolds(); editPanes[1].getTextArea().getFoldVisibilityManager().expandAllFolds(); } else { bufferOne.expandAllFolds(); bufferTwo.expandAllFolds(); } /* mode might not be determined, so setting it explicitly */ bufferOne.setMode(_mode); bufferTwo.setMode(_mode); } return this; } /* * displays dialog with version lists (main function for macro). */ compareBufferBackups(_buffer) { if(jEdit.getPlugin("jdiff.JDiffPlugin") == null) { Macros.error(view, "You must have the JDiff plugin " + "installed to use this macro."); return; } title = "Compare Buffer Backups"; dialog = new JDialog(view, title, false); content = new JPanel(new BorderLayout()); content.setBorder(new EmptyBorder(12,12,12,12)); dialog.setContentPane(content); backups = getBackups(_buffer); if(backups.size() <= 1) { Macros.error(view, "No backups found."); return; } fieldPanel = new JPanel(new GridLayout(4,1,0,6)); versionOne = new JComboBox(backups); labelOne = new JLabel("Version 1"); versionTwo = new JComboBox(backups); labelTwo = new JLabel("Version 2"); fieldPanel.add(labelOne); fieldPanel.add(versionOne); fieldPanel.add(labelTwo); fieldPanel.add(versionTwo); content.add(fieldPanel, BorderLayout.CENTER); buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.setBorder(new EmptyBorder(12,50,0,50)); buttonPanel.add(Box.createGlue()); compare = new JButton("Compare"); compareAndClose = new JButton("Compare and Close"); close = new JButton("Close"); compare.setPreferredSize(compareAndClose.getPreferredSize()); close.setPreferredSize(compareAndClose.getPreferredSize()); dialog.getRootPane().setDefaultButton(compare); buttonPanel.add(compare); buttonPanel.add(Box.createHorizontalStrut(6)); buttonPanel.add(compareAndClose); buttonPanel.add(Box.createHorizontalStrut(6)); buttonPanel.add(close); buttonPanel.add(Box.createGlue()); content.add(buttonPanel, BorderLayout.SOUTH); void compareVersions() { vOne = versionOne.getSelectedItem(); vTwo = versionTwo.getSelectedItem(); if(vOne == vTwo) { Macros.error(view, "Versions are identical."); return; } opener = openDiffFiles(view, vOne, vTwo, _buffer.getMode()); SwingUtilities.invokeLater(opener); } void actionPerformed(e) { if(e.getSource() == close) dialog.dispose(); else if (e.getSource() == compare) { compareVersions(); } else { compareVersions(); dialog.dispose(); } } compare.addActionListener(this); compareAndClose.addActionListener(this); close.addActionListener(this); dialog.pack(); dialog.setLocationRelativeTo(view); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } if(!buffer.getVFS().getName().equals("file")) { Macros.error(view, "This macro only works with local files (file VFS)."); return; } compareBufferBackups(buffer); /* Macro index data (in DocBook format) Compare_Buffer_Backups Displays two lists of backups of the current buffer, allowing the user to select two different versions and view the differences with the JDiff plugin. */