formsubmitactionhandler.java
来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 380 行 · 第 1/2 页
JAVA
380 行
// $Id: FormSubmitActionHandler.java,v 1.6 2007/11/05 14:54:57 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.util.Base64OutputStream;import org.faceless.pdf2.*;import java.util.*;import java.net.*;import java.io.*;import java.awt.event.*;import java.awt.*;import javax.swing.*;/** * Create a handler to handler "FormSubmit" actions. * Currently only HTML form submission is supported. * The name of this feature is "FormSubmitActionHandler". * <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.3 */public class FormSubmitActionHandler extends ActionHandler{ private static FormSubmitActionHandler instance; /** * Return the FormSubmitActionHandler */ public static FormSubmitActionHandler getInstance() { if (instance==null) instance = new FormSubmitActionHandler(); return instance; } private FormSubmitActionHandler() { super("FormSubmitActionHandler"); } public boolean matches(DocumentPanel panel, PDFAction action) { return action.getType().equals("FormSubmit"); } public void run(final DocumentPanel docpanel, final PDFAction action) { new FormSubmitter(docpanel, action).start(); }}/** * Actually run the form submission. */class FormSubmitter extends Thread { private static final int OK=0, DONE=1, CANCELLED=-1; private final DocumentPanel docpanel; private final PDFAction action; private int state; FormSubmitter(DocumentPanel docpanel, PDFAction action) { this.docpanel = docpanel; this.action = action; } /** * An EventQueue that blocks any MouseClicked/Pressed events on anything other * than the specified Window */ private class MyDialogEventQueue extends EventQueue { private final Window dialog; MyDialogEventQueue(Window dialog) { this.dialog = dialog; } public void dispatchEvent(AWTEvent event) { if (event.getID()==MouseEvent.MOUSE_CLICKED || event.getID()==MouseEvent.MOUSE_PRESSED) { Component root = SwingUtilities.getRoot((Component)event.getSource()); if (root!=dialog) { Toolkit.getDefaultToolkit().beep(); } else { super.dispatchEvent(event); } } else { super.dispatchEvent(event); } } public void pop() { super.pop(); } } public void run() { try { this.state = OK; Collection fields = action.getFormSubmitFields(); int flags = action.getFormSubmitFlags(); URL url = new URL(action.getURL()); // 1. Create the Dialog final JDialog dialog = new JDialog(JOptionPane.getFrameForComponent(docpanel), SuperJOptionPane.getLocalizedString("Submit")) { public void setVisible(boolean visible) { if (visible!=isVisible()) { if (visible) { Toolkit.getDefaultToolkit().getSystemEventQueue().push(new MyDialogEventQueue(this)); } else { ((MyDialogEventQueue)Toolkit.getDefaultToolkit().getSystemEventQueue()).pop(); } super.setVisible(visible); } } }; dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); dialog.setUndecorated(true); dialog.setLocationRelativeTo(docpanel); JPanel contentpane = new JPanel(new BorderLayout()); contentpane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createEmptyBorder(5,5,5,5))); contentpane.add(new JLabel(SuperJOptionPane.getLocalizedString("SubmittingTo", url.toString())), BorderLayout.CENTER); JButton cancelbutton = new JButton(SuperJOptionPane.getLocalizedString("Cancel")); cancelbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { cancel(); dialog.setVisible(false); dialog.dispose(); } }); JPanel closepanel = new JPanel(); closepanel.add(cancelbutton); contentpane.add(closepanel, BorderLayout.PAGE_END); contentpane.setOpaque(true); contentpane.setVisible(true); dialog.setContentPane(contentpane); dialog.validate(); dialog.pack(); // Run the submission. When done, if we created an FDF then import it. try { dialog.setVisible(true); Object response = submit(url, fields, flags, cancelbutton); if (response!=null && response instanceof FDF) { docpanel.getPDF().importFDF((FDF)response); docpanel.redraw(docpanel.getPDF()); } } finally { SwingUtilities.invokeLater(new Runnable() { public void run() { dialog.setVisible(false); dialog.dispose(); } }); } } catch (Throwable e) { if (e instanceof SyncFailedException) { // Not really sync failed, HTTP protocol error JOptionPane.showMessageDialog(docpanel.getViewer(), e.getMessage(), SuperJOptionPane.getLocalizedString("Error"), JOptionPane.ERROR_MESSAGE); } else { SuperJOptionPane.displayThrowable(SuperJOptionPane.getLocalizedString("Error"), e, docpanel); } } } /** * Indicate we want to cancel the submission at the earliest opportunity */ void cancel() { state = CANCELLED; } //--------------------------------------------------------------------------------- /** * Actually do the submission. Create the content, submit it to * the URL, wait for the response and if it's an FDF, create the * FDF and return it. Theoretically we could return other types * of content here too. * * This method monitors the "state" variable to see if it's been * cancelled. */ private Object submit(URL url, Collection fields, int flags, JButton cancelbutton) throws IOException { boolean post = true; if ((flags&(1<<2))!=0 && (flags&(1<<3))!=0) { // HTML GET boolean coords = (flags&(1<<4))!=0; boolean canonicalformat = (flags&(1<<9))!=0; url = createGetURL(url, fields, coords, canonicalformat); post = false; } HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setUseCaches(false); con.setInstanceFollowRedirects(true); if (url.getUserInfo()!=null) { con.setRequestProperty("Authorization", "Basic "+Base64OutputStream.encode(url.getUserInfo().getBytes("ISO-8859-1"))); } if (post) { con.setRequestMethod("POST");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?