annotationnotefactory.java

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

JAVA
187
字号
// $Id: AnnotationNoteFactory.java,v 1.10 2007/11/08 09:44:18 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.pdf2.*;import javax.swing.*;import java.awt.geom.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.*;import java.util.*;import java.text.DateFormat;/** * Create annotations that handle {@link AnnotationNote} objects * The name of this feature is "AnnotationNote". * <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 AnnotationNoteFactory extends AnnotationComponentFactory{    private static AnnotationNoteFactory instance;    /**     * Return the AnnotationNoteFactory     */    public static AnnotationNoteFactory getInstance() {        if (instance==null) instance = new AnnotationNoteFactory();        return instance;    }    private AnnotationNoteFactory() {        super("AnnotationNote");    }    public boolean matches(PDFAnnotation annot) {        return annot instanceof AnnotationNote;    }    public JComponent createComponent(final PagePanel pagepanel, PDFAnnotation a) {        final AnnotationNote annot = (AnnotationNote)a;        final JComponent comp = new JPanel() {            public void paintComponent(Graphics g) {                 AnnotationComponentFactory.paintComponent(this, "N", g);            }        };        comp.setToolTipText(annot.getContents());        comp.addMouseListener(new MouseListener() {            public void mouseEntered(MouseEvent event) { comp.setCursor(new Cursor(Cursor.HAND_CURSOR)); }            public void mouseExited(MouseEvent event) { comp.setCursor(Cursor.getDefaultCursor()); }            public void mousePressed(MouseEvent event) {}            public void mouseReleased(MouseEvent event) {}            public void mouseClicked(MouseEvent event) {                boolean readonly = annot.isReadOnly();                final String content = annot.getContents();                final Color color = annot.getColor();                final int opacity = annot.getOpacity();                final String type = annot.getType();                JOptionPane pane = new JOptionPane(createEditPanel(annot, readonly), JOptionPane.PLAIN_MESSAGE, readonly ? JOptionPane.OK_OPTION : JOptionPane.OK_CANCEL_OPTION);                JDialog dialog = pane.createDialog(pagepanel.getDocumentPanel().getViewer(), SuperJOptionPane.getLocalizedString("Annotation"));                dialog.setResizable(true);                dialog.pack();                dialog.setVisible(true);                if (!readonly) {                    if (pane.getValue()!=null && ((Integer)pane.getValue()).intValue()==JOptionPane.OK_OPTION) {                        comp.setToolTipText(annot.getContents());                        pagepanel.redrawAnnotation(annot);                    } else {                        annot.setContents(content);                        annot.setType(type, color);                        annot.setOpacity(opacity);                    }                }            }        });        if (!annot.isReadOnly()) {            AnnotationDragListener listener = new AnnotationDragListener(pagepanel, comp, annot);            comp.addMouseListener(listener);            comp.addMouseMotionListener(listener);        }        return comp;    }    /**      * Create a JComponent that will edit the Note Annotation properties     * if readonly is false, or display them if true.     *     * Not public yet, would be nice not to update it immediately but only on OK.     *     * @since 2.8.5     */    static JComponent createEditPanel(final AnnotationNote annot, final boolean readonly) {        final JPanel panel = new JPanel(new GridBagLayout());        GridBagConstraints gbc = new GridBagConstraints();        String name = annot.getAuthor();        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.weightx = 1;        panel.add(new JLabel(name), gbc);        gbc.gridwidth = gbc.REMAINDER;        panel.add(new JLabel(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(annot.getCreationDate().getTime())), gbc);        gbc.fill = GridBagConstraints.BOTH;        gbc.weighty = 1;        final JTextArea textarea = new JTextArea();        textarea.addFocusListener(new FocusListener() {            public void focusGained(FocusEvent e) {            }            public void focusLost(FocusEvent e) {                annot.setContents(textarea.getText());            }        });        textarea.setPreferredSize(new Dimension(300, 100));        textarea.setEditable(!readonly);        panel.add(textarea, gbc);        if (!readonly) {            gbc.gridwidth = 1;            gbc.weightx = 0;            gbc.weighty = 0;            final JPanel appearancepanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));            panel.add(appearancepanel, gbc);            final ButtonGroup appearancegroup = new ButtonGroup();            final String[] types = new String[] { "Note", "Comment", "Help", "Insert", "Key", "New Paragraph", "Paragraph", "Check", "Cross", "RightArrow", "RightPointer", "Star", "UpArrow", "UpLeftArrow", "Circle" };            ActionListener selectionlistener = new ActionListener() {                public void actionPerformed(ActionEvent evt) {                    for (int i=0;i<appearancepanel.getComponentCount();i++) {                        if (((AbstractButton)appearancepanel.getComponent(i)).isSelected()) {                            annot.setType(types[i], annot.getColor());                            break;                        }                    }                }            };            for (int i=0;i<types.length;i++) {                String type = types[i].replaceAll(" ","");                java.net.URL url = PDFViewer.class.getResource("resources/annots/note_"+type+".png");                if (url!=null) {                    JRadioButton button = new JRadioButton();                    button.setSelectedIcon(new ImageIcon(url));                    Image image = ((ImageIcon)button.getSelectedIcon()).getImage();                    image = GrayFilter.createDisabledImage(image);                    button.setIcon(new ImageIcon(image));                    button.setSelected(types[i].equals(annot.getType()));                    button.addActionListener(selectionlistener);                    button.setToolTipText(types[i]);                    appearancepanel.add(button);                    appearancegroup.add(button);                }            }            gbc.weightx = 1;            final JButton colorbox = new JButton() {                public void paintComponent(Graphics g) {                    g.setColor(annot.getColor());                    g.fillRect(0, 0, getWidth(), getHeight());                    g.setColor(Color.black);                    g.drawRect(0, 0, getWidth()-1, getHeight()-1);                }            };            colorbox.setMinimumSize(new Dimension(20, 20));            colorbox.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent evt) {                    Color c = JColorChooser.showDialog(panel, SuperJOptionPane.getLocalizedString("Color"), annot.getColor());                    if (c!=null) {                        annot.setType(annot.getType(), c);                        colorbox.repaint();                    }                }            });            colorbox.setToolTipText(SuperJOptionPane.getLocalizedString("annot.SetColor"));            panel.add(colorbox, gbc);            final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, annot.getOpacity());            slider.setToolTipText(SuperJOptionPane.getLocalizedString("annot.SetOpacity"));            slider.addChangeListener(new ChangeListener() {                public void stateChanged(ChangeEvent e) {                    if (!slider.getValueIsAdjusting()) {                        annot.setOpacity((int)slider.getValue());                    }                }            });            panel.add(slider, gbc);        }        return panel;    }}

⌨️ 快捷键说明

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