OSDN Git Service

・GUI仮想MIDIデバイスを一つに集約
authorAkiyoshi Kamide <kamide@yk.rim.or.jp>
Wed, 25 May 2016 16:49:20 +0000 (01:49 +0900)
committerAkiyoshi Kamide <kamide@yk.rim.or.jp>
Wed, 25 May 2016 16:49:20 +0000 (01:49 +0900)
・その他、周辺をリファクタリング

src/camidion/chordhelper/ChordHelperApplet.java
src/camidion/chordhelper/MidiChordHelper.java
src/camidion/chordhelper/mididevice/MidiTransceiverListModelList.java
src/camidion/chordhelper/midieditor/MidiSequenceEditor.java
src/camidion/chordhelper/pianokeyboard/PianoKeyboard.java

index 6984077..b618535 100644 (file)
@@ -59,10 +59,12 @@ import camidion.chordhelper.mididevice.MidiDeviceDialog;
 import camidion.chordhelper.mididevice.MidiTransceiverListModelList;
 import camidion.chordhelper.mididevice.SequencerMeasureView;
 import camidion.chordhelper.mididevice.SequencerTimeView;
+import camidion.chordhelper.mididevice.VirtualMidiDevice;
 import camidion.chordhelper.midieditor.Base64Dialog;
 import camidion.chordhelper.midieditor.KeySignatureLabel;
 import camidion.chordhelper.midieditor.MidiSequenceEditor;
 import camidion.chordhelper.midieditor.NewSequenceDialog;
+import camidion.chordhelper.midieditor.PlaylistTableModel;
 import camidion.chordhelper.midieditor.SequenceTickIndex;
 import camidion.chordhelper.midieditor.SequenceTrackListTableModel;
 import camidion.chordhelper.midieditor.TempoSelecter;
@@ -92,7 +94,7 @@ public class ChordHelperApplet extends JApplet {
         * @return 未保存の修正済み MIDI ファイルがあれば true
         */
        public boolean isModified() {
-               return deviceModelList.getEditorDialog().sequenceListTable.getModel().isModified();
+               return midiEditor.sequenceListTable.getModel().isModified();
        }
        /**
         * 指定された小節数の曲を、乱数で自動作曲してプレイリストへ追加します。
@@ -100,10 +102,9 @@ public class ChordHelperApplet extends JApplet {
         * @return 追加先のインデックス値(0から始まる)。追加できなかったときは -1
         */
        public int addRandomSongToPlaylist(int measureLength) {
-               MidiSequenceEditor editor = deviceModelList.getEditorDialog();
-               NewSequenceDialog d = editor.newSequenceDialog;
+               NewSequenceDialog d = midiEditor.newSequenceDialog;
                d.setRandomChordProgression(measureLength);
-               return editor.sequenceListTable.getModel().addSequenceAndPlay(d.getMidiSequence());
+               return midiEditor.sequenceListTable.getModel().addSequenceAndPlay(d.getMidiSequence());
        }
        /**
         * URLで指定されたMIDIファイルをプレイリストへ追加します。
@@ -115,14 +116,13 @@ public class ChordHelperApplet extends JApplet {
         * @return 追加先のインデックス値(0から始まる)。追加できなかったときは -1
         */
        public int addToPlaylist(String midiFileUrl) {
-               MidiSequenceEditor editor = deviceModelList.getEditorDialog();
                try {
-                       return editor.sequenceListTable.getModel().addSequenceFromURL(midiFileUrl);
+                       return midiEditor.sequenceListTable.getModel().addSequenceFromURL(midiFileUrl);
                } catch( URISyntaxException|IOException|InvalidMidiDataException e ) {
-                       editor.showWarning(e.getMessage());
+                       midiEditor.showWarning(e.getMessage());
                } catch( AccessControlException e ) {
                        e.printStackTrace();
-                       editor.showError(e.getMessage());
+                       midiEditor.showError(e.getMessage());
                }
                return -1;
        }
@@ -144,14 +144,13 @@ public class ChordHelperApplet extends JApplet {
         * @return 追加先のインデックス値(0から始まる)。追加できなかったときは -1
         */
        public int addToPlaylistBase64(String base64EncodedText, String filename) {
-               MidiSequenceEditor editor = deviceModelList.getEditorDialog();
-               Base64Dialog d = editor.base64Dialog;
+               Base64Dialog d = midiEditor.base64Dialog;
                d.setBase64Data(base64EncodedText);
                try {
-                       return editor.sequenceListTable.getModel().addSequence(d.getMIDIData(), filename);
+                       return midiEditor.sequenceListTable.getModel().addSequence(d.getMIDIData(), filename);
                } catch (IOException | InvalidMidiDataException e) {
                        e.printStackTrace();
-                       editor.showWarning(e.getMessage());
+                       midiEditor.showWarning(e.getMessage());
                        return -1;
                }
        }
@@ -160,7 +159,7 @@ public class ChordHelperApplet extends JApplet {
         * シーケンサへロードして再生します。
         */
        public void play() {
-               play(deviceModelList.getEditorDialog().sequenceListTable.getModel().sequenceListSelectionModel.getMinSelectionIndex());
+               play(midiEditor.sequenceListTable.getModel().sequenceListSelectionModel.getMinSelectionIndex());
        }
        /**
         * 指定されたインデックス値が示すプレイリスト上のMIDIシーケンスを、
@@ -168,7 +167,7 @@ public class ChordHelperApplet extends JApplet {
         * @param index インデックス値(0から始まる)
         */
        public void play(int index) {
-               deviceModelList.getEditorDialog().sequenceListTable.getModel().loadToSequencer(index);
+               midiEditor.sequenceListTable.getModel().loadToSequencer(index);
                deviceModelList.getSequencerModel().start();
        }
        /**
@@ -191,11 +190,10 @@ public class ChordHelperApplet extends JApplet {
         * @return MIDIデータをBase64テキストに変換した結果
         */
        public String getMidiDataBase64() {
-               MidiSequenceEditor editor = deviceModelList.getEditorDialog();
                SequenceTrackListTableModel sequenceModel =
-                       editor.sequenceListTable.getModel().sequencerModel.getSequenceTrackListTableModel();
-               editor.base64Dialog.setMIDIData(sequenceModel.getMIDIdata());
-               return editor.base64Dialog.getBase64Data();
+                       midiEditor.sequenceListTable.getModel().sequencerModel.getSequenceTrackListTableModel();
+               midiEditor.base64Dialog.setMIDIData(sequenceModel.getMIDIdata());
+               return midiEditor.base64Dialog.getBase64Data();
        }
        /**
         * 現在シーケンサにロードされているMIDIファイルのファイル名を返します。
@@ -384,8 +382,8 @@ public class ChordHelperApplet extends JApplet {
        private static final String IMAGE_ICON_PATH = "midichordhelper.png";
        //
        public ChordMatrix chordMatrix;
-       MidiTransceiverListModelList    deviceModelList;
-       //
+       MidiSequenceEditor midiEditor;
+       private MidiTransceiverListModelList deviceModelList;
        private JPanel keyboardSequencerPanel;
        private JPanel chordGuide;
        private Color rootPaneDefaultBgcolor;
@@ -468,15 +466,26 @@ public class ChordHelperApplet extends JApplet {
                        );
                        keyboardCenterPanel.keyboard.setPreferredSize(new Dimension(571, 80));
                }};
+               //
                // MIDIデバイスのセットアップ
-               deviceModelList = new MidiTransceiverListModelList(
-                       Arrays.asList(keyboardPanel.keyboardCenterPanel.keyboard.midiDevice)
-               );
-               MidiSequenceEditor midiEditor = deviceModelList.getEditorDialog();
-               midiEditor.setIconImage(iconImage);
+               VirtualMidiDevice guiMidiDevice = keyboardPanel.keyboardCenterPanel.keyboard.midiDevice;
+               deviceModelList = new MidiTransceiverListModelList(Arrays.asList(guiMidiDevice));
+               //
+               // MIDIデバイスダイアログの構築
+               (midiDeviceDialog = new MidiDeviceDialog(deviceModelList)).setIconImage(iconImage);
+               //
+               // MIDIデバイス一覧で取得したシーケンサと連携するプレイリストを構築
+               PlaylistTableModel playlist = new PlaylistTableModel(deviceModelList.getSequencerModel());
+               //
+               // MIDIエディタダイアログの構築
+               (midiEditor = new MidiSequenceEditor(playlist, guiMidiDevice)).setIconImage(iconImage);
+               //
+               // メイン画面へのMIDIファイルのドラッグ&ドロップ受付開始
                new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, midiEditor.dropTargetListener, true);
+               //
+               // MIDIエディタのイベントダイアログを、ピアノ鍵盤のイベント送出ダイアログと共用
                keyboardPanel.setEventDialog(midiEditor.eventDialog);
-               (midiDeviceDialog = new MidiDeviceDialog(deviceModelList)).setIconImage(iconImage);
+               //
                lyricDisplay = new ChordTextField(deviceModelList.getSequencerModel()) {{
                        addActionListener(new ActionListener() {
                                @Override
@@ -530,7 +539,7 @@ public class ChordHelperApplet extends JApplet {
                        @Override
                        public void stateChanged(ChangeEvent e) {
                                SequenceTrackListTableModel sequenceTableModel = deviceModelList.getSequencerModel().getSequenceTrackListTableModel();
-                               int loadedSequenceIndex = deviceModelList.getEditorDialog().sequenceListTable.getModel().indexOfSequenceOnSequencer();
+                               int loadedSequenceIndex = midiEditor.sequenceListTable.getModel().indexOfSequenceOnSequencer();
                                songTitleLabel.setText(
                                        "<html>"+(
                                                loadedSequenceIndex < 0 ? "[No MIDI file loaded]" :
@@ -662,7 +671,7 @@ public class ChordHelperApplet extends JApplet {
                                        add( Box.createHorizontalStrut(12) );
                                        add( songTitleLabel );
                                        add( Box.createHorizontalStrut(12) );
-                                       add( new JButton(deviceModelList.getEditorDialog().openAction) {{ setMargin(ZERO_INSETS); }});
+                                       add( new JButton(midiEditor.openAction) {{ setMargin(ZERO_INSETS); }});
                                }});
                                add(new JPanel() {{
                                        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
@@ -670,7 +679,7 @@ public class ChordHelperApplet extends JApplet {
                                        add( new JSlider(deviceModelList.getSequencerModel()) );
                                        add( new SequencerTimeView(deviceModelList.getSequencerModel()) );
                                        add( Box.createHorizontalStrut(5) );
-                                       add( new JButton(deviceModelList.getEditorDialog().sequenceListTable.getModel().moveToTopAction) {{
+                                       add( new JButton(midiEditor.sequenceListTable.getModel().moveToTopAction) {{
                                                setMargin(ZERO_INSETS);
                                        }});
                                        add(new JButton(deviceModelList.getSequencerModel().moveBackwardAction) {{
@@ -680,10 +689,10 @@ public class ChordHelperApplet extends JApplet {
                                        add(new JButton(deviceModelList.getSequencerModel().moveForwardAction) {{
                                                setMargin(ZERO_INSETS);
                                        }});
-                                       add(new JButton(deviceModelList.getEditorDialog().sequenceListTable.getModel().moveToBottomAction) {{
+                                       add(new JButton(midiEditor.sequenceListTable.getModel().moveToBottomAction) {{
                                                setMargin(ZERO_INSETS);
                                        }});
-                                       add(new JToggleButton(deviceModelList.getEditorDialog().sequenceListTable.getModel().toggleRepeatAction) {{
+                                       add(new JToggleButton(midiEditor.sequenceListTable.getModel().toggleRepeatAction) {{
                                                setMargin(ZERO_INSETS);
                                        }});
                                        add( Box.createHorizontalStrut(10) );
index 3b9ec6a..f539913 100644 (file)
@@ -29,7 +29,6 @@ import javax.swing.event.TableModelEvent;
 import javax.swing.event.TableModelListener;
 
 import camidion.chordhelper.mididevice.MidiSequencerModel;
-import camidion.chordhelper.mididevice.MidiTransceiverListModelList;
 import camidion.chordhelper.midieditor.MidiSequenceEditor;
 import camidion.chordhelper.midieditor.PlaylistTableModel;
 import camidion.chordhelper.midieditor.SequenceTrackListTableModel;
@@ -68,6 +67,7 @@ public class MidiChordHelper {
                        setTitle(title);
                }
                private MidiSequenceEditor editor;
+               private PlaylistTableModel playlist;
                public AppletFrame(ChordHelperApplet applet, List<File> fileList) {
                        setTitle(ChordHelperApplet.VersionInfo.NAME);
                        add( applet, BorderLayout.CENTER );
@@ -80,8 +80,17 @@ public class MidiChordHelper {
                        setLocationRelativeTo(null);
                        setVisible(true);
                        applet.start();
-                       MidiTransceiverListModelList deviceModelList = applet.deviceModelList;
-                       deviceModelList.getSequencerModel().addChangeListener(new ChangeListener() {
+                       playlist = (editor = applet.midiEditor).sequenceListTable.getModel();
+                       addWindowListener(new WindowAdapter() {
+                               @Override
+                               public void windowClosing(WindowEvent event) {
+                                       if( ! playlist.isModified() || editor.confirm(
+                                               "MIDI file not saved, exit anyway ?\n"+
+                                               "保存されていないMIDIファイルがありますが、終了してよろしいですか?"
+                                       )) System.exit(0);
+                               }
+                       });
+                       playlist.sequencerModel.addChangeListener(new ChangeListener() {
                                /**
                                 * シーケンサで切り替わった再生対象ファイル名をタイトルバーに反映
                                 */
@@ -91,17 +100,7 @@ public class MidiChordHelper {
                                        setFilenameToTitle(sequencerModel.getSequenceTrackListTableModel());
                                }
                        });
-                       editor = deviceModelList.getEditorDialog();
-                       addWindowListener(new WindowAdapter() {
-                               @Override
-                               public void windowClosing(WindowEvent event) {
-                                       if( ! editor.sequenceListTable.getModel().isModified() || editor.confirm(
-                                               "MIDI file not saved, exit anyway ?\n"+
-                                               "保存されていないMIDIファイルがありますが、終了してよろしいですか?"
-                                       )) System.exit(0);
-                               }
-                       });
-                       editor.sequenceListTable.getModel().addTableModelListener(new TableModelListener() {
+                       playlist.addTableModelListener(new TableModelListener() {
                                /**
                                 * プレイリスト上で変更された再生対象ファイル名をタイトルバーに反映
                                 */
index 35a83fa..70d713c 100644 (file)
@@ -10,8 +10,6 @@ import javax.sound.midi.Sequencer;
 import javax.sound.midi.Synthesizer;
 
 import camidion.chordhelper.ChordHelperApplet;
-import camidion.chordhelper.midieditor.MidiSequenceEditor;
-import camidion.chordhelper.midieditor.PlaylistTableModel;
 
 /**
  * 仮想MIDIデバイスを含めたすべてのMIDIデバイスモデル {@link MidiTransceiverListModel} を収容するリスト
@@ -20,15 +18,12 @@ public class MidiTransceiverListModelList extends Vector<MidiTransceiverListMode
 
        public String toString() { return "MIDI devices"; }
 
-       private MidiSequenceEditor editorDialog;
-       public MidiSequenceEditor getEditorDialog() { return editorDialog; }
-
        private MidiSequencerModel sequencerModel;
        public MidiSequencerModel getSequencerModel() { return sequencerModel; }
 
        public MidiTransceiverListModelList(List<VirtualMidiDevice> guiVirtualDeviceList) {
                //
-               // GUI仮想MIDIデバイスリストの構築
+               // GUI仮想MIDIデバイス
                MidiTransceiverListModel guiModels[] = new MidiTransceiverListModel[guiVirtualDeviceList.size()];
                for( int i=0; i<guiVirtualDeviceList.size(); i++ ) {
                        addElement(guiModels[i] = new MidiTransceiverListModel(guiVirtualDeviceList.get(i), this));
@@ -42,16 +37,12 @@ public class MidiTransceiverListModelList extends Vector<MidiTransceiverListMode
                        System.out.println(ChordHelperApplet.VersionInfo.NAME +" : MIDI sequencer unavailable");
                        e.printStackTrace();
                }
-               // MIDIエディタの生成
-               editorDialog = new MidiSequenceEditor(new PlaylistTableModel(sequencerModel));
-               MidiTransceiverListModel eventTableDeviceModel;
-               addElement(eventTableDeviceModel = new MidiTransceiverListModel(editorDialog.getEventTableMidiDevice(), this));
+               // その他のリアルMIDIデバイスの取得
                MidiTransceiverListModel synthModel = null;
                MidiTransceiverListModel firstMidiInModel = null;
                MidiTransceiverListModel firstMidiOutModel = null;
                MidiDevice.Info[] deviceInfos = MidiSystem.getMidiDeviceInfo();
                for( MidiDevice.Info info : deviceInfos ) {
-                       // MIDIデバイスの取得
                        MidiDevice device;
                        try {
                                device = MidiSystem.getMidiDevice(info);
@@ -95,7 +86,6 @@ public class MidiTransceiverListModelList extends Vector<MidiTransceiverListMode
                                firstMidiOutModel,
                                sequencerModel,
                                firstMidiInModel,
-                               eventTableDeviceModel,
                        };
                        for( MidiTransceiverListModel m : openModels ) if( m != null ) {
                                m.getMidiDevice().open();
@@ -127,9 +117,5 @@ public class MidiTransceiverListModelList extends Vector<MidiTransceiverListMode
                        sequencerModel.connectToReceiverOf(synthModel);
                        sequencerModel.connectToReceiverOf(firstMidiOutModel);
                }
-               if( eventTableDeviceModel != null ) {
-                       eventTableDeviceModel.connectToReceiverOf(synthModel);
-                       eventTableDeviceModel.connectToReceiverOf(firstMidiOutModel);
-               }
        }
 }
\ No newline at end of file
index c127a87..64e75a8 100644 (file)
@@ -70,7 +70,6 @@ import javax.swing.table.TableModel;
 
 import camidion.chordhelper.ButtonIcon;
 import camidion.chordhelper.ChordHelperApplet;
-import camidion.chordhelper.mididevice.AbstractVirtualMidiDevice;
 import camidion.chordhelper.mididevice.MidiSequencerModel;
 import camidion.chordhelper.mididevice.VirtualMidiDevice;
 import camidion.chordhelper.music.MIDISpec;
@@ -97,10 +96,11 @@ public class MidiSequenceEditor extends JDialog {
                }
        };
 
+       private VirtualMidiDevice outputMidiDevice;
        /**
         * イベントリスト操作音出力先MIDIデバイスを返します。
         */
-       public VirtualMidiDevice getEventTableMidiDevice() { return eventListTable.outputMidiDevice; }
+       //public VirtualMidiDevice getEventTableMidiDevice() { return eventListTable.outputMidiDevice; }
 
        /**
         * エラーメッセージダイアログを表示します。
@@ -648,21 +648,6 @@ public class MidiSequenceEditor extends JDialog {
         * MIDIイベントリストテーブルビュー(選択中のトラックの中身)
         */
        public class EventListTable extends JTable {
-               private VirtualMidiDevice outputMidiDevice = new AbstractVirtualMidiDevice() {
-                       {
-                               info = new MyInfo();
-                               setMaxReceivers(0); // 送信専用とする(MIDI IN はサポートしない)
-                       }
-                       /**
-                        * MIDIデバイス情報
-                        */
-                       protected MyInfo info;
-                       @Override
-                       public Info getDeviceInfo() { return info; }
-                       class MyInfo extends Info {
-                               protected MyInfo() { super("MIDI Event Table Editor","Unknown vendor","MIDI event table editor",""); }
-                       }
-               };
                /**
                 * 新しいイベントリストテーブルを構築します。
                 * <p>データモデルとして一つのトラックのイベントリストを指定できます。
@@ -1136,14 +1121,16 @@ public class MidiSequenceEditor extends JDialog {
        /**
         * 新しい {@link MidiSequenceEditor} を構築します。
         * @param playlistTableModel このエディタが参照するプレイリストモデル
+        * @param outputMidiDevice イベントテーブルの操作音出力先MIDIデバイス
         */
-       public MidiSequenceEditor(PlaylistTableModel playlistTableModel) {
+       public MidiSequenceEditor(PlaylistTableModel playlistTableModel, VirtualMidiDevice outputMidiDevice) {
+               this.outputMidiDevice = outputMidiDevice;
                sequenceListTable = new SequenceListTable(playlistTableModel);
                trackListTable = new TrackListTable(
                        new SequenceTrackListTableModel(sequenceListTable.getModel(), null, null)
                );
                eventListTable = new EventListTable(new TrackEventListTableModel(trackListTable.getModel(), null));
-               newSequenceDialog = new NewSequenceDialog(playlistTableModel, eventListTable.outputMidiDevice);
+               newSequenceDialog = new NewSequenceDialog(playlistTableModel, outputMidiDevice);
                setTitle("MIDI Editor/Playlist - MIDI Chord Helper");
                setBounds( 150, 200, 900, 500 );
                setLayout(new FlowLayout());
index 67c1422..9fe5fc4 100644 (file)
@@ -250,7 +250,7 @@ public class PianoKeyboard extends JComponent {
        public VirtualMidiDevice midiDevice = new AbstractVirtualMidiDevice() {
                class MyInfo extends Info {
                        protected MyInfo() {
-                               super("Built-in MIDI Keyboard","Unknown vendor","Software MIDI keyboard","");
+                               super("Virtual MIDI Keyboard","Unknown vendor","Software MIDI keyboard","");
                        }
                }
                /**