OSDN Git Service

リファクタリング
[midichordhelper/MIDIChordHelper.git] / src / camidion / chordhelper / mididevice / SequencerTimeView.java
1 package camidion.chordhelper.mididevice;
2
3 import java.awt.Color;
4
5 import javax.swing.BoxLayout;
6 import javax.swing.JLabel;
7 import javax.swing.JPanel;
8
9 /**
10  * シーケンサの現在位置(分:秒)を表示するビュー
11  */
12 public class SequencerTimeView extends JPanel {
13         private static abstract class TimeLabel extends JLabel {
14                 { setTimeInSecond(0); }
15                 protected String toTimeString(int sec) {
16                         int min = sec/60;
17                         return String.format("%02d:%02d", min, sec - min * 60);
18                 }
19                 private int valueInSec;
20                 private void setTimeInSecond(int sec) {
21                         setText(toTimeString(valueInSec = sec));
22                 }
23                 private void changeTimeInSecond(int sec) {
24                         if(valueInSec != sec) setTimeInSecond(sec);
25                 }
26         }
27         private TimeLabel timePositionLabel = new TimeLabel() {
28                 {
29                         setFont( getFont().deriveFont(getFont().getSize2D() + 4) );
30                         setForeground( new Color(0x80,0x00,0x00) );
31                         setToolTipText("Time position - 現在位置(分:秒)");
32                 }
33         };
34         private TimeLabel timeLengthLabel = new TimeLabel() {
35                 { setToolTipText("Time length - 曲の長さ(分:秒)"); }
36                 protected String toTimeString(int sec) {
37                         return "/"+super.toTimeString(sec);
38                 }
39         };
40         /**
41          * シーケンサの現在位置と曲の長さを(分:秒)で表示するビューを構築します。
42          * @param model MIDIシーケンサモデル
43          */
44         public SequencerTimeView(MidiSequencerModel model) {
45                 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
46                 add(timePositionLabel);
47                 add(timeLengthLabel);
48                 model.addChangeListener(e->{
49                         Object source = e.getSource();
50                         if( source instanceof MidiSequencerModel ) {
51                                 MidiSequencerModel sourceModel = (MidiSequencerModel)source;
52                                 timeLengthLabel.changeTimeInSecond(sourceModel.getMaximum()/1000);
53                                 timePositionLabel.changeTimeInSecond(sourceModel.getValue()/1000);
54                         }
55                 });
56         }
57 }