OSDN Git Service

リファクタリング(MIDIエディタの内部クラスを外出し)
[midichordhelper/MIDIChordHelper.git] / src / camidion / chordhelper / midieditor / MidiSequenceEditorDialog.java
1 package camidion.chordhelper.midieditor;
2
3 import java.awt.Container;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.datatransfer.DataFlavor;
7 import java.awt.event.ActionEvent;
8 import java.io.File;
9 import java.util.List;
10
11 import javax.sound.midi.Sequencer;
12 import javax.swing.AbstractAction;
13 import javax.swing.Action;
14 import javax.swing.Box;
15 import javax.swing.BoxLayout;
16 import javax.swing.Icon;
17 import javax.swing.JButton;
18 import javax.swing.JCheckBox;
19 import javax.swing.JComboBox;
20 import javax.swing.JDialog;
21 import javax.swing.JLabel;
22 import javax.swing.JOptionPane;
23 import javax.swing.JPanel;
24 import javax.swing.JScrollPane;
25 import javax.swing.JSplitPane;
26 import javax.swing.TransferHandler;
27 import javax.swing.border.EtchedBorder;
28
29 import camidion.chordhelper.ButtonIcon;
30 import camidion.chordhelper.ChordHelperApplet;
31 import camidion.chordhelper.mididevice.MidiSequencerModel;
32 import camidion.chordhelper.mididevice.VirtualMidiDevice;
33
34 /**
35  * MIDIエディタ(MIDI Editor/Playlist for MIDI Chord Helper)
36  *
37  * @author
38  *      Copyright (C) 2006-2017 Akiyoshi Kamide
39  *      http://www.yk.rim.or.jp/~kamide/music/chordhelper/
40  */
41 public class MidiSequenceEditorDialog extends JDialog {
42         /**
43          * このダイアログを開きます。表示されていなければ表示し、すでに表示されていたら最前面に移動します。
44          */
45         public void open() { if( isVisible() ) toFront(); else setVisible(true); }
46         /**
47          * このダイアログを表示するボタン用アクション
48          */
49         public final Action openAction = new AbstractAction("Edit/Playlist/Speed", new ButtonIcon(ButtonIcon.EDIT_ICON)) {
50                 {
51                         String tooltip = "MIDIシーケンスの編集/プレイリスト/再生速度調整";
52                         putValue(Action.SHORT_DESCRIPTION, tooltip);
53                 }
54                 @Override
55                 public void actionPerformed(ActionEvent e) { open(); }
56         };
57         /**
58          * ドロップされた複数のMIDIファイルを読み込むハンドラー
59          */
60         public final TransferHandler transferHandler = new TransferHandler() {
61                 @Override
62                 public boolean canImport(TransferSupport support) {
63                         return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
64                 }
65                 @SuppressWarnings("unchecked")
66                 @Override
67                 public boolean importData(TransferSupport support) {
68                         try {
69                                 Object data = support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
70                                 play((List<File>)data);
71                                 return true;
72                         } catch (Exception ex) {
73                                 JOptionPane.showMessageDialog(
74                                                 MidiSequenceEditorDialog.this, ex,
75                                                 ChordHelperApplet.VersionInfo.NAME,
76                                                 JOptionPane.ERROR_MESSAGE);
77                                 return false;
78                         }
79                 }
80         };
81         /**
82          * このエディタダイアログが表示しているプレイリストモデルを返します。
83          * @return プレイリストモデル
84          */
85         public PlaylistTableModel getPlaylistModel() {
86                 return playlistTable.getModel();
87         }
88         /**
89          * 指定されたリストに格納されたMIDIファイルを読み込んで再生します。
90          * すでに再生されていた場合、このエディタダイアログを表示します。
91          * @param fileList 読み込むMIDIファイルのリスト
92          */
93         public void play(List<File> fileList) {
94                 play(playlistTable.add(fileList));
95         }
96         /**
97          * 指定されたインデックス値(先頭が0)のMIDIシーケンスから再生します。
98          * すでに再生されていた場合、このエディタダイアログを表示します。
99          * @param index プレイリスト内にあるMIDIシーケンスのインデックス値
100          */
101         public void play(int index) {
102                 try {
103                         PlaylistTableModel playlist = getPlaylistModel();
104                         MidiSequencerModel sequencerModel = playlist.getSequencerModel();
105                         if( sequencerModel.getSequencer().isRunning() ) { open(); return; }
106                         if( index >= 0 ) playlist.play(index);
107                 } catch (Exception e) {
108                         JOptionPane.showMessageDialog(this, e, ChordHelperApplet.VersionInfo.NAME, JOptionPane.ERROR_MESSAGE);
109                 }
110         }
111
112         static final Icon deleteIcon = new ButtonIcon(ButtonIcon.X_ICON);
113         /**
114          * 新しいMIDIシーケンスを生成するダイアログ
115          */
116         public NewSequenceDialog newSequenceDialog;
117         /**
118          * プレイリストビュー(シーケンスリスト)
119          */
120         public PlaylistTable playlistTable;
121         /**
122          * 新しい {@link MidiSequenceEditorDialog} を構築します。
123          * @param playlistTableModel このエディタが参照するプレイリストモデル
124          * @param eventDialog MIDIイベント入力ダイアログ
125          * @param outputMidiDevice イベントテーブルの操作音出力先MIDIデバイス
126          * @param midiDeviceDialogOpenAction MIDIデバイスダイアログを開くアクション
127          */
128         public MidiSequenceEditorDialog(PlaylistTableModel playlistTableModel, MidiEventDialog eventDialog, VirtualMidiDevice outputMidiDevice, Action midiDeviceDialogOpenAction) {
129                 playlistTable = new PlaylistTable(playlistTableModel, midiDeviceDialogOpenAction);
130                 MidiEventTable eventListTable = new MidiEventTable(playlistTableModel.emptyEventListTableModel, eventDialog, outputMidiDevice);
131                 SequenceTrackListTable trackListTable = new SequenceTrackListTable(playlistTableModel.emptyTrackListTableModel, eventListTable);
132                 newSequenceDialog = new NewSequenceDialog(playlistTableModel, outputMidiDevice);
133                 setTitle("MIDI Editor/Playlist - "+ChordHelperApplet.VersionInfo.NAME);
134                 setBounds( 150, 200, 900, 500 );
135                 setLayout(new FlowLayout());
136                 setTransferHandler(transferHandler);
137                 //
138                 // パネルレイアウト
139                 JPanel playlistPanel = new JPanel() {{
140                         JPanel playlistOperationPanel = new JPanel() {{
141                                 setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
142                                 add(Box.createRigidArea(new Dimension(10, 0)));
143                                 add(new JButton(newSequenceDialog.openAction) {
144                                         { setMargin(ChordHelperApplet.ZERO_INSETS); }
145                                 });
146                                 if( playlistTable.midiFileChooser != null ) {
147                                         add( Box.createRigidArea(new Dimension(5, 0)) );
148                                         add(new JButton(playlistTable.midiFileChooser.openMidiFileAction) {
149                                                 { setMargin(ChordHelperApplet.ZERO_INSETS); }
150                                         });
151                                 }
152                                 if(playlistTable.base64EncodeAction != null) {
153                                         add(Box.createRigidArea(new Dimension(5, 0)));
154                                         add(new JButton(playlistTable.base64EncodeAction) {
155                                                 { setMargin(ChordHelperApplet.ZERO_INSETS); }
156                                         });
157                                 }
158                                 add(Box.createRigidArea(new Dimension(5, 0)));
159                                 add(new JButton(playlistTableModel.getMoveToTopAction()) {
160                                         { setMargin(ChordHelperApplet.ZERO_INSETS); }
161                                 });
162                                 add(Box.createRigidArea(new Dimension(5, 0)));
163                                 add(new JButton(playlistTableModel.getMoveToBottomAction()) {
164                                         { setMargin(ChordHelperApplet.ZERO_INSETS); }
165                                 });
166                                 if( playlistTable.midiFileChooser != null ) {
167                                         add(Box.createRigidArea(new Dimension(5, 0)));
168                                         add(new JButton(playlistTable.midiFileChooser.saveMidiFileAction) {
169                                                 { setMargin(ChordHelperApplet.ZERO_INSETS); }
170                                         });
171                                 }
172                                 add( Box.createRigidArea(new Dimension(5, 0)) );
173                                 add(new JButton(playlistTable.deleteSequenceAction) {
174                                         { setMargin(ChordHelperApplet.ZERO_INSETS); }
175                                 });
176                                 add( Box.createRigidArea(new Dimension(5, 0)) );
177                                 add(new SequencerSpeedSlider(playlistTableModel.getSequencerModel().speedSliderModel));
178                                 add( Box.createRigidArea(new Dimension(5, 0)) );
179                                 add(new JPanel() {{
180                                         setBorder(new EtchedBorder());
181                                         MidiSequencerModel sequencerModel = playlistTableModel.getSequencerModel();
182                                         add(new JLabel("Sync Master"));
183                                         add(new JComboBox<Sequencer.SyncMode>(sequencerModel.masterSyncModeModel));
184                                         add(new JLabel("Slave"));
185                                         add(new JComboBox<Sequencer.SyncMode>(sequencerModel.slaveSyncModeModel));
186                                 }});
187                         }};
188                         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
189                         add(new JScrollPane(playlistTable));
190                         add(Box.createRigidArea(new Dimension(0, 10)));
191                         add(playlistOperationPanel);
192                         add(Box.createRigidArea(new Dimension(0, 10)));
193                 }};
194                 JPanel trackListPanel = new JPanel() {{
195                         JPanel trackListOperationPanel = new JPanel() {{
196                                 add(new JButton(trackListTable.addTrackAction) {{ setMargin(ChordHelperApplet.ZERO_INSETS); }});
197                                 add(new JButton(trackListTable.deleteTrackAction) {{ setMargin(ChordHelperApplet.ZERO_INSETS); }});
198                         }};
199                         setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
200                         add(trackListTable.titleLabel);
201                         add(Box.createRigidArea(new Dimension(0, 5)));
202                         add(new JScrollPane(trackListTable));
203                         add(Box.createRigidArea(new Dimension(0, 5)));
204                         add(trackListOperationPanel);
205                 }};
206                 JPanel eventListPanel = new JPanel() {{
207                         JPanel eventListOperationPanel = new JPanel() {{
208                                 add(new JCheckBox("Pair NoteON/OFF") {{
209                                         setModel(eventListTable.pairNoteOnOffModel);
210                                         setToolTipText("NoteON/OFFをペアで同時選択する");
211                                 }});
212                                 add(new JButton(eventListTable.queryJumpEventAction) {{ setMargin(ChordHelperApplet.ZERO_INSETS); }});
213                                 add(new JButton(eventListTable.queryAddEventAction) {{ setMargin(ChordHelperApplet.ZERO_INSETS); }});
214                                 add(new JButton(eventListTable.copyEventAction) {{ setMargin(ChordHelperApplet.ZERO_INSETS); }});
215                                 add(new JButton(eventListTable.cutEventAction) {{ setMargin(ChordHelperApplet.ZERO_INSETS); }});
216                                 add(new JButton(eventListTable.queryPasteEventAction) {{ setMargin(ChordHelperApplet.ZERO_INSETS); }});
217                                 add(new JButton(eventListTable.deleteEventAction) {{ setMargin(ChordHelperApplet.ZERO_INSETS); }});
218                         }};
219                         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
220                         add(eventListTable.titleLabel);
221                         add(eventListTable.scrollPane);
222                         add(eventListOperationPanel);
223                 }};
224                 Container cp = getContentPane();
225                 cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
226                 cp.add(Box.createVerticalStrut(2));
227                 cp.add(
228                         new JSplitPane(JSplitPane.VERTICAL_SPLIT, playlistPanel,
229                                 new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, trackListPanel, eventListPanel) {{
230                                         setDividerLocation(300);
231                                 }}
232                         ) {{
233                                 setDividerLocation(160);
234                         }}
235                 );
236         }
237
238 }