3
* $Date: 2007-01-04 18:26:00 +0100 (Thu, 04 Jan 2007) $
6
* Copyright (C) 2002-2007 The Jmol Development Team
8
* Contact: jchempaint-devel@lists.sourceforge.net
10
* This program is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public License
12
* as published by the Free Software Foundation; either version 2.1
13
* of the License, or (at your option) any later version.
14
* All we ask is that proper credit is given for our work, which includes
15
* - but is not limited to - adding the above copyright notice to the beginning
16
* of your source code files, and to any copyright notice that you may distribute
17
* with programs based on this work.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU Lesser General Public License for more details.
24
* You should have received a copy of the GNU Lesser General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28
package org.openscience.cdk.applications.jchempaint.dialogs;
30
import java.awt.BorderLayout;
31
import java.awt.Container;
32
import java.awt.Cursor;
33
import java.awt.Dimension;
34
import java.awt.FlowLayout;
35
import java.awt.event.ActionEvent;
36
import java.awt.event.ActionListener;
37
import java.io.IOException;
38
import java.net.MalformedURLException;
41
import javax.swing.JButton;
42
import javax.swing.JDialog;
43
import javax.swing.JEditorPane;
44
import javax.swing.JFrame;
45
import javax.swing.JPanel;
46
import javax.swing.JScrollPane;
47
import javax.swing.JTextField;
48
import javax.swing.SwingUtilities;
49
import javax.swing.event.HyperlinkEvent;
50
import javax.swing.event.HyperlinkListener;
51
import javax.swing.text.Document;
54
* Dialog to show information on the internet. The Java HTML browser is very
55
* basic and only simple webpages can be shown. Use text only webpages whenever
56
* possible, and certainly no frames.
58
* <p>Taken from the Jmol Project.
59
* @cdk.module jchempaint
61
public class WebDialog extends JDialog implements HyperlinkListener {
63
private static final long serialVersionUID = -6928369087569980743L;
67
public WebDialog(JFrame fr, URL url) {
69
super(fr, "JChemPaint Webbrowser", true);
73
html = new JEditorPane(url);
75
html = new JEditorPane("text/plain",
76
"Unable to find url '" + url + "'.");
78
html.setEditable(false);
79
html.addHyperlinkListener(this);
80
} catch (MalformedURLException e) {
81
System.out.println("Malformed URL: " + e);
82
} catch (IOException e) {
83
System.out.println("IOException: " + e);
85
JScrollPane scroller = new JScrollPane() {
87
private static final long serialVersionUID = -1946954399372989440L;
89
public Dimension getPreferredSize() {
90
return new Dimension(800, 500);
93
public float getAlignmentX() {
94
return LEFT_ALIGNMENT;
97
scroller.getViewport().add(html);
99
JPanel htmlWrapper = new JPanel(new BorderLayout());
100
htmlWrapper.setAlignmentX(LEFT_ALIGNMENT);
101
htmlWrapper.add(scroller, BorderLayout.CENTER);
103
JPanel buttonPanel = new JPanel();
104
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
107
ok.addActionListener(new ActionListener() {
109
public void actionPerformed(ActionEvent e) {
114
getRootPane().setDefaultButton(ok);
116
JPanel container = new JPanel();
117
container.setLayout(new BorderLayout());
119
JTextField urlDisplay = new JTextField(url.toString());
120
urlDisplay.setEditable(false);
122
container.add(urlDisplay, BorderLayout.NORTH);
123
container.add(htmlWrapper, BorderLayout.CENTER);
124
container.add(buttonPanel, BorderLayout.SOUTH);
126
getContentPane().add(container);
131
public void hyperlinkUpdate(HyperlinkEvent e) {
132
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
133
linkActivated(e.getURL());
138
* Follows the reference in an
139
* link. The given url is the requested reference.
140
* By default this calls <a href="#setPage">setPage</a>,
141
* and if an exception is thrown the original previous
142
* document is restored and a beep sounded. If an
143
* attempt was made to follow a link, but it represented
144
* a malformed url, this method will be called with a
147
* @param u the URL to follow
149
protected void linkActivated(URL u) {
150
Cursor c = html.getCursor();
151
Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
152
html.setCursor(waitCursor);
153
SwingUtilities.invokeLater(new PageLoader(u, c));
157
* temporary class that loads synchronously (although later than
158
* the request so that a cursor change can be done).
160
class PageLoader implements Runnable {
162
PageLoader(URL u, Cursor c) {
171
// restore the original cursor
172
html.setCursor(cursor);
174
// remove this hack when automatic validation is
176
Container parent = html.getParent();
179
Document doc = html.getDocument();
182
} catch (IOException ioe) {
183
html.setDocument(doc);
187
// schedule the cursor to revert after the paint
190
SwingUtilities.invokeLater(this);
200
protected void centerDialog() {
202
Dimension screenSize = this.getToolkit().getScreenSize();
203
Dimension size = this.getSize();
204
screenSize.height = screenSize.height / 2;
205
screenSize.width = screenSize.width / 2;
206
size.height = size.height / 2;
207
size.width = size.width / 2;
208
int y = screenSize.height - size.height;
209
int x = screenSize.width - size.width;
210
this.setLocation(x, y);
213
public void OKPressed() {
214
this.setVisible(false);