Blame view

VRGNYMusicLights/Sources/TimeLineJPanel.java 4.78 KB
278cdee0   pfrison   VRGNYMusicLights ...
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
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  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.JTextField;
  import javax.swing.ListSelectionModel;
  
  public class TimeLineJPanel extends JPanel implements MouseWheelListener, FocusListener, ActionListener {
  	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;
  
  	public TimeLineJPanel(Interface interf, MusicList list) {
  		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);
  		
  		// timeline
  		
          listModel = new DefaultListModel<>();
          for(Music music : list.getList())
          	listModel.addElement(music);
  		
  		interf.timeLineList = new TimeLineJList(listModel, interf);
  		musicRenderer = new MusicRenderer(this);
  		interf.timeLineList.setCellRenderer(musicRenderer);
  		interf.timeLineList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  		scrollPane = new JScrollPane(interf.timeLineList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  		scrollPane.addMouseWheelListener(this);
  		scrollPane.setWheelScrollingEnabled(false);
  		scrollPane.setPreferredSize(new Dimension(800, 400));
  		add(scrollPane);
  	}
  
  	public double getZoomLevel() { return zoomLevel; }
  	
  	public void updateList(MusicList list) {
  		listModel.clear();
          for(Music music : list.getList())
          	listModel.addElement(music);
  	}
  	
  	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(e.getWheelRotation() < 0)
  	        		zoomLevel *= (double) e.getWheelRotation() * -1.1;
  	        	else
  	        		zoomLevel *= (double) e.getWheelRotation() / 1.1;
  	        	
  	        	int maxWidth = 0;
  	            for(PatternJPanel pattern : musicRenderer.patterns) {
  	            	pattern.refreshPreferedSize();
  	            	int width = pattern.getEnd();
  	            	if(width > maxWidth)
  	            		maxWidth = width;
  	            }
  	            
  	            interf.timeLineList.setPreferredSize(new Dimension(maxWidth, interf.getPreferredSize().height));
  	            scrollPane.repaint();
  	            scrollPane.getViewport().revalidate();
  	        } 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);
  	        }
  		}
      }
  
  	@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();
  	}
  }