/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meteoinfo.lab.gui;

import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatIntelliJLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.themes.FlatMacLightLaf;
import com.formdev.flatlaf.themes.FlatMacDarkLaf;
import com.l2fprod.common.swing.JFontChooser;
import java.awt.Font;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.meteoinfo.console.editor.TextEditor;

/**
 *
 * @author yaqiang
 */
public class FrmSetting extends javax.swing.JDialog {
    
    private FrmMain parent;

    /**
     * Creates new form FrmSetting
     */
    public FrmSetting(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = (FrmMain)parent;
        
        initComponents();
        
        //Look & feel
        UIManager.LookAndFeelInfo[] lnfs = UIManager.getInstalledLookAndFeels();
        for (UIManager.LookAndFeelInfo lnf : lnfs) {
            this.jComboBox_LookFeel.addItem(lnf.getName());
        }
        this.jComboBox_LookFeel.addItem("FlatLightLaf");
        this.jComboBox_LookFeel.addItem("FlatDarkLaf");
        this.jComboBox_LookFeel.addItem("FlatDarculaLaf");
        this.jComboBox_LookFeel.addItem("FlatIntelliJLaf");
        this.jComboBox_LookFeel.addItem("FlatMacLightLaf");
        this.jComboBox_LookFeel.addItem("FlatMacDarkLaf");
        this.jComboBox_LookFeel.setSelectedItem(this.parent.getOptions().getLookFeel());
        
        //Decoration
        this.jCheckBox_LafDecorated.setSelected(this.parent.getOptions().isLafDecorated());
        
        //Editor font
        TextEditor textEditor = this.parent.getEditorDock().getActiveTextEditor();
        Font font = textEditor.getTextArea().getFont();
        this.jTextField_Font.setText(font.getFontName() + "  " + String.valueOf(font.getSize()));
        
        //Double buffering
        this.jCheckBox_DoubleBuffering.setSelected(this.parent.getOptions().isDoubleBuffer());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel_Appearance = new javax.swing.JPanel();
        jLabel_LookFeel = new javax.swing.JLabel();
        jComboBox_LookFeel = new javax.swing.JComboBox<>();
        jLabel_Font = new javax.swing.JLabel();
        jTextField_Font = new javax.swing.JTextField();
        jButton_Font = new javax.swing.JButton();
        jCheckBox_LafDecorated = new javax.swing.JCheckBox();
        jCheckBox_DockWindowDecorated = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jPanel_Plot = new javax.swing.JPanel();
        jCheckBox_DoubleBuffering = new javax.swing.JCheckBox();
        jButton_Apply = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Setting");

        jLabel_LookFeel.setText("Look&Feel:");

        jComboBox_LookFeel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_LookFeelActionPerformed(evt);
            }
        });

        jLabel_Font.setText("Font:");

        jTextField_Font.setEditable(false);

        jButton_Font.setText("...");
        jButton_Font.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_FontActionPerformed(evt);
            }
        });

        jCheckBox_LafDecorated.setText("Laf decorated");

        jCheckBox_DockWindowDecorated.setText("Dock window decorated");

        jLabel1.setText("(Needs restart)");

        javax.swing.GroupLayout jPanel_AppearanceLayout = new javax.swing.GroupLayout(jPanel_Appearance);
        jPanel_Appearance.setLayout(jPanel_AppearanceLayout);
        jPanel_AppearanceLayout.setHorizontalGroup(
            jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_AppearanceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel_AppearanceLayout.createSequentialGroup()
                        .addGroup(jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_LookFeel)
                            .addComponent(jLabel_Font))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addGroup(jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_AppearanceLayout.createSequentialGroup()
                                .addComponent(jTextField_Font)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton_Font, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jComboBox_LookFeel, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel_AppearanceLayout.createSequentialGroup()
                        .addGroup(jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox_LafDecorated)
                            .addComponent(jCheckBox_DockWindowDecorated))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel_AppearanceLayout.setVerticalGroup(
            jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_AppearanceLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_LookFeel)
                    .addComponent(jComboBox_LookFeel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Font)
                    .addComponent(jTextField_Font, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Font))
                .addGroup(jPanel_AppearanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel_AppearanceLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCheckBox_LafDecorated)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox_DockWindowDecorated))
                    .addGroup(jPanel_AppearanceLayout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel1)))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Appearance", jPanel_Appearance);

        jCheckBox_DoubleBuffering.setText("Double Buffering");

        javax.swing.GroupLayout jPanel_PlotLayout = new javax.swing.GroupLayout(jPanel_Plot);
        jPanel_Plot.setLayout(jPanel_PlotLayout);
        jPanel_PlotLayout.setHorizontalGroup(
            jPanel_PlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_PlotLayout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jCheckBox_DoubleBuffering, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(169, Short.MAX_VALUE))
        );
        jPanel_PlotLayout.setVerticalGroup(
            jPanel_PlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_PlotLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jCheckBox_DoubleBuffering, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(105, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Plot", jPanel_Plot);

        jButton_Apply.setText("Apply");
        jButton_Apply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ApplyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGap(119, 119, 119)
                .addComponent(jButton_Apply, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton_Apply)
                .addGap(12, 12, 12))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_ApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ApplyActionPerformed
        // TODO add your handling code here:
        
        //Double buffering
        boolean doubleBuffer = this.jCheckBox_DoubleBuffering.isSelected();
        if (doubleBuffer != this.parent.getOptions().isDoubleBuffer()) {
            this.parent.getOptions().setDoubleBuffer(doubleBuffer);
            this.parent.getFigureDock().setDoubleBuffer(doubleBuffer);
        }
        
        //Look and feel
        String laf = this.jComboBox_LookFeel.getSelectedItem().toString();
        if (!laf.equals(this.parent.getOptions().getLookFeel())) {
            String lafName = UIManager.getLookAndFeel().getClass().getName();
            switch (laf) {
                case "CDE/Motif":
                    lafName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
                    break;
                case "Metal":
                    lafName = "javax.swing.plaf.metal.MetalLookAndFeel";
                    break;
                case "Windows":
                    lafName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
                    break;
                case "Windows Classic":
                    lafName = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
                    break;
                case "Nimbus":
                    lafName = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
                    break;
                case "Mac":
                    lafName = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
                    break;
                case "GTK":
                    lafName = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
                    break;
                case "FlatLightLaf":
                case "FlatDarkLaf":
                case "FlatDarculaLaf":
                case "FlatIntelliJLaf":
                case "FlatMacLightLaf":
                case "FlatMacDarkLaf":
                    lafName = laf;
                    break;
                default:
                    lafName = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
                    break;
            }

            try {
                JFrame.setDefaultLookAndFeelDecorated(true);
                if (lafName.equals("FlatLightLaf")) {
                    UIManager.setLookAndFeel(new FlatLightLaf());
                    this.changeStyleViaThemeXml("default");
                } else if (lafName.equals("FlatIntelliJLaf")) {
                    UIManager.setLookAndFeel(new FlatIntelliJLaf());
                    this.changeStyleViaThemeXml("default");
                } else if (lafName.equals("FlatDarculaLaf")) {
                    UIManager.setLookAndFeel(new FlatDarculaLaf());
                    this.changeStyleViaThemeXml("dark");
                } else if (lafName.equals("FlatDarkLaf")) {
                    UIManager.setLookAndFeel(new FlatDarkLaf());
                    this.changeStyleViaThemeXml("dark");
                } else if (lafName.equals("FlatMacLightLaf")) {
                    UIManager.setLookAndFeel(new FlatMacLightLaf());
                    this.changeStyleViaThemeXml("default");
                } else if (lafName.equals("FlatMacDarkLaf")) {
                    UIManager.setLookAndFeel(new FlatMacDarkLaf());
                    this.changeStyleViaThemeXml("dark");
                } else {
                    UIManager.setLookAndFeel(lafName);
                    this.changeStyleViaThemeXml("default");
                }
                SwingUtilities.updateComponentTreeUI(this);
                SwingUtilities.updateComponentTreeUI(this.parent);
                this.parent.getOptions().setLookFeel(laf);
                this.parent.getConsoleDockable().setLookFeel(laf);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(FrmSetting.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(FrmSetting.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(FrmSetting.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(FrmSetting.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        //Window decorations
        this.parent.getOptions().setLafDecorated(this.jCheckBox_LafDecorated.isSelected());
    }//GEN-LAST:event_jButton_ApplyActionPerformed

    private void jComboBox_LookFeelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_LookFeelActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_jComboBox_LookFeelActionPerformed

    private void jButton_FontActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_FontActionPerformed
        // TODO add your handling code here:
        TextEditor textEditor = this.parent.getEditorDock().getActiveTextEditor();
        Font tFont = JFontChooser.showDialog(this, null, textEditor.getTextArea().getFont());
        if (tFont != null) {
            this.parent.getEditorDock().setTextFont(tFont);
            this.parent.getConsoleDockable().getConsole().setFont(tFont);
            this.parent.getOptions().setTextFont(tFont);
            this.jTextField_Font.setText(tFont.getFontName() + "  " + String.valueOf(tFont.getSize()));
        }
    }//GEN-LAST:event_jButton_FontActionPerformed

    /**
    * Changes the styles used by the editor via an XML file specification. This
    * method is preferred because of its ease and modularity.
    */
   private void changeStyleViaThemeXml(String themeName) {
      try {
         Theme theme = Theme.load(getClass().getResourceAsStream(
               "/org/fife/ui/rsyntaxtextarea/themes/" +  themeName + ".xml"));
         this.parent.getEditorDock().setTheme(theme);
         for (TextEditor textEditor : this.parent.getEditorDock().getAllTextEditor()) {
             theme.apply(textEditor.getTextArea());
             textEditor.updateTextFont();
         }
      } catch (IOException ioe) { // Never happens
         ioe.printStackTrace();
      }
   }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmSetting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmSetting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmSetting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmSetting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmSetting dialog = new FrmSetting(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Apply;
    private javax.swing.JButton jButton_Font;
    private javax.swing.JCheckBox jCheckBox_DockWindowDecorated;
    private javax.swing.JCheckBox jCheckBox_DoubleBuffering;
    private javax.swing.JCheckBox jCheckBox_LafDecorated;
    private javax.swing.JComboBox<String> jComboBox_LookFeel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel_Font;
    private javax.swing.JLabel jLabel_LookFeel;
    private javax.swing.JPanel jPanel_Appearance;
    private javax.swing.JPanel jPanel_Plot;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField_Font;
    // End of variables declaration//GEN-END:variables
}
