8 changed files with 266 additions and 126 deletions
@ -0,0 +1,153 @@
@@ -0,0 +1,153 @@
|
||||
package de.srsoftware.belegscanner.gui; |
||||
|
||||
import java.awt.FlowLayout; |
||||
import java.awt.GridLayout; |
||||
import java.io.File; |
||||
import java.io.FilenameFilter; |
||||
import java.io.IOException; |
||||
import java.nio.file.Path; |
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Vector; |
||||
|
||||
import javax.swing.JButton; |
||||
import javax.swing.JLabel; |
||||
import javax.swing.JPanel; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import de.srsoftware.belegscanner.Constants; |
||||
|
||||
public class DocTable extends JPanel{ |
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DocTable.class); |
||||
private static FilenameFilter PAGES = (dir,name) -> name.toLowerCase().endsWith("pdf") && name.toLowerCase().startsWith("page"); |
||||
private static FilenameFilter PDFS = (dir,name) -> name.toLowerCase().endsWith("pdf"); |
||||
|
||||
public interface PreviewListener{ |
||||
public void show(String filename); |
||||
} |
||||
|
||||
private class Row{ |
||||
|
||||
private JLabel status; |
||||
|
||||
public Row(String path) { |
||||
rows.put(path, this); |
||||
add(new JLabel(path)); |
||||
add(status = new JLabel("neu")); |
||||
|
||||
JPanel buttons = new JPanel(); |
||||
buttons.setLayout(new FlowLayout()); |
||||
|
||||
JButton folderButton = new JButton("Ordner öffnen"); |
||||
folderButton.addActionListener(ev -> openFolder(path)); |
||||
buttons.add(folderButton); |
||||
|
||||
JButton joinButton = new JButton("Zusammenfügen"); |
||||
joinButton.addActionListener(ev -> joinDocs(path)); |
||||
buttons.add(joinButton); |
||||
|
||||
JButton preview = new JButton("Vorschau"); |
||||
preview.addActionListener(ev -> preview(path)); |
||||
buttons.add(preview); |
||||
|
||||
add(buttons); |
||||
} |
||||
|
||||
public Row status(String status) { |
||||
this.status.setText(status); |
||||
return this; |
||||
} |
||||
|
||||
} |
||||
|
||||
private static final long serialVersionUID = 1073955198529023744L; |
||||
|
||||
private final HashMap<String,Row> rows = new HashMap<>(); |
||||
private HashSet<PreviewListener> previewListeners; |
||||
|
||||
public DocTable() { |
||||
setLayout(new GridLayout(0, 3)); |
||||
add(new JLabel("Ordner")); |
||||
add(new JLabel("Status")); |
||||
add(new JLabel("Aktionen")); |
||||
previewListeners = new HashSet<>(); |
||||
} |
||||
|
||||
public DocTable addPreviewListener(PreviewListener listener) { |
||||
previewListeners.add(listener); |
||||
return this; |
||||
} |
||||
|
||||
public void preview(String path) { |
||||
LOG.debug("preview({})",path); |
||||
File folder = new File(path); |
||||
if (!folder.exists()) return; |
||||
List<String> pdfs = Arrays.asList(folder.list(PDFS)); |
||||
LOG.debug("PDFs: {}",pdfs); |
||||
if (!pdfs.isEmpty()) previewListeners.forEach(l -> l.show(Path.of(path,pdfs.get(0)).toString())); |
||||
} |
||||
|
||||
public void joinDocs(String path) { |
||||
|
||||
LOG.debug("joinFiles({})",path); |
||||
File folder = new File(path); |
||||
if (!folder.exists()) return; |
||||
|
||||
List<String> pdfs = Arrays.asList(folder.list(PAGES)); |
||||
Collections.sort(pdfs); |
||||
|
||||
Vector<String> cmd = new Vector<>(); |
||||
cmd.add("pdftk"); |
||||
cmd.addAll(pdfs); |
||||
cmd.add("cat"); |
||||
cmd.add("output"); |
||||
cmd.add(folder.getName()+".pdf"); |
||||
LOG.debug("executing {}",cmd); |
||||
ProcessBuilder builder = new ProcessBuilder(cmd); |
||||
builder.directory(folder); |
||||
try { |
||||
Process process = builder.start(); |
||||
setState(path,"Verbinde PDFs…"); |
||||
int errorCode = process.waitFor(); |
||||
if (errorCode != 0) { |
||||
LOG.error("error code: {} for {}",errorCode,cmd); |
||||
} else LOG.debug("error code: {}",errorCode); |
||||
} catch (InterruptedException | IOException e) { |
||||
LOG.error("{} terminated: ",builder,e); |
||||
} |
||||
for (String page : pdfs) Path.of(path,page).toFile().delete(); |
||||
setState(path,"Zusammengefügt."); |
||||
} |
||||
|
||||
public void openFolder(String path) { |
||||
Process process = null; |
||||
try { |
||||
process = new ProcessBuilder(List.of("killall",Constants.FILE_BROWSER)).start(); |
||||
process.waitFor(); |
||||
Thread.sleep(100); |
||||
} catch (IOException | InterruptedException e) { |
||||
LOG.error("{} terminated: ",process,e); |
||||
} |
||||
|
||||
try { |
||||
process = new ProcessBuilder(List.of(Constants.FILE_BROWSER,path)).start(); |
||||
} catch (IOException e) { |
||||
LOG.error("{} terminated: ",process,e); |
||||
} |
||||
} |
||||
|
||||
public void add(String path) { |
||||
if (!rows.containsKey(path)) new Row(path).status("neu"); |
||||
} |
||||
|
||||
public void setState(String path, String state) { |
||||
Row row = rows.get(path); |
||||
if (row != null) row.status(state); |
||||
} |
||||
} |
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
package de.srsoftware.belegscanner.gui; |
||||
|
||||
import java.awt.BorderLayout; |
||||
import java.awt.Container; |
||||
|
||||
import javax.swing.JPanel; |
||||
|
||||
import org.icepdf.ri.common.ComponentKeyBinding; |
||||
import org.icepdf.ri.common.SwingController; |
||||
import org.icepdf.ri.common.SwingViewBuilder; |
||||
|
||||
public class Preview extends JPanel{ |
||||
|
||||
private static final long serialVersionUID = -4534327261273493367L; |
||||
private SwingController controller; |
||||
|
||||
public Preview() { |
||||
// Instance the controller
|
||||
controller = new SwingController(); |
||||
// We created the SwingViewFactory configured with the controller
|
||||
SwingViewBuilder factory = new SwingViewBuilder(controller); |
||||
// We use the factory to build a preconfigured JPanel
|
||||
// with a full and active viewer user interface.
|
||||
JPanel viewerComponentPanel = factory.buildViewerPanel(); |
||||
// viewerComponentPanel.setPreferredSize(new Dimension(400, 243));
|
||||
// viewerComponentPanel.setMaximumSize(new Dimension(400, 243));
|
||||
// We add keyboard command
|
||||
ComponentKeyBinding.install(controller, viewerComponentPanel); |
||||
// add interactive mouse link annotation support via callback
|
||||
controller.getDocumentViewController().setAnnotationCallback( |
||||
new org.icepdf.ri.common.MyAnnotationCallback( |
||||
controller.getDocumentViewController())); |
||||
|
||||
Container reportViewerContainer = this; |
||||
// We add the component to visualize the report
|
||||
reportViewerContainer.add(viewerComponentPanel, BorderLayout.CENTER); |
||||
reportViewerContainer.invalidate(); |
||||
// We open the generated document
|
||||
|
||||
} |
||||
|
||||
public void display(String filename) { |
||||
System.err.println("display "+filename); |
||||
controller.openDocument(filename);; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue