outlinepanel.java
来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 165 行
JAVA
165 行
// $Id: OutlinePanel.java,v 1.11 2007/11/05 14:54:57 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.pdf2.*;import javax.swing.*;import java.awt.event.*;import javax.swing.tree.*;import javax.swing.event.*;import java.awt.*;import java.util.*;import java.util.List;/** * Create a {@link SidePanel} that will display the document bookmarks, as returned * by {@link PDF#getBookmarks}. * The name of this feature is "ShowHideBookmarks". * <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 OutlinePanel extends SidePanelFactory{ private static OutlinePanel instance; /** * Return the OutlinePanel */ public static OutlinePanel getInstance() { if (instance==null) instance = new OutlinePanel(); return instance; } private OutlinePanel() { super("ShowHideBookmarks", "Bookmarks"); } public boolean isSidePanelRequired(PDF pdf) { return pdf.getBookmarks().size()>0; } public SidePanel createSidePanel() { return new OutlinePanelImpl(); }}/** * A {@link SidePanel} representing the "Outline" or "Bookmark" panel. * * <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 OutlinePanelImpl extends JPanel implements TreeSelectionListener, SidePanel{ private JTree outline; private DefaultMutableTreeNode root; private JScrollPane scrollpane; private DocumentPanel panel; private PDF pdf; OutlinePanelImpl() { super(new BorderLayout()); setOpaque(true); scrollpane = new JScrollPane(); add(scrollpane, BorderLayout.CENTER); } public void redraw(Object o) { } public void panelVisible() { } public void panelHidden() { } public void setDocumentPanel(DocumentPanel panel) { this.panel = panel; this.pdf = panel==null ? null : panel.getPDF(); if (pdf!=null && !pdf.getBookmarks().isEmpty()) { root = new DefaultMutableTreeNode(null) { public String toString() { return SuperJOptionPane.getLocalizedString("Bookmarks"); } }; outline = new JTree(root); outline.setFont(new Font("SansSerif", Font.PLAIN, 12)); DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() { public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { PDFBookmark bookmark = (PDFBookmark)((DefaultMutableTreeNode)value).getUserObject(); if (bookmark!=null) { Color color = bookmark.getColor(); boolean bold = bookmark.isBold(); boolean italic = bookmark.isItalic(); setTextNonSelectionColor(color==null ? Color.black : color); Font font = getFont(); setFont(font.deriveFont((bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0))); } super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); return this; } }; outline.setCellRenderer(renderer); outline.setEditable(false); outline.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); outline.addTreeSelectionListener(this); outline.addMouseMotionListener(new MouseMotionListener() { public void mouseMoved(MouseEvent e) { int row = outline.getRowForLocation(e.getX(), e.getY()); setCursor(new Cursor(row==-1 ? Cursor.DEFAULT_CURSOR : Cursor.HAND_CURSOR)); } public void mouseDragged(MouseEvent e) { } }); scrollpane.setVisible(true); ArrayList l = new ArrayList(); loadOutline(pdf.getBookmarks(), root, l); for (int i=0;i<l.size();i++) { outline.expandPath((TreePath)l.get(i)); } scrollpane.setViewportView(outline); outline.setRootVisible(true); outline.expandRow(0); } else { scrollpane.setVisible(false); scrollpane.setViewportView(null); root = null; outline = null; } repaint(); } private void loadOutline(List bookmarks, DefaultMutableTreeNode root, Collection open) { for (Iterator i = bookmarks.iterator();i.hasNext();) { final PDFBookmark bookmark = (PDFBookmark)i.next(); DefaultMutableTreeNode node = new DefaultMutableTreeNode(bookmark) { public String toString() { return bookmark.getName(); } }; root.add(node); List children = bookmark.getBookmarks(); if (children != null) { node.setAllowsChildren(true); loadOutline(children, node, open); } if (bookmark.isOpen()) { open.add(new TreePath(node.getPath())); } } } public void valueChanged(TreeSelectionEvent e) { Object o = outline.getLastSelectedPathComponent(); if (o instanceof DefaultMutableTreeNode) { o = ((DefaultMutableTreeNode)o).getUserObject(); if (o instanceof PDFBookmark) { panel.getJSManager().runEventBookmarkMouseUp(panel, ((PDFBookmark)o)); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?