001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: AboutFrame.java,v $
023   Revision 1.10  2004/05/12 19:03:13  markl
024   comment block updates
025
026   Revision 1.9  2004/03/16 06:43:39  markl
027   LocaleManager method change
028
029   Revision 1.8  2003/01/19 09:48:26  markl
030   Replaced instances of JScrollPane with KScrollPane.
031
032   Revision 1.7  2001/06/26 06:15:27  markl
033   Added buildContentPanel().
034
035   Revision 1.6  2001/03/12 09:27:51  markl
036   Source code and Javadoc cleanup.
037
038   Revision 1.5  1999/11/19 06:05:13  markl
039   Fixed a NullPointerException.
040
041   Revision 1.4  1999/10/05 02:48:47  markl
042   Made Home button optional.
043
044   Revision 1.3  1999/06/28 09:37:06  markl
045   I18N.
046
047   Revision 1.2  1999/01/10 01:00:58  markl
048   added GPL header & RCS tag
049   ----------------------------------------------------------------------------
050*/
051
052package kiwi.ui;
053
054import java.awt.*;
055import java.awt.event.*;
056import java.net.URL;
057import java.io.IOException;
058import javax.swing.*;
059import javax.swing.border.EmptyBorder;
060import javax.swing.event.*;
061import javax.swing.text.Document;
062import javax.swing.text.html.HTMLEditorKit;
063
064import kiwi.util.*;
065
066/** An "About..." style window. The window displays an HTML document in a
067  * scroll pane and provides <i>Home</i> and <i>Close</i> buttons. This
068  * component is hyperlink sensitive and will follow links embedded in the
069  * documents it displays. Clicking the optional <i>Home</i> button causes the
070  * component to redisplay the initial document.
071  * <p>
072  * URLs may point to resources on the network or to system resources loaded
073  * using a <code>ResourceManager</code> or <code>ResourceLoader</code>.
074  *
075  * <p><center>
076  * <img src="snapshot/AboutFrame.gif"><br>
077  * <i>An example AboutFrame.</i>
078  * </center>
079  *
080  * @see kiwi.util.ResourceManager#getURL
081  * @see kiwi.util.ResourceLoader#getResourceAsURL
082  *
083  * @author Mark Lindner
084  */
085
086public class AboutFrame extends KFrame
087  {
088  private KButton b_ok, b_home = null;
089  private JEditorPane edit;
090  private URL startPage;
091  private String defaultTitle = null;
092  private _ActionListener actionListener;
093  private LocaleData loc, loc2;
094  private KPanel p_content;
095
096 /** Construct a new <code>AboutFrame</code> without a <i>Home</i> button.
097  *
098  * @param title The title for the window.
099  * @param source The URL of the initial HTML document to display.
100  */
101  
102  public AboutFrame(String title, URL source)
103    {
104    this(title, source, false);
105    }
106
107  /** Construct a new <code>AboutFrame</code>.
108    *
109    * @param title The title for the window.
110    * @param source The URL of the initial HTML document to display.
111    * @param hasHomeButton A flag specifying whether the dialog will be created
112    * with a <i>Home</i> button for URL navigation.
113    */
114
115  public AboutFrame(String title, URL source, boolean hasHomeButton)
116    {
117    super(title);
118
119    defaultTitle = title;
120    startPage = source;
121
122    actionListener = new _ActionListener();
123
124    loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs");
125    loc2 = LocaleManager.getDefault().getLocaleData("KiwiMisc");
126    
127    KPanel main = getMainContainer();    
128
129    main.setLayout(new BorderLayout(5, 5));
130    main.setBorder(KiwiUtils.defaultBorder);
131    
132    ButtonPanel p_buttons = new ButtonPanel();
133
134    if(hasHomeButton)
135      {
136      b_home = new KButton(loc.getMessage("kiwi.button.home"));
137      b_home.addActionListener(actionListener);
138      b_home.setEnabled(false);
139      p_buttons.addButton(b_home);
140      }
141
142    b_ok = new KButton(loc.getMessage("kiwi.button.close"));
143    b_ok.addActionListener(actionListener);
144    p_buttons.addButton(b_ok);
145
146    addWindowListener(new WindowAdapter()
147      {
148      public void windowClosing(WindowEvent evt)
149        {
150        _hide();
151        }
152      });
153
154    edit = new JEditorPane();
155
156    // the order here is IMPORTANT!
157
158    edit.setEditable(false);
159    edit.addHyperlinkListener(new HyperlinkListener()
160      {
161      public void hyperlinkUpdate(HyperlinkEvent evt)
162        {
163        if(evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
164          {
165          _showDocument(evt.getURL());
166          if(b_home != null)
167            b_home.setEnabled(true);
168          }
169        }
170      });
171    edit.setEditorKit(new HTMLEditorKit());
172    
173    KScrollPane scroll = new KScrollPane(edit);
174    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants
175      .HORIZONTAL_SCROLLBAR_NEVER);
176
177    p_content = new KPanel();
178    p_content.setLayout(new BorderLayout(5, 5));
179
180    Component content = buildContentPanel();
181    if(content != null)
182      p_content.add("South", content);
183
184    p_content.add("Center", scroll);
185    
186    main.add("Center", p_content);
187
188    main.add("South", p_buttons);
189
190    setSize(500, 600);
191    }
192
193  /** This method may be overridden to produce a component that will
194   * be added below the HTML pane in the frame. This can be used to
195   * provide additional content, in the frame. The default
196   * implementation returns <code>null</code>.
197   *
198   * @return A component to add to the frame.
199   *
200   * @since Kiwi 1.3
201   */
202
203  protected Component buildContentPanel()
204    {
205    return(null);
206    }
207  
208  /* hide the window */
209
210  private void _hide()
211    {
212    setVisible(false);
213    dispose();
214    }
215
216  /** Show or hide the window. */
217
218  public void setVisible(boolean flag)
219    {
220    if(flag)
221      _showDocument(startPage);
222
223    super.setVisible(flag);
224    }
225
226  /** show document */
227
228  private void _showDocument(URL url)
229    {
230    try
231      {
232      edit.setPage(url);
233      String docTitle = (String)edit.getDocument()
234        .getProperty(Document.TitleProperty);
235      setTitle((docTitle != null) ? docTitle : defaultTitle);
236      }
237    catch(IOException ex)
238      {
239      _showError(url);
240      }
241    }
242  
243  /* show error message */
244
245  private void _showError(URL url)
246    {
247    edit.setText(loc2.getMessage("kiwi.warning.html.doc_not_found",
248                                 url.toString()));
249    }
250
251  /* handle button events */
252  
253  private class _ActionListener implements ActionListener
254    {
255    public void actionPerformed(ActionEvent evt)
256      {
257      Object o = evt.getSource();
258      
259      if(o == b_ok)
260        _hide();
261      
262      else if((b_home != null) && (o == b_home))
263        {
264        _showDocument(startPage);
265        b_home.setEnabled(false);
266        }
267      }
268    }
269
270  }
271
272/* end of source file */