formtextwidgetfactory.java

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

JAVA
259
字号
// $Id: FormTextWidgetFactory.java,v 1.10 2007/11/12 05:29:58 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.pdf2.*;import org.faceless.pdf2.Event;import javax.swing.*;import javax.swing.text.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.*;import java.util.*;/** * Create annotations to handle {@link WidgetAnnotation} objects belonging to a {@link FormText}. * The name of this feature is "FormTextWidgetFactory". * <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 FormTextWidgetFactory extends AnnotationComponentFactory{    private static FormTextWidgetFactory instance;    /**     * Return the FormTextWidgetFactory     */    public static FormTextWidgetFactory getInstance() {        if (instance==null) instance = new FormTextWidgetFactory();        return instance;    }    private FormTextWidgetFactory() {        super("FormTextWidgetFactory");    }    public boolean matches(PDFAnnotation annot) {        return annot instanceof WidgetAnnotation && ((WidgetAnnotation)annot).getField() instanceof FormText;    }    /**     * Called by FormXXXWidgetFactory classes when the field is created, this keeps track     * of which fields need to be recalculated.     */    static void  createOtherChange(DocumentPanel docpanel, FormElement field) {        if (field.getAction(Event.OTHERCHANGE)!=null) {            Collection c = (Collection)docpanel.getClientProperty("field.otherchange");            if (c==null) {                c = new LinkedHashSet();                docpanel.putClientProperty("field.otherchange", c);            }            c.add(field);        }    }    /**     * Called by FormXXXWidgetFactory classes after their value has been updated, this     * runs any calculation scripts that need to be run     */    static void runOtherChange(DocumentPanel docpanel, WidgetAnnotation annot) {        Collection others = (Collection)docpanel.getClientProperty("field.otherchange");        if (others!=null) {            JSManager console = docpanel.getJSManager();            for (Iterator i = others.iterator();i.hasNext();) {                FormElement other = (FormElement)i.next();                console.runEventFieldCalculate(docpanel, other.getAnnotation(0), annot);            }        }    }    public JComponent createComponent(final PagePanel pagepanel, PDFAnnotation annot) {        final WidgetAnnotation widget = (WidgetAnnotation)annot;        final FormText field = (FormText)widget.getField();        final DocumentPanel docpanel = pagepanel.getDocumentPanel();        final JSManager js = docpanel.getJSManager();        int type = field.getType();        createOtherChange(docpanel, field);        if (field.isReadOnly()) {            JComponent comp = new JPanel() {                public void paintComponent(Graphics g) {                    AnnotationComponentFactory.paintComponent(this, "N", g);                }            };            return comp;        } else {            final JTextComponent comp;            if (type==FormText.TYPE_MULTILINE) {                comp = new JTextArea() {                    public void paintComponent(Graphics g) {                        AnnotationComponentFactory.paintComponent(this, "N", g);                        super.paintComponent(g);                    }                };                ((JTextArea)comp).setLineWrap(true);            } else {                if (type==FormText.TYPE_PASSWORD) {                    comp = new JPasswordField() {                        public void paintComponent(Graphics g) {                            AnnotationComponentFactory.paintComponent(this, "N", g);                            super.paintComponent(g);                        }                    };                } else {                    comp = new JTextField() {                        public void paintComponent(Graphics g) {                            AnnotationComponentFactory.paintComponent(this, "N", g);                            super.paintComponent(g);                        }                    };                }            }            final int columns = field.getNumberOfCombs()==0 ? field.getMaxLength()==0 ? Integer.MAX_VALUE : field.getMaxLength() : field.getNumberOfCombs();            final AbstractDocument document = (AbstractDocument)comp.getDocument();            if (comp instanceof JTextField) {                ((JTextField)comp).addActionListener(new ActionListener() {                    public void actionPerformed(ActionEvent event) {                        comp.putClientProperty("bfo.HasChanged", "true");                        if (js.runEventFieldKeystroke(docpanel, widget, 2, "", "", false, false, false, -1, -1, false, comp.getText(), true)) {                            comp.putClientProperty("bfo.willCommitEvent", event);                            comp.transferFocus();                        }                    }                });            }            // Filter that ensures the columns are adhered to and handles Keystroke events            // Run before the change is applied            final DocumentFilter documentfilter = new DocumentFilter() {                public void insertString(FilterBypass fb, int offset, String changeEx, AttributeSet attrs) throws BadLocationException {                    comp.putClientProperty("bfo.HasChanged", "true");                    String change = changeEx;                    if (columns < fb.getDocument().getLength() + changeEx.length()) {                        change = change.substring(0, columns-fb.getDocument().getLength());                    }                    if (js.runEventFieldKeystroke(docpanel, widget, 0, change, changeEx, change!=changeEx, false, false, offset, offset, false, comp.getText(), false)) {                        fb.insertString(offset, change, attrs);                        if (change!=changeEx) {                            Toolkit.getDefaultToolkit().beep();                        }                    }                }                public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {                    comp.putClientProperty("bfo.HasChanged", "true");                    fb.remove(offset, length);                }                public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String changeEx, AttributeSet attrs) throws BadLocationException {                    comp.putClientProperty("bfo.HasChanged", "true");                    String change = changeEx;                    if (columns < fb.getDocument().getLength() - length + changeEx.length()) {                        change = change.substring(0, columns - fb.getDocument().getLength() + length);                    }                    if (js.runEventFieldKeystroke(docpanel, widget, 0, change, changeEx, change!=changeEx, false, false, offset, offset+length, false, comp.getText(), false)) {                        fb.replace(offset, length, change, attrs);                        if (change!=changeEx) {                            Toolkit.getDefaultToolkit().beep();                        }                    }                }            };            // FocusListener that handles focus/blur events            final FocusListener focuslistener = new FocusListener() {                public void focusGained(FocusEvent event) {                    if (comp.getClientProperty("bfo.HasFocus")==null) {                        comp.putClientProperty("bfo.HasFocus", "true");                        js.runEventFieldFocus(docpanel, widget, false, false);                        float fontsize = widget.getTextStyle().getFontSize();                        if (fontsize==0) fontsize = 12;                        fontsize = fontsize * pagepanel.getDPI() / 72;                        if (fontsize != comp.getFont().getSize2D()) {                            comp.setFont(comp.getFont().deriveFont(fontsize));                        }                        document.setDocumentFilter(null);                        comp.setText(field.getValue());                        document.setDocumentFilter(documentfilter);                        comp.setBorder(BorderFactory.createEtchedBorder());                        Paint p = widget.getBackgroundStyle().getFillColor();                        comp.setBackground(p!=null && p instanceof Color ? (Color)p : Color.white);                        p = widget.getTextStyle().getFillColor();                        comp.setForeground(p!=null && p instanceof Color ? (Color)p : Color.black);                        comp.repaint();                    }                }                public void focusLost(FocusEvent event) {                    if (!event.isTemporary() && comp.getClientProperty("bfo.HasFocus")!=null) {                        boolean ok = true;                        if (comp.getClientProperty("bfo.HasChanged")!=null) {                            if (comp.getClientProperty("bfo.willCommitEvent")==null) {                                if (!js.runEventFieldKeystroke(docpanel, widget, 1, "", "", false, false, false, -1, -1, false, comp.getText(), true)) {                                    ok = false;                                }                            }                            if (ok) {                                JSManager console = docpanel.getJSManager();                                String value = comp.getText();                                if (docpanel.getJSManager().runEventFieldValidate(docpanel, widget, value, false, false, value, value, false)) {                                    field.setValue(value);                                    runOtherChange(docpanel, widget);                                } else {                                    ok = false;                                }                            }                            if (ok) {                                js.runEventFieldFormat(docpanel, widget, 1, false);                                js.runEventFieldBlur(docpanel, widget, false, false);                                field.rebuild();                            }                        }                        if (ok) {                            comp.setBorder(null);                            comp.setBackground(new Color(0,0,0,0));                            comp.setForeground(new Color(0,0,0,0));                            docpanel.redraw(field);                            pagepanel.repaint();                            comp.putClientProperty("bfo.HasFocus", null);                            comp.putClientProperty("bfo.HasChanged", null);                        } else {                            comp.requestFocus();                        }                    }                }            };            //-------------------------------------------------------            comp.setText(field.getValue());            comp.setBackground(new Color(0,0,0,0));            comp.setForeground(new Color(0,0,0,0));            comp.setBorder(null);            comp.setToolTipText(field.getDescription()!=null ? field.getDescription() : field.getForm().getName(field));            comp.addMouseListener(new MouseListener() {                public void mouseEntered(MouseEvent event) {                    js.runEventFieldMouseEnter(docpanel, widget, event);                }                public void mouseExited(MouseEvent event) {                    js.runEventFieldMouseExit(docpanel, widget, event);                }                public void mousePressed(MouseEvent event) {                    js.runEventFieldMouseDown(docpanel, widget, event);                }                public void mouseReleased(MouseEvent event) {                    js.runEventFieldMouseUp(docpanel, widget, event);                }                public void mouseClicked(MouseEvent event) {                }            });            comp.addFocusListener(focuslistener);            document.setDocumentFilter(documentfilter);            return comp;        }    }}

⌨️ 快捷键说明

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