]> ruin.nu Git - moosique.git/blob - MooTrackView.java
*** empty log message ***
[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 static final int NOTE_SIZE = 20;
65
66                 public NoteArea(Track track) {
67                         MidiEvent note;
68                         MooNoteElement elem;
69                         boolean isOccupied;
70                         int x, y, height;
71                         Insets insets = getInsets();
72                         for (int i = 0; i < track.size(); i++) {
73                                 note = track.get(i);
74                                 if (note instanceof MooNote) {
75                                         MooNote mn = (MooNote)note;
76                                         elem = new MooNoteElement(mn);
77                                         add(elem);
78                                         x = insets.left;
79                                         y = insets.top + (int)(mn.getTick() / 24) * NOTE_SIZE;
80                                         height = (mn.getDuration() / 24) * NOTE_SIZE;
81                                         while(findComponentAt(x, y) != this || findComponentAt(x, y + height - 1) != this) x += NOTE_SIZE;
82                                         elem.setBounds(x, y, NOTE_SIZE, height);
83                                 }
84                         }
85                 }
86
87                 public void paintComponent(Graphics g) {
88                         super.paintComponent(g);
89                         Graphics2D g2 = (Graphics2D)g;
90                         for (int c = 0; c < 1000; c += 20) {
91                                 for (int r = 0; r < 200; r += 20) {
92                                         box = new Rectangle(r, c, 20, 20);
93                                         g2.setColor(Color.gray);
94                                         g2.draw(box);
95                                 }
96                         }
97                 }
98         }
99
100         class PopupListener extends MouseAdapter {
101                 public void mousePressed(MouseEvent e) {
102                         maybeShowPopup(e);
103                 }
104
105                 public void mouseReleased(MouseEvent e) {
106                         maybeShowPopup(e);
107                 }
108
109                 private void maybeShowPopup(MouseEvent e) {
110                         if (e.isPopupTrigger()) {
111                                 popup.show(e.getComponent(), e.getX(), e.getY());
112                         }
113                 }
114         }
115 }