]> 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 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                         rects = new ArrayList(track.size() / 2);
85
86                         // Placing note elements
87                         Insets insets = getInsets();
88                         for (int i = 0; i < track.size(); i++) {
89                                 note = track.get(i);
90                                 if (note instanceof MooNote) {
91                                         // Adds the note element to the note area.
92                                         MooNote mn = (MooNote)note;
93                                         elem = new MooNoteElement(mn);
94                                         add(elem);
95
96                                         // Moves the note element to the appropriate place.
97                                         x = insets.left;
98                                         y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
99                                         height = (mn.getDuration() / 24) * NOTE_HEIGHT;
100                                         if (height == 0) height = NOTE_HEIGHT;
101                                         Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
102                                         while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
103                                         elem.setBounds(r);
104                                         rects.add(r);
105                                         if (viewLength < (y + height)) viewLength = y + height;
106                                 }
107                                 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength));
108                         }
109                         validate();
110                 }
111                 
112                 private boolean isOccupied(Rectangle r) {
113                         Iterator it = rects.iterator();
114                         while (it.hasNext()) {
115                                 if(r.intersects((Rectangle)it.next())) return true;
116                         }
117                         return false;
118                 }
119
120                 public void paintComponent(Graphics g) {
121                         super.paintComponent(g);
122                         Graphics2D g2 = (Graphics2D)g;
123                         for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
124                                 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
125                                         box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
126                                         g2.setColor(Color.gray);
127                                         g2.draw(box);
128                                 }
129                         }
130                 }
131         }
132
133         class PopupListener extends MouseAdapter {
134                 public void mousePressed(MouseEvent e) {
135                         maybeShowPopup(e);
136                 }
137
138                 public void mouseReleased(MouseEvent e) {
139                         maybeShowPopup(e);
140                 }
141
142                 private void maybeShowPopup(MouseEvent e) {
143                         if (e.isPopupTrigger()) {
144                                 if (findComponentAt(e.getX(), e.getY()) instanceof MooNoteElement)
145                                         notePopup.show(e.getComponent(), e.getX(), e.getY());
146                                 else
147                                         popup.show(e.getComponent(), e.getX(), e.getY());
148                         }
149                 }
150         }
151 }