working on pdf preview
This commit is contained in:
4
pom.xml
4
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>BelegScanner</groupId>
|
||||
<artifactId>BelegScanner</artifactId>
|
||||
<version>0.0.4</version>
|
||||
<version>0.0.5</version>
|
||||
|
||||
<name>BelegScanner</name>
|
||||
<packaging>jar</packaging>
|
||||
@@ -53,7 +53,7 @@
|
||||
<dependency>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>tools.gui.selectcombobox</artifactId>
|
||||
<version>0.1.1</version>
|
||||
<version>0.1.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -9,11 +9,15 @@
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="de.srsoftware.belegscanner.gui.MainFrame" level="info" additivity="false">
|
||||
<logger name="de.srsoftware.belegscanner.gui" level="info" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</logger>
|
||||
|
||||
<logger name="de.srsoftware.belegscanner.gui.Toolbar" level="debug" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</logger>
|
||||
|
||||
<logger name="de.srsoftware.belegscanner.gui.DocTable" level="debug" additivity="false">
|
||||
<logger name="de.srsoftware.belegscanner.Configuration" level="debug" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</logger>
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public class Configuration {
|
||||
}
|
||||
}
|
||||
|
||||
private void save() throws IOException {
|
||||
public void save() throws IOException {
|
||||
LOG.debug("Trying to save config…");
|
||||
File dir = file.getParentFile();
|
||||
if (!dir.exists()) dir.mkdirs();
|
||||
@@ -138,10 +138,5 @@ public class Configuration {
|
||||
localJson = localJson.getJSONObject(localKey);
|
||||
}
|
||||
LOG.debug("altered json: {}",json);
|
||||
try {
|
||||
save();
|
||||
} catch (IOException ex) {
|
||||
LOG.warn("Was not able to write config to {}!",file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,5 +4,7 @@ public class Constants {
|
||||
|
||||
public static final String APPLICATION_NAME = "BelegScanner";
|
||||
public static final String FILE_BROWSER = "thunar";
|
||||
public static final String DEFAULT_WIDTH = "209";
|
||||
public static final String DEFAULT_HEIGHT = "297";
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,11 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import org.icepdf.ri.common.ComponentKeyBinding;
|
||||
import org.icepdf.ri.common.SwingController;
|
||||
import org.icepdf.ri.common.SwingViewBuilder;
|
||||
import org.json.JSONArray;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -32,9 +36,9 @@ public class MainFrame extends JFrame {
|
||||
private static final String HOME = System.getProperty("user.home");
|
||||
private static final Pattern MARKEN = Pattern.compile("\\$([a-zA-Z]+)");
|
||||
|
||||
private static final String APP_WIDTH = "app.main.dimenstion.w";
|
||||
private static final String APP_WIDTH = "app.main.dimension.w";
|
||||
|
||||
private static final String APP_HEIGHT = "app.main.dimenstion.h";
|
||||
private static final String APP_HEIGHT = "app.main.dimension.h";
|
||||
|
||||
private static final String APP_X = "app.main.position.x";
|
||||
|
||||
@@ -55,14 +59,14 @@ public class MainFrame extends JFrame {
|
||||
|
||||
private Configuration config;
|
||||
|
||||
private int xSize = 209;
|
||||
private int ySize = 297;
|
||||
private int resoultion = 150;
|
||||
private String mode = "Color";
|
||||
|
||||
private DocTable docTable;
|
||||
|
||||
private Preview preview;
|
||||
private Dimension dimension = new Dimension(209, 297);
|
||||
|
||||
private SwingController pdfViewer;
|
||||
|
||||
public MainFrame(Configuration config) {
|
||||
super("BelegScanner");
|
||||
@@ -76,19 +80,19 @@ public class MainFrame extends JFrame {
|
||||
toolbar = new Toolbar(config);
|
||||
statusBar = new StatusBar();
|
||||
docTable = new DocTable();
|
||||
preview = new Preview();
|
||||
|
||||
add(toolbar,BorderLayout.EAST);
|
||||
add(statusBar,BorderLayout.SOUTH);
|
||||
add(docTable,BorderLayout.NORTH);
|
||||
add(preview,BorderLayout.CENTER);
|
||||
add(preview(),BorderLayout.CENTER);
|
||||
toolbar.addCategoryListener(this::setCategory)
|
||||
.addDateListener(this::setDate)
|
||||
.addDimensionListener(this::setDimension)
|
||||
.addFieldListener(this::setField)
|
||||
.addPathListener(this::setPath)
|
||||
.addScanListener(this::scan);
|
||||
|
||||
docTable.addPreviewListener(preview::display);
|
||||
docTable.addPreviewListener(pdfViewer::openDocument);
|
||||
|
||||
int x = config.getOrCreate(APP_X, 20);
|
||||
int y = config.getOrCreate(APP_Y, 20);
|
||||
@@ -99,6 +103,20 @@ public class MainFrame extends JFrame {
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private JPanel preview() {
|
||||
// https://stackoverflow.com/a/48248739/1285585
|
||||
|
||||
pdfViewer = new SwingController();
|
||||
SwingViewBuilder factory = new SwingViewBuilder(pdfViewer);
|
||||
JPanel viewerComponentPanel = factory.buildViewerPanel();
|
||||
|
||||
ComponentKeyBinding.install(pdfViewer, viewerComponentPanel);
|
||||
// add interactive mouse link annotation support via callback
|
||||
pdfViewer.getDocumentViewController().setAnnotationCallback(new org.icepdf.ri.common.MyAnnotationCallback(pdfViewer.getDocumentViewController()));
|
||||
|
||||
return viewerComponentPanel;
|
||||
}
|
||||
|
||||
private void addFieldsFor(String path) {
|
||||
Vector<String> marks = new Vector<>();
|
||||
Matcher matches = MARKEN.matcher(path);
|
||||
@@ -123,9 +141,9 @@ public class MainFrame extends JFrame {
|
||||
Vector<String> cmd = new Vector<>();
|
||||
cmd.add("scanimage");
|
||||
cmd.add("-x");
|
||||
cmd.add(xSize+"");
|
||||
cmd.add(dimension.width+"");
|
||||
cmd.add("-y");
|
||||
cmd.add(ySize+"");
|
||||
cmd.add(dimension.height+"");
|
||||
cmd.add("--mode");
|
||||
cmd.add(mode);
|
||||
cmd.add("--resolution");
|
||||
@@ -213,6 +231,11 @@ public class MainFrame extends JFrame {
|
||||
this.date = date;
|
||||
updatePath();
|
||||
}
|
||||
|
||||
private void setDimension(Dimension dim) {
|
||||
this.dimension = dim;
|
||||
}
|
||||
|
||||
|
||||
private void setField(String key, String val) {
|
||||
if (val == null || val.isEmpty()) {
|
||||
@@ -239,13 +262,21 @@ public class MainFrame extends JFrame {
|
||||
if (!existing.contains(val)) arr.put(val);
|
||||
}
|
||||
config.set(prefix+"path",path);
|
||||
config.set(prefix+"size.w", dimension.width);
|
||||
config.set(prefix+"size.h", dimension.height);
|
||||
|
||||
|
||||
Point loc = getLocation();
|
||||
config.set(APP_X, loc.x);
|
||||
config.set(APP_Y, loc.y);
|
||||
Dimension dim = getSize();
|
||||
config.set(APP_WIDTH, dim.width);
|
||||
config.set(APP_HEIGHT, dim.height);
|
||||
|
||||
try {
|
||||
config.save();
|
||||
} catch (IOException e) {
|
||||
LOG.error("Was not able to save config!");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@@ -264,4 +295,5 @@ public class MainFrame extends JFrame {
|
||||
for (Entry<String, String> entry : fields.entrySet()) patchedPath = patchedPath.replace("$"+entry.getKey(), entry.getValue());
|
||||
statusBar.setPath(patchedPath);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
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);;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package de.srsoftware.belegscanner.gui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -15,6 +18,7 @@ import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
@@ -22,6 +26,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.srsoftware.belegscanner.Configuration;
|
||||
import de.srsoftware.belegscanner.Constants;
|
||||
import de.srsoftware.tools.gui.DateChooser;
|
||||
import de.srsoftware.tools.gui.SelectComboBox;
|
||||
|
||||
@@ -37,6 +42,11 @@ public class Toolbar extends JPanel {
|
||||
public void setDate(Date date);
|
||||
}
|
||||
|
||||
public interface DimensionListener{
|
||||
public void setDimension(Dimension dimension);
|
||||
}
|
||||
|
||||
|
||||
public interface FieldListener{
|
||||
public void setField(String name, String value);
|
||||
}
|
||||
@@ -59,6 +69,7 @@ public class Toolbar extends JPanel {
|
||||
private HashSet<FieldListener> fieldListeners = new HashSet<>();
|
||||
private HashSet<PathListener> pathListeners = new HashSet<>();
|
||||
private HashSet<ActionListener> scanListeners = new HashSet<>();
|
||||
private HashSet<DimensionListener> dimensionListeners = new HashSet<>();
|
||||
|
||||
private DateChooser date;
|
||||
|
||||
@@ -68,16 +79,59 @@ public class Toolbar extends JPanel {
|
||||
|
||||
private JButton scanButton;
|
||||
|
||||
private JTextField width;
|
||||
private JTextField height;
|
||||
|
||||
public Toolbar(Configuration config) {
|
||||
this.config = config;
|
||||
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||
add(datePicker());
|
||||
add(input("Kategorie",categoryPicker(config)));
|
||||
add(input("Pfad",pathPicker = pathPicker()));
|
||||
addFormatSelector();
|
||||
add(scanButton());
|
||||
add(externButton());
|
||||
}
|
||||
|
||||
|
||||
private void addFormatSelector() {
|
||||
JPanel dummy = new JPanel();
|
||||
dummy.setLayout(new BorderLayout());
|
||||
|
||||
dummy.add(new JLabel("Breite:"),BorderLayout.WEST);
|
||||
dummy.add(width = new JTextField(209+""),BorderLayout.CENTER);
|
||||
dummy.add(new JLabel("px"),BorderLayout.EAST);
|
||||
add(dummy);
|
||||
|
||||
dummy = new JPanel();
|
||||
dummy.setLayout(new BorderLayout());
|
||||
|
||||
dummy.add(new JLabel("Höhe:"),BorderLayout.WEST);
|
||||
dummy.add(height = new JTextField(297+""),BorderLayout.CENTER);
|
||||
dummy.add(new JLabel("px"),BorderLayout.EAST);
|
||||
add(dummy);
|
||||
|
||||
KeyAdapter dimensionListener = new KeyAdapter() {
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
updateDimensions();
|
||||
}
|
||||
};
|
||||
|
||||
width.addKeyListener(dimensionListener);
|
||||
height.addKeyListener(dimensionListener);
|
||||
}
|
||||
|
||||
|
||||
protected void updateDimensions() {
|
||||
try {
|
||||
int w = Integer.parseInt(width.getText().trim());
|
||||
int h = Integer.parseInt(height.getText().trim());
|
||||
dimensionListeners.forEach(listener -> listener.setDimension(new Dimension(w < 0 ? 0 : w, h < 0 ? 0 : h)));
|
||||
} catch (NumberFormatException e) {
|
||||
LOG.warn("Invalid dimensions!");
|
||||
}
|
||||
}
|
||||
|
||||
public void addFieldsFor(Vector<String> marks) {
|
||||
LOG.debug("addFieldsFor({})",marks);
|
||||
|
||||
@@ -102,6 +156,11 @@ public class Toolbar extends JPanel {
|
||||
public Toolbar addDateListener(DateListener listener) {
|
||||
dateListeners.add(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Toolbar addDimensionListener(DimensionListener listener) {
|
||||
dimensionListeners.add(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Toolbar addFieldListener(FieldListener listener) {
|
||||
@@ -139,6 +198,7 @@ public class Toolbar extends JPanel {
|
||||
return btn;
|
||||
}
|
||||
|
||||
|
||||
public Date getDate() {
|
||||
return date.getSelectedDate();
|
||||
}
|
||||
@@ -187,9 +247,10 @@ public class Toolbar extends JPanel {
|
||||
}
|
||||
|
||||
|
||||
protected void updateCat(String newCat) {
|
||||
private void updateCat(String newCat) {
|
||||
LOG.debug("updateCat({})",newCat);
|
||||
categoryListeners.forEach(l -> l.setCategory(newCat));
|
||||
LOG.debug("Listeners notified.");
|
||||
if (!newCat.isEmpty()) {
|
||||
String prefix = "app.categories."+newCat+".";
|
||||
String path = config.get(prefix+"path");
|
||||
@@ -199,18 +260,26 @@ public class Toolbar extends JPanel {
|
||||
}
|
||||
|
||||
JSONObject fields = config.get(prefix+"fields");
|
||||
if (fields == null) return;
|
||||
LOG.debug("fields: {}",fields);
|
||||
for (String fieldName : fields.keySet()) {
|
||||
path = prefix+"fields."+fieldName;
|
||||
|
||||
JSONArray arr = config.getOrCreateArray(path);
|
||||
HashSet<Object> values = new HashSet<>();
|
||||
arr.forEach(values::add);
|
||||
SelectComboBox selector = getSelector(additonalComponents.get(fieldName));
|
||||
if (selector != null) selector.setElements(values);
|
||||
if (fields != null) {
|
||||
LOG.debug("fields: {}",fields);
|
||||
for (String fieldName : fields.keySet()) {
|
||||
path = prefix+"fields."+fieldName;
|
||||
|
||||
JSONArray arr = config.getOrCreateArray(path);
|
||||
HashSet<Object> values = new HashSet<>();
|
||||
arr.forEach(values::add);
|
||||
SelectComboBox selector = getSelector(additonalComponents.get(fieldName));
|
||||
if (selector != null) selector.setElements(values);
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG.debug("Trying to read size");
|
||||
Integer w = config.get(prefix+"size.w");
|
||||
LOG.debug("w: {}",w);
|
||||
width.setText(w == null ? Constants.DEFAULT_WIDTH : w+"");
|
||||
Integer h = config.get(prefix+"size.h");
|
||||
LOG.debug("h: {}",h);
|
||||
height.setText(h == null ? Constants.DEFAULT_HEIGHT : h+"");
|
||||
} else LOG.debug("newCat is empty!");
|
||||
}
|
||||
|
||||
private void updateField(String name, String val) {
|
||||
|
||||
Reference in New Issue
Block a user