/*
** Luxor - XML User Interface Language (XUL) Toolkit
** Copyright (c) 2001, 2002 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package luxor.core.menu;

import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import org.jdom.*;
import luxor.browser.*;
import luxor.core.*;
import luxor.online.*;
import luxor.spi.*;
import luxor.status.*;
import luxor.*;

public class DisplayUrlDef extends XulNode implements MenuItemFactory
{
   static Logger T = Logger.getLogger( DisplayUrlDef.class );

   public DisplayUrlDef( Element element )
   {
      super( element );
   }


   public JMenuItem createMenuItem()
   {

      String caption = getLabel();
      if( caption == null )
         caption = getId();

      ImageIcon icon = getIcon();

      JMenuItem mi = null;
      if( icon == null )
         mi = new JMenuItem( caption );
      else
         mi = new JMenuItem( caption, icon );

      char accessKey = getAccessKey();
      if( accessKey != 0 )
         mi.setMnemonic( accessKey );

      String tooltip = getTooltip();
      if( tooltip != null )
         mi.setToolTipText( tooltip );

      KeyStroke accelKey = getAccelKey();
      if( accelKey != null )
         mi.setAccelerator( accelKey );

      final String offline_url_string = getAttribute( Xul.Attribute.OFFLINE );
      final String url_string = getAttribute( Xul.Attribute.URL );
      final String target = getAttribute( Xul.Attribute.TARGET );

      if( url_string != null
             || offline_url_string != null )
      {

         mi.addActionListener(
            new ActionListener()
            {
               public void actionPerformed( ActionEvent ev )
               {
                  try
                  {
                     boolean is_online = OnlineManager.getOnlineManager().isOnline();

                     if( url_string != null
                            && ( is_online || offline_url_string == null ) )
                     {
                        URL url = new URL( url_string );

                        if( target != null )
                        {
                           BrowserService bs = XulManager.getXulManager().lookupBrowser( target );
                           if( bs == null )
                              Xul.error( "browser target '" + target + "' not found" );
                           else
                              bs.showDocument( url );
                        }
                        else
                           ExternalBrowser.showDocument( url );
                     }
                     else if( offline_url_string != null )
                     {
                        URL url = new URL(
                              OnlineManager.getOnlineManager().getDocumentBase()
                               + "/" + offline_url_string );

                        if( target != null )
                        {
                           BrowserService bs = XulManager.getXulManager().lookupBrowser( target );
                           if( bs == null )
                              Xul.error( "browser target '" + target + "' not found" );
                           else
                              bs.showDocument( url );
                        }
                        else
                           ExternalBrowser.showDocument( url );
                     }
                  }
                  catch( MalformedURLException mex )
                  {
                     Xul.error( "displayurl has invalid url: " + mex.toString() );
                  }
               }
            }
                );
      }
      else
      {
         Xul.error( "url attribute for displayurl element required" );
      }

      return mi;
   }
}