/*
 *  =======================================================================
 *  DemoSlider.java : This applet demonstrates APIs from the class JSlider.
 *  A slider is a control mechanism with a pointer knob that can slide on a 
 *  continuous range of values to select a specific value of the parameter
 *  for which the widge has been used. A common application of sliders is
 *  audio volumes.
 *
 *  Adapted from : Pantham S., Pure JFC Swing, 1999.
 *  Modified by : Mark Austin                                   March, 2001
 *  =======================================================================
 */

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class DemoSlider extends JApplet {

    // 1. Declare the required JSlider references.

    JSlider slider = null;
    JSlider slider1 = null;
    JSlider slider2 = null;
    JSlider slider3 = null;
    JSlider slider4 = null;
    JSlider slider5 = null;

    // 2. Other necessary fields

    JTextField textField  = null;
    JTextField textField1 = null;
    JTextField textField2 = null;
    GridBagLayout gridbag = null;
    GridBagConstraints c  = null;
    Container container   = null;
    boolean buttonPressed = false;

    public void init() {

        // 3. Get a handle on the container of JApplet
        // and assign the color and layout model.

        container = this.getContentPane();
        container.setBackground(Color.lightGray);
        gridbag = new GridBagLayout();
        container.setLayout(gridbag);

        // 4. Constraints for the layout

        c = new GridBagConstraints();
        c.weightx = 1.0; c.weighty = 1.0;
        c.fill = GridBagConstraints.NONE;
        c.insets = new Insets(4, 4, 4, 4);

        // 5. Label showing ëEqualizer (x 100Hz)

        c.gridx = 0; c.gridy = 0;
        c.gridwidth = 1; c.gridheight = 1;
        c.ipadx = 150;
        Font font = new Font("Helvetica", Font.BOLD, 14);
        JLabel label = new JLabel("Equalizer (X 100Hz)",
                                  JLabel.CENTER);
        label.setFont(font);
        gridbag.setConstraints(label, c);
        container.add(label);

        // 6. Create horizontal slider1.

        c.gridy = 1;
        setSlider(JSlider.HORIZONTAL, true,
                  0, 20, 5, 5, 1);
        slider3 = slider;
        slider3.setLabelTable(slider3.createStandardLabels(5));
        slider3.setPaintLabels(true);

        // 7. Create horizontal slider2.

        c.gridy = 2;
        setSlider(JSlider.HORIZONTAL, true,
                  20, 100, 60, 10, 5);
        slider4 = slider;
        slider4.setLabelTable(slider4.createStandardLabels(10));
        slider4.setPaintLabels(true);

        // 8. Create horizontal slider3.

        c.gridy = 3;
        setSlider(JSlider.HORIZONTAL, true,
                  100, 200, 150, 20, 10);
        slider5 = slider;
        slider5.setLabelTable(slider5.createStandardLabels(20));
        slider5.setPaintLabels(true);

        // 9. Create the toggle button for the volume balance.

        c.ipadx = 0;
        c.gridx = 1; c.gridy = 4;
        c.gridwidth = 2; c.gridheight = 1;
        setButton("L|R Balance");

        // 10. Create volume slider1.

        c.ipady = 75;
        c.gridy = 1;
        c.gridwidth = 1; c.gridheight = 3;
        setSlider(JSlider.VERTICAL, false, 0, 10, 8);
        slider1 = slider;

        // 11. Create volume slider2.

        c.gridx = 2; c.gridy = 1;
        setSlider(JSlider.VERTICAL, false, 0, 10, 8);
        slider2 = slider;

        // 12. Create textfield1 for the volume slider1.

        c.ipadx = 0; c.ipady = 0;
        c.gridx = 1; c.gridy = 0;
        c.gridwidth = 1; c.gridheight = 1;
        setTextField(slider1);
        textField1 = textField;

        // 13. Create textfield2 for the volume slider2.

        c.gridx = 2;
        setTextField(slider2);
        textField2 = textField;
    }

    // 14. Creates a slider object.

    public void setSlider(int orientation,
                          boolean paintTicks,
                          int minimumValue, int maximumValue,
                          int initValue) {
        setSlider(orientation, paintTicks,
                  minimumValue, maximumValue, initValue, 0, 0);
    }

    // 15. Overload the previous above.

    public void setSlider(int orientation,
                          boolean paintTicks,
                          int minimumValue, int maximumValue,
                          int initValue,
                          int majorTickSpacing,
                          int minorTickSpacing) {
        slider = new JSlider(orientation,
                             minimumValue, maximumValue,
                             initValue);
        slider.addChangeListener(new SliderListener());
        slider.setPaintTicks(paintTicks);
        slider.setMajorTickSpacing(majorTickSpacing);
        slider.setMinorTickSpacing(minorTickSpacing);
        gridbag.setConstraints(slider, c);
        container.add(slider);
    }

    // 16. Create the toggle button for the balance of channels.

    public void setButton(String name) {
        JToggleButton button = new JToggleButton(name);
        button.setBackground(Color.lightGray);
        gridbag.setConstraints(button, c);
        button.addActionListener(new ButtonListener());
        container.add(button);
    }

    // 17. Create text field objects.

    public void setTextField(JSlider slider) {
        textField = new JTextField(2);
        textField.setText(Integer.toString(slider.getValue()));
        gridbag.setConstraints(textField, c);
        container.add(textField);
    }

    // 18. Button listener for the channel balance

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent actEvt) {
            JToggleButton buttonTemp = (JToggleButton)
                                        actEvt.getSource();
            buttonPressed = buttonTemp.isSelected();
            slider2.setValue(slider1.getValue());
        }
    }

    // 19. The slider knob position change listener

    class SliderListener implements ChangeListener {
        public void stateChanged(ChangeEvent chngEvt) {
            updateTextField(slider1.getValue(),
                            slider2.getValue());

            JSlider sliderTemp = (JSlider) chngEvt.getSource();
            if(buttonPressed) {
                if(sliderTemp == slider1) {
                    slider2.setValue(slider1.getValue());
                }
                else if(sliderTemp == slider2) {
                    slider1.setValue(slider2.getValue());
                }
            }
        }
    }

    public void updateTextField(int currValue1, int currValue2) {
        textField1.setText(Integer.toString(currValue1));
        textField2.setText(Integer.toString(currValue2));
    }
}
