001         package com.croftsoft.core.gui.multi;
002    
003         import java.applet.*;
004         import java.awt.*;
005         import java.awt.event.*;
006         import java.io.*;
007         import java.net.*;
008         import javax.swing.*;
009         import javax.swing.event.*;
010         import javax.swing.text.*;
011         import javax.swing.text.html.*;
012    
013         import com.croftsoft.core.CroftSoftConstants;
014         import com.croftsoft.core.lang.NullArgumentException;
015         import com.croftsoft.core.jnlp.JnlpLib;
016    
017         /*********************************************************************
018         * Downloads and displays the latest news web page.
019         *
020         * <p>
021         * For a partial explanation of the source code, please see
022         * Kim Topley, <i>Core Swing Advanced Programming</i>,
023         * Chapter 4 "JEditorPane and the Swing HTML Package", 2000.
024         * </p>
025         *
026         * @version
027         *   2003-05-04
028         * @since
029         *   2002-02-27
030         * @author
031         *   <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
032         *********************************************************************/
033    
034         public final class  MultiAppletNews
035           extends JPanel
036         //////////////////////////////////////////////////////////////////////
037         //////////////////////////////////////////////////////////////////////
038         {
039    
040         private static final String  DEFAULT_NEWS_HTML
041           = "<html><body><pre>"
042           + CroftSoftConstants.DEFAULT_ATTRIBUTION_NOTICE
043           + "</pre></body></html>";
044    
045         //
046    
047         private final AppletContext  appletContext;
048    
049         private final JEditorPane    jEditorPane;
050    
051         //////////////////////////////////////////////////////////////////////
052         //////////////////////////////////////////////////////////////////////
053    
054         /*********************************************************************
055         * Main constructor.
056         *
057         * @param  applet
058         *
059         *   May be null.
060         *********************************************************************/
061         public  MultiAppletNews (
062           String  newsHTML,
063           String  newsPage,
064           Applet  applet )
065         //////////////////////////////////////////////////////////////////////
066         {
067           super ( new BorderLayout ( ) );
068    
069           AppletContext  appletContext = null;
070    
071           try
072           {
073             appletContext = applet.getAppletContext ( );
074           }
075           catch ( Exception  ex )
076           {
077           }
078    
079           this.appletContext = appletContext;
080    
081           if ( newsHTML == null )
082           {
083             if ( newsPage != null )
084             {
085               newsHTML
086                 = "<html><body>"
087                 + "Loading "
088                 + newsPage
089                 + "..."
090                 + "</body></html>";
091             }
092             else
093             {
094               newsHTML = DEFAULT_NEWS_HTML;
095             }
096           }
097    
098           jEditorPane = new JEditorPane ( "text/html", newsHTML );
099    
100           jEditorPane.setEditable ( false );
101    
102           jEditorPane.setCaretPosition ( 0 );
103    
104           jEditorPane.addHyperlinkListener (
105             new HyperlinkListener ( )
106             {
107               public void  hyperlinkUpdate ( HyperlinkEvent  hyperlinkEvent )
108               {
109                 processHyperlinkEvent ( hyperlinkEvent );
110               }
111             } );
112    
113           add ( new JScrollPane ( jEditorPane ), BorderLayout.CENTER );
114    
115           if ( newsPage != null )
116           {
117             try
118             {
119               final URL  newsURL = new URL ( newsPage );
120    
121               new Thread (
122                 new Runnable ( )
123                 {
124                   public void  run ( )
125                   {
126                     try
127                     {
128                       jEditorPane.setPage ( newsURL );
129                     }
130                     catch ( Exception  ex )
131                     {
132                       ex.printStackTrace ( );
133                     }             
134                   }
135                 } ).start ( );
136             }
137             catch ( MalformedURLException  ex )
138             {
139               ex.printStackTrace ( );
140             }
141           }
142         }
143    
144         //////////////////////////////////////////////////////////////////////
145         // private methods
146         //////////////////////////////////////////////////////////////////////
147    
148         private void  processHyperlinkEvent ( HyperlinkEvent  hyperlinkEvent )
149         //////////////////////////////////////////////////////////////////////
150         {
151           try
152           {
153             if ( hyperlinkEvent.getEventType ( )
154               == HyperlinkEvent.EventType.ACTIVATED )
155             {
156               if ( hyperlinkEvent instanceof HTMLFrameHyperlinkEvent )
157               {
158                 HTMLDocument  htmlDocument
159                   = ( HTMLDocument ) jEditorPane.getDocument ( );
160    
161                 htmlDocument.processHTMLFrameHyperlinkEvent (
162                   ( HTMLFrameHyperlinkEvent ) hyperlinkEvent );
163               }
164               else
165               {
166                 URL  url = hyperlinkEvent.getURL ( );
167    
168                 if ( appletContext != null )
169                 {
170                   appletContext.showDocument ( url, "_blank" );
171                 }             
172                 else
173                 {
174                   try
175                   {
176                     JnlpLib.showDocument ( url );
177                   }
178                   catch ( UnsupportedOperationException  ex )
179                   {
180                     jEditorPane.setPage ( url );
181                   }
182                 }
183               }
184             }
185           }
186           catch ( Exception  ex )
187           {
188             ex.printStackTrace ( );
189           }
190         }
191    
192         //////////////////////////////////////////////////////////////////////
193         //////////////////////////////////////////////////////////////////////
194         }