]> 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         class NoteArea extends JPanel {
55
56                 public NoteArea(Track track) {
57                         MidiEvent note;
58                         for (int i = 0; i < track.size(); i++) {
59                                 note = track.get(i);
60                                 if (note instanceof MooNote) {
61                                         add(new MooNoteElement((MooNote)note));
62                                 }
63                         }
64                         validate();
65                 }
66
67                 public void paintComponent(Graphics g) {
68                         super.paintComponent(g);
69                         Graphics2D g2 = (Graphics2D)g;
70                         for (int c = 0; c < 1000; c += 20) {
71                                 for (int r = 0; r < 200; r += 20) {
72                                         box = new Rectangle(r, c, 20, 20);
73                                         g2.setColor(Color.gray);
74                                         g2.draw(box);
75                                 }
76                         }
77                 }
78         }
79
80         class PopupListener extends MouseAdapter {
81                 public void mousePressed(MouseEvent e) {
82                         maybeShowPopup(e);
83                 }
84
85                 public void mouseReleased(MouseEvent e) {
86                         maybeShowPopup(e);
87                 }
88
89                 private void maybeShowPopup(MouseEvent e) {
90                         if (e.isPopupTrigger()) {
91                                 popup.show(e.getComponent(), e.getX(), e.getY());
92                         }
93                 }
94         }
95 }