Package jpianotrain.gui

Source Code of jpianotrain.gui.RandomPanel

/* License see bottom */
package jpianotrain.gui;

import static jpianotrain.util.ResourceKeys.INFO_PAGE_RANDOM;
import static jpianotrain.util.ResourceKeys.LABEL_BOTH_HANDS;
import static jpianotrain.util.ResourceKeys.LABEL_BOUND_LOWER_BASS;
import static jpianotrain.util.ResourceKeys.LABEL_BOUND_LOWER_TREBLE;
import static jpianotrain.util.ResourceKeys.LABEL_BOUND_UPPER_BASS;
import static jpianotrain.util.ResourceKeys.LABEL_BOUND_UPPER_TREBLE;
import static jpianotrain.util.ResourceKeys.LABEL_NUMBER_NOTES;
import static jpianotrain.util.ResourceKeys.LABEL_SCALE;
import static jpianotrain.util.ResourceKeys.LABEL_SCALE_MAJOR;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import jpianotrain.Constants;
import jpianotrain.staff.NoteName;
import jpianotrain.staff.RandomTuneFactory;
import jpianotrain.staff.Scale;
import jpianotrain.staff.TuneFactory;
import jpianotrain.util.ResourceFactory;

/**
* Panel to configure the Random Tune Factory
*
* @see RandomTuneFactory
* @since 0
* @author Alexander Methke
*/
public class RandomPanel extends ConfigurationPanel
             implements ActionListener {
  public RandomPanel() {
    super();
    initGui();
  }

  private void initGui() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc=new GridBagConstraints();
   
    JPanel confPanel=createConfPanel();
   
    InfoPanel infoPanel=new InfoPanel(INFO_PAGE_RANDOM, true);
    infoPanel.setPreferredSize(new Dimension(260, 100));
    infoPanel.setMaximumSize(infoPanel.getPreferredSize());

    gbc.fill=GridBagConstraints.BOTH;
    gbc.weighty=1.0;
    gbc.gridx=0;
    gbc.gridy=0;
    gbc.gridheight=GridBagConstraints.REMAINDER;
    add(infoPanel, gbc);
   
    gbc.gridx++;
    gbc.weightx=1.0;
    add(confPanel, gbc);

    majorCheck.addActionListener(this);
  }

  private JPanel createConfPanel() {
    GridBagConstraints gbc=new GridBagConstraints();

    JPanel confPanel=new JPanel();
    confPanel.setLayout(new GridBagLayout());
    gbc.gridx=0;
    gbc.gridy=0;
    gbc.anchor=GridBagConstraints.EAST;
    gbc.insets=new Insets(2,2,2,2);
    JLabel l=new JLabel(ResourceFactory.getString(LABEL_BOTH_HANDS));
    confPanel.add(l, gbc);

    l=new JLabel(ResourceFactory.getString(LABEL_BOUND_LOWER_BASS));
    gbc.gridy++;
    confPanel.add(l, gbc);

    l=new JLabel(ResourceFactory.getString(LABEL_BOUND_UPPER_BASS));
    gbc.gridy++;
    confPanel.add(l, gbc);

    l=new JLabel(ResourceFactory.getString(LABEL_BOUND_LOWER_TREBLE));
    gbc.gridy++;
    confPanel.add(l, gbc);

    l=new JLabel(ResourceFactory.getString(LABEL_BOUND_UPPER_TREBLE));
    gbc.gridy++;
    confPanel.add(l, gbc);

    l=new JLabel(ResourceFactory.getString(LABEL_SCALE_MAJOR));
    gbc.gridy++;
    confPanel.add(l, gbc);

    l=new JLabel(ResourceFactory.getString(LABEL_SCALE));
    gbc.gridy++;
    confPanel.add(l, gbc);

    l=new JLabel(ResourceFactory.getString(LABEL_NUMBER_NOTES));
    gbc.gridy++;
    confPanel.add(l, gbc);

    gbc.gridx=1;
    gbc.gridy=0;
    gbc.anchor=GridBagConstraints.WEST;
    bothHandsCheck=new JCheckBox();
    confPanel.add(bothHandsCheck, gbc);

    lowerBoundBass=createMidiSpinner();
    gbc.gridy++;
    confPanel.add(lowerBoundBass, gbc);

    upperBoundBass=createMidiSpinner();
    gbc.gridy++;
    confPanel.add(upperBoundBass, gbc);

    lowerBoundTreble=createMidiSpinner();
    gbc.gridy++;
    confPanel.add(lowerBoundTreble, gbc);

    upperBoundTreble=createMidiSpinner();
    gbc.gridy++;
    confPanel.add(upperBoundTreble, gbc);

    majorCheck=new JCheckBox();
    gbc.gridy++;
    confPanel.add(majorCheck, gbc);

    scaleBox=new JComboBox();
    gbc.gridy++;
    confPanel.add(scaleBox, gbc);

    numberSpinner=new JSpinner();
    numberSpinner.setModel(new SpinnerNumberModel(0,0,Constants.MAX_RANDOM_NOTES,1));
    gbc.gridy++;
    confPanel.add(numberSpinner, gbc);
    return confPanel;
  }
  @Override
  public void initData() {
    TuneFactory rtf=TuneFactory.getRandomFactory();
    bothHandsCheck.setSelected(rtf.isBothHands());
    lowerBoundBass.setValue(rtf.getLowerBoundBass());
    upperBoundBass.setValue(rtf.getUpperBoundBass());
    lowerBoundTreble.setValue(rtf.getLowerBoundTreble());
    upperBoundTreble.setValue(rtf.getUpperBoundTreble());
   
    if (rtf.getLowerBoundBass()==0 && rtf.getLowerBoundTreble()==0 &&
        rtf.getUpperBoundBass()==0 && rtf.getUpperBoundTreble()==0) {
      lowerBoundBass.setValue(0);
      upperBoundBass.setValue(63);
      lowerBoundTreble.setValue(64);
      upperBoundTreble.setValue(127);
    }
    numberSpinner.setValue(rtf.getNoteCount());

    Scale s=rtf.getScale();
    if (s==null) {
      majorCheck.setSelected(true);
      for (NoteName nn:Scale.MAJOR_SCALES_CLASSIC_EN) {
        scaleBox.addItem(nn);
      }
    } else {
      majorCheck.setSelected(s.isMajor());
      if (s.isMajor()) {
        for (NoteName nn:Scale.MAJOR_SCALES_CLASSIC_EN) {
          scaleBox.addItem(nn);
        }
      } else {
        for (NoteName nn:Scale.MINOR_SCALES_CLASSIC_EN) {
          scaleBox.addItem(nn);
        }
      }
      scaleBox.setSelectedItem(s.getRoot().getName());
    }
  }

  /**
   * TODO: Make sure, that bounds create correct non-negative
   * ranges. Show error otherwise. Emit some signal so that
   * configuration dialog stays open.
   */
  @Override
  public void saveData() {
    TuneFactory rtf=TuneFactory.getRandomFactory();
    rtf.holdEvents();
    rtf.setBothHands(bothHandsCheck.isSelected());
    rtf.setLowerBoundBass((Integer)lowerBoundBass.getValue());
    rtf.setUpperBoundBass((Integer)upperBoundBass.getValue());
    rtf.setLowerBoundTreble((Integer)lowerBoundTreble.getValue());
    rtf.setUpperBoundTreble((Integer)upperBoundTreble.getValue());
    rtf.setNoteCount((Integer)numberSpinner.getValue());

    Scale s=new Scale((NoteName)scaleBox.getSelectedItem());
    rtf.setScale(s);
    rtf.flushEvents();
  }

  @Override
  public void rejectData() {
  }

// ActionListener
  public void actionPerformed(ActionEvent e) {
    if (e.getSource()==majorCheck) {
      scaleBox.removeAllItems();
      if (majorCheck.isSelected()) {
        for (NoteName nn:Scale.MAJOR_SCALES_CLASSIC_EN) {
          scaleBox.addItem(nn);
        }
      } else {
        for (NoteName nn:Scale.MINOR_SCALES_CLASSIC_EN) {
          scaleBox.addItem(nn);
        }
      }
    }
  }

  private JSpinner createMidiSpinner() {
    JSpinner sp=new JSpinner();
    SpinnerNumberModel snm=new SpinnerNumberModel(0,0,127,1);
    sp.setModel(snm);
    return sp;
  }

  private JCheckBox bothHandsCheck;
  private JCheckBox majorCheck;

  private JComboBox scaleBox;

  private JSpinner numberSpinner;
  private JSpinner lowerBoundBass;
  private JSpinner upperBoundBass;
  private JSpinner lowerBoundTreble;
  private JSpinner upperBoundTreble;
}
/*
    Copyright (C) 2008  Alexander Methke

    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 3 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 (gplv3.txt).
*/
TOP

Related Classes of jpianotrain.gui.RandomPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.