001    package jgb.examples;
002    
003    import jgb.builder.WindowContext;
004    
005    import javax.swing.*;
006    import javax.swing.event.HyperlinkEvent;
007    import java.io.IOException;
008    import java.net.URL;
009    import java.util.EventObject;
010    
011    public class WebBrowserEventManager {
012        private JEditorPane view;
013        private JTextField urlField;
014    
015        public void windowOpened(EventObject event, WindowContext context) {
016            updateContext(context);
017            urlField.setText((view.getPage().toExternalForm()));
018        }
019    
020        public void go(EventObject event, WindowContext context) {
021            updateContext(context);
022            SwingUtilities.invokeLater(new Runnable() {
023                public void run() {
024                    try {
025                        view.setPage(urlField.getText());
026                    } catch (final IOException e) {
027                        SwingUtilities.invokeLater(new Runnable() {
028                            public void run() {
029                                JOptionPane.showMessageDialog(
030                                        view, e, "Error retrieving URL",
031                                        JOptionPane.ERROR_MESSAGE);
032                            }
033                        });
034                    }
035                }
036            });
037        }
038    
039        public void hyperlinkUpdate(EventObject event, WindowContext context) {
040            updateContext(context);
041    
042            HyperlinkEvent hEvent = (HyperlinkEvent)event;
043            if (hEvent.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
044                URL newUrl = hEvent.getURL();
045                urlField.setText(newUrl.toExternalForm());
046                go(event, context);
047            }
048        }
049    
050        public void quit(EventObject event, WindowContext context) {
051            System.exit(1);
052        }
053    
054        private void updateContext(WindowContext context) {
055            view = (JEditorPane)context.getObject("view");
056            urlField = (JTextField)context.getObject("url");
057        }
058    }