jsmanager.java
来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 532 行 · 第 1/2 页
JAVA
532 行
// $Id: JSManager.java,v 1.9 2007/11/16 05:05:31 mike Exp $package org.faceless.pdf2.viewer2;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.util.*;import java.awt.event.*;import org.faceless.pdf2.*;/** * <p> * Handles the {@link Event}s - primarily JavaScript events - raised during the lifetime of * the viewer. This class will raise the PDF lifetime events defined in the * Acrobat JavaScript Scripting Reference, which may have {@link PDFAction} objects * associated with them. These are unrelated to Swing events and to the * {@link DocumentPanelEvent}, {@link PagePanelEvent} and similar. * </p><p> * We recommend developers and those working with the viewer steer well clear * of calling into this class, as the API is subject to change without notice. * </p> * @since 2.9 */public class JSManager extends JSCoreMethods{ private final JComponent root; private JSEngine engine; private JDialog dialog; private JTextArea textarea; private boolean initialized; private ArrayList initplugins; private HashMap docpanels; // Map of PDF->DocPanel, for redrawing JSManager(final JComponent root) { this.root = root; textarea = new JTextArea(); textarea.setFont(new Font("Monospaced", Font.PLAIN, 10)); textarea.setLineWrap(true); textarea.setEditable(false); textarea.setWrapStyleWord(true); textarea.setAutoscrolls(true); engine = new JSEngine(this); initplugins = new ArrayList(); docpanels = new HashMap(); } /** * Return a description of the JavaScript implementation in use */ public String getImplementationDescription() { return engine.getImplementationDescription(); } /** * Define an Object in the Global JavaScript namespace. This method is * intended for use by JavaScript plugins. * * @param key the key to bind the optional object to. May be null, in which case no binding takes place. * @param value the value if bind to the key * @param script the JavaScript to run after the object is defined - may be null */ public void defineObject(String key, Object value, String script) { synchronized(this) { if (initialized) { engine.bind(key, value, script); } else { initplugins.add(new Object[] { key, value, script}); } } } //---------------------------------------------------------------------- /** * Notify the DocumentPanel that a specific Object is suitable for redrawing. * This method is called by JavaScript when the state of an object (usually * a {@link WidgetAnnotation} or {@link FormElement} has been updated. * @param o the Object that is to be redrawn. * @see DocumentPanel#redraw */ public void notifyForRedraw(Object o) { DocumentPanel panel = null; if (o instanceof WidgetAnnotation) { panel = (DocumentPanel)docpanels.get(((WidgetAnnotation)o).getPage().getPDF()); } else if (o instanceof FormElement) { panel = (DocumentPanel)docpanels.get(((FormElement)o).getForm().getPDF()); } if (panel!=null) { panel.redraw(o); } } /** * An implementation of the <code>App.beep</code> JavaScript method */ public void appBeep() { Toolkit.getDefaultToolkit().beep(); } /** * An implementation of the <code>Console.show</code> JavaScript method */ public synchronized void consoleShow() { if (dialog==null) { Window window = JOptionPane.getFrameForComponent(root); if (window instanceof Frame) { dialog = new JDialog((Frame)window, SuperJOptionPane.getLocalizedString("JavaScriptConsole"), false); } else { dialog = new JDialog((Dialog)window, SuperJOptionPane.getLocalizedString("JavaScriptConsole"), false); } final JPanel body = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = gbc.BOTH; gbc.weighty = 1; gbc.gridwidth = gbc.REMAINDER; final JScrollPane scroll = new JScrollPane(textarea); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scroll.setPreferredSize(new Dimension(600, 300)); body.add(scroll, gbc); gbc.weightx = 1; gbc.weighty = 0; gbc.gridwidth = 1; final JTextField field = new JTextField(); field.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { String text = field.getText(); runEventConsoleExec(text); field.setText(""); ArrayList history = (ArrayList)field.getClientProperty("history"); if (history==null) { history = new ArrayList(); field.putClientProperty("history", history); } history.add(text); field.putClientProperty("historyix", null); } }); field.getActionMap().put("UpArrow", new AbstractAction() { public void actionPerformed(ActionEvent e) { ArrayList history = (ArrayList)field.getClientProperty("history"); if (history!=null) { int historyix; if (field.getClientProperty("historyix")==null) { historyix = history.size() - 1; } else { historyix = ((Integer)field.getClientProperty("historyix")).intValue() -1; } if (historyix>=0) { field.setText((String)history.get(historyix)); field.putClientProperty("historyix", new Integer(historyix)); } } } }); field.getActionMap().put("DownArrow", new AbstractAction() { public void actionPerformed(ActionEvent e) { ArrayList history = (ArrayList)field.getClientProperty("history"); if (history!=null && field.getClientProperty("historyix")!=null) { int historyix = ((Integer)field.getClientProperty("historyix")).intValue() + 1; if (historyix<history.size()) { field.setText((String)history.get(historyix)); field.putClientProperty("historyix", new Integer(historyix)); } else if (historyix==history.size()) { field.setText(""); field.putClientProperty("historyix", new Integer(historyix)); } } } }); field.getInputMap().put(KeyStroke.getKeyStroke("UP"), "UpArrow"); field.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "DownArrow"); body.add(field, gbc); gbc.weightx = 0; final JButton clearbutton = new JButton(SuperJOptionPane.getLocalizedString("Clear")); clearbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { consoleClear(); } }); body.add(clearbutton, gbc); final JButton hidebutton = new JButton(SuperJOptionPane.getLocalizedString("Hide")); hidebutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { consoleHide(); } }); body.add(hidebutton, gbc); dialog.setContentPane(body); dialog.setResizable(true); dialog.pack(); dialog.setLocationRelativeTo(root); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { consoleHide(); } }); SwingUtilities.invokeLater(new Runnable() { public void run() { dialog.setVisible(true); } }); } } /** * An implementation of the <code>Console.hide</code> JavaScript method */ public synchronized void consoleHide() { if (dialog!=null) { SwingUtilities.invokeLater(new Runnable() { public void run() { dialog.setVisible(false); dialog = null; } }); } } /** * An implementation of the <code>Console.println</code> JavaScript method */ public void consolePrintln(final String message) { SwingUtilities.invokeLater(new Runnable() { public void run() { textarea.append(message+"\n"); } }); } /** * An implementation of the <code>Console.clear</code> JavaScript method */ public void consoleClear() { SwingUtilities.invokeLater(new Runnable() { public void run() { textarea.setText(""); } }); } /** * An implementation of the <code>App.alert</code> JavaScript method */ public int appAlert(String message, int nIcon, int nType, String cTitle, Object oDoc, Object oCheckbox) { JOptionPane pane = new JOptionPane() { public int getMaxCharactersPerLineCount() { return 60; } }; pane.setMessage(message); if (nIcon==1) pane.setMessageType(JOptionPane.WARNING_MESSAGE); else if (nIcon==2) pane.setMessageType(JOptionPane.QUESTION_MESSAGE); else if (nIcon==3) pane.setMessageType(JOptionPane.INFORMATION_MESSAGE); else pane.setMessageType(JOptionPane.ERROR_MESSAGE); if (nType==1) pane.setOptionType(JOptionPane.OK_CANCEL_OPTION); else if (nType==2) pane.setOptionType(JOptionPane.YES_NO_OPTION); else if (nType==3) pane.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION); else pane.setOptionType(JOptionPane.DEFAULT_OPTION); pane.createDialog(root, cTitle==null ? "JavaScript" : cTitle).setVisible(true);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?