📄 webdialog.java
字号:
/* $RCSfile$ * $Author: egonw $ * $Date: 2007-01-04 18:26:00 +0100 (Thu, 04 Jan 2007) $ * $Revision: 7634 $ * * Copyright (C) 2002-2007 The Jmol Development Team * * Contact: jchempaint-devel@lists.sourceforge.net * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * All we ask is that proper credit is given for our work, which includes * - but is not limited to - adding the above copyright notice to the beginning * of your source code files, and to any copyright notice that you may distribute * with programs based on this work. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */package org.openscience.cdk.applications.jchempaint.dialogs;import java.awt.BorderLayout;import java.awt.Container;import java.awt.Cursor;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JEditorPane;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.SwingUtilities;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkListener;import javax.swing.text.Document;/** * Dialog to show information on the internet. The Java HTML browser is very * basic and only simple webpages can be shown. Use text only webpages whenever * possible, and certainly no frames. * * <p>Taken from the Jmol Project. * @cdk.module jchempaint */public class WebDialog extends JDialog implements HyperlinkListener { private static final long serialVersionUID = -6928369087569980743L; JEditorPane html; public WebDialog(JFrame fr, URL url) { super(fr, "JChemPaint Webbrowser", true); try { if (url != null) { html = new JEditorPane(url); } else { html = new JEditorPane("text/plain", "Unable to find url '" + url + "'."); } html.setEditable(false); html.addHyperlinkListener(this); } catch (MalformedURLException e) { System.out.println("Malformed URL: " + e); } catch (IOException e) { System.out.println("IOException: " + e); } JScrollPane scroller = new JScrollPane() { private static final long serialVersionUID = -1946954399372989440L; public Dimension getPreferredSize() { return new Dimension(800, 500); } public float getAlignmentX() { return LEFT_ALIGNMENT; } }; scroller.getViewport().add(html); JPanel htmlWrapper = new JPanel(new BorderLayout()); htmlWrapper.setAlignmentX(LEFT_ALIGNMENT); htmlWrapper.add(scroller, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); JButton ok = new JButton("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { OKPressed(); } }); buttonPanel.add(ok); getRootPane().setDefaultButton(ok); JPanel container = new JPanel(); container.setLayout(new BorderLayout()); JTextField urlDisplay = new JTextField(url.toString()); urlDisplay.setEditable(false); container.add(urlDisplay, BorderLayout.NORTH); container.add(htmlWrapper, BorderLayout.CENTER); container.add(buttonPanel, BorderLayout.SOUTH); getContentPane().add(container); pack(); centerDialog(); } public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { linkActivated(e.getURL()); } } /** * Follows the reference in an * link. The given url is the requested reference. * By default this calls <a href="#setPage">setPage</a>, * and if an exception is thrown the original previous * document is restored and a beep sounded. If an * attempt was made to follow a link, but it represented * a malformed url, this method will be called with a * null argument. * * @param u the URL to follow */ protected void linkActivated(URL u) { Cursor c = html.getCursor(); Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); html.setCursor(waitCursor); SwingUtilities.invokeLater(new PageLoader(u, c)); } /** * temporary class that loads synchronously (although later than * the request so that a cursor change can be done). */ class PageLoader implements Runnable { PageLoader(URL u, Cursor c) { url = u; cursor = c; } public void run() { if (url == null) { // restore the original cursor html.setCursor(cursor); // remove this hack when automatic validation is // activated. Container parent = html.getParent(); parent.repaint(); } else { Document doc = html.getDocument(); try { html.setPage(url); } catch (IOException ioe) { html.setDocument(doc); getToolkit().beep(); } finally { // schedule the cursor to revert after the paint // has happended. url = null; SwingUtilities.invokeLater(this); } } } URL url; Cursor cursor; } protected void centerDialog() { Dimension screenSize = this.getToolkit().getScreenSize(); Dimension size = this.getSize(); screenSize.height = screenSize.height / 2; screenSize.width = screenSize.width / 2; size.height = size.height / 2; size.width = size.width / 2; int y = screenSize.height - size.height; int x = screenSize.width - size.width; this.setLocation(x, y); } public void OKPressed() { this.setVisible(false); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -