documentpanel.java

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

JAVA
671
字号
// $Id: DocumentPanel.java,v 1.25 2007/11/19 08:26:07 mike Exp $package org.faceless.pdf2.viewer2;import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.*;import org.faceless.pdf2.*;import java.util.*;import java.io.File;import java.util.List;import javax.print.*;import java.awt.print.*;import javax.print.attribute.*;import javax.print.attribute.standard.*;import org.faceless.pdf2.Event;/** * <p> * A <code>DocumentPanel</code> is the basic component that displays a PDF, and may be * instantiated on it's own or as part of a {@link PDFViewer}. It contains a * {@link DocumentViewport} and optionally one or more {@link SidePanel} objects on the * left, and may process {@link PDFAction}s on the PDF. * See the <a href="doc-files/tutorial.html">viewer tutorial</a> for more detail on how to use this class and the "viewer" package. * </p> * <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 DocumentPanel extends JPanel{    private PDF pdf;                            // The PDF    private JSManager jsmanager;    private PDFParser parser;                   // The parser for the PDF    private PDFViewer viewer;                   // The parent viewer - may be null    private DocumentViewport viewport;          // The viewport    private JTabbedPane tabbedpane;             // If not null, contains the SidePanels    private JSplitPane splitpane;               // If not null, contains tabbedpane and viewport    private int preferredsize, thresholdsize;   // The sizing values for splitpane    private Map panels;                         // The SidePanels, name->SidePanel    private final Collection actionhandlers;    // A collection of ActionHandler    private final Collection annotfactories;    // A collection of AnnotationComponentFactory    private final Collection panelfactories;    // A collection of ViewerFeaurte.SidePanelFactory    private final Collection listeners;         // A collection of DocumentPanelListeners    private boolean initialpageset;             // False until a valid page has been set    /**     * Create a new DocumentPanel     */    public DocumentPanel() {        super(new BorderLayout());        this.actionhandlers = new LinkedHashSet();        this.annotfactories = new LinkedHashSet();        this.panelfactories = new LinkedHashSet();        this.listeners = new LinkedHashSet();        this.panels = new LinkedHashMap();        setOpaque(true);        setBackground(Color.gray);    }    static DocumentViewport createDefaultViewport() {        DocumentViewport viewport = new SinglePageDocumentViewport();//        DocumentViewport viewport = new ContinuousDocumentViewport();        RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);        hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);        hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);        viewport.setRenderingHints(hints);        return viewport;    }    //----------------------------------------------------------------------------------    // Viewer and Viewport    /**     * Set the {@link DocumentViewport} used by this DocumentPanel.     * @param viewport the Viewport     */    public void setViewport(DocumentViewport viewport) {        if (this.viewport!=null) {            this.viewport.setDocumentPanel(null);        }        if (viewport.getDocumentPanel()!=null) {            throw new IllegalArgumentException("Viewport associated with another DocumentPanel");        }        this.viewport = viewport;        viewport.setDocumentPanel(this);        raiseDocumentPanelEvent(DocumentPanelEvent.createViewportChanged(DocumentPanel.this));        // Add listeners to handle page open/close actions and to trigger "document redrawn"        viewport.addPagePanelListener(new PagePanelListener() {            public void pageUpdated(PagePanelEvent event) {                if (event.getType()=="visible") {                    getJSManager().runEventPageOpen(DocumentPanel.this, event.getPage());                } else if (event.getType()=="hidden") {                    getJSManager().runEventPageClose(DocumentPanel.this, event.getPage());                } else if (event.getType()=="redrawn") {                    raiseDocumentPanelEvent(DocumentPanelEvent.createRedraw(DocumentPanel.this));                }            }        });        viewport.requestFocusInWindow();        if (getPDF()!=null) {            if (splitpane!=null) {                splitpane.setBottomComponent(viewport);            } else {                add(viewport, BorderLayout.CENTER);            }        }        repaint();        viewport.setFocusCycleRoot(true);    }    /**     * Return the {@link DocumentViewport} contained by this DocumentPanel     */    public DocumentViewport getViewport() {        if (viewport==null) setViewport(createDefaultViewport());        return viewport;    }    /**     * Return the JSManager object for this DocumentPanel.     * @since 2.9     */    public JSManager getJSManager() {        if (jsmanager==null) {            jsmanager = new JSManager(this);        }        return jsmanager;    }    /**     * Set the JSManager object for this DocumentPanel.     * This method should only be called if multiple DocumentPanel     * object are used in the same non-PDFViewer container.     * @since 2.9     */    public void setJSManager(JSManager jsmanager) {        this.jsmanager = jsmanager;    }    /**     * Return the {@link PDFViewer} that contains this DocumentPanel.     * Note a DocumentPanel does <i>not</i> have to be contained inside     * a PDFViewer, in which case this method will return <code>null</code>.     */    public PDFViewer getViewer() {        return viewer;    }    void setViewer(PDFViewer viewer) {        this.viewer = viewer;        setJSManager(viewer.getJSManager());    }    //--------------------------------------------------------------------------------    // Resources and settings    /**     * Control the size of the leftmost pane. The two values specify the threshold     * below which the pane is considered to be closed, and the default size of the     * pane when it's opened.     *     * @param threshold the minimum size, below which the panel is assumed to be closed     * @param preferred the default size of the leftmost pane when opened     */    public void setSidePanelSize(int threshold, int preferred) {        this.thresholdsize = Math.max(thresholdsize, splitpane.getMinimumDividerLocation());        this.preferredsize = preferred;    }    /**     * Add a {@link SidePanelFactory} to this     * <code>DocumentPanel</code>. When a PDF is set, the panels that are     * appropriate for that PDF will be created from this list of factories.     * @param panelfactory the factory     */    public void addSidePanelFactory(SidePanelFactory panelfactory) {        if (panelfactory!=null) panelfactories.add(panelfactory);    }    /**     * Add a {@link AnnotationComponentFactory} to this     * <code>DocumentPanel</code>. Any PDF's displayed by this panel will have annotations     * created by these factories.     * @param annotationfactory the factory     */    public void addAnnotationComponentFactory(AnnotationComponentFactory annotationfactory) {        if (annotationfactory!=null) annotfactories.add(annotationfactory);    }    /**     * Return the set of AnnotationFactories - called by PagePanel     */    Collection getAnnotationFactories() {        return Collections.unmodifiableCollection(annotfactories);    }    /**     * Add a {@link ActionHandler} to this <code>DocumentPanel</code>.     * Any actions passed to {@link #runAction} will by handled by this list of handlers.     * @param actionhandler the handler     */    public void addActionHandler(ActionHandler actionhandler) {        if (actionhandler!=null) actionhandlers.add(actionhandler);    }    /**     * Run the specified action on the PDF. Actions are handled by     * {@link ActionHandler}s, which should be registered     * with this class via the {@link #addActionHandler addActionHandler()} method.     * @param action the PDFAction to run.     * @return true if the action was recognised and run successfully, false otherwise.     */    public boolean runAction(PDFAction action) {        boolean success = false;        while (action!=null) {            for (Iterator i = actionhandlers.iterator();i.hasNext();) {                ActionHandler handler = (ActionHandler)i.next();                if (handler.matches(this, action)) {                    handler.run(this, action);                    success = true;                    break;                }            }            action = action.getNext();        }        return success;    }    /**     * Add a {@link DocumentPanelListener} to this DocumentPanel.     * @param listener the listener     */    public void addDocumentPanelListener(DocumentPanelListener listener) {        listeners.add(listener);    }    /**     * Remove a {@link DocumentPanelListener} from this DocumentPanel.     * @param listener the listener     */    public void removeDocumentPanelListener(DocumentPanelListener listener) {        listeners.remove(listener);    }    void raiseDocumentPanelEvent(DocumentPanelEvent event) {        PropertyManager propertymanager = getViewer()==null ? PDF.getPropertyManager() : getViewer().getPropertyManager();        if (propertymanager.getProperty("debug.Event")!=null) {            System.out.println("Raise Document "+event);        }        List l = new ArrayList(listeners);        for (Iterator i=l.iterator();i.hasNext();) {            ((DocumentPanelListener)i.next()).documentUpdated(event);        }    }    //-----------------------------------------------------------------------------    // Panels    /**     * Return a read-only map containing the {@link SidePanel} objects in use by this     * <code>DocumentPanel</code>. The map is keyed by the name of the panel and the     * values are the {@link SidePanel} objects.     */    public Map getSidePanels() {        return Collections.unmodifiableMap(panels);    }    /**     * Set the currently displayed {@link SidePanel}     * @param name the name of the SidePanel to display, or <code>null</code> to hide     * the side panel frame. If no such panel exists this method performs no action.     */    public void setSelectedSidePanel(String name) {        if (tabbedpane!=null) {            if (name==null) {                splitpane.setDividerLocation(splitpane.getMinimumDividerLocation());            } else {                Component comp = (Component)panels.get(name);                if (comp!=null) {                    tabbedpane.setSelectedComponent(comp);                    if (splitpane.getDividerLocation() < thresholdsize) {                        splitpane.setDividerLocation(preferredsize);                    }                }            }        }    }    /**     * Return the name of the currently selected {@link SidePanel}, or     * <code>null</code> if no panels are displayed.     */    public String getSelectedSidePanel() {        String name = null;        if (tabbedpane!=null) {            Component comp = tabbedpane.getSelectedComponent();            for (Iterator i = panels.entrySet().iterator();i.hasNext();) {                Map.Entry e = (Map.Entry)i.next();                if (e.getValue()==comp) {                    name = (String)e.getKey();                    break;                }            }        }        return name;    }    //-----------------------------------------------------------------------------    // setPDF    /**     * Set the PDF to be displayed by this <code>DocumentFrame</code>.     * @param pdf the PDF     */    public void setPDF(PDF pdf) {        initialpageset = false;        getViewport();        if (this.pdf!=pdf && this.pdf!=null) {            getJSManager().runEventDocWillClose(this);            raiseDocumentPanelEvent(DocumentPanelEvent.createClosing(DocumentPanel.this));        }        this.pdf = pdf;        removeAll();        if (pdf==null) {            this.parser = null;            viewport.setDocumentPanel(this);            for (Iterator i = panels.values().iterator();i.hasNext();) {                ((SidePanel)i.next()).setDocumentPanel(this);            }        } else {            this.parser = new PDFParser(pdf);            // Creation

⌨️ 快捷键说明

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