/*
 * PreferencesEditorDialog.java
 *
 *
 * Copyright 2006-2018 James F. Bowring, CIRDLES.org, and Earth-Time.org
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.earthtime.dialogs;

import org.earthtime.dialogs.DialogEditor;
import java.awt.Dimension;
import java.awt.Toolkit;
import org.earthtime.UPb_Redux.ReduxConstants;
import org.earthtime.UPb_Redux.user.ReduxPreferences;

/**
 *
 * @author  James F. Bowring
 */
public class PreferencesEditorDialog extends DialogEditor {
    
    private ReduxPreferences reduxPreferences = null;
  
    
    /** Creates new form SampleEditorDialog
     * @param parent 
     * @param myPrefs
     * @param modal  
     */
    public PreferencesEditorDialog(
            java.awt.Frame parent, 
            boolean modal,
            ReduxPreferences myPrefs) {
        super(parent, modal);
        
        setReduxPreferences(myPrefs);
        
        initComponents();
        
        
        this.preferencesPanel.setBackground(ReduxConstants.myFractionGreenColor);
        
        // set radio buttons
        if (reduxPreferences.isFractionDataOverriddenOnImport())
            radioYesOverride.setSelected(true);
        else
            radioNoOverride.setSelected(true);
        
   //     importLabel.setText
   //             ("<html><p>"
   //             + "When importing a fraction with the same name as an existing fraction, "
   //             + "choose whether to override non-zero values:"
   //             + "</p></html>");
        
            setSize(375, 540);
    }

    /**
     * 
     * @param width
     * @param height
     */
    @Override
    public void setSize(int width, int height) {
        super.setSize(width, height);

        //Get the screen size
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();

        //Calculate the frame location
        int x = (screenSize.width - getWidth()) / 2;
        int y = (screenSize.height - getHeight()) / 2;

        //Set the new frame location
        setLocation(x, y);
    }

    /**
     * 
     * @param size
     */
    @Override
    public void setSize(Dimension size) {
        setSize(size.width, size.height);
    }

    /**
     * 
     * @return
     */
    public ReduxPreferences getReduxPreferences() {
        return reduxPreferences;
    }

    /**
     * 
     * @param reduxPreferences
     */
    public void setReduxPreferences(ReduxPreferences reduxPreferences) {
        this.reduxPreferences = reduxPreferences;
    }
    
    /**
     * 
     */
    public void close(){
        getReduxPreferences().setFractionDataOverriddenOnImport(radioYesOverride.isSelected());
        super.close();
    }
    
    
   /* private void InitSampleFields(){
        // init input fields
        sampleName_text.setDocument(
                new UnDoAbleDocument(sampleName_text, (mySample.getFractions().size() == 0)));
        sampleName_text.setText(getMySample().getSampleName());
        
        
        // init display fields
        sampleReduxFileName_label.setText(
                "<html>"
                + getMySample().getReduxSampleFilePath()
                + "</html>");
    }
    
    private void SaveSampleData(){
        // validate fields - make this more sophisticated
        if (sampleName_text.getText().length() == 0) return;
        getMySample().setSampleName(sampleName_text.getText());
        
        
        setInitialized(true);
    }
    

    */
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupFractionImport = new javax.swing.ButtonGroup();
        preferencesPanel = new javax.swing.JPanel();
        chooseLabel = new javax.swing.JLabel();
        importLabel = new javax.swing.JLabel();
        radioNoOverride = new javax.swing.JRadioButton();
        radioYesOverride = new javax.swing.JRadioButton();
        toolBar = new javax.swing.JPanel();
        done_button = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        preferencesPanel.setBackground(new java.awt.Color(204, 255, 255));
        preferencesPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        chooseLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        chooseLabel.setText("Choose preferred behaviors:");

        importLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        importLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        importLabel.setText("<html><p>(1) When importing a fraction with the same name as an existing fraction, select yes to automatically override all values:</p></html>");
        importLabel.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        importLabel.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        radioNoOverride.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupFractionImport.add(radioNoOverride);
        radioNoOverride.setSelected(true);
        radioNoOverride.setText("no");
        radioNoOverride.setBorderPainted(true);
        radioNoOverride.setContentAreaFilled(false);
        radioNoOverride.setMargin(new java.awt.Insets(0, 0, 0, 5));

        radioYesOverride.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupFractionImport.add(radioYesOverride);
        radioYesOverride.setText("yes");
        radioYesOverride.setContentAreaFilled(false);
        radioYesOverride.setMargin(new java.awt.Insets(0, 0, 0, 5));

        org.jdesktop.layout.GroupLayout preferencesPanelLayout = new org.jdesktop.layout.GroupLayout(preferencesPanel);
        preferencesPanel.setLayout(preferencesPanelLayout);
        preferencesPanelLayout.setHorizontalGroup(
            preferencesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(preferencesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(chooseLabel)
                .addContainerGap(198, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, preferencesPanelLayout.createSequentialGroup()
                .addContainerGap(64, Short.MAX_VALUE)
                .add(preferencesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, preferencesPanelLayout.createSequentialGroup()
                        .add(radioNoOverride)
                        .add(72, 72, 72)
                        .add(radioYesOverride)
                        .add(93, 93, 93))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, preferencesPanelLayout.createSequentialGroup()
                        .add(importLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 309, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        preferencesPanelLayout.setVerticalGroup(
            preferencesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(preferencesPanelLayout.createSequentialGroup()
                .add(20, 20, 20)
                .add(chooseLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(importLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(preferencesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(radioNoOverride)
                    .add(radioYesOverride))
                .addContainerGap(356, Short.MAX_VALUE))
        );

        toolBar.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        done_button.setForeground(new java.awt.Color(255, 51, 0));
        done_button.setText("Done");
        done_button.setMargin(new java.awt.Insets(2, 1, 2, 1));
        done_button.setPreferredSize(new java.awt.Dimension(110, 23));
        done_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                done_buttonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout toolBarLayout = new org.jdesktop.layout.GroupLayout(toolBar);
        toolBar.setLayout(toolBarLayout);
        toolBarLayout.setHorizontalGroup(
            toolBarLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(toolBarLayout.createSequentialGroup()
                .addContainerGap()
                .add(done_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                .addContainerGap())
        );
        toolBarLayout.setVerticalGroup(
            toolBarLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(done_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(toolBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(preferencesPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(preferencesPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(toolBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
            
    private void done_buttonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_done_buttonActionPerformed
        //SaveSampleData();
        close();
    }//GEN-LAST:event_done_buttonActionPerformed
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupFractionImport;
    private javax.swing.JLabel chooseLabel;
    private javax.swing.JButton done_button;
    private javax.swing.JLabel importLabel;
    private javax.swing.JPanel preferencesPanel;
    private javax.swing.JRadioButton radioNoOverride;
    private javax.swing.JRadioButton radioYesOverride;
    private javax.swing.JPanel toolBar;
    // End of variables declaration//GEN-END:variables
    
}
