/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* 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 3 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.utilities.swing.pdf;
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import de.chris_soft.utilities.pdf.PdfPageRenderUtility;
import de.chris_soft.utilities.swing.FileList;
/**
* Object for navigating through many PDF files.
* @author Christian Packenius.
*/
public class MultiPdfZapper implements ListSelectionListener {
private final JPanel xPanel = new JPanel();
private PdfThumbnailZapper pdfThumbnailZapper;
private FileList pdfList;
private List<CurrentFileListener> currentFileListeners = new ArrayList<CurrentFileListener>();
/**
* Constructor.
*/
public MultiPdfZapper() {
xPanel.setLayout(new BorderLayout(2, 2));
pdfThumbnailZapper = new PdfThumbnailZapper();
xPanel.add(BorderLayout.CENTER, pdfThumbnailZapper.getComponent());
pdfList = new FileList();
xPanel.add(BorderLayout.WEST, pdfList.getComponent());
pdfList.addListSelectionListener(this);
}
/**
* Adds a PDF file to the navigator.
* @param pdfFile PDF file to view.
* @throws IOException
*/
public void add(File pdfFile) throws IOException {
pdfList.add(pdfFile);
}
/**
* @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
*/
@Override
public void valueChanged(ListSelectionEvent listselectionevent) {
if (listselectionevent == null) {
showNoDocument();
}
else {
showCurrentSelectedDocument();
}
}
private void showCurrentSelectedDocument() {
try {
File currentFile = pdfList.getSelectedFile();
informCurrentFileListeners(currentFile);
pdfThumbnailZapper.set(new PdfPageRenderUtility(currentFile));
} catch (Exception exception) {
showNoDocument();
}
}
private void showNoDocument() {
try {
pdfThumbnailZapper.set(null);
} catch (Exception exception1) {
// Ignore.
}
}
private void informCurrentFileListeners(File currentFile) {
for (CurrentFileListener fileListener : currentFileListeners) {
fileListener.currentFileChanged(currentFile);
}
}
/**
* Returns the swing component of this object.
* @return JPanel object.
*/
public Component getComponent() {
return xPanel;
}
/**
* Adds a new file lister to the list.
* @param fileListener
*/
public void addCurrentFileListener(CurrentFileListener fileListener) {
currentFileListeners.add(fileListener);
}
}