From 05eecdedd5e03c15417fd704236a850c1579e4aa Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 20 Mar 2008 20:12:27 +0000 Subject: [PATCH] * forgot to add renamed files again --- .../panel/rename/SelectionSynchronizer.java | 70 +++++++++++++++++++ .../ui/panel/rename/ViewPortSynchronizer.java | 70 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 source/net/sourceforge/filebot/ui/panel/rename/SelectionSynchronizer.java create mode 100644 source/net/sourceforge/filebot/ui/panel/rename/ViewPortSynchronizer.java diff --git a/source/net/sourceforge/filebot/ui/panel/rename/SelectionSynchronizer.java b/source/net/sourceforge/filebot/ui/panel/rename/SelectionSynchronizer.java new file mode 100644 index 00000000..3cd155c2 --- /dev/null +++ b/source/net/sourceforge/filebot/ui/panel/rename/SelectionSynchronizer.java @@ -0,0 +1,70 @@ + +package net.sourceforge.filebot.ui.panel.rename; + + +import javax.swing.JList; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; + + +class SelectionSynchronizer { + + private JList list1; + private JList list2; + + private SelectionSynchronizeListener selectionSynchronizeListener1; + private SelectionSynchronizeListener selectionSynchronizeListener2; + + + public SelectionSynchronizer(JList list1, JList list2) { + this.list1 = list1; + this.list2 = list2; + + selectionSynchronizeListener1 = new SelectionSynchronizeListener(list2); + selectionSynchronizeListener2 = new SelectionSynchronizeListener(list1); + + setEnabled(true); + } + + + public void setEnabled(boolean enabled) { + // remove listeners to avoid adding them multiple times + list1.removeListSelectionListener(selectionSynchronizeListener1); + list2.removeListSelectionListener(selectionSynchronizeListener2); + + // if enabled add them again + if (enabled) { + list1.addListSelectionListener(selectionSynchronizeListener1); + list2.addListSelectionListener(selectionSynchronizeListener2); + } + } + + + private static class SelectionSynchronizeListener implements ListSelectionListener { + + private JList target; + + + public SelectionSynchronizeListener(JList to) { + this.target = to; + } + + + public void valueChanged(ListSelectionEvent e) { + JList source = (JList) e.getSource(); + int index = source.getSelectedIndex(); + + if (target.getModel().getSize() > index) { + if (index != target.getSelectedIndex()) { + target.setSelectedIndex(index); + } + + target.ensureIndexIsVisible(index); + } else { + target.clearSelection(); + } + } + + } + +} diff --git a/source/net/sourceforge/filebot/ui/panel/rename/ViewPortSynchronizer.java b/source/net/sourceforge/filebot/ui/panel/rename/ViewPortSynchronizer.java new file mode 100644 index 00000000..c125a25a --- /dev/null +++ b/source/net/sourceforge/filebot/ui/panel/rename/ViewPortSynchronizer.java @@ -0,0 +1,70 @@ + +package net.sourceforge.filebot.ui.panel.rename; + + +import java.awt.Point; + +import javax.swing.JViewport; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + + +class ViewPortSynchronizer { + + private JViewport viewport1; + private JViewport viewport2; + + private ViewPortSynchronizeListener viewPortSynchronizeListener1; + private ViewPortSynchronizeListener viewPortSynchronizeListener2; + + + public ViewPortSynchronizer(JViewport viewport1, JViewport viewport2) { + this.viewport1 = viewport1; + this.viewport2 = viewport2; + + viewPortSynchronizeListener1 = new ViewPortSynchronizeListener(viewport2); + viewPortSynchronizeListener2 = new ViewPortSynchronizeListener(viewport1); + + setEnabled(true); + } + + + public void setEnabled(boolean enabled) { + // remove listeners to avoid adding them multiple times + viewport1.removeChangeListener(viewPortSynchronizeListener1); + viewport2.removeChangeListener(viewPortSynchronizeListener2); + + // if enabled add them again + if (enabled) { + viewport1.addChangeListener(viewPortSynchronizeListener1); + viewport2.addChangeListener(viewPortSynchronizeListener2); + } + } + + + private static class ViewPortSynchronizeListener implements ChangeListener { + + private JViewport target; + + + public ViewPortSynchronizeListener(JViewport target) { + this.target = target; + } + + + @Override + public void stateChanged(ChangeEvent e) { + JViewport source = (JViewport) e.getSource(); + + Point viewPosition = source.getViewPosition(); + + // return if both viewports have the same view position + if (viewPosition.equals(target.getViewPosition())) + return; + + target.setViewPosition(viewPosition); + target.repaint(); + } + } + +}