/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2004 Gregor Koukkoullis ( phex <at> kouk <dot> de )
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  --- CVS Information ---
 *  $Id: DisplayPromptsPane.java,v 1.2 2004/01/18 16:59:31 gregork Exp $
 */
package phex.gui.dialogs.options;

import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.HashMap;

import javax.swing.*;

import phex.common.ServiceManager;
import phex.utils.Localizer;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.*;

public class DisplayPromptsPane extends OptionsSettingsPane
{
    private JCheckBox showCloseOptionsChkbx;
    private JCheckBox showUpdateNotificationChkbx;
    private JCheckBox showBetaUpdateNotificationChkbx;

    public DisplayPromptsPane()
    {
        super( "PromptSettings_Prompts" );
    }

    /**
     * Called when preparing this settings pane for display the first time. Can
     * be overriden to implement the look of the settings pane.
     */
    protected void prepareComponent()
    {
        setLayout( new BorderLayout() );
        
        //JPanel contentPanel = new FormDebugPanel();
        JPanel contentPanel = new JPanel();
        add( contentPanel, BorderLayout.CENTER );
        
        FormLayout layout = new FormLayout(
            "10dlu, d, 6dlu, d, 2dlu:grow", // columns
            "p, 3dlu, p, 3dlu, p" ); // 5 rows 
        layout.setRowGroups( new int[][]{{3, 5 }} );
        contentPanel.setLayout( layout );
        
        PanelBuilder builder = new PanelBuilder( contentPanel, layout );
        CellConstraints cc = new CellConstraints();
        
        builder.addSeparator( Localizer.getString( "PromptSettings_PromptSettings" ), 
            cc.xywh( 1, 1, 5, 1 ) );

        showCloseOptionsChkbx = new JCheckBox(
            Localizer.getString( "PromptSettings_ClosePhexOptions" ),
            ServiceManager.sCfg.showCloseOptionsDialog );
        showCloseOptionsChkbx.setToolTipText( Localizer.getString( 
            "PromptSettings_TTTClosePhexOptions" ) );
        builder.add( showCloseOptionsChkbx, cc.xy( 2, 3 ) );

        showUpdateNotificationChkbx = new JCheckBox(
            Localizer.getString( "PromptSettings_OnNewRelease" ),
            ServiceManager.sCfg.showUpdateNotification );
        showUpdateNotificationChkbx.setToolTipText(
            Localizer.getString( "PromptSettings_TTTOnNewRelease" ) );
        showUpdateNotificationChkbx.addActionListener(
            new ActionListener()
            {
                public void actionPerformed( ActionEvent e )
                {
                    refreshEnableState();
                }
            } );
        builder.add( showUpdateNotificationChkbx, cc.xy( 2, 5 ) );

        showBetaUpdateNotificationChkbx = new JCheckBox(
            Localizer.getString( "PromptSettings_OnNewBeta" ),
            ServiceManager.sCfg.showBetaUpdateNotification );
        showBetaUpdateNotificationChkbx.setToolTipText(
            Localizer.getString( "PromptSettings_TTTOnNewBeta" ) );
        builder.add( showBetaUpdateNotificationChkbx, cc.xy( 4, 5 ) );

        refreshEnableState();
    }

    /**
     * Override this method if you like to apply and save changes made on
     * settings pane. To trigger saving of the configuration if any value was
     * changed call triggerConfigSave().
     */
    public void saveAndApplyChanges( HashMap inputDic )
    {
        // close options
        boolean closeOptions = showCloseOptionsChkbx.isSelected();
        if ( ServiceManager.sCfg.showCloseOptionsDialog != closeOptions )
        {
            ServiceManager.sCfg.showCloseOptionsDialog = closeOptions;
            OptionsSettingsPane.triggerConfigSave( inputDic );
        }

        // update notify
        boolean updateNotify = showUpdateNotificationChkbx.isSelected();
        boolean betaUpdateNotify;
        if ( updateNotify )
        {
            betaUpdateNotify = showBetaUpdateNotificationChkbx.isSelected();
        }
        else
        {
            betaUpdateNotify = false;
        }
        if ( ServiceManager.sCfg.showUpdateNotification != updateNotify )
        {
            ServiceManager.sCfg.showUpdateNotification = updateNotify;
            OptionsSettingsPane.triggerConfigSave( inputDic );
        }
        if ( ServiceManager.sCfg.showBetaUpdateNotification != betaUpdateNotify )
        {
            ServiceManager.sCfg.showBetaUpdateNotification = betaUpdateNotify;
            OptionsSettingsPane.triggerConfigSave( inputDic );
        }
    }

    private void refreshEnableState()
    {
        boolean enableState = showUpdateNotificationChkbx.isSelected();
        showBetaUpdateNotificationChkbx.setEnabled( enableState );
    }
}