jp.nichicom.ndk.affair.nu.nu001.NU001.java Source code

Java tutorial

Introduction

Here is the source code for jp.nichicom.ndk.affair.nu.nu001.NU001.java

Source

/*
 * Project code name "ORCA"
 * ?? QKANCHOJMA care benefit management software
 * Copyright(C) 2002 JMA (Japan Medical Association)
 *
 * This program is part of "QKANCHO (JMA care benefit management software)".
 *
 * This program is distributed in the hope that it will be useful
 * for further advancement in medical care, according to JMA Open
 * Source License, but WITHOUT ANY WARRANTY.
 * Everyone is granted permission to use, copy, modify and
 * redistribute this program, but only under the conditions described
 * in the JMA Open Source License. You should have received a copy of
 * this license along with this program. If not, stop using this
 * program and contact JMA, 2-28-16 Honkomagome, Bunkyo-ku, Tokyo,
 * 113-8621, Japan.
 *****************************************************************
 * : QKANCHO
 * : 
 * ?: 2006/09/13  ?  ??
 * : ----/--/--
 *  nichicom (N)
 *   (U)
 *  PDF (001)
 *  PDF (NU001)
 *
 *****************************************************************
 */
package jp.nichicom.ndk.affair.nu.nu001;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;

import jp.nichicom.ac.ACConstants;
import jp.nichicom.ac.core.ACAffairInfo;
import jp.nichicom.ac.core.ACFrame;
import jp.nichicom.ac.filechooser.ACFileChooser;
import jp.nichicom.ac.filechooser.ACFileFilter;
import jp.nichicom.ac.util.ACMessageBox;
import jp.nichicom.vr.util.VRHashMap;
import jp.nichicom.vr.util.VRMap;

import com.itextpdf.text.pdf.PdfReader;

/**
 * PDF(NU001) 
 */
@SuppressWarnings("serial")
public class NU001 extends NU001Event {
    /**
     * ??
     */
    public NU001() {
    }

    //??

    public void initAffair(ACAffairInfo affair) throws Exception {
        super.initAffair(affair);
        setTitle("PDF");
    }

    /**
     * ???
     * @param e 
     * @throws Exception ?
     */
    protected void compareActionPerformed(ActionEvent e) throws Exception {
        // 

        if (!new File(getPdf1().getText()).exists()) {
            ACMessageBox.showExclamation("PDF1?????");
            return;
        }
        if (!new File(getPdf2().getText()).exists()) {
            ACMessageBox.showExclamation("PDF2?????");
            return;
        }

        final PdfReader p1 = new PdfReader(getPdf1().getText());
        final PdfReader p2 = new PdfReader(getPdf2().getText());
        final int pages1 = p1.getNumberOfPages();
        final int pages2 = p2.getNumberOfPages();

        if (pages1 != pages2) {
            if (pages1 > pages2) {
                ACMessageBox.showExclamation("PDF1PDF2??????");
                return;
            }
            if (ACMessageBox.showOkCancel("PDF?????" + ACConstants.LINE_SEPARATOR
                    + "PDF1??????PDF2?????????") != ACMessageBox.RESULT_OK) {
                return;
            }
        }

        new Runnable() {
            private boolean firstLock;

            public void run() {
                final int stopKeyCode = KeyEvent.VK_CAPS_LOCK;
                firstLock = Toolkit.getDefaultToolkit().getLockingKeyState(stopKeyCode);

                LinkedList<Integer> pages = new LinkedList<Integer>();
                for (int i = 1; i <= pages2; i++) {
                    pages.add(new Integer(i));
                }

                errorCount = 0;
                pageOfProcessed = 0;
                pageOfCount = pages1;
                StringBuilder sb = new StringBuilder();

                for (int i = 1; i <= pages1; i++) {
                    try {
                        boolean match = false;
                        byte[] b1 = p1.getPageContent(i);
                        Iterator<Integer> it = pages.iterator();
                        while (it.hasNext()) {
                            int page = ((Integer) it.next()).intValue();
                            byte[] b2 = p2.getPageContent(page);
                            if (Arrays.equals(b1, b2)) {
                                sb.append("PDF1(" + i + ") = PDF2(" + page + ")" + ACConstants.LINE_SEPARATOR);
                                match = true;
                                it.remove();
                                break;
                            }
                        }
                        if (!match) {
                            sb.append("PDF1(" + i + ") ???" + ACConstants.LINE_SEPARATOR);
                            errorCount++;
                        }
                    } catch (Exception ex) {
                        sb.append("PDF1(" + i + ") ??" + ACConstants.LINE_SEPARATOR);
                        errorCount++;
                    }

                    setProgress(sb);
                    pageOfProcessed = i;

                    if (Toolkit.getDefaultToolkit().getLockingKeyState(stopKeyCode) != firstLock) {
                        getResult().setText(
                                "?(" + pageOfProcessed + " / " + pageOfCount + ") / ?:"
                                        + errorCount + " " + ACConstants.LINE_SEPARATOR + sb.toString());
                        return;
                    }

                }

                getResult().setText("(" + pageOfProcessed + " / " + pageOfCount + ") / ?:"
                        + errorCount + " " + ACConstants.LINE_SEPARATOR + sb.toString());

            }

        }.run();

    }

    private int errorCount;
    private int pageOfCount;
    private int pageOfProcessed;

    private void setProgress(StringBuilder sb) {
        getResult().setText("(" + pageOfProcessed + " / " + pageOfCount + ") / ?:"
                + errorCount + "  CAPS-LOCK?" + ACConstants.LINE_SEPARATOR + sb.toString());
        repaint();
        getThis().paintImmediately(getThis().getVisibleRect());

    }

    /**
     * ???
     * @param e 
     * @throws Exception ?
     */
    protected void exitActionPerformed(ActionEvent e) throws Exception {
        // 
        if (ACMessageBox.showOkCancel("????") != ACMessageBox.RESULT_OK) {
            return;
        }
        System.exit(0);
    }

    /**
     * PDF1????
     * @param e 
     * @throws Exception ?
     */
    protected void pdf1BrowseActionPerformed(ActionEvent e) throws Exception {
        // PDF1?
        ACFileFilter filter = new ACFileFilter();
        filter.setDescription("PDF(*.pdf)");
        filter.setFileExtensions(new String[] { "pdf" });
        File f = new ACFileChooser().showOpenDialog(getPdf1().getText(), getPdf1().getText(), "PDF1?",
                filter);
        if (f == null) {
            return;
        }
        getPdf1().setText(f.getAbsolutePath());
    }

    /**
     * PDF2????
     * @param e 
     * @throws Exception ?
     */
    protected void pdf2BrowseActionPerformed(ActionEvent e) throws Exception {
        // PDF2?
        ACFileFilter filter = new ACFileFilter();
        filter.setDescription("PDF(*.pdf)");
        filter.setFileExtensions(new String[] { "pdf" });
        File f = new ACFileChooser().showOpenDialog(getPdf2().getText(), getPdf2().getText(), "PDF2?",
                filter);
        if (f == null) {
            return;
        }
        getPdf2().setText(f.getAbsolutePath());
    }

    public static void main(String[] args) {
        //?
        VRMap param = new VRHashMap();
        //param?????????????
        ACFrame.debugStart(new ACAffairInfo(NU001.class.getName(), param));
    }

}