org.jpedal.examples.viewer.gui.MultiViewTransferHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of OpenViewerFX Show documentation
Show all versions of OpenViewerFX Show documentation
Open Source (LGPL) JavaFX PDF Viewer
/*
* ===========================================
* Java Pdf Extraction Decoding Access Library
* ===========================================
*
* Project Info: http://www.idrsolutions.com
* Help section for developers at http://www.idrsolutions.com/support/
*
* (C) Copyright 1997-2017 IDRsolutions and Contributors.
*
* This file is part of JPedal/JPDF2HTML5
*
@LICENSE@
*
* ---------------
* MultiViewTransferHandler.java
* ---------------
*/
package org.jpedal.examples.viewer.gui;
import java.awt.datatransfer.Transferable;
import java.beans.PropertyVetoException;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import org.jpedal.examples.viewer.Commands;
import org.jpedal.examples.viewer.Values;
import org.jpedal.examples.viewer.commands.OpenFile;
import org.jpedal.gui.GUIFactory;
import org.jpedal.utils.LogWriter;
import org.jpedal.utils.Messages;
import org.jpedal.utils.SwingWorker;
public class MultiViewTransferHandler extends BaseTransferHandler {
private int fileCount;
public MultiViewTransferHandler(final Values commonValues, final GUIFactory currentGUI, final Commands currentCommands) {
super(commonValues, currentGUI, currentCommands);
}
@Override
public boolean importData(final JComponent src, final Transferable transferable) {
try {
final Object dragImport = getImport(transferable);
if (dragImport instanceof String) {
final String url = (String) dragImport;
System.out.println(url);
final String testURL = url.toLowerCase();
if (testURL.startsWith("http:/")) {
currentCommands.handleTransferedFile(testURL);
return true;
} else if (testURL.startsWith("file:/")) {
final String[] urls = url.split("file:/");
final List files = new LinkedList();
for (final String file : urls) {
if (!file.isEmpty()) {
final File file2 = new File(new URL("file:/" + file).getFile());
System.out.println(file2);
files.add(file2);
}
}
return openFiles(files);
}
} else if (dragImport instanceof List) {
final List files = (List) dragImport;
return openFiles(files);
}
} catch (final Exception e) {
LogWriter.writeLog("Exception attempting to import data " + e);
}
return false;
}
private boolean openFiles(final List files) {
fileCount = 0;
final List flattenedFiles = getFlattenedFiles(files, new ArrayList());
if (fileCount == commonValues.getMaxMiltiViewers()) {
currentGUI.showMessageDialog("You have choosen to import more files than your current set " +
"maximum (" + commonValues.getMaxMiltiViewers() + "). Only the first " +
commonValues.getMaxMiltiViewers() + " files will be imported.\nYou can change this value " +
"in View | Preferences",
"Maximum number of files reached", JOptionPane.INFORMATION_MESSAGE);
}
final List[] filterdFiles = filterFiles(flattenedFiles);
final List allowedFiles = filterdFiles[0];
final List disAllowedFiles = filterdFiles[1];
final int noOfDisAllowedFiles = disAllowedFiles.size();
final int noOfAllowedFiles = allowedFiles.size();
if (noOfDisAllowedFiles > 0) {
final StringBuilder unOpenableFiles = new StringBuilder();
for (final Object disAllowedFile : disAllowedFiles) {
final String file = (String) disAllowedFile;
final String fileName = new File(file).getName();
unOpenableFiles.append(fileName).append('\n');
}
final int result = currentGUI.showConfirmDialog("You have selected " + flattenedFiles.size() +
" files to open. The following file(s) cannot be opened\nas they are not valid PDFs " +
"or images.\n" + unOpenableFiles + "\nWould you like to open the remaining " +
noOfAllowedFiles + " files?", "File Import", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.NO_OPTION) {
return false;
}
}
final SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
for (final Object allowedFile : allowedFiles) {
final String file = (String) allowedFile;
try {
currentCommands.handleTransferedFile(file);
} catch (final Exception e) {
LogWriter.writeLog("Exception e " + e.getMessage());
final int result;
if (allowedFiles.size() == 1) {
currentGUI.showMessageDialog(Messages.getMessage("PdfViewerOpenerror"), commonValues.getSelectedFile(), JOptionPane.ERROR_MESSAGE);
result = JOptionPane.NO_OPTION;
} else {
result = currentGUI.showConfirmDialog(Messages.getMessage("PdfViewerOpenerror") + ". Continue opening remaining files?", commonValues.getSelectedFile(),
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
}
final JInternalFrame[] allFrames = ((JDesktopPane) currentGUI.getMultiViewerFrames()).getAllFrames();
for (final JInternalFrame internalFrame : allFrames) {
if (internalFrame.getTitle().equals(commonValues.getSelectedFile())) {
try {
internalFrame.setClosed(true);
} catch (final PropertyVetoException e1) {
LogWriter.writeLog("Exception " + e1 + " attempting getframes");
}
break;
}
}
if (result == JOptionPane.NO_OPTION) {
return null;
}
}
}
return null;
}
};
worker.start();
//
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// JInternalFrame[] allFrames = currentGUI.getMultiViewerFrames().getAllFrames();
//
// for (int i = allFrames.length - 1; i >= 0; i--) {
// JInternalFrame pdf = allFrames[i];
//
// pdf.updateUI();
// pdf.repaint();
// try {
// pdf.setSelected(true);
// } catch (PropertyVetoException e) {
// e.printStackTrace();
// }
// }
// currentGUI.getMultiViewerFrames().repaint();
// }
// });
return true;
}
private static List[] filterFiles(final List flattenedFiles) {
final List allowedFiles = new LinkedList();
final List disAllowedFiles = new LinkedList();
for (final String flattenedFile : flattenedFiles) {
final boolean isValid = OpenFile.isSupportedFileExtension(flattenedFile.toLowerCase());
if (isValid) {
allowedFiles.add(flattenedFile);
} else {
disAllowedFiles.add(flattenedFile);
}
}
return new List[]{allowedFiles, disAllowedFiles};
}
private List getFlattenedFiles(final List files, final List flattenedFiles) {
for (final Object file1 : files) {
if (fileCount == commonValues.getMaxMiltiViewers()) {
return flattenedFiles;
}
final File file = (File) file1;
// System.out.println(file);
if (file.isDirectory()) {
getFlattenedFiles(Arrays.asList(file.listFiles()), flattenedFiles);
} else {
flattenedFiles.add(file.getAbsolutePath());
fileCount++;
}
}
return flattenedFiles;
}
// protected void openTransferedFile(String file) {
// String testFile = file.toLowerCase();
//
// boolean isValid = ((testFile.endsWith(".pdf"))
// || (testFile.endsWith(".fdf")) || (testFile.endsWith(".tif"))
// || (testFile.endsWith(".tiff")) || (testFile.endsWith(".png"))
// || (testFile.endsWith(".jpg")) || (testFile.endsWith(".jpeg")));
//
// if (isValid) {
// currentCommands.openTransferedFile(file);
// } else {
// currentGUI.showMessageDialog("You may only import a valid PDF or image");
// }
// }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy