thumbnailpanel.java

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

JAVA
222
字号
// $Id: ThumbnailPanel.java,v 1.8 2007/09/12 10:04:59 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.pdf2.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;/** * Creates a {@link SidePanel} that displays the page thumbnails. * The name of this feature is "ShowHideThumbnails". * <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 ThumbnailPanel extends SidePanelFactory{    private static ThumbnailPanel instance;        /**     * Return the ThumbnailPanel     */    public static ThumbnailPanel getInstance() {        if (instance==null) instance = new ThumbnailPanel();        return instance;    }    private ThumbnailPanel() {        super("ShowHideThumbnails", "Pages");    }    public SidePanel createSidePanel() {        return new ThumbnailPanelImpl();    }}/** * Represents the "thumbnails" panel on the left of the Viewer * * <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 ThumbnailPanelImpl extends JScrollPane implements DocumentPanelListener, Runnable, SidePanel{    private DocumentPanel panel;    private PDFParser parser;    private Image[] images;    private Rectangle[] locations;    private static final int HSPACER = 10, VSPACER = 20;    private boolean completed, paused;    private Thread thread;    ThumbnailPanelImpl() {        ViewPanel view = new ViewPanel();        setViewportView(view);        getVerticalScrollBar().setUnitIncrement(15);        getVerticalScrollBar().setBlockIncrement(15);        getHorizontalScrollBar().setUnitIncrement(15);        getHorizontalScrollBar().setBlockIncrement(15);        view.addMouseListener(view);        view.addMouseMotionListener(view);        paused = true;    }    public synchronized void setDocumentPanel(DocumentPanel panel) {        this.panel = panel;        PDF pdf = panel==null ? null : panel.getPDF();        if (pdf!=null) {            panel.addDocumentPanelListener(this);            this.parser = panel.getParser();            completed = false;            images = new Image[parser.getNumberOfPages()];            locations = new Rectangle[parser.getNumberOfPages()];            thread = new Thread(this);            thread.setDaemon(true);            thread.setPriority(Thread.MIN_PRIORITY+2);            thread.start();            notifyAll();        } else {            parser = null;            notifyAll();            thread.interrupt();            images = null;            locations = null;        }        repaint();    }    /**     * Called to start rendering thumbnails.     */    public synchronized void panelVisible() {        paused = false;        notifyAll();    }    public void panelHidden() {    }    /**     * Invalidate a page - this will cause the page to be redrawn     * @param pagenumber the page number to mark as "dirty"     */    public void redraw(Object o) {        if (o instanceof PDFPage && ((PDFPage)o).getPDF()==panel.getPDF()) {            int pagenumber = ((PDFPage)o).getPageNumber();            if (images!=null && pagenumber>=0 && pagenumber < images.length) {                images[pagenumber] = null;                completed = false;            }        }    }    public void documentUpdated(DocumentPanelEvent event) {        repaint();    }    public void run() {        while (parser!=null) {            try { synchronized(this) { wait(); } } catch (InterruptedException e) { }            PDFParser tempparser = parser;            if (!paused && !completed && parser!=null) {                int numpages = tempparser.getNumberOfPages();                boolean newpaint = false;                for (int i=0;parser!=null && i<numpages;i++) {                    if (images[i]==null && tempparser!=null) {                        PagePainter painter = tempparser.getPagePainter(i);                        images[i] = painter.getImage(9);                        repaint();                        newpaint = true;                    }                }                if (!newpaint) completed = true;            }        }    }    class ViewPanel extends JPanel implements MouseListener, MouseMotionListener {        ViewPanel() {            super();            setPreferredSize(new Dimension(100, 500));            setVisible(true);        }        public void paint(Graphics g) {            super.paint(g);            if (images != null) {                Point prev = new Point(0, 5);                int h = 0;                int w = 0;                int currentPage = panel.getPageNumber();                for (int i=0; i<images.length; i++) {                    if (images[i]==null) break;                    Image img = (Image)images[i];                    Point point;                    if ((prev.x + w + img.getWidth(null) + HSPACER) < getViewport().getWidth()) {                        point = new Point(prev.x + w + HSPACER, prev.y);                    } else {                        point = new Point(HSPACER, prev.y + h + VSPACER);                    }                    h = img.getHeight(null);                    w = img.getWidth(null);                    g.setColor(Color.GRAY);                    g.fillRect(point.x + 2, point.y + 2, w, h);                    g.drawImage(img, point.x, point.y, null);                    g.setColor(Color.BLACK);                    g.drawString(Integer.toString(i + 1), point.x + (w / 2), point.y + h + 15);                    g.setColor(i!=currentPage ? Color.BLACK : Color.RED);                    g.drawRect(point.x, point.y, w, h);                    locations[i] = new Rectangle(point, new Dimension(w, h));                    prev = point;                    setPreferredSize(new Dimension(100, point.y + h + VSPACER));                    revalidate();                }            }        }        public void mouseClicked(MouseEvent e) {            requestFocusInWindow();            Point select = e.getPoint();            for (int i = 0; i < locations.length; i++) {                if (locations[i] == null) break;                if (locations[i].contains(select)) {                    panel.setPageNumber(i);                    break;                }            }        }        public void mouseMoved(MouseEvent e) {            Point select = e.getPoint();            for (int i = 0; i < locations.length; i++) {                if (locations[i] == null) break;                if (locations[i].contains(select)) {                    e.getComponent().setCursor(new Cursor(Cursor.HAND_CURSOR));                    break;                } else {                    e.getComponent().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));                }            }        }        public void mousePressed(MouseEvent event) { requestFocusInWindow(); }        public void mouseReleased(MouseEvent event) { }        public void mouseEntered(MouseEvent event) { }        public void mouseExited(MouseEvent event) { }        public void mouseDragged(MouseEvent event) { }    }}

⌨️ 快捷键说明

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