signaturepanel.java

来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 199 行

JAVA
199
字号
// $Id: SignaturePanel.java,v 1.5 2007/11/05 14:54:57 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.pdf2.*;import javax.swing.*;import java.awt.event.*;import javax.swing.tree.*;import javax.swing.event.*;import java.awt.*;import java.util.*;import java.util.List;/** * Create a {@link SidePanel} that will display the document Signatures. * The name of this feature is "ShowHideSignatures". * <p><i>This code is copyright the Big Faceless Organization. You're welcome to use, modify and distribute it in any form in your own projects, provided those projects continue to make use of the Big Faceless PDF library.</i></p> * @since 2.8 */public class SignaturePanel extends SidePanelFactory{    private static SignaturePanel instance;    private FormSignedSignatureWidgetFactory fsswf;    private FormBlankSignatureWidgetFactory fbswf;        /**     * Return the SignaturePanel     */    public static SignaturePanel getInstance() {        if (instance==null) instance = new SignaturePanel();        return instance;    }    private SignaturePanel() {        super("ShowHideSignatures", "Signatures");        fbswf = new FormBlankSignatureWidgetFactory();        fsswf = new FormSignedSignatureWidgetFactory();    }    public boolean isSidePanelRequired(PDF pdf) {        return pdf.getBasicOutputProfile().isSet(OutputProfile.Feature.DigitallySigned);    }    public SidePanel createSidePanel() {        return new SignaturePanelImpl(fbswf, fsswf);    }    /**     * If the DocumentPanel is not being used as part of a {@link PDFViewer},     * this method can be called to set the factory called when a blank signature     * is double clicked on     * @param factory the Factory to run the <code>sign</code> method on for blank signatures     */    public void setFormBlankSignatureWidgetFactory(FormBlankSignatureWidgetFactory factory) {        this.fbswf = factory;    }    /**     * If the DocumentPanel is not being used as part of a {@link PDFViewer},     * this method can be called to set the factory called when a signed signature     * is double clicked on     * @param factory the Factory to run the <code>validate</code> method on for signed signatures     */    public void setFormSignedSignatureWidgetFactory(FormSignedSignatureWidgetFactory factory) {        this.fsswf = factory;    }}/** * A {@link SidePanel} representing the "Signature" or "Bookmark" panel. * * <p><i> * This code is copyright the Big Faceless Organization. You're welcome to * use, modify and distribute it in any form in your own projects, provided * those projects continue to make use of the Big Faceless PDF library. * </i></p> */class SignaturePanelImpl extends JPanel implements SidePanel, ListCellRenderer{    private JScrollPane scrollpane;    private DocumentPanel docpanel;    private FormSignedSignatureWidgetFactory fsswf;    private FormBlankSignatureWidgetFactory fbswf;    private PDF pdf;    SignaturePanelImpl(FormBlankSignatureWidgetFactory fbswf, FormSignedSignatureWidgetFactory fsswf) {        super(new BorderLayout());        setOpaque(true);        scrollpane = new JScrollPane();        add(scrollpane, BorderLayout.CENTER);        this.fsswf = fsswf;        this.fbswf = fbswf;    }    public void redraw(Object o) {    }    public void panelVisible() {    }    public void panelHidden() {    }    public void setDocumentPanel(final DocumentPanel docpanel) {        this.docpanel = docpanel;        if (docpanel.getViewer()!=null) {            ViewerFeature[] features = docpanel.getViewer().getFeatures();            for (int i=0;i<features.length;i++) {                if (features[i] instanceof FormSignedSignatureWidgetFactory) {                    fsswf = (FormSignedSignatureWidgetFactory)features[i];                } else if (features[i] instanceof FormBlankSignatureWidgetFactory) {                    fbswf = (FormBlankSignatureWidgetFactory)features[i];                }            }        }        this.pdf = docpanel==null ? null : docpanel.getPDF();        final Vector v = new Vector();        if (pdf!=null) {            for (Iterator i = pdf.getForm().getElements().entrySet().iterator();i.hasNext();) {                Map.Entry e = (Map.Entry)i.next();                if (e.getValue() instanceof FormSignature) {                    FormSignature sig = (FormSignature)e.getValue();                    v.add(sig);                }            }        }        if (v.size()>0) {            final JList list = new JList(v);            list.setCellRenderer(this);            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);            list.addMouseListener(new MouseAdapter() {                public void mouseClicked(MouseEvent e) {                    if (e.getClickCount() == 2) {                        int ix = list.locationToIndex(e.getPoint());                        if (ix>=0) {                            FormSignature sig = (FormSignature)v.get(ix);                            if (sig.getState()==FormSignature.STATE_SIGNED) {                                if (fsswf!=null) {                                    FormSignedSignatureWidgetFactory.SignatureState state = (FormSignedSignatureWidgetFactory.SignatureState)docpanel.getClientProperty(sig);                                    if (state==null) {                                        state = fsswf.validate(sig);                                        docpanel.putClientProperty(sig, state);                                    }                                    fsswf.displaySignatureState(state, docpanel);                                }                            } else {                                if (fbswf!=null) {                                    try {                                        fbswf.sign(sig, docpanel);                                    } catch (Exception e2) {                                        SuperJOptionPane.displayThrowable(SuperJOptionPane.getLocalizedString("Error"), e2, docpanel);                                    }                                }                            }                        }                    }                }            });            scrollpane.setVisible(true);            scrollpane.setViewportView(list);        } else {            scrollpane.setVisible(false);            scrollpane.setViewportView(null);        }        repaint();    }    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {        JPanel panel = new JPanel();        panel.setOpaque(true);        panel.setLayout(new BorderLayout());        panel.setBackground(isSelected ? list.getSelectionBackground() : list .getBackground());        panel.setForeground(isSelected ? list.getSelectionForeground() : list .getForeground());        panel.setFont(list.getFont());        FormSignature sig = (FormSignature)value;        if (sig.getState()==FormSignature.STATE_SIGNED) {            FormSignedSignatureWidgetFactory.SignatureState state = (FormSignedSignatureWidgetFactory.SignatureState)docpanel.getClientProperty(sig);            Icon icon = fsswf.getIcon(state);            String val = "<b>"+sig.getName()+"</b><br>\n";            val += SuperJOptionPane.getLocalizedString("Date")+": "+sig.getSignDate().getTime()+"<br>\n";            val += SuperJOptionPane.getLocalizedString("Field")+": "+sig.getForm().getName(sig)+"<br>\n";            val += SuperJOptionPane.getLocalizedString("Reason")+": "+(sig.getReason()==null?SuperJOptionPane.getLocalizedString("NotSpecified"):sig.getReason())+"<br>\n";            JLabel label = new JLabel("<html>"+val+"<br></html>");            label.setIcon(icon);            panel.add(label);        } else {            JLabel label = new JLabel("<i>"+SuperJOptionPane.getLocalizedString("Blank")+"</i>");            panel.add(label);        }       return panel;    }}

⌨️ 快捷键说明

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