TimeLineJPanel.java
7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TimeLineJPanel extends JPanel implements MouseWheelListener, FocusListener, ActionListener, ChangeListener, AdjustmentListener {
private static final long serialVersionUID = 1L;
private static final Font titleFont = new Font(new JLabel().getFont().getName(), Font.BOLD, 14);
private static final int SCROLL_SPEED_HORIZONTAL = 30;
private static final int SCROLL_SPEED_VERTICAL = 30;
private DefaultListModel<Music> listModel;
private double zoomLevel = 1;
private JScrollPane scrollPane;
private MusicRenderer musicRenderer;
private Interface interf;
private JTextField currentTick;
private JLabel maxTicks;
private JPanel scrollPanel;
public TimeLineJPanel(Interface interf) {
this.interf = interf;
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
// timing infos
JPanel timingPanel = new JPanel();
timingPanel.setLayout(new BoxLayout(timingPanel, BoxLayout.LINE_AXIS));
JLabel title = new JLabel("Timeline : ");
title.setFont(titleFont);
timingPanel.add(title);
currentTick = new JTextField("00:00.000", 8);
currentTick.setMaximumSize( currentTick.getPreferredSize() );
currentTick.addFocusListener(this);
currentTick.addActionListener(this);
timingPanel.add(currentTick);
maxTicks = new JLabel(" / 00:00.0000 (MM:ss:ms)");
timingPanel.add(maxTicks);
timingPanel.add(Box.createHorizontalGlue());
add(timingPanel);
// scroll pane
scrollPanel = new JPanel();
scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.addMouseWheelListener(this);
scrollPane.setWheelScrollingEnabled(false);
scrollPane.getHorizontalScrollBar().addAdjustmentListener(this);
scrollPane.setPreferredSize(new Dimension(800, 400));
add(scrollPane);
// slider
interf.timeSlider = new JSlider(JSlider.HORIZONTAL, 0, 0, 0);
interf.timeSlider.setMajorTickSpacing(1000);
interf.timeSlider.setMinorTickSpacing(500);
interf.timeSlider.setPaintTicks(true);
interf.timeSlider.setAlignmentX(LEFT_ALIGNMENT);
interf.timeSlider.setEnabled(false);
interf.timeSlider.setUI(new CustomSliderUI(interf.timeSlider));
interf.timeSlider.addChangeListener(this);
scrollPanel.add(interf.timeSlider);
// timeline
listModel = new DefaultListModel<>();
for(Music music : interf.musicList.getList())
listModel.addElement(music);
interf.timeLineList = new TimeLineJList(listModel, interf);
musicRenderer = new MusicRenderer(this);
interf.timeLineList.setCellRenderer(musicRenderer);
interf.timeLineList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
interf.timeLineList.setAlignmentX(LEFT_ALIGNMENT);
scrollPanel.add(interf.timeLineList);
}
public double getZoomLevel() { return zoomLevel; }
public void updateList() {
final MusicList musicList = interf.musicList;
listModel.clear();
for(Music music : musicList.getList())
listModel.addElement(music);
// compute max slider
long maxTick = 0;
for(Music music : musicList.getList()) {
if(music.getMusicPattern().getStop() > maxTick)
maxTick = music.getMusicPattern().getStop();
}
interf.timeSlider.setMaximum((int) maxTick);
// update need a delay and have to be call with another thread (note sure why...)
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(musicList.getList().size() != 0)
updateTimeLine(0);
}
}).start();
}
public void updateTick() {
currentTick.setText(Util.tickToText(interf.tick));
long maxTick = 0;
for(PatternJPanel pattern : musicRenderer.patterns) {
pattern.refreshPreferedSize();
long tick = pattern.getMusic().getMusicPattern().getStop();
if(tick > maxTick)
maxTick = tick;
}
maxTicks.setText(Util.tickToText(maxTick) + " (mm:ss:ms)");
}
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.getSource() == scrollPane) {
if (e.isControlDown()) {
if(interf.musicList.getList().size() != 0)
updateTimeLine(e.getWheelRotation());
} else if(e.isShiftDown()) {
JScrollBar sb = scrollPane.getHorizontalScrollBar();
sb.setValue(sb.getValue() + e.getWheelRotation() * SCROLL_SPEED_HORIZONTAL);
} else {
JScrollBar sb = scrollPane.getVerticalScrollBar();
sb.setValue(sb.getValue() + e.getWheelRotation() * SCROLL_SPEED_VERTICAL);
}
}
}
public void updateTimeLine(int wheelRot) {
if(wheelRot < 0)
zoomLevel *= (double) wheelRot * -1.1;
else if (wheelRot > 0)
zoomLevel *= (double) wheelRot / 1.1;
interf.timeSlider.setEnabled(musicRenderer.patterns.size() != 0);
int maxWidth = 0;
if(musicRenderer.patterns.size() == 0) {
interf.timeSlider.setValue(0);
maxWidth = (int) (scrollPane.getPreferredSize().width - new JScrollBar().getPreferredSize().getWidth()) - 1;
} else {
for(PatternJPanel pattern : musicRenderer.patterns) {
pattern.refreshPreferedSize();
int width = pattern.getEnd();
if(width > maxWidth)
maxWidth = width;
}
}
interf.timeSlider.setMaximumSize(new Dimension(maxWidth, interf.timeSlider.getPreferredSize().height));
interf.timeLineList.setMaximumSize(new Dimension(maxWidth, PatternJPanel.PATTERN_HEIGHT * musicRenderer.patterns.size()));
interf.timeLineList.setPreferredSize(new Dimension(maxWidth, PatternJPanel.PATTERN_HEIGHT * musicRenderer.patterns.size()));
scrollPanel.setPreferredSize(new Dimension(maxWidth, PatternJPanel.PATTERN_HEIGHT * musicRenderer.patterns.size()));
scrollPane.repaint();
scrollPane.getViewport().revalidate();
scrollPanel.revalidate();
}
@Override
public void focusLost(FocusEvent e) {
if(e.getSource() == currentTick) {
interf.tick = Util.textToTick(currentTick.getText());
currentTick.setText(Util.tickToText(interf.tick));
interf.computeTick();
}
}
@Override
public void focusGained(FocusEvent e) {
if(e.getSource() == currentTick)
currentTick.setCaretPosition(currentTick.getDocument().getLength());
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == currentTick)
interf.getContentPane().requestFocus();
}
@Override
public void stateChanged(ChangeEvent e) {
if(e.getSource() == interf.timeSlider) {
interf.tick = interf.timeSlider.getValue();
interf.computeTick();
}
}
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
if(e.getSource() == scrollPane.getHorizontalScrollBar()) {
musicRenderer.setInfoPadding(scrollPane.getHorizontalScrollBar().getValue());
scrollPane.repaint();
scrollPane.getViewport().revalidate();
scrollPanel.revalidate();
}
}
}