viewerwidget.java
来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 239 行
JAVA
239 行
// $Id: ViewerWidget.java,v 1.12 2007/11/05 14:54:57 mike Exp $package org.faceless.pdf2.viewer2;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.awt.Container;import java.util.*;import org.faceless.pdf2.viewer2.feature.*;/** * A type of ViewerFeature that adds a "widget" to a {@link PDFViewer}. Widgets are typically * buttons on the toolbar, menu items and so on. * <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 ViewerWidget extends ViewerFeature{ private String toolbarname, icon, tooltip, menu; private boolean documentrequired, toolbarenabled, toolbarenabledalways, toolbarfloatable, floating; private JComponent component; private PDFViewer viewer; /** * Create a new Widget */ public ViewerWidget(String name) { super(name); setDocumentRequired(true); setToolBarEnabled(true); setToolBarFloatable(true); } public String toString() { return "Widget:"+super.toString(); } public void initialize(final PDFViewer viewer) { if (getViewer()!=null) { throw new IllegalStateException("Feature already added to another viewer"); } this.viewer = viewer; ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent evt) { action(new ViewerEvent(evt, viewer)); } }; if (toolbarname!=null) { JComponent toolbar = viewer.getToolBar(toolbarname, toolbarenabled, toolbarenabledalways, toolbarfloatable, floating); String compname = getName(); if (component==null) { JButton button; ImageIcon iconobj= new ImageIcon(PDFViewer.class.getResource(icon)); button = new JButton(iconobj); button.setToolTipText(SuperJOptionPane.getLocalizedString(tooltip)); button.setBorderPainted(false); button.setEnabled(!documentrequired); // Hack to improve GTK Look&Feel on Linux - still flawed, see bug 6349137 if (UIManager.getSystemLookAndFeelClassName().equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel")) { int width = iconobj.getIconWidth(); int height = iconobj.getIconHeight(); button.setPreferredSize(new Dimension(width+12, height+12)); } button.addActionListener(listener); component = button; compname = "Button"+compname; } if (toolbar instanceof JInternalFrame) { ((JInternalFrame)toolbar).getContentPane().add(component); ((JInternalFrame)toolbar).pack(); } else { toolbar.add(component); } viewer.putNamedComponent(compname, component); if (documentrequired) { viewer.addDocumentDependentComponent(component); } } if (menu!=null) { String[] sections = menu.split("\t"); JMenu menu = viewer.getMenu(SuperJOptionPane.getLocalizedString(sections[0])); for (int i=1;i<sections.length-1;i++) { JMenu submenu = null; String section = sections[i]; int sort = 0; int pos = section.lastIndexOf("("); try { if (!section.endsWith(")")) throw new Exception(); sort = Integer.parseInt(section.substring(pos+1, section.length()-1)); section = section.substring(0,pos); } catch (Exception e) {} section = SuperJOptionPane.getLocalizedString(section); pos = 0; for (int j=0;submenu==null && j<menu.getItemCount();j++) { int oldsort = ((Integer)menu.getItem(j).getClientProperty("bfo.sort")).intValue(); if (oldsort <= sort) pos = j + (oldsort==sort?1:0); if (menu.getItem(j).getText().equals(SuperJOptionPane.getLocalizedString(sections[i]))) { submenu = (JMenu)menu.getItem(j); } } if (submenu==null) { submenu = new JMenu(section); submenu.putClientProperty("bfo.sort", new Integer(sort)); menu.add(submenu, pos); } menu = submenu; } String section = sections[sections.length-1]; int sort = 0; int pos = section.lastIndexOf("("); try { if (!section.endsWith(")")) throw new Exception(); sort = Integer.parseInt(section.substring(pos+1, section.length()-1)); section = section.substring(0,pos); } catch (Exception e) {} section = SuperJOptionPane.getLocalizedString(section); pos=0; JMenuItem item = new JMenuItem(section); item.putClientProperty("bfo.sort", new Integer(sort)); for (int j=0;j<menu.getItemCount();j++) { int oldsort = ((Integer)menu.getItem(j).getClientProperty("bfo.sort")).intValue(); if (oldsort <= sort) pos = j + 1; } menu.add(item, pos); if (component==null) component = item; item.setEnabled(!documentrequired); item.addActionListener(listener); viewer.putNamedComponent("Menu"+getName(), item); if (documentrequired) { viewer.addDocumentDependentComponent(item); } } } /** * Get the Viewer this Feature has been added to. */ public final PDFViewer getViewer() { return viewer; } /** * Set whether this feature requires a PDF to be loaded. Most * features except for the "Open" widget do, so the default is "true" */ protected final void setDocumentRequired(boolean required) { this.documentrequired = required; } /** * Return whether this widget should be inactive if no Document is * selected. * @see #setDocumentRequired */ boolean isDocumentRequired() { return this.documentrequired; } /** * Set a custom component to be displayed in the ToolBar for this feature. * @param toolbar the name of the toolbar to put the component in * @param component the component */ protected final void setComponent(String toolbar, JComponent component) { this.toolbarname = toolbar; this.component = component; } /** * Return the component representing this Widget. * @since 2.8.5 */ public JComponent getComponent() { return component; } /** * Set this feature to use a regular button in the toolbar. The button will be * created using the specified icon and with the specified tooltip. * @param toolbar the name of the toolbar to put the component in * @param icon the URL of the icon to use * @param tooltip the tooltip to display for this button */ protected final void setButton(String toolbar, String icon, String tooltip) { this.toolbarname = toolbar; this.icon = icon; this.tooltip = tooltip; } /** * Set whether the toolbar this feature is stored in is enabled by default */ protected final void setToolBarEnabled(boolean enabled) { this.toolbarenabled = enabled; } /** * Set whether the toolbar this feature is stored in can be enabled or disabled * @see ToolbarDisabling */ protected final void setToolBarEnabledAlways(boolean always) { this.toolbarenabledalways = always; } /** * Set whether the toolbar this feature is stored in can be floated */ protected final void setToolBarFloatable(boolean floatable) { this.toolbarfloatable = floatable; } /** * Set whether this toolbar is always floating or not. Toolbars with this set * are implemented as JInternalFrame objects, and are never attached to the * regular tool bar * @since 2.8.3 */ protected final void setToolBarFloating(boolean floating) { this.floating = floating; } /** * Set a menu item for this feature. Activating the menu item is the * same as pressing the button. * @param menu the menu hierarchy to use, separated with tabs - eg "File\tOpen" */ protected final void setMenu(String menu) { this.menu = menu; } /** * The method that's run when this feature is activated */ public void action(ViewerEvent event) { }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?