]> ruin.nu Git - moosique.git/blob - MooTrackView.java
no 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
23         public MooTrackView (Track track) {
24                 this.track = track;
25                 setPreferredSize(new Dimension(200, 200));
26                 setLayout(new BorderLayout());
27                 this.setBorder(BorderFactory.createLineBorder(Color.black));
28
29                 title = new MooTrackTitle(track);
30                 title.setBorder(BorderFactory.createLineBorder(Color.black));
31                 add(title, BorderLayout.NORTH);
32
33                 notes = new NoteArea(track);    
34                 notes.setBackground(Color.white);
35                 notes.setBorder(BorderFactory.createLineBorder(Color.black));
36
37                 popup = new JPopupMenu();
38                 menuItem = new JMenuItem("Add...");
39                 menuItem.addActionListener(this);
40                 popup.add(menuItem);
41                 menuItem = new JMenuItem("Preferences...");
42                 menuItem.addActionListener(this);
43                 popup.add(menuItem);
44
45                 notes.addMouseListener(new PopupListener());
46                 add(notes, BorderLayout.CENTER);
47         }
48
49         public void actionPerformed(ActionEvent e) {}
50
51         public Track getTrack() {
52                 return track;
53         }
54         
55
56         /** 
57          * Updates the track view.
58          */
59         public void update() {
60                 repaint();
61         }
62
63         class NoteArea extends JPanel {
64                 public static final int NOTE_HEIGHT = 10;
65                 public static final int NOTE_WIDTH = 40;
66                 private int trackLength;
67
68                 public NoteArea(Track track) {
69                         System.out.println("Creating track view...");
70                         setLayout(null);
71                         trackLength = 140;
72                         MidiEvent note;
73                         MooNoteElement elem;
74                         boolean isOccupied;
75                         int x, y, height;
76                         Insets insets = getInsets();
77                         for (int i = 0; i < track.size(); i++) {
78                                 note = track.get(i);
79                                 if (note instanceof MooNote) {
80                                         // Adds the note element to the note area.
81                                         MooNote mn = (MooNote)note;
82                                         elem = new MooNoteElement(mn);
83                                         add(elem);
84
85                                         // Places the note element in the appropriate place.
86                                         x = insets.left;
87                                         y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
88                                         height = NOTE_HEIGHT;
89                                         // height = (mn.getDuration() / 24) * NOTE_HEIGHT;
90                                         // System.out.println("Comp at: " + x + ", " + y + " is: " + findComponentAt(x + 10, y + 10));
91                                         while(findComponentAt(x, y) instanceof MooNoteElement ||
92                                               findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
93                                         elem.setBounds(x, y, NOTE_WIDTH, height);
94                                 }
95                         }
96                         validate();
97                 }
98
99                 public void paintComponent(Graphics g) {
100                         super.paintComponent(g);
101                         Graphics2D g2 = (Graphics2D)g;
102                         for (int c = 0; c < (trackLength*NOTE_HEIGHT); c += NOTE_HEIGHT) {
103                                 for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
104                                         box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
105                                         g2.setColor(Color.gray);
106                                         g2.draw(box);
107                                 }
108                         }
109                 }
110         }
111
112         class PopupListener extends MouseAdapter {
113                 public void mousePressed(MouseEvent e) {
114                         maybeShowPopup(e);
115                 }
116
117                 public void mouseReleased(MouseEvent e) {
118                         maybeShowPopup(e);
119                 }
120
121                 private void maybeShowPopup(MouseEvent e) {
122                         if (e.isPopupTrigger()) {
123                                 popup.show(e.getComponent(), e.getX(), e.getY());
124                         }
125                 }
126         }
127 }