Blame view

VRGNYMusicLights/Sources/ApplicationJava/TimeLineJPanel.java 7.34 KB
278cdee0   pfrison   VRGNYMusicLights ...
1
2
3
4
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
d175ba55   pfrison   VRGNYMusicLight a...
5
6
  import java.awt.event.AdjustmentEvent;
  import java.awt.event.AdjustmentListener;
278cdee0   pfrison   VRGNYMusicLights ...
7
8
9
10
11
12
13
14
15
16
17
18
  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;
d175ba55   pfrison   VRGNYMusicLight a...
19
  import javax.swing.JSlider;
278cdee0   pfrison   VRGNYMusicLights ...
20
21
  import javax.swing.JTextField;
  import javax.swing.ListSelectionModel;
d175ba55   pfrison   VRGNYMusicLight a...
22
23
  import javax.swing.event.ChangeEvent;
  import javax.swing.event.ChangeListener;
278cdee0   pfrison   VRGNYMusicLights ...
24
  
d175ba55   pfrison   VRGNYMusicLight a...
25
  public class TimeLineJPanel extends JPanel implements MouseWheelListener, FocusListener, ActionListener, ChangeListener, AdjustmentListener {
278cdee0   pfrison   VRGNYMusicLights ...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  	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;
d175ba55   pfrison   VRGNYMusicLight a...
40
  	private JPanel scrollPanel;
278cdee0   pfrison   VRGNYMusicLights ...
41
  
d175ba55   pfrison   VRGNYMusicLight a...
42
  	public TimeLineJPanel(Interface interf) {
278cdee0   pfrison   VRGNYMusicLights ...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  		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);
  		
d175ba55   pfrison   VRGNYMusicLight a...
67
  		// scroll pane
278cdee0   pfrison   VRGNYMusicLights ...
68
  		
d175ba55   pfrison   VRGNYMusicLight a...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  		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
278cdee0   pfrison   VRGNYMusicLights ...
91
  		
d175ba55   pfrison   VRGNYMusicLight a...
92
93
94
  		listModel = new DefaultListModel<>();
  		for(Music music : interf.musicList.getList())
  			listModel.addElement(music);
278cdee0   pfrison   VRGNYMusicLights ...
95
96
97
98
  		interf.timeLineList = new TimeLineJList(listModel, interf);
  		musicRenderer = new MusicRenderer(this);
  		interf.timeLineList.setCellRenderer(musicRenderer);
  		interf.timeLineList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
d175ba55   pfrison   VRGNYMusicLight a...
99
100
  		interf.timeLineList.setAlignmentX(LEFT_ALIGNMENT);
  		scrollPanel.add(interf.timeLineList);
278cdee0   pfrison   VRGNYMusicLights ...
101
102
103
104
  	}
  
  	public double getZoomLevel() { return zoomLevel; }
  	
d175ba55   pfrison   VRGNYMusicLight a...
105
106
107
  	public void updateList() {
  		final MusicList musicList = interf.musicList;
  		
278cdee0   pfrison   VRGNYMusicLights ...
108
  		listModel.clear();
d175ba55   pfrison   VRGNYMusicLight a...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  		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();
278cdee0   pfrison   VRGNYMusicLights ...
133
134
135
136
  	}
  	
  	public void updateTick() {
  		currentTick.setText(Util.tickToText(interf.tick));
d175ba55   pfrison   VRGNYMusicLight a...
137
138
139
140
141
142
143
144
  		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)");
278cdee0   pfrison   VRGNYMusicLights ...
145
146
147
148
  	}
  	
  	public void mouseWheelMoved(MouseWheelEvent e) {
  		if(e.getSource() == scrollPane) {
d175ba55   pfrison   VRGNYMusicLight a...
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
  			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;
  			}
278cdee0   pfrison   VRGNYMusicLights ...
180
  		}
d175ba55   pfrison   VRGNYMusicLight a...
181
182
183
184
185
186
187
188
189
  		
  		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();
  	}
278cdee0   pfrison   VRGNYMusicLights ...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  
  	@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();
  	}
d175ba55   pfrison   VRGNYMusicLight a...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  
  	@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();
  		}
  	}
278cdee0   pfrison   VRGNYMusicLights ...
228
  }