Package jpianotrain.gui

Source Code of jpianotrain.gui.ChordScalePane

package jpianotrain.gui;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import jpianotrain.staff.Chord;
import jpianotrain.staff.ScaleName;

import org.apache.log4j.Logger;

/**
* Panel to choose notes to form a chord.
*
* @author onkobu
* @since 0.0.3
*/
public class ChordScalePane extends JPanel
             implements ActionListener,
                     ItemListener,
                     MouseListener {
  private static final Logger log=Logger.getLogger(ChordScalePane.class);
 
  public ChordScalePane() {
    this(true);
  }

  public ChordScalePane(boolean showHelp) {
    //scaleList=new ArrayList<Scale>();
    //Scale s=null;
    // create scales according to notes
    /*
    for (ScaleName sn:ScaleName.values()) {
      if (sn==ScaleName.MINOR ||
          sn==ScaleName.MINOR_WITH_FLATS) {
        for (NoteName nn:Scale.MINOR_SCALES_CLASSIC) {
          log.debug("Note name="+nn);
          s=new Scale(nn,sn);
          scaleList.add(s);
        }
      } else {
        for (NoteName nn:Scale.MAJOR_SCALES_CLASSIC) {
          log.debug("Note name="+nn);
          s=new Scale(nn,sn);
          scaleList.add(s);
        }
      }
    }*/
    createUI(showHelp);
  }

  /**
   * Returns the selected chord. This
   * may be <code>null</code> if
   * nothing is selected.
   *
   */
  public Chord getChord() {
    Chord c=null;
    return c;
  }

  protected void createUI(boolean showHelp) {
    popup=new JPopupMenu();
    playMidiItem=new JMenuItem("Play Midi");
    playMidiItem.addActionListener(this);
    popup.add(playMidiItem);

    setLayout(new BorderLayout());

    Vector<ScaleName> v=new Vector<ScaleName>();
    for (ScaleName sn:ScaleName.values()) {
      v.add(sn);
    }

    scaleComboBox=new JComboBox(v);
    scaleComboBox.setSelectedItem(ScaleName.MAJOR);
    scaleComboBox.setEditable(false);
    scaleComboBox.addItemListener(this);
    add(scaleComboBox, BorderLayout.NORTH);

    scaleTable=new JTable();
    scaleTable.setDefaultRenderer(Chord.class, new VLTableCellRenderer());
    ChordTableModel ctm=new ChordTableModel((ScaleName)scaleComboBox.getSelectedItem());
    //log.debug("cols="+ctm.getColumnCount());
    //log.debug("rows="+ctm.getRowCount());
    scaleTable.setModel(ctm);
    scaleTable.setCellEditor(null);
    scaleTable.setCellSelectionEnabled(true);
    scaleTable.addMouseListener(this);
    scaleTable.getTableHeader().setReorderingAllowed(false);
    add(new JScrollPane(scaleTable), BorderLayout.CENTER);
  }

//ItemListener
  @Override
  public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange()!=ItemEvent.SELECTED) {
      return;
    }
    ScaleName sn=(ScaleName)scaleComboBox.getSelectedItem();
    showScale(sn);
  }
 
  private void showScale(ScaleName sn) {
    scaleTable.setModel(new ChordTableModel(sn));
  }

// MouseListener
  @Override
  public void mouseClicked(MouseEvent e) {
  }

  @Override
  public void mousePressed(MouseEvent e) {
    if (e.isPopupTrigger() ||
      e.getButton()==MouseEvent.BUTTON3_MASK) {
      //log.debug("Context Menu anzeigen");
      popup.show(scaleTable,e.getX(), e.getY());
      Point p=e.getPoint();
      selectedColumn=scaleTable.columnAtPoint(p);
      selectedRow=scaleTable.rowAtPoint(p);
      scaleTable.clearSelection();
      scaleTable.changeSelection(selectedRow,
                     selectedColumn,
                     false,
                     false);
    }
  }

  @Override
  public void mouseReleased(MouseEvent e) {
  }

  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
  }

// ActionListener
  @Override
  public void actionPerformed(ActionEvent e) {
    /*
    Object src=e.getSource();
    if (src==playMidiItem) {
      Object obj=scaleTable.getValueAt(selectedRow,
                       selectedColumn);
      if (obj==null ||
        !(obj instanceof Chord)) {
        return;
      }
      Chord c=(Chord)obj;
      log.debug("Playing: "+c.toShortString(false,true));
      MidiThread mt=MidiThread.getInstance();
      if (mt==null) {
        ResourceBundle bdl=ResourceBundle.getBundle("vl.vlc");
        JOptionPane.showMessageDialog(this,bdl.getString("msg.midi_not_ready"));
        return;
      }
      mt.play(c);
    }
    */
  }

  private int selectedColumn;
  private int selectedRow;

  //private ArrayList<Scale> scaleList;

  private JPopupMenu popup;
  private JMenuItem playMidiItem;

  private JComboBox scaleComboBox;
  private JTable scaleTable;
}
TOP

Related Classes of jpianotrain.gui.ChordScalePane

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.