package com.dtwain.test.fulldemo;
import com.dynarithmic.twain.*;
import java.awt.Toolkit;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class DTwainFullDemo extends javax.swing.JFrame {

    /**
     * Creates new form DTwainDemoFrame
     */
    DTwainJavaAPI m_api;
    DTwainSource m_SourceWrapper = new DTwainSource(0);
    String sOrigTitle;
    DTwainDemoImageListener iListener = null;
    DTwainLogger m_Logger;
    javax.swing.JMenu m_AcquireFileMenu;
    TreeMap<Integer, String> m_fileTypeMap;
    TreeMap<Integer, javax.swing.JMenuItem> m_FileTypeToMenu;
    TreeMap<Integer, javax.swing.JMenuItem> m_CompressTypeToMenu;
    
    public DTwainFullDemo(DTwainJavaAPI api, DTwainLogger theLogger) 
    {
        initComponents();
        setLocation((Toolkit.getDefaultToolkit().getScreenSize().width)/2 - getWidth()/2, (Toolkit.getDefaultToolkit().getScreenSize().height)/2 - getHeight()/2);
        m_api = api;
        m_SourceWrapper.setInterface(m_api);
        m_Logger = theLogger;
        m_Logger.activate();
        sOrigTitle = getTitle();
        iListener = new DTwainDemoImageListener(this);
        iListener.activate();
        ShowImagePreview.setState(true);
        DisplaySourceUI.setState(true);
        DiscardBlankPages.setState(true);
        LogCalls.setState(false);
        m_AcquireFileMenu = new javax.swing.JMenu();
        m_FileTypeToMenu = new TreeMap<Integer, javax.swing.JMenuItem>();
        m_CompressTypeToMenu = new TreeMap<Integer, JMenuItem>();
        
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_JFIF, JPEGFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_BMP, BMPFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_TIFF, TIFFFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_TIFFMULTI, TIFFMultipageFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_PNG, PNGFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_PDF, PDFFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_PDFA, PDFAFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_PDFA2, PDFA2FileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_FPX, FPXFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_EXIF, EXIFFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_JP2, JP2FileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_SPIFF, SPIFFFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_XBM, XBMFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_PICT, PICTFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_JPX, JPXFileMode);
        m_FileTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_FF_DEJAVU, DEJAVUFileMode);
        
        m_CompressTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_CP_NONE, AcquireCompressNone);
        m_CompressTypeToMenu.put(DTwainJavaAPIConstants.DTWAIN_CP_JPEG, AcquireJPEGCompressed);
        enableSourceItems(false);
    }

    public boolean isShowPreviewImage()
    {
        return ShowImagePreview.getState();
    }
    
    public DTwainJavaAPI getTwainInterface()
    {
        return m_api;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        SelectSource = new javax.swing.JMenuItem();
        SelectSourceByName = new javax.swing.JMenuItem();
        SelectDefaultSource = new javax.swing.JMenuItem();
        SelectSourceCustom = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        SourceProperties = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        CloseSource = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        ExitDemo = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        AcquireNative = new javax.swing.JMenuItem();
        AcquireBuffered = new javax.swing.JMenuItem();
        AcquireCompressed = new javax.swing.JMenu();
        AcquireCompressNone = new javax.swing.JMenuItem();
        AcquireGroup31Compressed = new javax.swing.JMenuItem();
        AcquireGroup31dEOLCompressed = new javax.swing.JMenuItem();
        AcquireGroup32dCompressed = new javax.swing.JMenuItem();
        AcquireGroup4Compressed = new javax.swing.JMenuItem();
        AcquireJPEGCompressed = new javax.swing.JMenuItem();
        AcquireFile = new javax.swing.JMenu();
        AcquireFileBMP = new javax.swing.JMenuItem();
        AcquireFileJPEG = new javax.swing.JMenuItem();
        AcquireFileJP2 = new javax.swing.JMenuItem();
        AcquireFileGIF = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        javax.swing.JMenuItem AcquireFileTIFF1 = new javax.swing.JMenuItem();
        AcquireFileTIFFG3 = new javax.swing.JMenuItem();
        AcquireFileTIFFG4 = new javax.swing.JMenuItem();
        AcquireFileTIFFLZW = new javax.swing.JMenuItem();
        AcquireFileTIFFPackBits = new javax.swing.JMenuItem();
        AcquireFileTIFFFlate = new javax.swing.JMenuItem();
        AcquireFileTIFFJPEG = new javax.swing.JMenuItem();
        AcquireFilePNG = new javax.swing.JMenuItem();
        AcquireFilePDF = new javax.swing.JMenuItem();
        jMenu8 = new javax.swing.JMenu();
        AcquireFilePSLevel1 = new javax.swing.JMenuItem();
        AcquireFilePSLevel2 = new javax.swing.JMenuItem();
        AcquireFilePCX = new javax.swing.JMenuItem();
        AcquireFileTGA = new javax.swing.JMenuItem();
        AcquireFilePSD = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        AcquireFileWMF = new javax.swing.JMenuItem();
        AcquireFileEMF = new javax.swing.JMenuItem();
        jMenu7 = new javax.swing.JMenu();
        AcquireFileICO = new javax.swing.JMenuItem();
        AcquireFileICOLARGE = new javax.swing.JMenuItem();
        AcquireFileWBMP = new javax.swing.JMenuItem();
        AcquireFileWithDriver = new javax.swing.JMenu();
        BMPFileMode = new javax.swing.JMenuItem();
        JPEGFileMode = new javax.swing.JMenuItem();
        JP2FileMode = new javax.swing.JMenuItem();
        TIFFFileMode = new javax.swing.JMenuItem();
        TIFFMultipageFileMode = new javax.swing.JMenuItem();
        PNGFileMode = new javax.swing.JMenuItem();
        PDFFileMode = new javax.swing.JMenuItem();
        PDFAFileMode = new javax.swing.JMenuItem();
        PDFA2FileMode = new javax.swing.JMenuItem();
        FPXFileMode = new javax.swing.JMenuItem();
        EXIFFileMode = new javax.swing.JMenuItem();
        SPIFFFileMode = new javax.swing.JMenuItem();
        XBMFileMode = new javax.swing.JMenuItem();
        PICTFileMode = new javax.swing.JMenuItem();
        JPXFileMode = new javax.swing.JMenuItem();
        DEJAVUFileMode = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        ShowImagePreview = new javax.swing.JCheckBoxMenuItem();
        DisplaySourceUI = new javax.swing.JCheckBoxMenuItem();
        DiscardBlankPages = new javax.swing.JCheckBoxMenuItem();
        jMenu9 = new javax.swing.JMenu();
        LogCalls = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("DTWAIN Java Demo Program");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                ShutdownTwain(evt);
            }
        });

        jMenu1.setText("Source Selection Test");

        SelectSource.setText("Select Source...");
        SelectSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectSourceActionPerformed(evt);
            }
        });
        jMenu1.add(SelectSource);

        SelectSourceByName.setLabel("Select Source By Name...");
        SelectSourceByName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectSourceByNameActionPerformed(evt);
            }
        });
        jMenu1.add(SelectSourceByName);
        SelectSourceByName.getAccessibleContext().setAccessibleName("selSourceByName");

        SelectDefaultSource.setText("Select Default Source");
        SelectDefaultSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectDefaultSourceActionPerformed(evt);
            }
        });
        jMenu1.add(SelectDefaultSource);

        SelectSourceCustom.setText("Select Source Custom...");
        SelectSourceCustom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectSourceCustomActionPerformed(evt);
            }
        });
        jMenu1.add(SelectSourceCustom);
        jMenu1.add(jSeparator1);

        SourceProperties.setText("Source Properties...");
        SourceProperties.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SourcePropertiesActionPerformed(evt);
            }
        });
        jMenu1.add(SourceProperties);
        jMenu1.add(jSeparator2);

        CloseSource.setText("Close Source");
        CloseSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseSourceActionPerformed(evt);
            }
        });
        jMenu1.add(CloseSource);
        jMenu1.add(jSeparator3);

        ExitDemo.setText("Exit");
        ExitDemo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitDemoActionPerformed(evt);
            }
        });
        jMenu1.add(ExitDemo);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Acquire Test");

        AcquireNative.setText("Acquire Native...");
        AcquireNative.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireNativeActionPerformed(evt);
            }
        });
        jMenu2.add(AcquireNative);

        AcquireBuffered.setText("Acquire Buffered...");
        AcquireBuffered.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireBufferedActionPerformed(evt);
            }
        });
        jMenu2.add(AcquireBuffered);

        AcquireCompressed.setText("Acquire Compressed ...");

        AcquireCompressNone.setText("No Compression");
        AcquireCompressNone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireCompressNoneActionPerformed(evt);
            }
        });
        AcquireCompressed.add(AcquireCompressNone);

        AcquireGroup31Compressed.setText("Group 3 (1 dimensional)");
        AcquireCompressed.add(AcquireGroup31Compressed);

        AcquireGroup31dEOLCompressed.setText("Group 3 (1 dimensional EOL)");
        AcquireCompressed.add(AcquireGroup31dEOLCompressed);

        AcquireGroup32dCompressed.setText("Group 3 (2 dimensional)");
        AcquireCompressed.add(AcquireGroup32dCompressed);

        AcquireGroup4Compressed.setText("Group 4 ");
        AcquireGroup4Compressed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireGroup4CompressedActionPerformed(evt);
            }
        });
        AcquireCompressed.add(AcquireGroup4Compressed);

        AcquireJPEGCompressed.setText("JPEG");
        AcquireJPEGCompressed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireJPEGCompressedActionPerformed(evt);
            }
        });
        AcquireCompressed.add(AcquireJPEGCompressed);

        jMenu2.add(AcquireCompressed);

        AcquireFile.setText("Acquire To File...");

        AcquireFileBMP.setText("Windows BMP");
        AcquireFileBMP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileBMPActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFileBMP);

        AcquireFileJPEG.setText("JPEG");
        AcquireFileJPEG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileJPEGActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFileJPEG);

        AcquireFileJP2.setText("JPEG-2000");
        AcquireFileJP2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileJP2ActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFileJP2);

        AcquireFileGIF.setText("GIF");
        AcquireFileGIF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileGIFActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFileGIF);

        jMenu4.setText("TIFF");

        AcquireFileTIFF1.setText("Uncompressed");
        AcquireFileTIFF1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileTIFF1ActionPerformed(evt);
            }
        });
        jMenu4.add(AcquireFileTIFF1);

        AcquireFileTIFFG3.setText("Group 3 FAX");
        AcquireFileTIFFG3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileTIFFG3ActionPerformed(evt);
            }
        });
        jMenu4.add(AcquireFileTIFFG3);

        AcquireFileTIFFG4.setText("Group 4 FAX");
        AcquireFileTIFFG4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileTIFFG4ActionPerformed(evt);
            }
        });
        jMenu4.add(AcquireFileTIFFG4);

        AcquireFileTIFFLZW.setText("LZW Compressed");
        AcquireFileTIFFLZW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileTIFFLZWActionPerformed(evt);
            }
        });
        jMenu4.add(AcquireFileTIFFLZW);

        AcquireFileTIFFPackBits.setText("PackBits Compressed");
        AcquireFileTIFFPackBits.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileTIFFPackBitsActionPerformed(evt);
            }
        });
        jMenu4.add(AcquireFileTIFFPackBits);

        AcquireFileTIFFFlate.setText("Flate Compressed");
        AcquireFileTIFFFlate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileTIFFFlateActionPerformed(evt);
            }
        });
        jMenu4.add(AcquireFileTIFFFlate);

        AcquireFileTIFFJPEG.setText("JPEG Compressed");
        AcquireFileTIFFJPEG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileTIFFJPEGActionPerformed(evt);
            }
        });
        jMenu4.add(AcquireFileTIFFJPEG);

        AcquireFile.add(jMenu4);

        AcquireFilePNG.setText("PNG");
        AcquireFilePNG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFilePNGActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFilePNG);

        AcquireFilePDF.setText("Adobe PDF");
        AcquireFilePDF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFilePDFActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFilePDF);

        jMenu8.setText("PostScript");

        AcquireFilePSLevel1.setText("Level 1");
        AcquireFilePSLevel1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFilePSLevel1ActionPerformed(evt);
            }
        });
        jMenu8.add(AcquireFilePSLevel1);

        AcquireFilePSLevel2.setText("Level 2");
        AcquireFilePSLevel2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFilePSLevel2ActionPerformed(evt);
            }
        });
        jMenu8.add(AcquireFilePSLevel2);

        AcquireFile.add(jMenu8);

        AcquireFilePCX.setText("PCX / DCX");
        AcquireFilePCX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFilePCXActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFilePCX);

        AcquireFileTGA.setText("Targa");
        AcquireFileTGA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileTGAActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFileTGA);

        AcquireFilePSD.setText("Adobe PSD (Photoshop)");
        AcquireFilePSD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFilePSDActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFilePSD);

        jMenu5.setText("Windows MetaFile");

        AcquireFileWMF.setText("WMF");
        AcquireFileWMF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileWMFActionPerformed(evt);
            }
        });
        jMenu5.add(AcquireFileWMF);

        AcquireFileEMF.setText("Enhanced Meta File");
        AcquireFileEMF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileEMFActionPerformed(evt);
            }
        });
        jMenu5.add(AcquireFileEMF);

        AcquireFile.add(jMenu5);

        jMenu7.setText("Windows Icon");

        AcquireFileICO.setText("Small Icon");
        AcquireFileICO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileICOActionPerformed(evt);
            }
        });
        jMenu7.add(AcquireFileICO);

        AcquireFileICOLARGE.setText("Large Icon ");
        AcquireFileICOLARGE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileICOLARGEActionPerformed(evt);
            }
        });
        jMenu7.add(AcquireFileICOLARGE);

        AcquireFile.add(jMenu7);

        AcquireFileWBMP.setText("Wireless Bitmap (WBMP)");
        AcquireFileWBMP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcquireFileWBMPActionPerformed(evt);
            }
        });
        AcquireFile.add(AcquireFileWBMP);

        jMenu2.add(AcquireFile);

        AcquireFileWithDriver.setText("Acquire To File (Driver Mode)...");

        BMPFileMode.setText("Windows BMP");
        BMPFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BMPFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(BMPFileMode);

        JPEGFileMode.setText("JPEG (JFIF)");
        JPEGFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JPEGFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(JPEGFileMode);

        JP2FileMode.setText("JPEG-2000");
        JP2FileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JP2FileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(JP2FileMode);

        TIFFFileMode.setText("TIFF");
        TIFFFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TIFFFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(TIFFFileMode);

        TIFFMultipageFileMode.setText("TIFF (Multipage)");
        TIFFMultipageFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TIFFMultipageFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(TIFFMultipageFileMode);

        PNGFileMode.setText("PNG");
        PNGFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PNGFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(PNGFileMode);

        PDFFileMode.setText("PDF");
        PDFFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PDFFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(PDFFileMode);

        PDFAFileMode.setText("PDF/A");
        PDFAFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PDFAFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(PDFAFileMode);

        PDFA2FileMode.setText("PDF/A2");
        PDFA2FileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PDFA2FileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(PDFA2FileMode);

        FPXFileMode.setText("FlashPix (FPX)");
        FPXFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FPXFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(FPXFileMode);

        EXIFFileMode.setText("EXIF");
        EXIFFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EXIFFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(EXIFFileMode);

        SPIFFFileMode.setText("SPIFF");
        SPIFFFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SPIFFFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(SPIFFFileMode);

        XBMFileMode.setText("XBM");
        XBMFileMode.setToolTipText("");
        XBMFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                XBMFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(XBMFileMode);

        PICTFileMode.setText("Macintosh PICT");
        PICTFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PICTFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(PICTFileMode);

        JPXFileMode.setText("JPX (JPEG ISO/IEC 15444-2)");
        JPXFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JPXFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(JPXFileMode);

        DEJAVUFileMode.setText("DEJAVU");
        DEJAVUFileMode.setToolTipText("");
        DEJAVUFileMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DEJAVUFileModeActionPerformed(evt);
            }
        });
        AcquireFileWithDriver.add(DEJAVUFileMode);

        jMenu2.add(AcquireFileWithDriver);
        jMenu2.add(jSeparator4);

        ShowImagePreview.setSelected(true);
        ShowImagePreview.setText("Show Preview of Image...");
        ShowImagePreview.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ShowImagePreviewActionPerformed(evt);
            }
        });
        jMenu2.add(ShowImagePreview);

        DisplaySourceUI.setSelected(true);
        DisplaySourceUI.setText("Display Source User Interface...");
        jMenu2.add(DisplaySourceUI);

        DiscardBlankPages.setSelected(true);
        DiscardBlankPages.setText("Discard Blank Pages...");
        jMenu2.add(DiscardBlankPages);

        jMenuBar1.add(jMenu2);

        jMenu9.setText("TWAIN Logging...");

        LogCalls.setSelected(true);
        LogCalls.setText("Log DTWAIN Calls...");
        LogCalls.setToolTipText("");
        LogCalls.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogCallsActionPerformed(evt);
            }
        });
        jMenu9.add(LogCalls);

        jMenuBar1.add(jMenu9);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 540, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 419, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void disableAllFormats()
    {
        Collection c = m_FileTypeToMenu.values();
        //obtain an Iterator for Collection
        Iterator itr = c.iterator();
   
        //iterate through TreeMap values iterator
        while(itr.hasNext())
        {
            javax.swing.JMenuItem menu = (javax.swing.JMenuItem)itr.next();
            menu.setEnabled(false);
        }
    }
    
    private void enableSourceItems(boolean bEnable)
    {
        CloseSource.setEnabled(bEnable);
        SourceProperties.setEnabled(bEnable);
        AcquireFile.setEnabled(bEnable);
        AcquireBuffered.setEnabled(bEnable);
        AcquireNative.setEnabled(bEnable);
        AcquireFileWithDriver.setEnabled(bEnable);
        AcquireCompressed.setEnabled(bEnable);
        
        if ( bEnable )
        {
            long TwainSource = m_SourceWrapper.getHandle();
            try {
                disableAllFormats();
                int [] filetypes = m_SourceWrapper.getAllImageFileFormats();
                for (int i = 0; i < filetypes.length; ++i)
                {
                    if (m_FileTypeToMenu.containsKey(filetypes[i]))
                    {
                        javax.swing.JMenuItem menu = m_FileTypeToMenu.get(filetypes[i]);
                        menu.setEnabled(true);
                    }
                }
                
                // now do the compression types for TIFF 
                int [] compressTypes = m_SourceWrapper.getAllCompressions();
                for (int i = 0; i < compressTypes.length; ++i)
                {
                    if (m_CompressTypeToMenu.containsKey(compressTypes[i]))
                    {
                        javax.swing.JMenuItem menu = m_CompressTypeToMenu.get(compressTypes[i]);
                        menu.setEnabled(true);
                    }
                }
                
            }
            catch(DTwainJavaAPIException e)
            {               
            }
        }
        else
        {
            Collection c = m_FileTypeToMenu.values();
            Iterator itr = c.iterator();
            while(itr.hasNext())
            {
                javax.swing.JMenuItem menu = (javax.swing.JMenuItem)itr.next();
                menu.setEnabled(bEnable);
            }
        }
    }
    
    private void setDialogTitle() {
        long TwainSource = m_SourceWrapper.getHandle();
        try {
            if ( TwainSource != 0 ) {
                String sText = sOrigTitle;
                sText += " - " + m_SourceWrapper.getProductName();
                setTitle(sText);
                DTwainAcquireArea area = new DTwainAcquireArea();
                m_api.DTWAIN_SetAcquireArea(TwainSource, DTwainJavaAPIConstants.DTWAIN_AREARESET, area);
            }
            else
                setTitle(sOrigTitle);
            enableSourceItems((TwainSource!=0)?true:false);
        }
        
        catch (DTwainJavaAPIException e)
        { 
            System.out.print(e.getMessage());
        }
    }
    
    private void SelectSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectSourceActionPerformed
        if ( CloseCurrentSource() )
        {
            try 
            {
                long selectedSource = m_api.DTWAIN_SelectSource();
                setupNewSource(selectedSource);
            }
            catch (Exception e)
            { 
                System.out.print(e.getMessage());
            }
        }
    }//GEN-LAST:event_SelectSourceActionPerformed

    private void SourcePropertiesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SourcePropertiesActionPerformed
        long TwainSource = m_SourceWrapper.getHandle();
        if ( TwainSource != 0 ) {
                DTwainSourcePropertiesDialog srcProperties = new DTwainSourcePropertiesDialog(m_api, m_SourceWrapper, this, true);
                srcProperties.setVisible(true);
        }
    }//GEN-LAST:event_SourcePropertiesActionPerformed

    private void SelectDefaultSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectDefaultSourceActionPerformed
        if ( CloseCurrentSource() )
        {
            try 
            {
                long selectedSource = m_api.DTWAIN_SelectDefaultSource();
                setupNewSource(selectedSource);
            }
            catch (DTwainJavaAPIException e)
            { 
                System.out.print(e.getMessage());
            }
        }
    }//GEN-LAST:event_SelectDefaultSourceActionPerformed

    private void CloseSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseSourceActionPerformed
        try {
            m_SourceWrapper.close();
            setDialogTitle();
        }
        catch (DTwainJavaAPIException e)
        { 
            System.out.print(e.getMessage());
        }
    }//GEN-LAST:event_CloseSourceActionPerformed

    private void setBlankPageDetection()
    {
        try {
            long TwainSource = m_SourceWrapper.getHandle();
            if (DiscardBlankPages.getState()) {
                m_api.DTWAIN_SetBlankPageDetection(TwainSource, 98.5, DTwainJavaAPIConstants.DTWAIN_BP_AUTODISCARD_ANY, true);
            } else {
                m_api.DTWAIN_SetBlankPageDetection(TwainSource, 0, 0, false);
            }
        } catch (DTwainJavaAPIException e) {
            System.out.print(e.getMessage());
        }
    }
    
    private void AcquireHelper(int nWhich)
    {
        long TwainSource = m_SourceWrapper.getHandle();
        if ( TwainSource != 0 ) 
        {
            try {
                setBlankPageDetection();
                DTwainAcquisitionArray acqArray = null;
                if ( nWhich == 0 )
                    acqArray = m_api.DTWAIN_AcquireNative(m_SourceWrapper.getHandle(), DTwainJavaAPIConstants.DTWAIN_PT_DEFAULT, DTwainJavaAPIConstants.DTWAIN_ACQUIREALL, DisplaySourceUI.getState(), false);
                else
                    acqArray = m_api.DTWAIN_AcquireBuffered(m_SourceWrapper.getHandle(), DTwainJavaAPIConstants.DTWAIN_PT_DEFAULT, DTwainJavaAPIConstants.DTWAIN_ACQUIREALL, DisplaySourceUI.getState(), false);
                long count = acqArray.getNumAcquisitions();
                System.out.println("Acquired " + count + " images.");
                if ( !acqArray.anyImagesAcquired() )
                    JOptionPane.showMessageDialog(null, "No images were acquired, or all images were discarded by user");
                else
                {
                    DTwainImageDisplayDialog imgDlg = new DTwainImageDisplayDialog(acqArray, DTwainJavaAPIConstants.DTWAIN_BMP);
                    imgDlg.setVisible(true);
                }
            }
            catch (DTwainJavaAPIException e)
            { 
                System.out.print(e.getMessage());
            }
        }
    }
    
    private void AcquireFileHelper(int nWhich, boolean useSourceMode)
    {
        AcquireFileHelper(nWhich, DTwainJavaAPIConstants.DTWAIN_PT_DEFAULT, useSourceMode);
    }
    
    private void AcquireCompressedHelper(int nWhich, int pixelType)
    {
        long TwainSource = m_SourceWrapper.getHandle();
        setBlankPageDetection();
        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showSaveDialog(this);
        if ( returnVal == JFileChooser.APPROVE_OPTION ) 
        {
            File file = fc.getSelectedFile();
            try
            {
                DTwainBufferedStripInfo stripInfo = m_api.DTWAIN_CreateBufferedStripInfo(TwainSource);
                m_api.DTWAIN_SetBufferedTransferInfo(TwainSource, stripInfo);
                stripInfo.setBufferSize(stripInfo.getPreferredSize());
                m_api.DTWAIN_EnableFeeder(TwainSource, true);
                iListener.enableCompression(true);
                iListener.initCompressedStream(stripInfo);
                iListener.setFileHandle(file);
                m_SourceWrapper.setCompression(nWhich);
//                m_api.DTWAIN_SetCompressionType(TwainSource, nWhich, true);
                m_api.DTWAIN_AcquireBuffered(m_SourceWrapper.getHandle(), pixelType, DTwainJavaAPIConstants.DTWAIN_ACQUIREALL, DisplaySourceUI.getState(), false);
                m_api.DTWAIN_EndBufferedTransfer(m_SourceWrapper.getHandle(), stripInfo);
                
                // saved the compressed string to a binary file
            }
            catch (DTwainJavaAPIException e)
            {
                
            }
            iListener.enableCompression(false);
        }
    }
    
    private void AcquireFileHelper(int nWhich, int pixelType, boolean useSourceMode)
    {
        long TwainSource = m_SourceWrapper.getHandle();
        setBlankPageDetection();
        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showSaveDialog(this);
        if ( returnVal == JFileChooser.APPROVE_OPTION) 
        {
            File file = fc.getSelectedFile();
            try
            {
                m_api.DTWAIN_EnableFeeder(TwainSource, true);
                int flags = DTwainJavaAPIConstants.DTWAIN_USENATIVE | DTwainJavaAPIConstants.DTWAIN_USELONGNAME;
                if ( useSourceMode )
                {
                    flags = flags & ~DTwainJavaAPIConstants.DTWAIN_USENATIVE;
                    flags |= DTwainJavaAPIConstants.DTWAIN_USESOURCEMODE;
                }
                if ( m_api.DTWAIN_AcquireFile(TwainSource, file.toString(), nWhich, flags,
                                         pixelType, DTwainJavaAPIConstants.DTWAIN_ACQUIREALL, DisplaySourceUI.getState(), false) != 0 )
                    JOptionPane.showMessageDialog(null, "Image " + file.toString() + " has been created.");
                else
                    JOptionPane.showMessageDialog(null, "Image " + file.toString() + " was not screated.");
            }
            catch (DTwainJavaAPIException e)
            { 
                System.out.print(e.getMessage());
            }
        }
    }
    
    private void AcquireNativeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireNativeActionPerformed
                AcquireHelper(0);
    }//GEN-LAST:event_AcquireNativeActionPerformed

    private void AcquireBufferedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireBufferedActionPerformed
                AcquireHelper(1);
    }//GEN-LAST:event_AcquireBufferedActionPerformed

    private void AcquireFileBMPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileBMPActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_BMP, false);
    }//GEN-LAST:event_AcquireFileBMPActionPerformed

    private void AcquireFileJPEGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileJPEGActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_JPEG, false);
    }//GEN-LAST:event_AcquireFileJPEGActionPerformed

    private void AcquireFileGIFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileGIFActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_GIF, false);
    }//GEN-LAST:event_AcquireFileGIFActionPerformed

    private void AcquireFileTIFF1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileTIFF1ActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_TIFFNONEMULTI, false);
    }//GEN-LAST:event_AcquireFileTIFF1ActionPerformed

    private void AcquireFileTIFFG3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileTIFFG3ActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_TIFFG3MULTI, DTwainJavaAPIConstants.DTWAIN_PT_BW, false);
    }//GEN-LAST:event_AcquireFileTIFFG3ActionPerformed

    private void AcquireFileTIFFG4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileTIFFG4ActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_TIFFG4MULTI, DTwainJavaAPIConstants.DTWAIN_PT_BW, false);
    }//GEN-LAST:event_AcquireFileTIFFG4ActionPerformed

    private void AcquireFileTIFFLZWActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileTIFFLZWActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_TIFFLZWMULTI, false);
    }//GEN-LAST:event_AcquireFileTIFFLZWActionPerformed

    private void AcquireFileTIFFPackBitsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileTIFFPackBitsActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_TIFFPACKBITSMULTI, false);
    }//GEN-LAST:event_AcquireFileTIFFPackBitsActionPerformed

    private void AcquireFileTIFFFlateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileTIFFFlateActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_TIFFDEFLATEMULTI, false);
    }//GEN-LAST:event_AcquireFileTIFFFlateActionPerformed

    private void AcquireFileTIFFJPEGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileTIFFJPEGActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_TIFFJPEGMULTI, false);
    }//GEN-LAST:event_AcquireFileTIFFJPEGActionPerformed

    private void AcquireFilePNGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFilePNGActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_PNG, false);
    }//GEN-LAST:event_AcquireFilePNGActionPerformed

    private void AcquireFilePDFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFilePDFActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_PDFMULTI, false);
    }//GEN-LAST:event_AcquireFilePDFActionPerformed

    private void AcquireFilePCXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFilePCXActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_DCX, false);
    }//GEN-LAST:event_AcquireFilePCXActionPerformed

    private void AcquireFileTGAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileTGAActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_TGA, false);
    }//GEN-LAST:event_AcquireFileTGAActionPerformed

    private void AcquireFilePSDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFilePSDActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_PSD, false);
    }//GEN-LAST:event_AcquireFilePSDActionPerformed

    private void AcquireFileWMFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileWMFActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_WMF, false);
    }//GEN-LAST:event_AcquireFileWMFActionPerformed

    private void AcquireFileEMFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileEMFActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_EMF, false);
    }//GEN-LAST:event_AcquireFileEMFActionPerformed

    private void AcquireFileICOActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileICOActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_ICO, false);
    }//GEN-LAST:event_AcquireFileICOActionPerformed

    private void AcquireFileICOLARGEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileICOLARGEActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_ICO_VISTA, false);
    }//GEN-LAST:event_AcquireFileICOLARGEActionPerformed

    private void AcquireFileWBMPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileWBMPActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_WBMP, DTwainJavaAPIConstants.DTWAIN_PT_BW, false);
    }//GEN-LAST:event_AcquireFileWBMPActionPerformed

    private void AcquireFilePSLevel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFilePSLevel1ActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_POSTSCRIPT1, false);
    }//GEN-LAST:event_AcquireFilePSLevel1ActionPerformed

    private void AcquireFilePSLevel2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFilePSLevel2ActionPerformed
                AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_POSTSCRIPT2, false);
    }//GEN-LAST:event_AcquireFilePSLevel2ActionPerformed

    private void ShowImagePreviewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ShowImagePreviewActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ShowImagePreviewActionPerformed

    private void LogCallsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogCallsActionPerformed
        try 
        {
            if (LogCalls.getState()) 
                m_api.DTWAIN_SetTwainLog(DTwainJavaAPIConstants.DTWAIN_LOG_ALL - DTwainJavaAPIConstants.DTWAIN_LOG_USEFILE, "");
            else 
                m_api.DTWAIN_SetTwainLog(0, "");
        } 
        catch (DTwainJavaAPIException e) 
        {
            System.out.print(e.getMessage());
        }
    }//GEN-LAST:event_LogCallsActionPerformed

    private void ExitDemoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitDemoActionPerformed
        ShutdownTwain();    
        dispose();
    }//GEN-LAST:event_ExitDemoActionPerformed

    private void ShutdownTwain()
    {
        if ( m_api != null )
        {
            try {
                m_api.DTWAIN_JavaSysDestroy();
                m_api = null;
            } 
            catch (Exception e) 
            {
                System.out.print(e.getMessage());
            }
        }
    }
    
    private void ShutdownTwain(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_ShutdownTwain
        ShutdownTwain();    
    }//GEN-LAST:event_ShutdownTwain

    private void BMPFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BMPFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_BMP, true);
    }//GEN-LAST:event_BMPFileModeActionPerformed

    private void JPEGFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JPEGFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_JFIF, true);
    }//GEN-LAST:event_JPEGFileModeActionPerformed

    private void TIFFFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TIFFFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_TIFF, true);
    }//GEN-LAST:event_TIFFFileModeActionPerformed

    private void TIFFMultipageFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TIFFMultipageFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_TIFFMULTI, true);
    }//GEN-LAST:event_TIFFMultipageFileModeActionPerformed

    private void PNGFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PNGFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_PNG, true);
    }//GEN-LAST:event_PNGFileModeActionPerformed

    private void PDFFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PDFFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_PDF, true);
    }//GEN-LAST:event_PDFFileModeActionPerformed

    private void PDFAFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PDFAFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_PDFA, true);
    }//GEN-LAST:event_PDFAFileModeActionPerformed

    private void PDFA2FileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PDFA2FileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_PDFA2, true);
    }//GEN-LAST:event_PDFA2FileModeActionPerformed

    private void FPXFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FPXFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_FPX, true);
    }//GEN-LAST:event_FPXFileModeActionPerformed

    private void EXIFFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EXIFFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_EXIF, true);
    }//GEN-LAST:event_EXIFFileModeActionPerformed

    private void AcquireFileJP2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireFileJP2ActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_JPEG2000, false);
    }//GEN-LAST:event_AcquireFileJP2ActionPerformed

    private void JP2FileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JP2FileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_JP2, true);
    }//GEN-LAST:event_JP2FileModeActionPerformed

    private void SPIFFFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SPIFFFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_SPIFF, true);
    }//GEN-LAST:event_SPIFFFileModeActionPerformed

    private void XBMFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_XBMFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_XBM, true);
    }//GEN-LAST:event_XBMFileModeActionPerformed

    private void PICTFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PICTFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_PICT, true);
    }//GEN-LAST:event_PICTFileModeActionPerformed

    private void JPXFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JPXFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_JPX, true);
    }//GEN-LAST:event_JPXFileModeActionPerformed

    private void DEJAVUFileModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DEJAVUFileModeActionPerformed
        AcquireFileHelper(DTwainJavaAPIConstants.DTWAIN_FF_DEJAVU, true);
    }//GEN-LAST:event_DEJAVUFileModeActionPerformed

    private void setupNewSource(long theSource)
    {
        if ( theSource != 0 )
        {
            try 
            {
                m_SourceWrapper = new DTwainSource(theSource, m_api);
                setDialogTitle();
            }
            catch (DTwainException e)
            { }
        }
    }
    
    private void SelectSourceByNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectSourceByNameActionPerformed

        if ( CloseCurrentSource() )
        {
            DTwainSelectSourceByNameDialog dlg = new DTwainSelectSourceByNameDialog(this, true);
            dlg.setVisible(true);
            if ( dlg.isOkPressed() )
            {
                String sName = dlg.getSourceName();
                try
                {
                    long localTwainSource = m_api.DTWAIN_SelectSourceByName( sName );
                    setupNewSource(localTwainSource);
                }
                catch (DTwainJavaAPIException e)
                { }
            }
        }
    }//GEN-LAST:event_SelectSourceByNameActionPerformed

    private void SelectSourceCustomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectSourceCustomActionPerformed
        if ( CloseCurrentSource() )
        {
            DTwainSelectSourceCustomDialog dlg = new DTwainSelectSourceCustomDialog(this, true, m_api);
            dlg.setVisible(true);
            long localTwainSource = dlg.getSelectedSource();
            if ( localTwainSource != 0 )
                setupNewSource(localTwainSource);
        }
    }//GEN-LAST:event_SelectSourceCustomActionPerformed


    private boolean CloseCurrentSource()
    {
        if ( m_SourceWrapper.getHandle() != 0 )
        {
            int nValue = JOptionPane.showOptionDialog(null, "Close current open Twain Source?", "Close Current Source",
                                                      JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
            if ( nValue == JOptionPane.YES_OPTION)
            {
                try
                {
                    m_SourceWrapper.close();
                    return true;
                }
                catch (DTwainJavaAPIException e )
                {}
            }
            else
                return false;
        }
        return true;
    }
        
    private void AcquireCompressNoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireCompressNoneActionPerformed
        AcquireCompressedHelper(DTwainJavaAPIConstants.DTWAIN_CP_NONE, DTwainJavaAPIConstants.DTWAIN_PT_DEFAULT); // TODO add your handling code here:
    }//GEN-LAST:event_AcquireCompressNoneActionPerformed

    private void AcquireJPEGCompressedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireJPEGCompressedActionPerformed
        AcquireCompressedHelper(DTwainJavaAPIConstants.DTWAIN_CP_JPEG, DTwainJavaAPIConstants.DTWAIN_PT_DEFAULT); // TODO add your handling code here:
    }//GEN-LAST:event_AcquireJPEGCompressedActionPerformed

    private void AcquireGroup4CompressedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcquireGroup4CompressedActionPerformed
        AcquireCompressedHelper(DTwainJavaAPIConstants.DTWAIN_CP_GROUP4, DTwainJavaAPIConstants.DTWAIN_PT_BW); // TODO add your handling code here:
    }//GEN-LAST:event_AcquireGroup4CompressedActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) 
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DTwainFullDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DTwainFullDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DTwainFullDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DTwainFullDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DTwainJavaAPI api = null;
				try {
					api = new DTwainJavaAPI(DTwainJavaAPI.JNIDLLName_64U);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
                DTwainLogger theLogger = new DTwainLogger();
                try 
                {
                        try 
                        {
                            api.DTWAIN_JavaSysInitialize();
                        }
                        catch (Exception e)
                        {
                            System.out.println(e.getStackTrace());
                            return;
                        }
                        DTwainAppInfo appInfo = new DTwainAppInfo();
                        appInfo.setManufacturer("My Java Manufacturer");
                        appInfo.setProductName("My Java Product Name");
                        appInfo.setVersionInfo("My Java Version Info");
                        appInfo.setProductFamily("My Java Product Family");
                        api.DTWAIN_SetAppInfo(appInfo);
                        DTwainAppInfo appInfo2 = api.DTWAIN_GetAppInfo();
                        DTwainVersionInfo vInfo = api.DTWAIN_GetVersionInfo();
                        api.DTWAIN_EnableMsgNotify(1);
                        new DTwainFullDemo(api, theLogger).setVisible(true);
                }
                catch (DTwainJavaAPIException e)
                { 
                    System.out.print(e.getMessage());
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem AcquireBuffered;
    private javax.swing.JMenuItem AcquireCompressNone;
    private javax.swing.JMenu AcquireCompressed;
    private javax.swing.JMenu AcquireFile;
    private javax.swing.JMenuItem AcquireFileBMP;
    private javax.swing.JMenuItem AcquireFileEMF;
    private javax.swing.JMenuItem AcquireFileGIF;
    private javax.swing.JMenuItem AcquireFileICO;
    private javax.swing.JMenuItem AcquireFileICOLARGE;
    private javax.swing.JMenuItem AcquireFileJP2;
    private javax.swing.JMenuItem AcquireFileJPEG;
    private javax.swing.JMenuItem AcquireFilePCX;
    private javax.swing.JMenuItem AcquireFilePDF;
    private javax.swing.JMenuItem AcquireFilePNG;
    private javax.swing.JMenuItem AcquireFilePSD;
    private javax.swing.JMenuItem AcquireFilePSLevel1;
    private javax.swing.JMenuItem AcquireFilePSLevel2;
    private javax.swing.JMenuItem AcquireFileTGA;
    private javax.swing.JMenuItem AcquireFileTIFFFlate;
    private javax.swing.JMenuItem AcquireFileTIFFG3;
    private javax.swing.JMenuItem AcquireFileTIFFG4;
    private javax.swing.JMenuItem AcquireFileTIFFJPEG;
    private javax.swing.JMenuItem AcquireFileTIFFLZW;
    private javax.swing.JMenuItem AcquireFileTIFFPackBits;
    private javax.swing.JMenuItem AcquireFileWBMP;
    private javax.swing.JMenuItem AcquireFileWMF;
    private javax.swing.JMenu AcquireFileWithDriver;
    private javax.swing.JMenuItem AcquireGroup31Compressed;
    private javax.swing.JMenuItem AcquireGroup31dEOLCompressed;
    private javax.swing.JMenuItem AcquireGroup32dCompressed;
    private javax.swing.JMenuItem AcquireGroup4Compressed;
    private javax.swing.JMenuItem AcquireJPEGCompressed;
    private javax.swing.JMenuItem AcquireNative;
    private javax.swing.JMenuItem BMPFileMode;
    private javax.swing.JMenuItem CloseSource;
    private javax.swing.JMenuItem DEJAVUFileMode;
    private javax.swing.JCheckBoxMenuItem DiscardBlankPages;
    private javax.swing.JCheckBoxMenuItem DisplaySourceUI;
    private javax.swing.JMenuItem EXIFFileMode;
    private javax.swing.JMenuItem ExitDemo;
    private javax.swing.JMenuItem FPXFileMode;
    private javax.swing.JMenuItem JP2FileMode;
    private javax.swing.JMenuItem JPEGFileMode;
    private javax.swing.JMenuItem JPXFileMode;
    private javax.swing.JCheckBoxMenuItem LogCalls;
    private javax.swing.JMenuItem PDFA2FileMode;
    private javax.swing.JMenuItem PDFAFileMode;
    private javax.swing.JMenuItem PDFFileMode;
    private javax.swing.JMenuItem PICTFileMode;
    private javax.swing.JMenuItem PNGFileMode;
    private javax.swing.JMenuItem SPIFFFileMode;
    private javax.swing.JMenuItem SelectDefaultSource;
    private javax.swing.JMenuItem SelectSource;
    private javax.swing.JMenuItem SelectSourceByName;
    private javax.swing.JMenuItem SelectSourceCustom;
    private javax.swing.JCheckBoxMenuItem ShowImagePreview;
    private javax.swing.JMenuItem SourceProperties;
    private javax.swing.JMenuItem TIFFFileMode;
    private javax.swing.JMenuItem TIFFMultipageFileMode;
    private javax.swing.JMenuItem XBMFileMode;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenu jMenu8;
    private javax.swing.JMenu jMenu9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    // End of variables declaration//GEN-END:variables
}
