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