~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/applications/jchempaint/dialogs/WebDialog.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $RCSfile$
 
2
 * $Author: egonw $
 
3
 * $Date: 2007-01-04 18:26:00 +0100 (Thu, 04 Jan 2007) $
 
4
 * $Revision: 7634 $
 
5
 *
 
6
 * Copyright (C) 2002-2007  The Jmol Development Team
 
7
 *
 
8
 * Contact: jchempaint-devel@lists.sourceforge.net
 
9
 *
 
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.
 
18
 * 
 
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.
 
23
 * 
 
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.
 
27
 */
 
28
package org.openscience.cdk.applications.jchempaint.dialogs;
 
29
 
 
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;
 
39
import java.net.URL;
 
40
 
 
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;
 
52
 
 
53
/**
 
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.
 
57
 *
 
58
 * <p>Taken from the Jmol Project.
 
59
 * @cdk.module jchempaint
 
60
 */
 
61
public class WebDialog extends JDialog implements HyperlinkListener {
 
62
 
 
63
        private static final long serialVersionUID = -6928369087569980743L;
 
64
        
 
65
        JEditorPane html;
 
66
    
 
67
    public WebDialog(JFrame fr, URL url) {
 
68
        
 
69
        super(fr, "JChemPaint Webbrowser", true);
 
70
        
 
71
        try {
 
72
            if (url != null) {
 
73
                html = new JEditorPane(url);
 
74
            } else {
 
75
                html = new JEditorPane("text/plain",
 
76
                "Unable to find url '" + url + "'.");
 
77
            }
 
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);
 
84
        }
 
85
        JScrollPane scroller = new JScrollPane() {
 
86
 
 
87
                        private static final long serialVersionUID = -1946954399372989440L;
 
88
 
 
89
                        public Dimension getPreferredSize() {
 
90
                return new Dimension(800, 500);
 
91
            }
 
92
            
 
93
            public float getAlignmentX() {
 
94
                return LEFT_ALIGNMENT;
 
95
            }
 
96
        };
 
97
        scroller.getViewport().add(html);
 
98
        
 
99
        JPanel htmlWrapper = new JPanel(new BorderLayout());
 
100
        htmlWrapper.setAlignmentX(LEFT_ALIGNMENT);
 
101
        htmlWrapper.add(scroller, BorderLayout.CENTER);
 
102
        
 
103
        JPanel buttonPanel = new JPanel();
 
104
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
 
105
        JButton ok =
 
106
        new JButton("OK");
 
107
        ok.addActionListener(new ActionListener() {
 
108
            
 
109
            public void actionPerformed(ActionEvent e) {
 
110
                OKPressed();
 
111
            }
 
112
        });
 
113
        buttonPanel.add(ok);
 
114
        getRootPane().setDefaultButton(ok);
 
115
        
 
116
        JPanel container = new JPanel();
 
117
        container.setLayout(new BorderLayout());
 
118
        
 
119
        JTextField urlDisplay = new JTextField(url.toString());
 
120
        urlDisplay.setEditable(false);
 
121
        
 
122
        container.add(urlDisplay, BorderLayout.NORTH);
 
123
        container.add(htmlWrapper, BorderLayout.CENTER);
 
124
        container.add(buttonPanel, BorderLayout.SOUTH);
 
125
        
 
126
        getContentPane().add(container);
 
127
        pack();
 
128
        centerDialog();
 
129
    }
 
130
 
 
131
    public void hyperlinkUpdate(HyperlinkEvent e) {
 
132
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
 
133
            linkActivated(e.getURL());
 
134
        }
 
135
    }
 
136
    
 
137
    /**
 
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
 
145
    * null argument.
 
146
    *
 
147
    * @param u the URL to follow
 
148
    */
 
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));
 
154
    }
 
155
    
 
156
    /**
 
157
    * temporary class that loads synchronously (although later than
 
158
    * the request so that a cursor change can be done).
 
159
    */
 
160
    class PageLoader implements Runnable {
 
161
        
 
162
        PageLoader(URL u, Cursor c) {
 
163
            url = u;
 
164
            cursor = c;
 
165
        }
 
166
        
 
167
        public void run() {
 
168
            
 
169
            if (url == null) {
 
170
                
 
171
                // restore the original cursor
 
172
                html.setCursor(cursor);
 
173
                
 
174
                // remove this hack when automatic validation is
 
175
                // activated.
 
176
                Container parent = html.getParent();
 
177
                parent.repaint();
 
178
            } else {
 
179
                Document doc = html.getDocument();
 
180
                try {
 
181
                    html.setPage(url);
 
182
                } catch (IOException ioe) {
 
183
                    html.setDocument(doc);
 
184
                    getToolkit().beep();
 
185
                } finally {
 
186
                    
 
187
                    // schedule the cursor to revert after the paint
 
188
                    // has happended.
 
189
                    url = null;
 
190
                    SwingUtilities.invokeLater(this);
 
191
                }
 
192
            }
 
193
        }
 
194
        
 
195
        URL url;
 
196
        Cursor cursor;
 
197
    }
 
198
    
 
199
    
 
200
    protected void centerDialog() {
 
201
        
 
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);
 
211
    }
 
212
    
 
213
    public void OKPressed() {
 
214
        this.setVisible(false);
 
215
    }
 
216
    
 
217
}