pagepanel.java

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

JAVA
595
字号
            positionPDFBasedComponent((JComponent)c);        }    }    private void positionPDFBasedComponent(JComponent c) {        float[] rect = (float[])((JComponent)c).getClientProperty("pdf.rect");        final float zoom = dpi/72f;        int x = (int)((rect[0]-x1) * zoom);        int y = (int)((y2-rect[3]) * zoom);        int w = (int)((rect[2]-rect[0]) * zoom);        int h = (int)((rect[3]-rect[1]) * zoom);        if (w<0) { x+=w; w=-w; }        if (h<0) { y+=h; h=-h; }        c.setBounds(x, y, w, h);    }    //------------------------------------------------------------------------------------    // Getters    /**     * Redraw the specified annotation on this page. This should be     * called if the annotation is changed, if it's newly added to the     * page or if it's been removed from the page.     * @throws IllegalStateException if the PagePanel is not part of     * a {@link DocumentPanel} and so has no annotations.     */    public void redrawAnnotation(PDFAnnotation annot) {        if (annot.getPage()!=getPage()) {            JComponent c = (JComponent)annotcomponents.remove(annot);            if (c!=null) remove(c);        } else {            JComponent c = (JComponent)annotcomponents.get(annot);            if (c==null) {                c = createComponentForAnnotation(annot);                c.setVisible(false);                annotcomponents.put(annot, c);                add(c);            }        }        paintthread.updateAnnotation(annot);    }    /**     * Return true if this PagePanel draws annotations as children     */    private boolean hasAnnotations() {        return viewport!=null && viewport.getDocumentPanel()!=null && !viewport.getDocumentPanel().getAnnotationFactories().isEmpty();    }    /**     * Create a component for an annotation     */    private JComponent createComponentForAnnotation(PDFAnnotation annot) {        JComponent c = null;        Collection factories = viewport.getDocumentPanel().getAnnotationFactories();        for (Iterator i=factories.iterator();c==null && i.hasNext();) {            AnnotationComponentFactory factory = (AnnotationComponentFactory)i.next();            if (factory.matches(annot)) {                c = factory.createComponent(this, annot);            }        }        if (c==null) {            c = AnnotationComponentFactory.getDefaultFactory().createComponent(this, annot);        }        AnnotationComponentFactory.bindComponentLocation(c, annot);        return c;    }    private void raisePagePanelEvent(PagePanelEvent event) {        PropertyManager propertymanager = getDocumentPanel()==null || getDocumentPanel().getViewer()==null ? PDF.getPropertyManager() : getDocumentPanel().getViewer().getPropertyManager();        if (propertymanager.getProperty("debug.Event")!=null) {            System.out.println("Raise page "+event);        }        List l = new ArrayList(listeners);        for (Iterator i=l.iterator();i.hasNext();) {            ((PagePanelListener)i.next()).pageUpdated(event);        }    }    private class PainterThread extends Thread {        private float tdpi, tx1, ty1, tx2, ty2;         // The values we're going to redraw with        private PDFPage tpage;        private PageExtractor textractor;        private PagePainter tpainter;        private boolean repaintcontent, wait;                 // Whether to repaint the page.        private List repaintannotations = Collections.synchronizedList(new ArrayList());        /**         * If the paintthread is painting, interrupt it.         */        synchronized void interruptPaint() {            if (tpainter!=null && tpainter.isPainting()) {                tpainter.interrupt();            }        }        /**         * Redraw with the specified page and position         */        synchronized void redraw(PagePainter painter, PDFPage page, float x1, float y1, float x2, float y2, float dpi) {            PropertyManager propertymanager = getDocumentPanel()==null || getDocumentPanel().getViewer()==null ? PDF.getPropertyManager() : getDocumentPanel().getViewer().getPropertyManager();            if (propertymanager.getProperty("debug.Event")!=null) {                System.out.println("redraw(P"+(page==null?-1:(page.getPageNumber()-1))+", "+x1+", "+y1+", "+x2+", "+y2+", "+dpi+")");            }            interruptPaint();            pageimage = null;            if ((tpainter!=painter || tdpi!=dpi) && page!=null && hasAnnotations()) {                synchronized(repaintannotations) {      // need to redraw all annotations                    repaintannotations.addAll(page.getAnnotations());                }            }            synchronized(this) {                tx1 = x1;                tx2 = x2;                ty1 = y1;                ty2 = y2;                tdpi = dpi;                tpage = page;                tpainter = painter;                this.repaintcontent = true;            }            redraw();        }        /**         * Cause the painter to redraw the specified annotations         */        synchronized void updateAnnotation(PDFAnnotation annot) {            if (hasAnnotations()) {                synchronized(repaintannotations) {                    repaintannotations.add(annot);                }                redraw();            } else {                throw new IllegalStateException("PagePanel has no annotation children");            }        }        private void redraw() {            wait = false;            notifyAll();        }        public void run() {            while (true) {                if (wait) {                    try { synchronized(this) { wait(); } } catch (InterruptedException e) { }                }                wait = true;                try {                    float zoom = dpi/72;                    while (repaintcontent) {                  // The page needs to be recreated                        repaintcontent = false;                        BufferedImage img;                        raisePagePanelEvent(PagePanelEvent.createPageRedrawing(PagePanel.this, tpage));                        if (tpage==null) {                            img = new BufferedImage((int)((tx2-ty1)/72*dpi), (int)((ty2-ty1)/72*dpi), BufferedImage.TYPE_INT_ARGB);                            Graphics g = img.getGraphics();                            g.setColor(new Color(0xC0C0C0));                            g.fillRect(0, 0, img.getWidth(), img.getHeight());                        } else {                            textractor = extract && tpage!=PagePanel.this.page ? parser.getPageExtractor(tpage) : null;                            tpainter.setPageExtractor(textractor);                            img = tpainter.getSubImage(tx1, ty1, tx2, ty2, tdpi, getColorModel());                        }                        if (img!=null) {                        // Image was completed                            PagePanelEvent update = PagePanelEvent.createPageRedrawn(PagePanel.this, tpage);                            PagePanelEvent show = null;                            PagePanelEvent hide = null;                            synchronized(this) {                                PagePanel.this.pageimage = img;                                PagePanel.this.x1 = tx1;                                PagePanel.this.y1 = ty1;                                PagePanel.this.x2 = tx2;                                PagePanel.this.y2 = ty2;                                PagePanel.this.dpi = tdpi;                                PagePanel.this.orientation = tpage.getPageOrientation();                                if (tpage!=PagePanel.this.page) {                                    if (tpage!=null) show = PagePanelEvent.createPageVisible(PagePanel.this, tpage);                                    if (PagePanel.this.page!=null) hide = PagePanelEvent.createPageHidden(PagePanel.this, PagePanel.this.page);                                    PagePanel.this.page = tpage;                                    PagePanel.this.extractor = textractor;                                }                            }                            zoom = dpi/72;                            setSize((int)((x2-x1)*zoom), (int)((y2-y1)*zoom));                            for (Iterator i = repaintannotations.iterator();i.hasNext();) {                                ((JComponent)annotcomponents.get(i.next())).setVisible(false);                            }                            raisePagePanelEvent(update);                            if (show!=null) raisePagePanelEvent(show);                            if (hide!=null) raisePagePanelEvent(hide);                        }                    }                    while (repaintannotations.size()>0) { // Some annotations need to be redrawn                        PDFAnnotation annot = (PDFAnnotation)repaintannotations.get(0);                        JComponent comp = (JComponent)annotcomponents.get(annot);                        if (comp!=null) {                            float[] rect = annot.getRectangle();                            int w = (int)((rect[2]-rect[0])*zoom);                            int h = (int)((rect[3]-rect[1])*zoom);                            if (w>=1 && h>=1) {                                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);                                Graphics2D g = (Graphics2D)image.getGraphics();                                if (hints!=null) g.setRenderingHints(hints);                                g.setTransform(new AffineTransform(zoom, 0, 0, -zoom, -rect[0]*zoom, rect[3]*zoom));                                if (tpainter.paintAnnotation(annot, "N", g, rect)) {                                    synchronized(repaintannotations) { repaintannotations.remove(0); }                                    comp.putClientProperty("image.N", image);                                    comp.putClientProperty("pdf.rect", rect);                                    comp.setVisible(annot.isVisible());                                    positionPDFBasedComponent(comp);                                    if (tpainter.hasAnnotationState(annot, "D")) {                                        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);                                        g = (Graphics2D)image.getGraphics();                                        if (hints!=null) g.setRenderingHints(hints);                                        g.setTransform(new AffineTransform(zoom, 0, 0, -zoom, -rect[0]*zoom, rect[3]*zoom));                                        if (tpainter.paintAnnotation(annot, "D", g, rect)) {                                            comp.putClientProperty("image.D", image);                                        }                                    }                                    if (tpainter.hasAnnotationState(annot, "R")) {                                        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);                                        g = (Graphics2D)image.getGraphics();                                        if (hints!=null) g.setRenderingHints(hints);                                        g.setTransform(new AffineTransform(zoom, 0, 0, -zoom, -rect[0]*zoom, rect[3]*zoom));                                        if (tpainter.paintAnnotation(annot, "R", g, rect)) {                                            comp.putClientProperty("image.R", image);                                        }                                    }                                    comp.repaint();                                } else {                                    comp.setVisible(false);                                }                            } else {                                synchronized(repaintannotations) { repaintannotations.remove(0); }                                comp.setVisible(false);                            }                        }                    }                    for (int i=0;i<getComponentCount();i++) {                        Component c = getComponent(i);                        if (c instanceof JComponent && ((JComponent)c).getClientProperty("pdf.rect")!=null) {                            positionPDFBasedComponent((JComponent)c);                        }                    }                    repaint();                } catch (RuntimeException e) {                    SuperJOptionPane.displayThrowable(SuperJOptionPane.getLocalizedString("Error"), e, PagePanel.this);                }            }        }    }    public void paintComponent(Graphics g) {        if (pageimage != null) {            g.drawImage(pageimage, 0, 0, this);        }    }    private class Listener implements MouseMotionListener, MouseListener {        private void raiseEvent(PagePanelInteractionEvent event) {            for (Iterator i=ilisteners.iterator();i.hasNext();) {                ((PagePanelInteractionListener)i.next()).pageAction(event);            }        }        public void mouseEntered(MouseEvent event) {            raiseEvent(PagePanelInteractionEvent.createMouseEntered(PagePanel.this, event));        }        public void mouseExited(MouseEvent event) {            raiseEvent(PagePanelInteractionEvent.createMouseExited(PagePanel.this, event));        }        public void mousePressed(MouseEvent event) {            raiseEvent(PagePanelInteractionEvent.createMousePressed(PagePanel.this, event));        }        public void mouseReleased(MouseEvent event) {            raiseEvent(PagePanelInteractionEvent.createMouseReleased(PagePanel.this, event));        }        public void mouseClicked(MouseEvent event) {            raiseEvent(PagePanelInteractionEvent.createMouseClicked(PagePanel.this, event));        }        public void mouseMoved(MouseEvent event) {            raiseEvent(PagePanelInteractionEvent.createMouseMoved(PagePanel.this, event));        }        public void mouseDragged(MouseEvent event) {            raiseEvent(PagePanelInteractionEvent.createMouseDragged(PagePanel.this, event));        }    }}

⌨️ 快捷键说明

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