]> 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 import java.util.*;
6
7 /**
8  * Graphical representation of a MIDI track.
9  * 
10  * @author  Andersson , Andreen, Lanneskog, Pehrson
11  * @version 1
12  */
13
14 public class MooTrackView extends JPanel implements ActionListener {
15
16         private Track track;
17         private MooTrackTitle title;
18         private NoteArea notes;
19         private Rectangle box;
20         private JPopupMenu popup;
21         private JPopupMenu notePopup;
22         private JMenuItem menuItem;
23         protected static int viewLength = 0;
24         public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
25
26         public MooTrackView (Track track) {
27                 super(true);
28                 this.track = track;
29                 //setPreferredSize(new Dimension(200, 9000));
30                 setLayout(new BorderLayout());
31                 // setBorder(BorderFactory.createLineBorder(Color.black));
32
33                 //title = new MooTrackTitle(track);
34                 //title.setBorder(BorderFactory.createLineBorder(Color.black));
35                 //add(title, BorderLayout.NORTH);
36
37                 notes = new NoteArea(track);    
38                 notes.setBackground(Color.white);
39                 notes.setBorder(BorderFactory.createLineBorder(Color.black));
40
41                 popup = new JPopupMenu();
42                 menuItem = new JMenuItem("Add...");
43                 menuItem.addActionListener(this);
44                 popup.add(menuItem);
45
46
47                 notePopup = new JPopupMenu();
48                 menuItem = new JMenuItem("Preferences...");
49                 menuItem.addActionListener(this);
50                 notePopup.add(menuItem);
51
52                 notes.addMouseListener(new PopupListener());
53                 add(notes, BorderLayout.CENTER);
54         }
55
56         public void actionPerformed(ActionEvent e) {}
57
58         public Track getTrack() {
59                 return track;
60         }
61
62
63         /** 
64          * Updates the track view.
65          */
66         public void update(long tickPosition) {
67                 repaint();
68         }
69
70         class NoteArea extends JPanel {
71                 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
72                 private ArrayList rects;
73
74                 public NoteArea(Track track) {
75                         // Configuring panel
76                         super(true);
77                         setLayout(null);
78                         setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
79
80                         // Creating temporary variables
81                         MidiEvent note;
82                         MooNoteElement elem;
83                         int x, y, height;
84                         int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
85                         rects = new ArrayList(track.size() / 2);
86
87                         // Placing note elements
88                         Insets insets = getInsets();
89                         for (int i = 0; i < track.size(); i++) {
90                                 note = track.get(i);
91                                 if (note instanceof MooNote) {
92                                         // Adds the note element to the note area.
93                                         MooNote mn = (MooNote)note;
94                                         elem = new MooNoteElement(mn);
95                                         add(elem);
96
97                                         // Moves the note element to the appropriate place.
98                                         x = insets.left;
99                                         y = insets.top + (int)(mn.getTick() / beatsPerSixteenth) * NOTE_HEIGHT;
100                                         height = (mn.getDuration() / beatsPerSixteenth) * NOTE_HEIGHT;
101                                         if (height == 0) height = NOTE_HEIGHT;
102                                         Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
103                                         while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
104                                         elem.setBounds(r);
105                                         rects.add(r);
106                                         if (viewLength < (y + height)) viewLength = y + height;
107                                 }
108                                 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength));
109                         }
110                         validate();
111                 }
112                 
113                 private boolean isOccupied(Rectangle r) {
114                         Iterator it = rects.iterator();
115                         while (it.hasNext()) {
116                                 if(r.intersects((Rectangle)it.next())) return true;
117                         }
118                         return false;
119                 }
120
121                 public void paintComponent(Graphics g) {
122                         super.paintComponent(g);
123                         Graphics2D g2 = (Graphics2D)g;
124                         for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
125                                 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
126                                         box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
127                                         g2.setColor(Color.gray);
128                                         g2.draw(box);
129                                 }
130                         }
131                 }
132         }
133
134         class PopupListener extends MouseAdapter {
135                 public void mousePressed(MouseEvent e) {
136                         maybeShowPopup(e);
137                 }
138
139                 public void mouseReleased(MouseEvent e) {
140                         maybeShowPopup(e);
141                 }
142
143                 private void maybeShowPopup(MouseEvent e) {
144                         if (e.isPopupTrigger()) {
145                                 if (findComponentAt(e.getX(), e.getY()) instanceof MooNoteElement)
146                                         notePopup.show(e.getComponent(), e.getX(), e.getY());
147                                 else
148                                         popup.show(e.getComponent(), e.getX(), e.getY());
149                         }
150                 }
151         }
152 }