Package jrackattack.gui

Source Code of jrackattack.gui.MidiSettings$NullDeviceInfo

/* For License see bottom */
/*
* MidiSettings.java
*
* Created on 29. Juni 2007, 21:34
*/

package jrackattack.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.Serializable;
import java.util.ResourceBundle;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import jonkoshare.gui.DefaultComboBoxModel;
import jonkoshare.util.VersionInformation;
import jrackattack.JRackAttack;
import jrackattack.midi.MidiThread;
import jrackattack.util.UserConfiguration;

import org.apache.log4j.Logger;

/**
* The panel for all MIDI-settings.
*
* @author methke01
*/
@VersionInformation(//
lastChanged = "$LastChangedDate: 2009-09-15 13:42:26 -0500 (Tue, 15 Sep 2009) $", //
authors = { "Alexander Methke" }, //
revision = "$LastChangedRevision: 12 $", //
lastEditor = "$LastChangedBy: onkobu $", //
id = "$Id")
public class MidiSettings extends JPanel implements Serializable {
  private static final class NullDeviceInfo extends MidiDevice.Info {
    protected NullDeviceInfo() {
      super(ResourceBundle.getBundle("jrackattack/gui").getString(
          "list.item.undef"), "", "no device", "0");
    }

  }

  private static final MidiDevice.Info NULL_DEVICE_INFO = new NullDeviceInfo();

  private static final Insets INSETS = new Insets(2, 2, 2, 2);
  private final Logger log = Logger.getLogger(MidiSettings.class);

  /** Creates new form MidiSettings */
  public MidiSettings() {
    initComponents();
    initValues();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   */
  private void initComponents() {
    GridBagConstraints gridBagConstraints;

    chooseButton = new JButton();
    channelSpinner = new JSpinner();
    midiOutBoxModel = new DefaultComboBoxModel<MidiDevice.Info>();
    midiOutBox = new JComboBox(midiOutBoxModel);
    jLabel1 = new JLabel();
    jLabel2 = new JLabel();
    midiInBoxModel = new DefaultComboBoxModel<MidiDevice.Info>();
    midiInBox = new JComboBox(midiInBoxModel);
    jLabel3 = new JLabel();
    keyboardInBoxModel = new DefaultComboBoxModel<MidiDevice.Info>();
    keyboardInBox = new JComboBox(keyboardInBoxModel);

    testSettings = new TestSettings_1();
    channelLabel = new JLabel();
    deviceDescription = new DeviceDescription();
    describeButton1 = new JButton();
    describeButton2 = new JButton();
    describeButton3 = new JButton();
    spoolBox = new JCheckBox();

    setLayout(new GridBagLayout());

    ResourceBundle bundle = ResourceBundle.getBundle("jrackattack/gui"); // NOI18N
    chooseButton.setText(bundle.getString("button.choose")); // NOI18N
    chooseButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        chooseButtonActionPerformed(evt);
      }
    });

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 6;
    gridBagConstraints.anchor = GridBagConstraints.EAST;
    gridBagConstraints.insets = INSETS;
    add(chooseButton, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = INSETS;
    add(channelSpinner, gridBagConstraints);

    midiOutBox.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent evt) {
        midiOutBoxItemStateChanged(evt);
      }
    });

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = INSETS;
    add(midiOutBox, gridBagConstraints);

    jLabel1.setText(bundle.getString("label.device.in")); // NOI18N
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = GridBagConstraints.EAST;
    gridBagConstraints.insets = INSETS;
    add(jLabel1, gridBagConstraints);

    jLabel2.setText(bundle.getString("label.device.out")); // NOI18N
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = GridBagConstraints.EAST;
    gridBagConstraints.insets = INSETS;
    add(jLabel2, gridBagConstraints);

    midiInBox.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent evt) {
        midiInBoxItemStateChanged(evt);
      }
    });

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = INSETS;
    add(midiInBox, gridBagConstraints);

    jLabel3.setText(bundle.getString("label.device.key")); // NOI18N
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = GridBagConstraints.EAST;
    gridBagConstraints.insets = INSETS;
    add(jLabel3, gridBagConstraints);

    keyboardInBox.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent evt) {
        keyboardInBoxItemStateChanged(evt);
      }
    });

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = INSETS;
    add(keyboardInBox, gridBagConstraints);

    testSettings.setBorder(BorderFactory.createTitledBorder(bundle
        .getString("title.test"))); // NOI18N
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 5;
    gridBagConstraints.gridwidth = 2;
    add(testSettings, gridBagConstraints);

    channelLabel.setText(bundle.getString("label.midi_channel")); // NOI18N
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = GridBagConstraints.EAST;
    gridBagConstraints.insets = INSETS;
    add(channelLabel, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridheight = 6;
    gridBagConstraints.fill = GridBagConstraints.BOTH;
    add(deviceDescription, gridBagConstraints);

    describeButton1.setText(bundle.getString("label.describe")); // NOI18N
    describeButton1.setToolTipText(bundle.getString("tooltip.describe")); // NOI18N
    describeButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        describeButton1ActionPerformed(evt);
      }
    });

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 0;
    add(describeButton1, gridBagConstraints);

    describeButton2.setText(bundle.getString("label.describe")); // NOI18N
    describeButton2.setToolTipText(bundle.getString("tooltip.describe")); // NOI18N
    describeButton2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        describeButton2ActionPerformed(evt);
      }
    });

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 1;
    add(describeButton2, gridBagConstraints);

    describeButton3.setText(bundle.getString("label.describe")); // NOI18N
    describeButton3.setToolTipText(bundle.getString("tooltip.describe")); // NOI18N
    describeButton3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        describeButton3ActionPerformed(evt);
      }
    });

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 3;
    add(describeButton3, gridBagConstraints);

    spoolBox.setText(bundle.getString("checkbox.log_midi")); // NOI18N
    spoolBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    spoolBox.setMargin(new Insets(0, 0, 0, 0));
    spoolBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        spoolBoxActionPerformed(evt);
      }
    });

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    add(spoolBox, gridBagConstraints);

  }

  private void spoolBoxActionPerformed(ActionEvent evt) {
    JRackAttack.setSpoolMidi(spoolBox.isSelected());
  }

  private void describeButton3ActionPerformed(ActionEvent evt) {
    if (keyboardInBox.getSelectedIndex() > 0) {
      deviceDescription.setDeviceInfo((MidiDevice.Info) keyboardInBox
          .getSelectedItem());
    } else {
      deviceDescription.setDeviceInfo(null);
    }
  }

  private void describeButton2ActionPerformed(ActionEvent evt) {
    deviceDescription.setDeviceInfo(midiInBoxModel.getSelectedItem());
  }

  private void describeButton1ActionPerformed(ActionEvent evt) {

    deviceDescription.setDeviceInfo(midiOutBoxModel.getSelectedItem());
  }

  private void keyboardInBoxItemStateChanged(ItemEvent evt) {

    if (initiating) {
      return;
    }
    if (keyboardInBox.getSelectedIndex() > 0) {
      deviceDescription.setDeviceInfo(keyboardInBoxModel
          .getSelectedItem());
    }
  }

  private void midiInBoxItemStateChanged(ItemEvent evt) {

    if (initiating) {
      return;
    }
    deviceDescription.setDeviceInfo(midiInBoxModel.getSelectedItem());
  }

  private void midiOutBoxItemStateChanged(ItemEvent evt) {

    if (initiating) {
      return;
    }
    deviceDescription.setDeviceInfo(midiOutBoxModel.getSelectedItem());
  }

  private void chooseButtonActionPerformed(ActionEvent evt) {

    ResourceBundle bdl = ResourceBundle.getBundle("jrackattack.gui");
    boolean flag = false;
    UserConfiguration uc = UserConfiguration.getInstance();
    this.midiOutIndex = midiOutBox.getSelectedIndex();
    uc.putProperty(UserConfiguration.MIDI_OUT_DEVICE, midiOutIndex);
    MidiDevice.Info outInfo = midiOutBoxModel.getSelectedItem();
    this.midiInIndex = midiInBox.getSelectedIndex();
    uc.putProperty(UserConfiguration.MIDI_IN_DEVICE, midiInIndex);
    MidiDevice.Info inInfo = midiInBoxModel.getSelectedItem();
    MidiDevice.Info keyInfo = null;
    this.keyInIndex = keyboardInBox.getSelectedIndex();
    uc.putProperty(UserConfiguration.KEYBOARD_IN_DEVICE, keyInIndex);
    if (keyInIndex > 0) {
      keyInfo = keyboardInBoxModel.getSelectedItem();
    }
    try {
      MidiDevice inDev;
      inDev = MidiSystem.getMidiDevice(inInfo);
      if (inDev.getMaxTransmitters() == 0) {
        JOptionPane.showMessageDialog(getParentDialog(), bdl
            .getString("message.no_transmitter"), bdl
            .getString("title.midi_init_failed"),
            JOptionPane.ERROR_MESSAGE);
      }
      MidiDevice outDev = MidiSystem.getMidiDevice(outInfo);
      if (outDev.getMaxReceivers() == 0) {
        JOptionPane.showMessageDialog(getParentDialog(), bdl
            .getString("message.no_receiver"), bdl
            .getString("title.midi_init_failed"),
            JOptionPane.ERROR_MESSAGE);
      }
      if (keyInfo == null) {
        flag = MidiThread.getInstance().init(inDev, outDev);
      } else {
        MidiDevice keyDev = MidiSystem.getMidiDevice(keyInfo);
        if (keyDev.getMaxTransmitters() == 0) {
          JOptionPane.showMessageDialog(getParentDialog(), bdl
              .getString("message.no_transmitter"), bdl
              .getString("title.midi_init_failed"),
              JOptionPane.ERROR_MESSAGE);
        }
        flag = MidiThread.getInstance().init(inDev, outDev, keyDev);
      }
    } catch (MidiUnavailableException e) {
      log.error("error", e);
    }

    if (flag) {
      getParentDialog().dispose();
    } else {
      JOptionPane.showMessageDialog(getParentDialog(), bdl
          .getString("message.midi_init_failed"), bdl
          .getString("title.midi_init_failed"),
          JOptionPane.ERROR_MESSAGE);
    }
  }

  private void initValues() {
    spoolBox.setSelected(JRackAttack.spoolMidi());
    testSettings.setSettingsParent(this);
    ((SpinnerNumberModel) channelSpinner.getModel()).setMinimum(0);
    ((SpinnerNumberModel) channelSpinner.getModel()).setMaximum(15);

    // Load available Midi Devices
    MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();
    int c = info.length;
    MidiDevice dev;
    keyboardInBoxModel.removeAll();
    midiInBoxModel.removeAll();
    midiOutBoxModel.removeAll();
    UserConfiguration uc = UserConfiguration.getInstance();
    if (keyInIndex == -1) {
      keyInIndex = uc.getIntProperty(
          UserConfiguration.KEYBOARD_IN_DEVICE, -1);
    }
    if (midiInIndex == -1) {
      midiInIndex = uc.getIntProperty(UserConfiguration.MIDI_IN_DEVICE,
          -1);
    }
    if (midiOutIndex == -1) {
      midiOutIndex = uc.getIntProperty(UserConfiguration.MIDI_OUT_DEVICE,
          -1);
    }
    try {
      keyboardInBoxModel.add(NULL_DEVICE_INFO);
      for (int i = 0; i < c; i++) {
        dev = MidiSystem.getMidiDevice(info[i]);
        int max = dev.getMaxReceivers();
        if (max == 0) {
          midiInBoxModel.add(info[i]);
          keyboardInBoxModel.add(info[i]);
        } else {
          midiOutBoxModel.add(info[i]);
        }
      }
      if (keyInIndex != -1) {
        keyboardInBox.setSelectedIndex(keyInIndex);
      }
      if (midiInIndex != -1) {
        midiInBox.setSelectedIndex(midiInIndex);
      }
      if (midiOutIndex != -1) {
        midiOutBox.setSelectedIndex(midiOutIndex);
      }
    } catch (Exception ex) {
      log.error("error", ex);
    }
  }

  public MidiDevice.Info getSelectedOutDevice() {
    return midiOutBoxModel.getSelectedItem();
  }

  public int getSelectedChannel() {
    return ((Integer) channelSpinner.getValue()).intValue();
  }

  /**
   * Getter for property parent.
   *
   * @return Value of property parent.
   */
  protected JDialog getParentDialog() {
    return this.parent;
  }

  /**
   * Setter for property parent.
   *
   * @param parent
   *            New value of property parent.
   */
  protected void setParentDialog(JDialog parent) {
    this.parent = parent;
  }

  // Variables declaration - do not modify
  private JLabel channelLabel;
  private JSpinner channelSpinner;
  private JButton chooseButton;
  private JButton describeButton1;
  private JButton describeButton2;
  private JButton describeButton3;
  private DeviceDescription deviceDescription;
  private JLabel jLabel1;
  private JLabel jLabel2;
  private JLabel jLabel3;
  private JComboBox keyboardInBox;
  private DefaultComboBoxModel<MidiDevice.Info> keyboardInBoxModel;

  private JComboBox midiInBox;
  private DefaultComboBoxModel<MidiDevice.Info> midiInBoxModel;

  private JComboBox midiOutBox;
  private DefaultComboBoxModel<MidiDevice.Info> midiOutBoxModel;

  private JCheckBox spoolBox;
  private TestSettings_1 testSettings;
  // End of variables declaration

  private int midiInIndex = -1;
  private int midiOutIndex = -1;
  private int keyInIndex = -1;

  /**
   * Holds value of property parent.
   */
  private JDialog parent;

  private boolean initiating;
}
/*
* 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 jrackattack.gui.MidiSettings$NullDeviceInfo

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.