formchoicewidgetfactory.java
来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 345 行 · 第 1/2 页
JAVA
345 行
// $Id: FormChoiceWidgetFactory.java,v 1.11 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 FormChoice}. * The name of this feature is "FormChoiceWidgetFactory". * <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 FormChoiceWidgetFactory extends AnnotationComponentFactory{ private static FormChoiceWidgetFactory instance; /** * Return the FormChoiceWidgetFactory */ public static FormChoiceWidgetFactory getInstance() { if (instance==null) instance = new FormChoiceWidgetFactory(); return instance; } private FormChoiceWidgetFactory() { super("FormChoiceWidgetFactory"); } public boolean matches(PDFAnnotation annot) { return annot instanceof WidgetAnnotation && ((WidgetAnnotation)annot).getField() instanceof FormChoice; } public JComponent createComponent(final PagePanel pagepanel, PDFAnnotation annot) { final WidgetAnnotation widget = (WidgetAnnotation)annot; final FormChoice field = (FormChoice)widget.getField(); JComponent comp; if (field.isReadOnly()) { comp = new JPanel() { public void paintComponent(Graphics g) { AnnotationComponentFactory.paintComponent(this, "N", g); } }; } else if (field.getType()==FormChoice.TYPE_DROPDOWN) { comp = createComboComponent(pagepanel, field, widget, true); } else if (field.getType()==FormChoice.TYPE_COMBO) { comp = createComboComponent(pagepanel, field, widget, false); } else { comp = createMenuComponent(pagepanel, field, widget); } FormTextWidgetFactory.createOtherChange(pagepanel.getDocumentPanel(), field); return comp; } private JComponent createComboComponent(final PagePanel pagepanel, final FormChoice field, final WidgetAnnotation widget, boolean readonly) { final DocumentPanel docpanel = pagepanel.getDocumentPanel(); final JSManager js = docpanel.getJSManager(); final JTextField comp = new JTextField() { public void paintComponent(Graphics g) { AnnotationComponentFactory.paintComponent(this, "N", g); super.paintComponent(g); } }; comp.setBackground(new Color(0,0,0,0)); comp.setForeground(new Color(0,0,0,0)); comp.setBorder(null); comp.setToolTipText(widget.getField().getDescription()); final AbstractDocument document = (AbstractDocument)comp.getDocument(); comp.setEditable(!readonly); final JPopupMenu menu = new JPopupMenu(); final DocumentFilter documentfilter = new DocumentFilter() { public void insertString(FilterBypass fb, int offset, String change, AttributeSet attrs) throws BadLocationException { comp.putClientProperty("bfo.HasChanged", "true"); if (js.runEventFieldKeystroke(docpanel, widget, 0, change, "", false, false, false, offset, offset, false, comp.getText(), false)) { fb.insertString(offset, change, attrs); } } public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String change, AttributeSet attrs) throws BadLocationException { comp.putClientProperty("bfo.HasChanged", "true"); if (js.runEventFieldKeystroke(docpanel, widget, 0, change, "", false, false, false, offset, offset+length, false, comp.getText(), false)) { fb.replace(offset, length, change, attrs); } } public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException { comp.putClientProperty("bfo.HasChanged", "true"); fb.remove(offset, length); } }; ActionListener popuplistener = new ActionListener() { public void actionPerformed(ActionEvent event) { String change = ((JMenuItem)event.getSource()).getText(); String changeEx = (String)field.getOptions().get(change); if (js.runEventFieldKeystroke(docpanel, widget, 0, change, changeEx, false, false, false, -1, -1, false, comp.getText(), false)) { comp.putClientProperty("bfo.HasChanged", "true"); document.setDocumentFilter(null); field.setValue(changeEx==null ? change : changeEx); document.setDocumentFilter(documentfilter); } comp.requestFocus(); } }; document.setDocumentFilter(documentfilter); for (Iterator i = field.getOptions().keySet().iterator();i.hasNext();) { JMenuItem val = new JMenuItem((String)i.next()); val.addActionListener(popuplistener); menu.add(val); } 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(); } } }); comp.addFocusListener(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); String value = field.getValue(); Map options = field.getOptions(); for (Iterator i = options.entrySet().iterator();i.hasNext();) { Map.Entry e = (Map.Entry)i.next(); if (e.getValue().equals(value)) { value = (String)e.getKey(); break; } } comp.setText(value); 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() && event.getOppositeComponent()!=menu && 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();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?