Java Swing - ImageToPdf
package jlib5.app; import com.formdev.flatlaf.FlatLightLaf; import jlib5.java.FileApi; import jlib5.java.PdfApi; import jlib5.java.Runnable1; import jlib5.swing.Frame; import jlib5.swing.Drop; import jlib5.swing.Swing; import jlib5.swing.Table; import java.io.File; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class ImageToPdf { String[] columns = { "Type", "Path", "Size", "Status" }; int[] widths = { 100, 200, 100, 100}; int[] aligns = { 0, -1, 1, 0 }; Frame frame; Table list; JLabel status; public static void main(String[] args) { FlatLightLaf.setup(); new ImageToPdf(); } public ImageToPdf() { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { start(); } }); } void start() { frame = new Frame("ImageToPdf"); frame.setTop( getMenuBar() ); frame.setCenter( (list = new Table(columns, widths, aligns, null)).layout ); frame.setBottom(Swing.getHbox(Swing.HExpand( status = new JLabel("Ready")), Swing.getButton("Run", null, this::runHandler) ) ); setDrop( list.ctrl, this::dropHandler ); frame.show(460,300); } public void setDrop(JComponent node, Runnable1 handler) { node.setTransferHandler( new Drop(handler) ); } public JMenuItem getMenuItem( String name, Runnable handler ) { JMenuItem item = new JMenuItem(name); item.addActionListener( (e) -> handler.run() ); return item; } public JMenu getFileMenu() { JMenu menu = new JMenu("File"); menu.add(getMenuItem( "Exit", () -> { System.exit(0); } )); return menu; } public JMenuBar getMenuBar() { JMenuBar menubar = new JMenuBar(); menubar.add(getFileMenu()); return menubar; } void dropHandler(Object...obj) { java.util.List<File> files = (java.util.List<File>) obj[0]; for( File f : files ) { if( f.isFile() ) { list.addRow( new Object[] { "File", f.getAbsolutePath(), f.length(), "Ready" } ); } else { list.addRow( new Object[] { "Dir", f.getAbsolutePath(), FileApi.getFileSize2(f.getAbsolutePath()), "Ready" } ); } } } void runHandler() { new Thread( () -> { for( int i = 0; i < list.getRowCount(); i++ ) { try { final int index = i; Object[] row = list.getRow(i); String fname = (String)row[1]; if( new File(fname).isFile() ) { PdfApi.imageZipToPdf(fname, fname + ".pdf", null, (e) -> { final String progress = "" + ((int)e[0] * 100 / (int)e[1]) + "%"; Swing.runLater( () -> list.setValue( index, 3, progress) ); Swing.runLater( () -> status.setText( "" + (index+1) + " / " + list.getRowCount() + " (" + progress + ")" ) ); }); } else { PdfApi.imageDirToPdf(fname, fname + ".pdf", null, (e) -> { final String progress = "" + ((int)e[0] * 100 / (int)e[1]) + "%"; Swing.runLater( () -> list.setValue( index, 3, progress) ); Swing.runLater( () -> status.setText( "" + (index+1) + " / " + list.getRowCount() + " (" + progress + ")" ) ); }); } System.out.println( "" + row[0] + " " + row[1] ); Swing.runLater( () -> list.setValue( index, 3, "OK") ); } catch (Exception e) { e.printStackTrace(); } } Swing.runLater( () -> status.setText( "Ready" ) ); }).start(); } }
댓글 없음:
댓글 쓰기