@ -1,20 +1,35 @@
@@ -1,20 +1,35 @@
package de.srsoftware.belegscanner.gui ;
import static de.srsoftware.belegscanner.Application.t ;
import static de.srsoftware.belegscanner.Constants.CONFIG_TARGET ;
import static de.srsoftware.belegscanner.Constants.FILE_BROWSER ;
import static de.srsoftware.belegscanner.Constants.JPG ;
import static de.srsoftware.belegscanner.Constants.PDF ;
import java.awt.BorderLayout ;
import java.awt.Color ;
import java.awt.Dimension ;
import java.awt.Image ;
import java.awt.Point ;
import java.awt.event.ActionEvent ;
import java.io.File ;
import java.io.IOException ;
import java.nio.file.Path ;
import java.util.* ;
import java.util.Arrays ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map.Entry ;
import java.util.Vector ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import java.util.stream.Collectors ;
import javax.imageio.ImageIO ;
import javax.swing.BorderFactory ;
import javax.swing.ImageIcon ;
import javax.swing.JComponent ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
import javax.swing.JOptionPane ;
@ -29,10 +44,7 @@ import org.json.JSONArray;
@@ -29,10 +44,7 @@ import org.json.JSONArray;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import static de.srsoftware.belegscanner.Application.* ;
import de.srsoftware.belegscanner.Configuration ;
import de.srsoftware.belegscanner.Constants ;
/ * *
* The "guts" of the application :
@ -46,7 +58,7 @@ public class MainFrame extends JFrame {
@@ -46,7 +58,7 @@ public class MainFrame extends JFrame {
private static final long serialVersionUID = 210283601541223645L ;
private static final String HOME = System . getProperty ( "user.home" ) ;
private static final Pattern MARKEN = Pattern . compile ( "\\$([a-zA-Z]+)" ) ;
private static final Pattern MARKEN = Pattern . compile ( "\\$([a-zäöü A-ZÄÖÜ ]+)" ) ;
private static final String APP_WIDTH = "app.main.dimension.w" ;
@ -83,6 +95,10 @@ public class MainFrame extends JFrame {
@@ -83,6 +95,10 @@ public class MainFrame extends JFrame {
private boolean isScanning = false ;
private JComponent previewComponent = null ;
private JPanel pdfPreview ;
/ * *
* Create application main frame .
* @param config
@ -103,7 +119,9 @@ public class MainFrame extends JFrame {
@@ -103,7 +119,9 @@ public class MainFrame extends JFrame {
add ( toolbar , BorderLayout . EAST ) ;
add ( statusBar , BorderLayout . SOUTH ) ;
add ( docTable , BorderLayout . NORTH ) ;
add ( preview ( ) , BorderLayout . CENTER ) ;
pdfPreview = preview ( ) ;
add ( previewComponent = pdfPreview , BorderLayout . CENTER ) ;
toolbar . addCategoryListener ( this : : setCategory )
. addDateListener ( this : : setDate )
. addDimensionListener ( this : : setDimension )
@ -138,7 +156,7 @@ public class MainFrame extends JFrame {
@@ -138,7 +156,7 @@ public class MainFrame extends JFrame {
} ;
public void checkDependencies ( ) {
List < String > missing = List . of ( "convert --version" , "killall --version" , "pdfsandwich -version" , "pdftk --version" , "scanimage --version" , Constants . FILE_BROWSER + " --version" ) . stream ( ) . map ( this : : appMissing ) . filter ( app - > app ! = null ) . collect ( Collectors . toList ( ) ) ;
List < String > missing = List . of ( "convert --version" , "killall --version" , "pdfsandwich -version" , "pdftk --version" , "scanimage --version" , FILE_BROWSER + " --version" ) . stream ( ) . map ( this : : appMissing ) . filter ( app - > app ! = null ) . collect ( Collectors . toList ( ) ) ;
if ( ! missing . isEmpty ( ) ) notifyAppsMissing ( missing ) ;
}
@ -185,6 +203,9 @@ public class MainFrame extends JFrame {
@@ -185,6 +203,9 @@ public class MainFrame extends JFrame {
* /
private void performScan ( String path , Dimension dimension ) {
LOG . debug ( "performScan({})" , path ) ;
/* We need to all required config values before starting to scan, because the might be changed during scanning */
String target = config . getOrCreate ( CONFIG_TARGET , "pdf" ) ;
setScanning ( true ) ;
File folder = new File ( path ) ;
@ -230,8 +251,13 @@ public class MainFrame extends JFrame {
@@ -230,8 +251,13 @@ public class MainFrame extends JFrame {
LOG . error ( t ( "{} terminated: " ) , builder , e ) ;
}
setScanning ( false ) ;
if ( JPG . equals ( target ) ) {
docTable . setState ( path , t ( "Image scanned" ) ) ;
docTable . preview ( path ) ;
return ;
} ;
String pdf = "page." + timestamp + ".pdf" ;
String pdf = timestamp + ".page .pdf" ;
cmd = new Vector < > ( ) ;
cmd . add ( "convert" ) ;
cmd . add ( raw ) ;
@ -253,8 +279,13 @@ public class MainFrame extends JFrame {
@@ -253,8 +279,13 @@ public class MainFrame extends JFrame {
}
Path . of ( path , raw ) . toFile ( ) . delete ( ) ;
String ocr = "page." + timestamp + ".ocr.pdf" ;
if ( PDF . equals ( target ) ) {
docTable . setState ( path , t ( "PDF created" ) ) ;
docTable . preview ( path ) ;
return ;
}
String ocr = timestamp + ".page.ocr.pdf" ;
cmd = new Vector < > ( ) ;
cmd . add ( "pdfsandwich" ) ;
@ -319,9 +350,37 @@ public class MainFrame extends JFrame {
@@ -319,9 +350,37 @@ public class MainFrame extends JFrame {
* open file in preview panel
* @param file
* /
private void preview ( String file ) {
pdfViewer . openDocument ( file ) ;
pdfViewer . setPageFitMode ( DocumentViewController . PAGE_FIT_WINDOW_HEIGHT , false ) ;
private void preview ( Path filePath ) {
if ( filePath = = null ) return ;
String file = filePath . toString ( ) ;
if ( file . toLowerCase ( ) . endsWith ( "pdf" ) ) {
if ( previewComponent ! = pdfPreview ) {
remove ( previewComponent ) ;
previewComponent = pdfPreview ;
add ( previewComponent , BorderLayout . CENTER ) ;
pack ( ) ;
}
pdfViewer . openDocument ( file ) ;
pdfViewer . setPageFitMode ( DocumentViewController . PAGE_FIT_WINDOW_HEIGHT , false ) ;
}
if ( file . toLowerCase ( ) . endsWith ( "jpg" ) ) {
try {
Image img = ImageIO . read ( new File ( file ) ) ;
Dimension dim = previewComponent . getSize ( ) ;
double w = img . getWidth ( null ) ;
double h = img . getHeight ( null ) ;
double factor = Math . min ( dim . width / w , dim . height / h ) ;
if ( factor ! = 1 . 0 ) img = img . getScaledInstance ( ( int ) ( w * factor ) , ( int ) ( h * factor ) , Image . SCALE_DEFAULT ) ;
ImageIcon icon = new ImageIcon ( img ) ;
JLabel label = new JLabel ( icon ) ;
this . remove ( previewComponent ) ;
previewComponent = label ;
this . add ( previewComponent , BorderLayout . CENTER ) ;
pack ( ) ;
} catch ( IOException e ) {
LOG . error ( "Was not able to preview {}:" , file , e ) ;
}
}
fileName . setText ( file ) ;
}