formsignedsignaturewidgetfactory.java

来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 534 行 · 第 1/2 页

JAVA
534
字号
        final JDialog dialog;         if (window instanceof Frame) {            dialog = new JDialog((Frame)window, SuperJOptionPane.getLocalizedString("DigitalIdentities"), true);        } else {            dialog = new JDialog((Dialog)window, SuperJOptionPane.getLocalizedString("DigitalIdentities"), true);        }        final JPanel buttonpane = new JPanel();        /*        if (state.getCertificate()!=null) {            final JButton trustbutton = new JButton("Trust Certificate");            trustbutton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent evt) {                    try {                        getKeyStoreManager().trustCertificate(null, state.getCertificate());                        FormSignature field = state.getSignature();                        SignatureState newstate = validate(field);                        docpanel.putClientProperty(field, newstate);                        tabbedpane.setComponentAt(0, getSignatureStatePanel(newstate));                        trustbutton.setEnabled(false);                    } catch (Exception e) {                        SuperJOptionPane.displayThrowable("Error", e, docpanel);                    }                }            });            buttonpane.add(trustbutton);        }        */        JButton okbutton = new JButton(SuperJOptionPane.getLocalizedString("OK"));        okbutton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent evt) {                dialog.dispose();            }        });        buttonpane.add(okbutton);        JPanel body = new JPanel(new BorderLayout());        body.add(tabbedpane, BorderLayout.CENTER);        body.add(buttonpane, BorderLayout.SOUTH);        dialog.setContentPane(body);        dialog.setResizable(true);        dialog.pack();        dialog.setLocationRelativeTo(docpanel);        dialog.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent evt) {                dialog.dispose();            }        });        dialog.setVisible(true);    }    /**     * Return an {@link Icon} that visually represents the state of the signature. This     * will be displayed in the document and in the Popup used by     * {@link #displaySignatureState displaySignatureState()}     * @param state the state     */    public Icon getIcon(SignatureState state) {        Icon icon;        if (state==null || state.getValidity()==null) {            icon = new ImageIcon(PDFViewer.class.getResource("resources/icons/help.png"));        } else if (state.getValidity()==Boolean.FALSE) {            icon = new ImageIcon(PDFViewer.class.getResource("resources/icons/cross.png"));        } else {            int type = state.getSignature().getCertificationType();            if (type==FormSignature.CERTIFICATION_UNCERTIFIED) {                icon = new ImageIcon(PDFViewer.class.getResource("resources/icons/tick.png"));            } else {                icon = new ImageIcon(PDFViewer.class.getResource("resources/icons/rosette.png"));            }        }        return icon;    }    /**     * Return a JComponent that contains information about the SignatureState.     * This method is used internally by the {@link #displaySignatureState displaySignatureState()}     * method, and there's generally no reason to call it directly.     * @param state the SignatureState to display     */    private JComponent getSignatureStatePanel(SignatureState state) {        String title, reason;        FormSignature sig = state.getSignature();        if (state.getValidity()==null) {            title = SuperJOptionPane.getLocalizedString("ssig.UnknownValidity");            reason = state.getReason();        } else if (state.getValidity()==Boolean.FALSE) {            title = SuperJOptionPane.getLocalizedString("ssig.InvalidSignature");            reason = state.getReason();        } else {            int type = sig.getCertificationType();            if (type==FormSignature.CERTIFICATION_UNCERTIFIED) {                if (state.isAlteredSince()) {                    title = SuperJOptionPane.getLocalizedString("ssig.ValidSignatureAlt");                } else {                    title = SuperJOptionPane.getLocalizedString("ssig.ValidSignature");                }            } else {                if (state.isAlteredSince()) {                    title = SuperJOptionPane.getLocalizedString("ssig.DocumentCertifiedAlt");                } else {                    title = SuperJOptionPane.getLocalizedString("ssig.DocumentCertified");                }            }            if (state.getCertificate()!=null) {                String name = "unknown";                try {                    String z = FormSignature.getSubjectField(state.getCertificate(), "CN");                    if (z==null) z = FormSignature.getSubjectField(state.getCertificate(), "O");                    name = "\""+z+"\"";                } catch (Exception e) {}                reason = SuperJOptionPane.getLocalizedString("ssig.SigNotCertVerified", name);            } else {                reason = SuperJOptionPane.getLocalizedString("ssig.SigCertBothVerified");            }        }        GridBagConstraints all = new GridBagConstraints();        GridBagConstraints key = new GridBagConstraints();        GridBagConstraints val = new GridBagConstraints();        JPanel body = new JPanel(new GridBagLayout());        all.weightx = 0.5;        all.fill = key.fill = val.fill = GridBagConstraints.HORIZONTAL;        key.weighty = all.weighty = val.weighty = 0;        key.gridx = all.gridx = 0;        all.gridwidth = 3;        key.gridwidth = 1;        key.insets = new Insets(0, 0, 0, 20);        val.gridx = 1;        val.gridwidth = GridBagConstraints.REMAINDER;        JLabel label = new JLabel(title);        label.setIcon(getIcon(state));        label.setFont(new Font("SansSerif", Font.BOLD, 14));        label.setBorder(new EmptyBorder(0,0,12,0));        body.add(label, all);        all.weighty = 1;        label = new JLabel(reason);        label.setBorder(new EmptyBorder(0,0,12,0));        body.add(label, all);        body.add(new JLabel(SuperJOptionPane.getLocalizedString("Name")), key);        body.add(new JLabel(sig.getName()==null?"":sig.getName()), val);        body.add(new JLabel(SuperJOptionPane.getLocalizedString("Date")), key);        body.add(new JLabel(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(sig.getSignDate().getTime())), val);        body.add(new JLabel(SuperJOptionPane.getLocalizedString("Revision")), key);        body.add(new JLabel(sig.getNumberOfRevisionsCovered()+" / "+sig.getForm().getPDF().getNumberOfRevisions()), val);        body.add(new JLabel(SuperJOptionPane.getLocalizedString("Reason")), key);        body.add(new JLabel(sig.getReason()==null?SuperJOptionPane.getLocalizedString("NotSpecified"):sig.getReason()), val);        body.add(new JLabel(SuperJOptionPane.getLocalizedString("Location")), key);        body.add(new JLabel(sig.getLocation()==null?SuperJOptionPane.getLocalizedString("NotSpecified"):sig.getLocation()), val);        if (sig.getCertificationType()!=0) {            String[] certifications = { "cert.Uncertified", "cert.NoChanges", "cert.ModifyForms", "cert.ModifyComments" };            body.add(new JLabel(SuperJOptionPane.getLocalizedString("Certification")), key);            body.add(new JLabel(SuperJOptionPane.getLocalizedString(certifications[sig.getCertificationType()])), val);        }        body.setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(), new EmptyBorder(10,10,10,10)));        return body;    }    /**     * Return a JComponent that contains information about the X.509 certificates used in the signature.     * This method is used internally by the {@link #displaySignatureState displaySignatureState()}     * method, and there's generally no reason to call it directly.     * @param certs the list of Certificates to display     */    private JComponent getCertificatesPanel(final X509Certificate[] certs, final FormSignature field, final X509Certificate cert, final JTabbedPane tabbedpane, final DocumentPanel docpanel) throws CertificateException {        Vector v = new Vector();        for (int i=0;i<certs.length;i++) {            String name = FormSignature.getSubjectField(certs[i], "CN");            if (name==null) name = FormSignature.getSubjectField(certs[i], "O");            v.add(name);        }        final JList list = new JList(v);        JScrollPane leftscroll = new JScrollPane(list);        leftscroll.setPreferredSize(new Dimension(200, 200));        final JPanel right = new JPanel(new BorderLayout());        final JScrollPane rightscroll = new JScrollPane(new JPanel());        rightscroll.setPreferredSize(new Dimension(250, 200));        right.add(rightscroll, BorderLayout.CENTER);        if (cert!=null) {            final JButton trustbutton = new JButton(SuperJOptionPane.getLocalizedString("ssig.TrustCertificate"));            trustbutton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent evt) {                    try {                        getKeyStoreManager().trustCertificate(null, certs[list.getSelectedIndex()]);                        SignatureState newstate = validate(field);                        docpanel.putClientProperty(field, newstate);                        tabbedpane.setComponentAt(0, getSignatureStatePanel(newstate));                        trustbutton.setEnabled(false);                    } catch (Exception e) {                        SuperJOptionPane.displayThrowable(SuperJOptionPane.getLocalizedString("Error"), e, docpanel);                    }                }            });            right.add(trustbutton, BorderLayout.SOUTH);        }        JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftscroll, right);        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);        list.addListSelectionListener(new ListSelectionListener() {            public void valueChanged(ListSelectionEvent event) {                rightscroll.setViewportView(getCertificatePanel(certs[list.getSelectedIndex()]));            }        });        list.setSelectedIndex(0);        splitpane.setBorder(BorderFactory.createEtchedBorder());        return splitpane;    }    /**     * Return a JComponent that contains information about a single X.509 certificate.     * This method is used internally by the {@link #displaySignatureState displaySignatureState()}     * method, and there's generally no reason to call it directly.     * @param cert the Certificate to display     */    private JComponent getCertificatePanel(X509Certificate cert) {        JTextArea field = new JTextArea();        field.setFont(new Font("Monospace", 0, 9));        field.setText(cert.toString());        return field;    }    private JComponent getTimestampPanel(SignatureState state, DocumentPanel docpanel) {        X509Certificate[] certs = null;        FormSignature sig = state.getSignature();        JPanel panel = new JPanel(new BorderLayout());        Calendar when = sig.getSignDate();        if (sig.getSignatureHandler() instanceof PKCS7SignatureHandler) {            try {                certs = ((PKCS7SignatureHandler)sig.getSignatureHandler()).getTimeStampCertificates();                if (certs==null) {                    panel.add(new JLabel(SuperJOptionPane.getLocalizedString("ssig.DateFromComputer")), BorderLayout.NORTH);                } else {                    String issuer = null;                    X509Certificate badcert = null;                    if (getKeyStoreManager().getKeyStore()==null) throw new Exception("No KeyStore available");                    issuer = FormSignature.getSubjectField(certs[0], "CN");                    badcert = FormSignature.verifyCertificates(certs, getKeyStoreManager().getKeyStore(), null, when);                    String validity;                    if (badcert==null) {                        validity = SuperJOptionPane.getLocalizedString("ssig.DateGuaranteedKnown", issuer);                    } else {                        validity = SuperJOptionPane.getLocalizedString("ssig.DateGuaranteedUnknown", issuer);                    }                    panel.add(new JLabel(validity), BorderLayout.NORTH);                    try {                        panel.add(getCertificatesPanel(certs, state.getSignature(), badcert, null, docpanel), BorderLayout.CENTER);                    } catch (Exception e) {                        e.printStackTrace();                    }                }            } catch (Exception e) {                panel.add(new JLabel(SuperJOptionPane.getLocalizedString("ssig.ErrorVerifyingTimestamp", e.getMessage())), BorderLayout.NORTH);            }        }        panel.setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(), new EmptyBorder(10,10,10,10)));        return panel;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?