/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2005 Phex Development Group
 *  Copyright (C) 2001 - 2006 This file - Nimish
 *
 *  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
 * 
 */
package phex.gui.dialogs;

import phex.gui.common.GUIRegistry;

/**
 *
 * @author  manu
 */
public class MProfilerDialog extends javax.swing.JDialog {
    int temp_optimize;
    int temp_free;
    
    /**
     * Creates new form MProfilerDialog
     */
    public MProfilerDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** 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() {
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        memoryStatusBar = new javax.swing.JProgressBar();
        jLabel2 = new javax.swing.JLabel();
        optimizeSlider = new javax.swing.JSlider();
        selectSlider = new javax.swing.JSlider();
        jLabel4 = new javax.swing.JLabel();
        maxMemory1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        maxMemory2 = new javax.swing.JLabel();
        profilerEnableBox = new javax.swing.JCheckBox();
        optimizeButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        memoryStatusLabel = new javax.swing.JLabel();
        optimizeDynamic = new javax.swing.JLabel();
        freeDynamic = new javax.swing.JLabel();

        setTitle("Memory Profiler");
        setLocationByPlatform(true);
        setModal(true);
        setName("ProfilerDialog");
        setResizable(false);
        jPanel2.setToolTipText("");
        jLabel1.setText("Optimize Memory when falls Below:");

        memoryStatusBar.setToolTipText("Shows the current amount of free Memory.");

        jLabel2.setText("Additional Memory to free:");
        jLabel2.setToolTipText("FunToosh will try to free this much amount of Additional Memory.");

        optimizeSlider.setToolTipText("The Minimum Memory before the Optimization takes place.");
        optimizeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                optimizeSliderStateChanged(evt);
            }
        });

        selectSlider.setToolTipText("FunToosh will try to free this much amount of Additional Memory.");
        selectSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                selectSliderStateChanged(evt);
            }
        });

        jLabel4.setText("0 MB");

        maxMemory1.setText("1024 MB");

        jLabel6.setText("0 MB");

        maxMemory2.setText("1024 MB");

        profilerEnableBox.setSelected(true);
        profilerEnableBox.setText("Enable Memory Profiler");
        profilerEnableBox.setToolTipText("Enable the Memory Profiler.(Recommended)");
        profilerEnableBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        profilerEnableBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        profilerEnableBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                profilerEnableBoxItemStateChanged(evt);
            }
        });

        optimizeButton.setText("Optimize Now");
        optimizeButton.setToolTipText("Click to Optimize Memory NOW.");
        optimizeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optimizeButtonActionPerformed(evt);
            }
        });

        okButton.setText("Cancel");
        okButton.setToolTipText("Click to Cancel any changes.");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("OK");
        cancelButton.setToolTipText("Click to Save changes.");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        memoryStatusLabel.setText("Current Free Memory Status:");

        optimizeDynamic.setText("0");

        freeDynamic.setText("0");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(12, 12, 12)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 392, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(optimizeSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
                            .add(selectSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                                .add(6, 6, 6)
                                .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 243, Short.MAX_VALUE)
                                .add(optimizeDynamic, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 62, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(133, 133, 133)
                                .add(maxMemory1))
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(6, 6, 6)
                                .add(jLabel6)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 265, Short.MAX_VALUE)
                                .add(freeDynamic, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(119, 119, 119)
                                .add(maxMemory2))))
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(profilerEnableBox)
                        .add(32, 32, 32)
                        .add(optimizeButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(okButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancelButton))
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(memoryStatusLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(memoryStatusBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(optimizeSlider, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel4)
                    .add(optimizeDynamic)
                    .add(maxMemory1))
                .add(9, 9, 9)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(selectSlider, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel6)
                            .add(freeDynamic))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(optimizeButton)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(profilerEnableBox)
                                    .add(10, 10, 10)))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, cancelButton)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, okButton)))
                    .add(maxMemory2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 86, Short.MAX_VALUE)
                .add(memoryStatusLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(memoryStatusBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    public int getMaxMemory1()
    {
        try
        {
            return Integer.parseInt(maxMemory1.getText());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }
    
    public void setMaxMemory1(int value)
    {
        maxMemory1.setText(String.valueOf(value)+" MB");
    }
    
    public int getMaxMemory2()
    {
        try
        {
            return Integer.parseInt(maxMemory2.getText());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }
    
    public void setMaxMemory2(int value)
    {
        maxMemory2.setText(String.valueOf(value)+" MB");
    }
    
    public int getselectSlider()
    {
        try
        {
            return selectSlider.getValue();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }
    
    public void setselectSliderMAXVALUE(int value)
    {
        selectSlider.setMaximum(value);
    }
    
    public void setselectSlider(int value)
    {
        selectSlider.setValue(value);
    }
    
    public void setmemoryStatusBarMAXVALUE(int value)
    {
        memoryStatusBar.setMaximum(value);
    }
    
    public void setmemoryStatusBar(int value)
    {
        memoryStatusBar.setValue(value);
    }
    
    public int getoptimizeSlider()
    {
        try
        {
            return optimizeSlider.getValue();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }
    
    public void setoptimizeSliderMAXVALUE(int value)
    {
        optimizeSlider.setMaximum(value);
    }
    
    public void setoptimizeSlider(int value)
    {
        optimizeSlider.setValue(value);
    }
    
    public boolean isMemoryProfilerEnabled()
    {
        return profilerEnableBox.isSelected();
    }
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
// TODO add your handling code here:
        //cancelActionPerformed is actually Button OK
        GUIRegistry.getInstance().getMProfiler().optimizeMemory=temp_optimize;
        GUIRegistry.getInstance().getMProfiler().memToFree=temp_free;
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
// TODO add your handling code here:
        //okActionPerformed is actually Button CANCEL
        this.setVisible(false);
        temp_optimize=0;
        temp_free=0;
        
    }//GEN-LAST:event_okButtonActionPerformed

    private void profilerEnableBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_profilerEnableBoxItemStateChanged
// TODO add your handling code here:
    }//GEN-LAST:event_profilerEnableBoxItemStateChanged

    private void selectSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_selectSliderStateChanged
// TODO add your handling code here:
        temp_free=selectSlider.getValue();
        freeDynamic.setText(String.valueOf(temp_free)+" MB");
        
    }//GEN-LAST:event_selectSliderStateChanged

    private void optimizeSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_optimizeSliderStateChanged
// TODO add your handling code here:
        temp_optimize=optimizeSlider.getValue();
        optimizeDynamic.setText(String.valueOf(temp_optimize)+" MB");
    }//GEN-LAST:event_optimizeSliderStateChanged

    private void optimizeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optimizeButtonActionPerformed
// TODO add your handling code here:
        GUIRegistry.getInstance().getMProfiler().optimizeMemory=temp_optimize;
        GUIRegistry.getInstance().getMProfiler().memToFree=temp_free;
        GUIRegistry.getInstance().getMProfiler().optimizeMemory();
    }//GEN-LAST:event_optimizeButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel freeDynamic;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel maxMemory1;
    private javax.swing.JLabel maxMemory2;
    public javax.swing.JProgressBar memoryStatusBar;
    private javax.swing.JLabel memoryStatusLabel;
    private javax.swing.JButton okButton;
    public javax.swing.JButton optimizeButton;
    private javax.swing.JLabel optimizeDynamic;
    private javax.swing.JSlider optimizeSlider;
    private javax.swing.JCheckBox profilerEnableBox;
    private javax.swing.JSlider selectSlider;
    // End of variables declaration//GEN-END:variables
    
}
