📄 pdfviewer.java
字号:
// $Id: PDFViewer.java,v 1.107 2007/11/15 01:33:26 mike Exp $package org.faceless.pdf2.viewer2;import org.faceless.pdf2.*;import org.faceless.pdf2.viewer2.feature.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import java.io.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.awt.*;import java.util.*;import java.util.Timer;import java.util.List;import java.beans.PropertyChangeEvent;import java.beans.PropertyVetoException;import java.beans.PropertyChangeListener;import javax.swing.filechooser.FileFilter;import javax.swing.plaf.metal.*;import javax.swing.plaf.*;/** * The <code>PDFViewer</code> class is a simple Swing PDF viewer application. * It demonstrates the {@link DocumentPanel} class, and can be run directly * from the JAR (via the {@link PDFTool} class) like so. * <pre> * java -jar bfopdf.jar <i>filename</i> * </pre> * The <i>filename</i> argument is optional, but if supplied will load the * specified 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><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.5, with major rewrites in 2.8 */public class PDFViewer extends JPanel{ static final ImageIcon BFO16 = new ImageIcon(PDFViewer.class.getResource("resources/BFO16.png")); private static final int MINIMIZEDWIDTH = 400; private static final int MINIMIZEDHEIGHT = 200; private JFileChooser filechooser; // Components private JDesktopPane desktop; // Used when MultiWindow is set private Map components; // Map containing all components private Map toolbars; // Map containing all the Toolbars that can be activated private Map menus; // Map containing all the Menus private Collection pdfcomponents; // Components that require a PDF to be active private Collection listeners; // Collection of DocumentPanelListeners // Other bits private final Collection features; private int internalFrameCounter = 0; private JInternalFrame activeWindow; private DocumentPanel activedocument; private KeyStoreManager keystoremanager; private PropertyManager propertymanager; private JSManager jsmanager; private LinkedHashMap aboutkeys; /** * Creates new PDFViewer with the specified features * @param features a Collection of {@link ViewerFeature} objects that are enabled * @see ViewerFeature * @since 2.7.9 */ public PDFViewer(Collection features) { this.features = Collections.unmodifiableCollection(features); setPropertyManager(new PropertyManager() { public String getProperty(String key) { try { return System.getProperty("org.faceless.pdf2.viewer2."+key); } catch (SecurityException e) { return null; } } public java.net.URL getURLProperty(String key) throws java.net.MalformedURLException { String val = getProperty(key); return val==null ? null : new java.net.URL(val); } }); setPreferredSize(new Dimension(650, 500)); setLayout(new BorderLayout()); components = new LinkedHashMap(); toolbars = new LinkedHashMap(); menus = new LinkedHashMap(); pdfcomponents = new HashSet(); listeners = new LinkedHashSet(); for (Iterator i = features.iterator();i.hasNext();) { Object o = i.next(); if (o instanceof ViewerFeature) { ((ViewerFeature)o).initialize(this); } } if (hasFeature(Toolbars.getInstance())) { JPanel toolbarPanel = new JPanel(new ToolBarLayout()); for (Iterator i = toolbars.entrySet().iterator();i.hasNext();) { Map.Entry e = (Map.Entry)i.next(); String key = (String)e.getKey(); JComponent toolbar = (JComponent)e.getValue(); if (toolbar instanceof JToolBar) { if (toolbar.getComponentCount()>0) { toolbarPanel.add(toolbar); } } else { add(toolbar); } } if (toolbarPanel.getComponentCount()>0) { toolbarPanel.setBorder(BorderFactory.createEtchedBorder()); add(toolbarPanel, BorderLayout.NORTH); } } if (hasFeature(Menus.getInstance())) { final JMenuBar menubar = new JMenuBar(); menubar.setDoubleBuffered(true); if (hasFeature(Toolbars.getInstance()) && hasFeature(ToolbarDisabling.getInstance())) { JMenuItem mToolbars = new JMenu(SuperJOptionPane.getLocalizedString("Toolbars")); for (Iterator i = toolbars.entrySet().iterator();i.hasNext();) { Map.Entry e = (Map.Entry)i.next(); final String key = (String)e.getKey(); final JComponent toolbar = (JComponent)e.getValue(); if (!key.endsWith(" ")) { final JCheckBoxMenuItem toolbaritem = new JCheckBoxMenuItem(); mToolbars.add(toolbaritem); toolbaritem.setText(SuperJOptionPane.getLocalizedString(key)); toolbaritem.setSelected(toolbar.isVisible()); toolbaritem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolbar.setVisible(toolbaritem.getState()); } }); } } getMenu(SuperJOptionPane.getLocalizedString("View")).add(mToolbars, 0); } if (hasFeature(MultiWindow.getInstance())) { JMenu menu = getMenu(SuperJOptionPane.getLocalizedString("Window")); JMenuItem mCascade = new JMenuItem(SuperJOptionPane.getLocalizedString("Cascade")); components.put("MenuCascade", mCascade); mCascade.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { cascadeFrames(); } }); menu.add(mCascade); } for (Iterator i = menus.entrySet().iterator();i.hasNext();) { Map.Entry e = (Map.Entry)i.next(); String key = (String)e.getKey(); JMenu menu = (JMenu)e.getValue(); if (menu.getItemCount()>0) { menubar.add(menu); } } if (menubar.getComponentCount()>0) { // Add the menubar to the parenet frame when the frame is set addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { if ("ancestor".equals(event.getPropertyName())) { JFrame frame = (JFrame)SwingUtilities.getAncestorOfClass(JFrame.class, PDFViewer.this); if (frame!=null) { frame.setJMenuBar(menubar); } else { JApplet applet = (JApplet)SwingUtilities.getAncestorOfClass(JApplet.class, PDFViewer.this); if (applet!=null) { applet.setJMenuBar(menubar); } } } } }); } } if (hasFeature(MultiWindow.getInstance())) { desktop = new JDesktopPane(); desktop.setBackground(Color.gray); add(desktop, BorderLayout.CENTER); addDocumentPanelListener(new DocumentPanelListener() { public void documentUpdated(DocumentPanelEvent e) { if (e.getType()=="activated" || e.getType()=="deactivated") { boolean enable = e.getType()=="activated"; for (Iterator i = pdfcomponents.iterator();i.hasNext();) { JComponent comp = (JComponent)i.next(); comp.setEnabled(enable); } } } }); } else { activedocument = new DocumentPanel(); activedocument.setViewer(this); activedocument.setFocusable(true); for (Iterator i = features.iterator();i.hasNext();) { ViewerFeature feature = (ViewerFeature)i.next(); if (feature instanceof SidePanelFactory) { activedocument.addSidePanelFactory((SidePanelFactory)feature); } else if (feature instanceof AnnotationComponentFactory) { activedocument.addAnnotationComponentFactory((AnnotationComponentFactory)feature); } else if (feature instanceof ActionHandler) { activedocument.addActionHandler((ActionHandler)feature); } } add(activedocument, BorderLayout.CENTER); } setVisible(true); filechooser = new JFileChooser((File)null); filechooser.setFileFilter(new FileFilter() { public boolean accept(File f) { return f.isDirectory() || f.getName().toLowerCase().endsWith(".pdf"); } public String getDescription() { return SuperJOptionPane.getLocalizedString("FilesPDF"); } }); // Add Drag/Drop handler to load files dragged in from Operating Systems setTransferHandler(new TransferHandler() { public boolean canImport(JComponent comp, DataFlavor[] flavors) { return Arrays.asList(flavors).contains(DataFlavor.javaFileListFlavor); } public boolean importData(JComponent comp, Transferable tran) { try { if (canImport(comp, tran.getTransferDataFlavors())) { List list = (List)tran.getTransferData(DataFlavor.javaFileListFlavor); for (int i=0;i<list.size();i++) { loadPDF((File)list.get(i)); } } return true; } catch (Exception e) { e.printStackTrace(); return false; } } }); } /** * Return the JSManager object for this PDFViewer. * @since 2.9 */ public JSManager getJSManager() { if (jsmanager==null) { // Create a JSManager and initialize it the moment this object is displayed for the first time. jsmanager = new JSManager(this); if (isShowing()) { jsmanager.runEventAppInit(); } else { addHierarchyListener(new HierarchyListener() { public void hierarchyChanged(HierarchyEvent event) { if ((event.getChangeFlags()&event.SHOWING_CHANGED)!=0 && isShowing()) { removeHierarchyListener(this); jsmanager.runEventAppInit(); } } }); } } return jsmanager; } /** * Set the {@link PropertyManager} in use by this PDFViewer * @since 2.8.5 */ public final void setPropertyManager(PropertyManager manager) { if (manager==null) throw new NullPointerException(); this.propertymanager = manager; } /** * Get the {@link PropertyManager} in use by this PDFViewer * @since 2.8.5 */ public final PropertyManager getPropertyManager() { return this.propertymanager; } /** * Get the {@link KeyStoreManager} in use by this PDFViewer * @since 2.8.3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -