//This file has been modified by Nimish Jain on March 03, 2006 at 1930 Hrs IST

/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2005 Phex Development Group
 *
 *  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: GeneralUIPane.java,v 1.23 2005/10/03 00:18:26 gregork Exp $
 */
package phex.gui.dialogs.options;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;

import phex.common.ServiceManager;
import phex.gui.common.*;
import phex.gui.renderer.LAFListCellRenderer;
import phex.gui.renderer.LAFThemeListCellRenderer;
import phex.gui.tabs.search.SearchTab;
import phex.utils.Localizer;
import phex.utils.NLogger;
import phex.utils.NLoggerNames;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class GeneralUIPane extends OptionsSettingsPane {
    private static final String INSTANCIATED_LAF_KEY = "InstanciatedLAF";
    private static final String INSTANCIATED_THEME_KEY = "InstanciatedLAFTheme";
    private static final String LAF_ERROR_KEY = "LAFError";
    
    private static final String LAF_NOT_SUPPORTED = "LAFNotSupported";
    private static final String LAF_NOT_FOUND = "LAFNotFound";
    private static final String LAF_ACCESS_ERROR = "LAFAccessError";
    private static final String LAF_INSTANTIATION_ERROR = "LAFInstantiationError";
    private static final String THEME_NOT_FOUND = "ThemeNotFound";
    private static final String THEME_ACCESS_ERROR = "ThemeAccessError";
    private static final String THEME_INSTANTIATION_ERROR = "ThemeInstantiationError";
    
    
    private GUIRegistry guiRegistry;
    
    private JComboBox lafBox;
    private DefaultComboBoxModel themeModel;
    private JComboBox themeBox;
    
    private JCheckBox displayTooltipChkbx;
    private JCheckBox showTableHorizontalLinesChkbx;
    private JCheckBox showTableVerticalLinesChkbx;
    private JCheckBox minimizeWhenClosingChkbx;
    //Nimish changed scope from local to global
    private JLabel themeLabel;
    
    //Nimish
    private String oldTheme=null;
    private String FunTooshTheme;
    private String themeName;
    private String imagePath;
    private String themeColor;
    private final String LAF_THEME_FILE=System.getProperty("user.home")+"\\FunToosh\\FunToosh.laf";
    private JTextField imageBox;
    private JComboBox colorBox;
    private JButton browseImage;
    private JLabel imageLabel;
    private JLabel colorLabel;
    
    public GeneralUIPane() {
        super( "General" );
        guiRegistry = GUIRegistry.getInstance();
    }
    
    /**
     * Override this method if you like to verify inputs before storing them.
     * A input dictionary is given to the pane. It can be used to store values
     * like error flags or prepared values for saving. The dictionary is given
     * to every settings pane checkInput(), displayErrorMessage() and
     * saveAndApplyChanges() method.
     * When the input has been flaged as invalid with the method setInputValid()
     * the method displayErrorMessage() is called directly after return of
     * checkInput() and the focus is given to settings pane.
     * After checking all settings pane without any error the method
     * saveAndApplyChanges() is called for all settings panes to save the
     * changes.
     */
    public void checkInput( HashMap inputDic ) {
        // clear the reference to remove last change...
        inputDic.remove( INSTANCIATED_LAF_KEY );
        inputDic.remove( LAF_ERROR_KEY );
        inputDic.remove( INSTANCIATED_THEME_KEY );
        
        // verify if LAF is supported on plattform.
        //Nimish
        /*UIManager.LookAndFeelInfo lafInfo =
            ( UIManager.LookAndFeelInfo ) lafBox.getSelectedItem();
        try
        {
            Class lnfClass = Class.forName( lafInfo.getClassName() );
            LookAndFeel laf = (LookAndFeel)lnfClass.newInstance();
            if ( !laf.isSupportedLookAndFeel() )
            {
                inputDic.put( LAF_ERROR_KEY, LAF_NOT_SUPPORTED );
                setInputValid( inputDic, false );
                return;
            }
            // this is a valid instance... safe reference to apply it later.
            inputDic.put( INSTANCIATED_LAF_KEY, laf );
        }
        catch ( ClassNotFoundException exp )
        {
            inputDic.put( LAF_ERROR_KEY, LAF_NOT_FOUND );
            setInputValid( inputDic, false );
            return;
        }
        catch ( IllegalAccessException exp )
        {
            inputDic.put( LAF_ERROR_KEY, LAF_ACCESS_ERROR );
            setInputValid( inputDic, false );
            return;
        }
        catch ( InstantiationException exp )
        {
            inputDic.put( LAF_ERROR_KEY, LAF_INSTANTIATION_ERROR );
            setInputValid( inputDic, false );
            return;
        }
         
        LookAndFeelUtils.ThemeInfo themeInfo =
            (LookAndFeelUtils.ThemeInfo) themeBox.getSelectedItem();
        LookAndFeelUtils.ThemeInfo currentTheme = LookAndFeelUtils.getCurrentTheme(
            UIManager.getLookAndFeel().getClass().getName() );
        if ( themeInfo != null && currentTheme != null &&
            themeInfo.getClassName().equals( currentTheme.getClassName() ) )
        {// theme has not changed..
            themeInfo = null;
        }
        if ( themeInfo != null )
        {
            try
            {
                Class themeClass = Class.forName( themeInfo.getClassName() );
                Object theme = themeClass.newInstance();
                inputDic.put( INSTANCIATED_THEME_KEY, theme );
            }
            catch ( ClassNotFoundException exp )
            {
                inputDic.put( LAF_ERROR_KEY, THEME_NOT_FOUND );
                setInputValid( inputDic, false );
                return;
            }
            catch ( IllegalAccessException exp )
            {
                inputDic.put( LAF_ERROR_KEY, THEME_ACCESS_ERROR );
                setInputValid( inputDic, false );
                return;
            }
            catch ( InstantiationException exp )
            {
                inputDic.put( LAF_ERROR_KEY, THEME_INSTANTIATION_ERROR );
                setInputValid( inputDic, false );
                return;
            }
        }*/
        setInputValid( inputDic, true );
    }
    
    /**
     * When isInputValid() returns a false this method is called.
     * The input dictionary should contain the settings pane specific information
     * of the error.
     * The settings pane should override this method to display a error
     * message. Before calling the method the focus is given to the
     * settings pane.
     */
    public void displayErrorMessage( HashMap inputDic ) {
        Object value = inputDic.get( LAF_ERROR_KEY );
        if ( value != null ) {
            JOptionPane.showMessageDialog( this, Localizer.getString( (String)value ),
                    Localizer.getString( "LAFError" ), JOptionPane.ERROR_MESSAGE  );
        }
    }
    
    /**
     * 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 ) {
        // LAF
        //Nimish
        /*LookAndFeel laf = (LookAndFeel) inputDic.get( INSTANCIATED_LAF_KEY );
        if ( !UIManager.getLookAndFeel().getClass().getName().equals(
            laf.getClass().getName() ) )
        {
            try
            {
                LookAndFeelUtils.setLookAndFeel( laf );
            }
            catch (LookAndFeelFailedException exp)
            {
                JOptionPane.showMessageDialog(
                    GUIRegistry.getInstance().getMainFrame(),
                    "Error loading Look & Feel " + laf.getName(), "Error",
                    JOptionPane.ERROR_MESSAGE );
            }
        }
         
        // theme
        Object theme = inputDic.get( INSTANCIATED_THEME_KEY );
        if ( theme != null )
        {
            LookAndFeelUtils.setCurrentTheme( laf.getClass().getName(), theme );
        }*/
        
        //Nimish Save-LAF
        saveThemeVariables();
        
        // TOOLTIP
        boolean tooltip = displayTooltipChkbx.isSelected();
        if ( ServiceManager.sCfg.mUIDisplayTooltip != tooltip ) {
            ServiceManager.sCfg.mUIDisplayTooltip = tooltip;
            ToolTipManager.sharedInstance().setEnabled( tooltip );
            OptionsSettingsPane.triggerConfigSave( inputDic );
        }
        
        // minimize on close
        boolean minimizeOnClose = minimizeWhenClosingChkbx.isSelected();
        if ( ServiceManager.sCfg.minimizeToBackground != minimizeOnClose ) {
            ServiceManager.sCfg.minimizeToBackground = minimizeOnClose;
            OptionsSettingsPane.triggerConfigSave( inputDic );
        }
        
        // table lines
        boolean triggerUIUpdate = false;
        boolean showHorizontalLines = showTableHorizontalLinesChkbx.isSelected();
        if ( guiRegistry.getShowTableHorizontalLines() != showHorizontalLines ) {
            guiRegistry.setShowTableHorizontalLines( showHorizontalLines );
            triggerUIUpdate = true;
        }
        boolean showVerticalLines = showTableVerticalLinesChkbx.isSelected();
        if ( guiRegistry.getShowTableVerticalLines() != showVerticalLines ) {
            guiRegistry.setShowTableVerticalLines( showVerticalLines );
            triggerUIUpdate = true;
        }
        if ( triggerUIUpdate ) {
            GUIUtils.updateComponentsUI();
        }
        
        //Nimish
        if((! themeBox.getSelectedItem().equals(themeName)) || (!lafBox.getSelectedItem().equals(FunTooshTheme)) || (!imageBox.getText().equals(imagePath)) || (!colorBox.getSelectedItem().equals(themeColor))) {
            JOptionPane.showMessageDialog(guiRegistry.getMainFrame(),"FunToosh needs to be restarted for \ntheme changes to take place.");
        }
    }
    
    /**
     * 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() );
        //Nimish
        InitializeThemeVariables();
        
        //JPanel contentPanel = new FormDebugPanel();
        JPanel contentPanel = new JPanel();
        add( contentPanel, BorderLayout.CENTER );
        //Nimish
        FormLayout layout = new FormLayout(
                "10dlu, d, 2dlu, d, 2dlu, 60dlu", // columns
                "p, 3dlu, p, 3dlu, p, 5dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 5dlu, p, 3dlu, p, 5dlu, p " ); // 13 rows
        layout.setRowGroups( new int[][]{{3, 5, 9, 11,13,15}} );
        contentPanel.setLayout( layout );
        
        PanelBuilder builder = new PanelBuilder( layout, contentPanel );
        CellConstraints cc = new CellConstraints();
        
        builder.addSeparator( Localizer.getString( "GeneralUserInterfaceSettings" ),
                cc.xywh( 1, 1, 5, 1 ) );
        
        // L&F
        JLabel lafLabel = builder.addLabel( Localizer.getString( "LookAndFeel" ),
                cc.xy( 2, 3 ) );
        lafLabel.setToolTipText( Localizer.getString( "TTTLookAndFeel" ) );
        //Nimish
        String[] lafs=new String[2];
        lafs[0]="FunToosh Special Themes          ";
        lafs[1]="FunToosh Custom Theme            ";
        //UIManager.LookAndFeelInfo[] lafs = LookAndFeelUtils.getAvailableLAFs();
        lafBox = new JComboBox( lafs );
        lafBox.addItemListener( new LAFItemListener() );
        //Nimish
        //int currentLAFIndex = determineCurrentLAFIndex( );
        lafBox.setSelectedIndex( 0 );
        lafBox.setRenderer( new LAFListCellRenderer() );
        lafBox.setToolTipText( Localizer.getString( "TTTLookAndFeel" ) );
        builder.add( lafBox, cc.xy( 4, 3 ) );
        
        // L&F Theme
        themeLabel = builder.addLabel( Localizer.getString( "ColorTheme" ),
                cc.xy( 2, 5 ) );
        themeLabel.setToolTipText( Localizer.getString( "TTTColorTheme" ) );
        themeModel = new DefaultComboBoxModel();
        
        themeModel.addElement("Roses");
        themeModel.addElement("Pink Doll");
        themeModel.addElement("Snowy Winter");
        themeModel.addElement("The Taj Mahal");
        themeModel.addElement("FunToosh(Default)");
        themeModel.addElement("Japanese Doll");
        themeModel.addElement("Great Wall of China");
        themeModel.addElement("Animal Kingdom-Tiger");
        
        themeModel.addElement("Car Theme 1");
        themeModel.addElement("Car Theme 2");
        themeModel.addElement("Car Theme 3");
        themeModel.addElement("Car Theme 4");
        themeModel.addElement("Car Theme 5");
        
        themeModel.addElement("Spooky Theme 1");
        themeModel.addElement("Spooky Theme 2");
        themeModel.addElement("Spooky Theme 3");
        
        themeModel.addElement("Noble Thoughts 1");
        themeModel.addElement("Noble Thoughts 2");
        themeModel.addElement("Noble Thoughts 3");
        
        themeModel.addElement("Military Aircrafts 1");
        themeModel.addElement("Military Aircrafts 2");
        
        themeModel.addElement("Dream Locations 1");
        themeModel.addElement("Dream Locations 2");
        themeModel.addElement("Dream Locations 3");
        themeModel.addElement("Dream Locations 4");
        themeModel.addElement("Dream Locations 5");
        themeModel.addElement("Dream Locations 6");
        themeModel.addElement("Dream Locations 7");
        themeModel.addElement("Dream Locations 8");
        themeModel.addElement("Dream Locations 9");
        
        themeModel.addElement("Shakira Theme");
        themeModel.addElement("Brad Pitt Theme");
        themeModel.addElement("Tom Cruise Theme");
        themeModel.addElement("The Beatles Theme");
        themeModel.addElement("Angelina Jolie Theme");
        themeModel.addElement("Enrique Iglesias Theme");
        themeModel.addElement("Leonardo Di Caprio Theme");
        
        
        themeBox = new JComboBox( themeModel );
        themeBox.setToolTipText( Localizer.getString( "TTTColorTheme" ) );
        builder.add( themeBox, cc.xy( 4, 5 ) );
        //Nimish
        //LookAndFeelInfo laf = (UIManager.LookAndFeelInfo)lafBox.getSelectedItem();
        //updateThemes( laf );
        //Nimish updateThemes();
        //updateThemes();
        
        //Nimish
        imageLabel=new JLabel("Image Path");
        builder.add(imageLabel,cc.xy(2,7));
        
        imageBox=new JTextField();
        imageBox.setToolTipText("Path to background image. Click Browse to select image.");
        imageBox.setEditable(false);
        builder.add(imageBox,cc.xy(4,7));
        
        browseImage=new JButton("Browse");
        browseImage.addActionListener(new browseImageListener());
        browseImage.setToolTipText("Click to browse Image.");
        builder.add( browseImage, cc.xy( 6, 7));
        
        colorLabel=new JLabel("Choose Color");
        builder.add(colorLabel,cc.xy(2,9));
        
        
        String[] colors=new String[]{"Aqua", 
                                     "Brown",
                                     "Orange",
                                     "Purple",
                                     "SunGlare",
                                     "Raspberry",
                                     "LightAqua",
                                     "LimeGreen",
                                     "BarbyPink",
                                     "BottleGreen",
                                     "Olive",
                                     "Sepia",
                                     "SteelBlue",
                                     "Terracotta",
                                     "Ebony",
                                     "Charcoal",
                                     "DarkViolet"};
        colorBox=new JComboBox(colors);
        colorBox.setToolTipText("Select a color theme.");
        builder.add(colorBox,cc.xy(4,9));
        
        
        themeBox.setRenderer( new LAFThemeListCellRenderer() );
        
        // ToolTip
        displayTooltipChkbx = new JCheckBox(
                Localizer.getString( "DisplayTooltipText" ),
                ServiceManager.sCfg.mUIDisplayTooltip );
        displayTooltipChkbx.setToolTipText( Localizer.getString(
                "TTTDisplayTooltipText" ) );
        
        //Nimish changed the y-constraint from 7 to 11 and so on for the rest
        builder.add( displayTooltipChkbx, cc.xywh( 2, 11, 3, 1 ) );
        
        // table lines...
        showTableHorizontalLinesChkbx = new JCheckBox(
                Localizer.getString( "ShowTableHorizontalLines" ),
                guiRegistry.getShowTableHorizontalLines() );
        showTableHorizontalLinesChkbx.setToolTipText( Localizer.getString(
                "TTTShowTableHorizontalLines") );
        builder.add( showTableHorizontalLinesChkbx, cc.xywh( 2, 13, 3, 1 ) );
        
        showTableVerticalLinesChkbx = new JCheckBox(
                Localizer.getString( "ShowTableVerticalLines" ),
                guiRegistry.getShowTableVerticalLines() );
        showTableVerticalLinesChkbx.setToolTipText( Localizer.getString(
                "TTTShowTableVerticalLines") );
        builder.add( showTableVerticalLinesChkbx, cc.xywh( 2, 15, 3, 1 ) );
        
        // minimize
        String backgroundText;
        // whether we have sys tray support or not
        if ( GUIRegistry.getInstance().getDesktopIndicator() != null ) {
            backgroundText = Localizer.getString( "WhenClosingMinToSysTray" );
        } else {
            backgroundText = Localizer.getString( "WhenClosingMinToBackground" );
        }
        minimizeWhenClosingChkbx = new JCheckBox( backgroundText,
                ServiceManager.sCfg.minimizeToBackground );
        builder.add( minimizeWhenClosingChkbx, cc.xywh( 2, 17, 3, 1 ) );
        
        JButton clearSearchHistory = new JButton( Localizer.getString( "UISettings_ClearSearchHistory" ) );
        clearSearchHistory.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                MainFrame mainFrame = GUIRegistry.getInstance().getMainFrame();
                SearchTab searchTab = (SearchTab) mainFrame.getTab( MainFrame.SEARCH_TAB_ID );
                searchTab.clearSearchHistory();
            }
        });
        builder.add( clearSearchHistory, cc.xywh( 2, 19, 3, 1, "left,center" ) );
        
        
        //Nimish
        if(FunTooshTheme.contains("FunToosh Special Themes")) {
            lafBox.setSelectedIndex(0);
        } else {
            lafBox.setSelectedIndex(1);
        }
        themeBox.setSelectedItem(themeName);
        imageBox.setText(imagePath);
        colorBox.setSelectedItem(themeColor);
        
        if(lafBox.getSelectedIndex()==0) {
            themeLabel.setEnabled(true);
            themeBox.setEnabled(true);
            imageBox.setEnabled(false);
            colorBox.setEnabled(false);
            browseImage.setEnabled(false);
            imageLabel.setEnabled(false);
            colorLabel.setEnabled(false);
        } else {
            themeLabel.setEnabled(false);
            themeBox.setEnabled(false);
            imageBox.setEnabled(true);
            colorBox.setEnabled(true);
            browseImage.setEnabled(true);
            imageLabel.setEnabled(true);
            colorLabel.setEnabled(true);
        }
    }
    
    //Nimish modified this method
    private int determineCurrentLAFIndex( ) {
        
       /* return 0;
        LookAndFeel laf = UIManager.getLookAndFeel();
        String lafClassName = laf.getClass().getName();
        
        for ( int i = 0; i < lafs.length; i++ )
        {
            if ( lafClassName.equals( lafs[i].getClassName() ) )
            {
                return i;
            }
        }*/
        return -1;
    }
    
    private int determineCurrentThemeIndex( UIManager.LookAndFeelInfo lafs ) {
        LookAndFeelUtils.ThemeInfo themeInfo = LookAndFeelUtils.getCurrentTheme(
                lafs.getClassName() );
        if ( themeInfo == null ) {
            return -1;
        }
        return themeModel.getIndexOf( themeInfo );
    }
    
    //Nimish added updateThemes() now deprecated
    private void updateThemes() {
        //Nimish
        //LookAndFeelUtils.ThemeInfo[] themes = LookAndFeelUtils.getAvailableThemes(
        //   lafInfo.getClassName() );
        //Nimish
        String[] themes1=new String[11];
        themes1[0]="Roses";
        themes1[1]="Spooky";
        themes1[2]="Pink Doll";
        themes1[3]="Ultimate Car";
        themes1[4]="FunToosh(Default)";
        themes1[5]="Taj Mahal";
        themes1[6]="Celebrity Theme 1";
        themes1[7]="Celebrity Theme 2";
        themes1[8]="Celebrity Theme 3";
        themes1[9]="Celebrity Theme 4";
        themes1[10]="Celebrity Theme 5";
        
        
        themeModel = new DefaultComboBoxModel( themes1 );
        themeBox.setModel( themeModel );
        
        String theme=null;
        /*try {
            File f=new File(System.getProperty("user.home")+"\\My Documents\\FunToosh Shared\\FunToosh.laf");
            FileReader fr=new FileReader(f);
            int ch=0;
            while((ch=fr.read())!=-1) {
                theme=theme+String.valueOf((char)ch);
            }
            fr.close();
        }catch(Exception e) {
            e.printStackTrace();
            themeBox.setSelectedIndex(4);
            return;
        }*/
        themeBox.setSelectedItem((Object)theme.substring(4));
        oldTheme=theme.substring(4);
    }
    
    //Deprecated Nimish
    private void updateThemes( UIManager.LookAndFeelInfo lafInfo ) {
        //Nimish
        //LookAndFeelUtils.ThemeInfo[] themes = LookAndFeelUtils.getAvailableThemes(
        //   lafInfo.getClassName() );
        //themeModel = new DefaultComboBoxModel( themes1 );
        //themeBox.setModel( themeModel );
        //Nimish
        //int currentThemeIndex = determineCurrentThemeIndex( lafInfo );
        //themeBox.setSelectedIndex( currentThemeIndex );
    }
    
    
    //Nimish added this method to load theme
    private void InitializeThemeVariables() {
        File file=new File(LAF_THEME_FILE);
        if(file.exists()) {
            try {
                DataInputStream dis=new DataInputStream(new FileInputStream(file));
                try {
                    FunTooshTheme=dis.readUTF();
                    themeName=dis.readUTF();
                    imagePath=dis.readUTF();
                    themeColor=dis.readUTF();
                    dis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
        } else {
            FunTooshTheme="FunToosh Special Themes";
            themeName="FunToosh(Default)";
            imagePath="No Image Selected.";
            themeColor="Not Available";
        }
    }
    
    
    //Nimish added this method to save theme
    private void saveThemeVariables() {
        File file=new File(LAF_THEME_FILE);
        try{
            DataOutputStream dos=new DataOutputStream(new FileOutputStream(file));
            dos.writeUTF((String) lafBox.getSelectedItem());
            dos.writeUTF((String) themeBox.getSelectedItem());
            dos.writeUTF(imageBox.getText());
            dos.writeUTF((String) colorBox.getSelectedItem());
            dos.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    private class LAFItemListener implements ItemListener {
        public void itemStateChanged( ItemEvent e ) {
            //Nimish
            /*try {
                if ( e.getStateChange() == ItemEvent.SELECTED ) {
                    UIManager.LookAndFeelInfo lafInfo =
                            ( UIManager.LookAndFeelInfo ) e.getItem();
                    if ( lafInfo != null ) {
                        updateThemes( lafInfo );
                    }
                }
             
            } catch ( Throwable th ) {
                NLogger.error(NLoggerNames.USER_INTERFACE, th, th );
            }*/
            
            //Nimish
            if(lafBox.getSelectedIndex()==0) {
                themeLabel.setEnabled(true);
                themeBox.setEnabled(true);
                imageBox.setEnabled(false);
                colorBox.setEnabled(false);
                browseImage.setEnabled(false);
                imageLabel.setEnabled(false);
                colorLabel.setEnabled(false);
            } else {
                themeLabel.setEnabled(false);
                themeBox.setEnabled(false);
                imageBox.setEnabled(true);
                colorBox.setEnabled(true);
                browseImage.setEnabled(true);
                imageLabel.setEnabled(true);
                colorLabel.setEnabled(true);
            }
        }
    }
    
    //Nimish
    private class browseImageListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser jf;
            File tempFile=null;
            jf=new JFileChooser(imagePath);
            jf.setFileSelectionMode(JFileChooser.FILES_ONLY);
            jf.setMultiSelectionEnabled(false);
            jf.addChoosableFileFilter(new fileFilter());
            int res=jf.showOpenDialog(GUIRegistry.getInstance().getMainFrame());
            if(res==JFileChooser.APPROVE_OPTION) {
                tempFile=jf.getSelectedFile();
            }
            else{
                return;
            }
            BufferedImage image;
            try {
                image = ImageIO.read(tempFile);
            } catch (IOException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(GUIRegistry.getInstance().getMainFrame(),"This file format is unknown or not supported.","Error",JOptionPane.ERROR_MESSAGE);
                return;
            }
            if(image==null)
            {
                JOptionPane.showMessageDialog(GUIRegistry.getInstance().getMainFrame(),"This file format is unknown or not supported.","Error",JOptionPane.ERROR_MESSAGE);
                return;
            }
            int width=image.getWidth();
            int height=image.getHeight();
            
            if(width<800 && height<600)
            {
                JOptionPane.showMessageDialog(GUIRegistry.getInstance().getMainFrame(),"Image too small!!\nMust be atleast 800x600 pixels.","Error",JOptionPane.ERROR_MESSAGE);
                return;
            }
            Image image1=(Image)image;
            image1=image1.getScaledInstance(1200,900,Image.SCALE_FAST);
            image1.flush();
            
            Image img=GUIRegistry.getInstance().getMainFrame().createImage(1200,900);
            if(img==null) {
                return;
            }
            Graphics g=img.getGraphics();
            g.drawImage(image1,0,0,null);
            
            try {
                imagePath=tempFile.getParent()+"\\"+tempFile.getName().substring(0,tempFile.getName().length()-4)+"-FunToosh.jpg";
                ImageIO.write((RenderedImage)img, "jpg", new File(imagePath));
                imageBox.setText(imagePath);
            } catch (Exception ioe) {
                ioe.printStackTrace();
                return;
            }
            
            
        }
    }
    
    
    //Nimish
    private class fileFilter extends FileFilter {
        public boolean accept(File f) {
            if(f.isDirectory()) {
                return true;
            }
            
            String name=f.getName();
            
            int i=name.lastIndexOf('.');
            String ext=name.substring(i+1).toString();
            ext=ext.toLowerCase();
            if(ext.equals("jpg")) {
                return true;
            } else if(ext.equals("jpeg")) {
                return true;
            }
            return false;
        }
        
        public String getDescription() {
            return "Images (.jpg, .jpeg)";
        }
    }
}