1 | package org.apollo.gui;
|
---|
2 |
|
---|
3 | import java.awt.Event;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.event.ComponentEvent;
|
---|
6 | import java.awt.event.ComponentListener;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 | import java.awt.event.MouseListener;
|
---|
9 |
|
---|
10 | import javax.sound.sampled.AudioFormat;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 |
|
---|
13 | import org.apollo.util.AudioMath;
|
---|
14 | import org.expeditee.gio.swing.SwingConversions;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * A time scale along the X axis that can be rendered.
|
---|
18 | *
|
---|
19 | * Observes a SampledTrackGraphView - keeps time axis consistent with
|
---|
20 | * current time scale on the observed SampledTrackGraphView.
|
---|
21 | *
|
---|
22 | * @author Brook Novak
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class TimeAxisPanel extends JPanel implements TimelineChangeListener, MouseListener {
|
---|
26 |
|
---|
27 | private static final long serialVersionUID = 1L;
|
---|
28 |
|
---|
29 | private SampledTrackGraphView observedSubject;
|
---|
30 |
|
---|
31 | private TimeAxis timeAxis = new TimeAxis();
|
---|
32 |
|
---|
33 | public TimeAxisPanel(SampledTrackGraphView graph)
|
---|
34 | {
|
---|
35 | assert (graph != null);
|
---|
36 |
|
---|
37 | observedSubject = graph;
|
---|
38 | graph.addTimeScaleChangeListener(this); // observe
|
---|
39 |
|
---|
40 | // Auto-adjust scale when component resizes.
|
---|
41 | this.addComponentListener(new ComponentListener()
|
---|
42 | {
|
---|
43 | public void componentHidden(ComponentEvent e)
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void componentMoved(ComponentEvent e)
|
---|
48 | {
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void componentResized(ComponentEvent e)
|
---|
52 | {
|
---|
53 | updateAxis();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void componentShown(ComponentEvent e)
|
---|
57 | {
|
---|
58 | }
|
---|
59 | });
|
---|
60 |
|
---|
61 | updateAxis();
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | public void timelineChanged(Event e)
|
---|
66 | {
|
---|
67 | updateAxis();
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | private void updateAxis()
|
---|
72 | {
|
---|
73 | // Is track model even set yet?
|
---|
74 | if (observedSubject.getSampledTrackModel() == null || getWidth() == 0) return;
|
---|
75 |
|
---|
76 | AudioFormat format = observedSubject.getSampledTrackModel().getFormat();
|
---|
77 |
|
---|
78 | long startTimeMS = AudioMath.framesToMilliseconds(observedSubject.getTimeScaleStart(), format);
|
---|
79 |
|
---|
80 | long timeLengthMS = AudioMath.framesToMilliseconds(observedSubject.getTimeScaleLength(), format);
|
---|
81 |
|
---|
82 | long totalTrackMSLength = AudioMath.framesToMilliseconds(observedSubject.getSampledTrackModel().getFrameCount(), format);
|
---|
83 |
|
---|
84 | timeAxis.setAxis(
|
---|
85 | startTimeMS,
|
---|
86 | timeLengthMS,
|
---|
87 | observedSubject.getTimeScaleLength(),
|
---|
88 | totalTrackMSLength,
|
---|
89 | getWidth());
|
---|
90 |
|
---|
91 | repaint();
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public void paint(Graphics g)
|
---|
97 | {
|
---|
98 | timeAxis.paint(0, 0, getHeight(), SwingConversions.fromSwingColor(getBackground()));
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** BELOW: TRAPS MOUSE EVENTS TO GIVE FOCUS */
|
---|
102 | public void mouseClicked(MouseEvent e) {
|
---|
103 | }
|
---|
104 | public void mouseEntered(MouseEvent e) {
|
---|
105 | }
|
---|
106 | public void mouseExited(MouseEvent e) {
|
---|
107 | }
|
---|
108 | public void mousePressed(MouseEvent e) {
|
---|
109 | }
|
---|
110 | public void mouseReleased(MouseEvent e) {
|
---|
111 | }
|
---|
112 | }
|
---|