]> ruin.nu Git - moosique.git/blob - MooTrackView.java
Fixed some bugs
[moosique.git] / MooTrackView.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.sound.midi.*;
5
6 /**
7  * Graphical representation of a MIDI track.
8  * 
9  * @author  Andersson , Andreen, Lanneskog, Pehrson
10  * @version 1
11  */
12
13 public class MooTrackView extends JPanel implements ActionListener {
14
15         private Track track;
16         private MooTrackTitle title;
17         private NoteArea notes;
18         private Rectangle box;
19         private JPopupMenu popup;
20         private JMenuItem menuItem;
21
22         public MooTrackView (Track track) {
23                 this.track = track;
24                 setPreferredSize(new Dimension(200, 200));
25                 setLayout(new BorderLayout());
26                 this.setBorder(BorderFactory.createLineBorder(Color.black));
27
28                 title = new MooTrackTitle(track);
29                 title.setBorder(BorderFactory.createLineBorder(Color.black));
30                 add(title, BorderLayout.NORTH);
31
32                 notes = new NoteArea(track);    
33                 notes.setBackground(Color.white);
34                 notes.setBorder(BorderFactory.createLineBorder(Color.black));
35
36                 popup = new JPopupMenu();
37                 menuItem = new JMenuItem("Add...");
38                 menuItem.addActionListener(this);
39                 popup.add(menuItem);
40                 menuItem = new JMenuItem("Preferences...");
41                 menuItem.addActionListener(this);
42                 popup.add(menuItem);
43
44                 notes.addMouseListener(new PopupListener());
45                 add(notes, BorderLayout.CENTER);
46         }
47
48         public void actionPerformed(ActionEvent e) {}
49
50         public Track getTrack() {
51                 return track;
52         }
53         
54
55         /** 
56          * Updates the track view.
57          */
58         public void update() {
59         
60         }
61
62         class NoteArea extends JPanel {
63
64                 public NoteArea(Track track) {
65                         MidiEvent note;
66                         for (int i = 0; i < track.size(); i++) {
67                                 note = track.get(i);
68                                 if (note instanceof MooNote) {
69                                         add(new MooNoteElement((MooNote)note));
70                                 }
71                         }
72                         validate();
73                 }
74
75                 public void paintComponent(Graphics g) {
76                         super.paintComponent(g);
77                         Graphics2D g2 = (Graphics2D)g;
78                         for (int c = 0; c < 1000; c += 20) {
79                                 for (int r = 0; r < 200; r += 20) {
80                                         box = new Rectangle(r, c, 20, 20);
81                                         g2.setColor(Color.gray);
82                                         g2.draw(box);
83                                 }
84                         }
85                 }
86         }
87
88         class PopupListener extends MouseAdapter {
89                 public void mousePressed(MouseEvent e) {
90                         maybeShowPopup(e);
91                 }
92
93                 public void mouseReleased(MouseEvent e) {
94                         maybeShowPopup(e);
95                 }
96
97                 private void maybeShowPopup(MouseEvent e) {
98                         if (e.isPopupTrigger()) {
99                                 popup.show(e.getComponent(), e.getX(), e.getY());
100                         }
101                 }
102         }
103 }